@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
61 lines • 3.26 kB
JavaScript
import { jsx as _jsx } from "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
*/
import { PropertyValueFormat } from "@itwin/appui-abstract";
import { TypeConverterManager, useAsyncValue } from "@itwin/components-react";
import { Anchor } from "@itwin/itwinui-react";
import { useOptionalUnifiedSelectionContext } from "../common/UnifiedSelection.js";
import { translate } from "../common/Utils.js";
import { useUnifiedSelectionContext as useDeprecatedUnifiedSelectionContext } from "../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
*/
export class InstanceKeyValueRenderer {
canRender(record) {
return record.value.valueFormat === PropertyValueFormat.Primitive && (record.value.value === undefined || isInstanceKey(record.value.value));
}
render(record, context) {
return _jsx(InstanceKeyValueRendererImpl, { record: record, context: context });
}
}
const InstanceKeyValueRendererImpl = (props) => {
const stringValue = useAsyncValue(convertRecordToString(props.record));
const valueElement = stringValue ?? props.context?.defaultValue;
// eslint-disable-next-line @typescript-eslint/no-deprecated
const deprecatedSelectionContext = useDeprecatedUnifiedSelectionContext();
const selectionContext = 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 (_jsx(Anchor, { title: translate("instance-key-value-renderer.select-instance"), onClick: handleClick, children: valueElement }));
}
}
return (_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 ??
TypeConverterManager.getConverter(record.property.typename, record.property.converter?.name).convertPropertyToString(record.property, primitive.value));
}
//# sourceMappingURL=InstanceKeyValueRenderer.js.map