UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

93 lines 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuantityPropertyEditorInput = 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. *--------------------------------------------------------------------------------------------*/ const react_1 = require("react"); const appui_abstract_1 = require("@itwin/appui-abstract"); const core_bentley_1 = require("@itwin/core-bentley"); const itwinui_react_1 = require("@itwin/itwinui-react"); const SchemaMetadataContext_js_1 = require("../../common/SchemaMetadataContext.js"); const NumericPropertyInput_js_1 = require("./NumericPropertyInput.js"); const UseQuantityValueInput_js_1 = require("./UseQuantityValueInput.js"); /** @internal */ exports.QuantityPropertyEditorInput = (0, react_1.forwardRef)((props, ref) => { const schemaMetadataContext = (0, SchemaMetadataContext_js_1.useSchemaMetadataContext)(); if ( // eslint-disable-next-line @typescript-eslint/no-deprecated (!props.propertyRecord.property.kindOfQuantityName && !props.propertyRecord.property.quantityType) || !schemaMetadataContext) { return (0, jsx_runtime_1.jsx)(NumericPropertyInput_js_1.NumericPropertyInput, { ...props, ref: ref }); } // eslint-disable-next-line @typescript-eslint/no-deprecated const koqName = props.propertyRecord.property.kindOfQuantityName ?? props.propertyRecord.property.quantityType; (0, core_bentley_1.assert)(koqName !== undefined); const initialValue = props.propertyRecord.value?.value; return ((0, jsx_runtime_1.jsx)(QuantityPropertyValueInput, { ...props, ref: ref, koqName: koqName, schemaContext: schemaMetadataContext.schemaContext, initialRawValue: initialValue })); }); exports.QuantityPropertyEditorInput.displayName = "QuantityPropertyEditorInput"; const QuantityPropertyValueInput = (0, react_1.forwardRef)(({ propertyRecord, onCommit, koqName, schemaContext, initialRawValue, setFocus, onCancel }, ref) => { const property = propertyRecord.property; const { quantityValue, inputProps } = (0, UseQuantityValueInput_js_1.useQuantityValueInput)({ koqName, schemaContext, initialRawValue, constraints: property.constraints, }); const [isEditing, setEditing] = (0, react_1.useState)(false); const value = isEditing ? quantityValue.highPrecisionFormattedValue : quantityValue.defaultFormattedValue; const inputRef = (0, react_1.useRef)(null); (0, react_1.useImperativeHandle)(ref, () => ({ getValue: () => ({ valueFormat: appui_abstract_1.PropertyValueFormat.Primitive, value: quantityValue.rawValue, displayValue: quantityValue.defaultFormattedValue, roundingError: quantityValue.roundingError, }), htmlElement: inputRef.current, }), [quantityValue.defaultFormattedValue, quantityValue.rawValue, quantityValue.roundingError]); const onBlur = () => { onCommit && onCommit({ propertyRecord, newValue: { valueFormat: appui_abstract_1.PropertyValueFormat.Primitive, value: quantityValue.rawValue, displayValue: quantityValue.defaultFormattedValue, roundingError: quantityValue.roundingError, }, }); }; (0, react_1.useEffect)(() => { if (setFocus && !inputProps.disabled) { inputRef.current && inputRef.current.focus(); } }, [inputProps.disabled, setFocus]); const handleKeyDown = (e) => { if (e.key === "Escape") { onCancel?.(); } if (e.key === "Enter") { inputRef.current?.blur(); e.stopPropagation(); } }; return ((0, jsx_runtime_1.jsx)(itwinui_react_1.Input, { ...inputProps, value: value, size: "small", disabled: propertyRecord.isReadonly || inputProps.disabled, ref: inputRef, onBlur: () => { onBlur(); setEditing(false); }, onFocus: () => { setEditing(true); requestAnimationFrame(() => { if (quantityValue.highPrecisionFormattedValue === inputProps.placeholder) { inputRef.current?.setSelectionRange(0, 0); return; } inputRef.current?.setSelectionRange(0, 9999); }); }, onKeyDown: handleKeyDown })); }); QuantityPropertyValueInput.displayName = "QuantityPropertyValueInput"; //# sourceMappingURL=QuantityPropertyEditorInput.js.map