UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

127 lines 6.54 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.useQuantityValueInput = useQuantityValueInput; const react_1 = require("react"); const core_bentley_1 = require("@itwin/core-bentley"); const core_frontend_1 = require("@itwin/core-frontend"); const core_quantity_1 = require("@itwin/core-quantity"); const presentation_common_1 = require("@itwin/presentation-common"); const Utils_js_1 = require("./Utils.js"); /** * Custom hook that manages state for quantity values input. * @internal */ function useQuantityValueInput({ initialRawValue, schemaContext, koqName, constraints, }) { const initialRawValueRef = (0, react_1.useRef)(initialRawValue); const [{ quantityValue, placeholder }, setState] = (0, react_1.useState)(() => ({ quantityValue: { rawValue: initialRawValueRef.current, highPrecisionFormattedValue: "", defaultFormattedValue: "", roundingError: undefined, }, placeholder: "", })); const onFormatterLoad = (0, react_1.useRef)(({ newHighPrecisionFormatter, newParser, newDefaultFormatter }) => { setState((prev) => { /* v8 ignore next 1 -- @preserve */ const defaultValue = prev.quantityValue.rawValue !== undefined ? newDefaultFormatter.applyFormatting(prev.quantityValue.rawValue) : `-- ${newHighPrecisionFormatter.unitConversions[0].label}`; const newFormattedValue = prev.quantityValue.rawValue !== undefined ? newHighPrecisionFormatter.applyFormatting(prev.quantityValue.rawValue) : newHighPrecisionFormatter.unitConversions[0].label; const placeholderUnit = newHighPrecisionFormatter.unitConversions[0].label; const roundingError = (0, Utils_js_1.getPersistenceUnitRoundingError)(newFormattedValue, newParser); return { ...prev, quantityValue: { ...prev.quantityValue, highPrecisionFormattedValue: newFormattedValue, defaultFormattedValue: defaultValue, roundingError, }, placeholder: placeholderUnit, }; }); }); const useFormatterAndParserResult = useFormatterAndParser(koqName, schemaContext, onFormatterLoad.current); const onChange = (e) => { (0, core_bentley_1.assert)(useFormatterAndParserResult !== undefined); // input should be disabled if parser is `undefined` const { parser, defaultFormatter } = useFormatterAndParserResult; const newValue = e.currentTarget.value; const parseResult = parser.parseToQuantityValue(newValue); const roundingError = (0, Utils_js_1.getPersistenceUnitRoundingError)(newValue, parser); const defaultFormattedValue = parseResult.ok ? defaultFormatter?.applyFormatting(parseResult.value) : undefined; const rawValue = parseResult.ok ? (0, Utils_js_1.applyNumericConstraints)({ ...(0, Utils_js_1.getMinMaxFromPropertyConstraints)(constraints), value: parseResult.value }) : undefined; setState((prev) => ({ ...prev, quantityValue: { highPrecisionFormattedValue: newValue, defaultFormattedValue: defaultFormattedValue ?? newValue, rawValue, roundingError: parseResult.ok ? roundingError : undefined, }, })); }; return { quantityValue, inputProps: { onChange, placeholder, disabled: !useFormatterAndParserResult } }; } function useFormatterAndParser(koqName, schemaContext, onChange) { const [state, setState] = (0, react_1.useState)(); (0, react_1.useEffect)(() => { const findFormatterAndParser = async () => { // eslint-disable-next-line @typescript-eslint/no-deprecated const koqFormatter = new presentation_common_1.KoqPropertyValueFormatter(schemaContext, undefined, core_frontend_1.IModelApp.formatsProvider); const highPrecisionFormatter = await koqFormatter.getFormatterSpec({ koqName, unitSystem: core_frontend_1.IModelApp.quantityFormatter.activeUnitSystem, }); // formatter for default value should not have precision override const defaultFormatter = await koqFormatter.getFormatterSpec({ koqName, unitSystem: core_frontend_1.IModelApp.quantityFormatter.activeUnitSystem, }); const parserSpec = await koqFormatter.getParserSpec({ koqName, unitSystem: core_frontend_1.IModelApp.quantityFormatter.activeUnitSystem, }); if (highPrecisionFormatter && parserSpec && defaultFormatter) { if (highPrecisionFormatter.format.type === core_quantity_1.FormatType.Decimal) { highPrecisionFormatter.format.precision = 12; } onChange({ newHighPrecisionFormatter: highPrecisionFormatter, newParser: parserSpec, newDefaultFormatter: defaultFormatter, }); setState({ highPrecisionFormatter, parserSpec, defaultFormatter }); return; } setState(undefined); }; void findFormatterAndParser(); const listeners = [ core_frontend_1.IModelApp.quantityFormatter.onActiveFormattingUnitSystemChanged.addListener(findFormatterAndParser), ]; if (core_frontend_1.IModelApp.formatsProvider) { listeners.push(core_frontend_1.IModelApp.formatsProvider.onFormatsChanged.addListener(findFormatterAndParser)); } return () => { listeners.forEach((listener) => listener()); }; }, [koqName, schemaContext, onChange]); return state ? { highPrecisionFormatter: state.highPrecisionFormatter, parser: state.parserSpec, defaultFormatter: state.defaultFormatter, } : undefined; } //# sourceMappingURL=UseQuantityValueInput.js.map