@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
61 lines • 3.39 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.
*--------------------------------------------------------------------------------------------*/
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
import { PropertyValueFormat } from "@itwin/appui-abstract";
import { assert } from "@itwin/core-bentley";
import { Input } from "@itwin/itwinui-react";
import { useSchemaMetadataContext } from "../../common/SchemaMetadataContext.js";
import { NumericPropertyInput } from "./NumericPropertyInput.js";
import { useQuantityValueInput } from "./UseQuantityValueInput.js";
/** @internal */
export const QuantityPropertyEditorInput = forwardRef((props, ref) => {
const schemaMetadataContext = useSchemaMetadataContext();
// eslint-disable-next-line @typescript-eslint/no-deprecated
if ((!props.propertyRecord.property.kindOfQuantityName && !props.propertyRecord.property.quantityType) || !schemaMetadataContext) {
return _jsx(NumericPropertyInput, { ...props, ref: ref });
}
// eslint-disable-next-line @typescript-eslint/no-deprecated
const koqName = props.propertyRecord.property.kindOfQuantityName ?? props.propertyRecord.property.quantityType;
assert(koqName !== undefined);
const initialValue = props.propertyRecord.value?.value;
return (_jsx(QuantityPropertyValueInput, { ...props, ref: ref, koqName: koqName, schemaContext: schemaMetadataContext.schemaContext, initialRawValue: initialValue }));
});
QuantityPropertyEditorInput.displayName = "QuantityPropertyEditorInput";
const QuantityPropertyValueInput = forwardRef(({ propertyRecord, onCommit, koqName, schemaContext, initialRawValue, setFocus }, ref) => {
const { quantityValue, inputProps } = useQuantityValueInput({ koqName, schemaContext, initialRawValue });
const inputRef = useRef(null);
useImperativeHandle(ref, () => ({
getValue: () => ({
valueFormat: PropertyValueFormat.Primitive,
value: quantityValue.rawValue,
displayValue: quantityValue.formattedValue,
roundingError: quantityValue.roundingError,
}),
htmlElement: inputRef.current,
}), [quantityValue]);
const onBlur = () => {
onCommit &&
onCommit({
propertyRecord,
newValue: {
valueFormat: PropertyValueFormat.Primitive,
value: quantityValue.rawValue,
displayValue: quantityValue.formattedValue,
roundingError: quantityValue.roundingError,
},
});
};
useEffect(() => {
if (setFocus && !inputProps.disabled) {
inputRef.current && inputRef.current.focus();
}
}, [inputProps.disabled, setFocus]);
return (_jsx(Input, { size: "small", ...inputProps, disabled: propertyRecord.isReadonly || inputProps.disabled, ref: inputRef, onBlur: onBlur, onFocus: () => {
inputRef.current?.setSelectionRange(0, 9999);
} }));
});
QuantityPropertyValueInput.displayName = "QuantityPropertyValueInput";
//# sourceMappingURL=QuantityPropertyEditorInput.js.map