UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

65 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstanceKeyValueRenderer = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** * @packageDocumentation * @module Properties */ const appui_abstract_1 = require("@itwin/appui-abstract"); const components_react_1 = require("@itwin/components-react"); const itwinui_react_1 = require("@itwin/itwinui-react"); const UnifiedSelection_js_1 = require("../common/UnifiedSelection.js"); const Utils_js_1 = require("../common/Utils.js"); const UnifiedSelectionContext_js_1 = require("../unified-selection/UnifiedSelectionContext.js"); /** * Property value renderer for instance keys. If application provides a [[UnifiedSelectionContext]] and this value is * clicked, the current selection is replaced with the instance pointed by the key. The selection changes at the default * selection level as provided by the context. * @public */ class InstanceKeyValueRenderer { canRender(record) { return record.value.valueFormat === appui_abstract_1.PropertyValueFormat.Primitive && (record.value.value === undefined || isInstanceKey(record.value.value)); } render(record, context) { return (0, jsx_runtime_1.jsx)(InstanceKeyValueRendererImpl, { record: record, context: context }); } } exports.InstanceKeyValueRenderer = InstanceKeyValueRenderer; const InstanceKeyValueRendererImpl = (props) => { const stringValue = (0, components_react_1.useAsyncValue)(convertRecordToString(props.record)); const valueElement = stringValue ?? props.context?.defaultValue; // eslint-disable-next-line @typescript-eslint/no-deprecated const deprecatedSelectionContext = (0, UnifiedSelectionContext_js_1.useUnifiedSelectionContext)(); const selectionContext = (0, UnifiedSelection_js_1.useOptionalUnifiedSelectionContext)(); const instanceKey = props.record.value.value; if (instanceKey) { let handleClick; if (deprecatedSelectionContext) { handleClick = () => deprecatedSelectionContext.replaceSelection([instanceKey]); } else if (selectionContext && props.record.imodelKey?.length) { const imodelKey = props.record.imodelKey; handleClick = () => selectionContext.storage.replaceSelection({ imodelKey, source: "InstanceKeyValueRenderer", selectables: [instanceKey] }); } if (handleClick) { return ((0, jsx_runtime_1.jsx)(itwinui_react_1.Anchor, { title: (0, Utils_js_1.translate)("instance-key-value-renderer.select-instance"), onClick: handleClick, children: valueElement })); } } return ((0, jsx_runtime_1.jsx)("span", { style: props.context?.style, title: stringValue, children: valueElement })); }; function isInstanceKey(value) { const { className, id } = value; return typeof className === "string" && typeof id === "string"; } function convertRecordToString(record) { const primitive = record.value; return (primitive.displayValue ?? components_react_1.TypeConverterManager.getConverter(record.property.typename, record.property.converter?.name).convertPropertyToString(record.property, primitive.value)); } //# sourceMappingURL=InstanceKeyValueRenderer.js.map