-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuantityInput.tsx
166 lines (155 loc) · 5.03 KB
/
QuantityInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Inputs
*/
import classnames from "classnames";
import * as React from "react";
import type {
QuantityFormatsChangedArgs,
QuantityTypeArg,
} from "@itwin/core-frontend";
import { IModelApp } from "@itwin/core-frontend";
import { Parser } from "@itwin/core-quantity";
import type { ParseResults } from "@itwin/appui-abstract";
import type { CommonProps } from "@itwin/core-react";
import { ParsedInput } from "@itwin/components-react";
import { useTranslation } from "../useTranslation";
/** Props for [[QuantityInput]] control
* @beta
*/
export interface QuantityProps extends CommonProps {
/** Initial magnitude in 'persistence' units. See `getPersistenceUnitByQuantityType` in [QuantityFormatter]($core-frontend) */
initialValue: number;
/** Type of quantity being input. */
quantityType: QuantityTypeArg;
/** Function to call in the quantity value is changed. The value returned will be in 'persistence' units. */
onQuantityChange: (newQuantityValue: number) => void;
/** Set to `true` if value is for display only */
readonly?: boolean;
/** Provides ability to return reference to HTMLInputElement */
ref?: React.Ref<HTMLInputElement>;
}
/** Input control that allows users to input a quantity and show the formatted string that represents the value.
* @beta
*/
export function QuantityInput({
initialValue,
quantityType,
readonly,
className,
style,
onQuantityChange,
ref,
}: QuantityProps) {
const { translate } = useTranslation();
const [formatterSpec, setFormatterSpec] = React.useState(() =>
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(quantityType)
);
const [parserSpec, setParserSpec] = React.useState(() =>
IModelApp.quantityFormatter.findParserSpecByQuantityType(quantityType)
);
const formatValue = React.useCallback(
(value: number) => {
// istanbul ignore else
if (formatterSpec) {
return formatterSpec.applyFormatting(value);
}
// istanbul ignore next
return value.toFixed(2);
},
[formatterSpec]
);
const parseString = React.useCallback(
(userInput: string): ParseResults => {
// istanbul ignore else
if (parserSpec) {
const parseResult = IModelApp.quantityFormatter.parseToQuantityValue(
userInput,
parserSpec
);
// istanbul ignore else
if (Parser.isParsedQuantity(parseResult)) {
return { value: parseResult.value };
} else {
const statusId = Parser.isParseError(parseResult)
? parseResult.error.toString()
: "Unknown";
return {
parseError: `${translate(
"QuantityInput.NoParserDefined"
)}${statusId}`,
};
}
}
// istanbul ignore next
return {
parseError: translate("QuantityInput.NoParserDefined"),
};
},
[parserSpec, translate]
);
const classNames = classnames(className, "components-quantity-input");
React.useEffect(() => {
const handleUnitSystemChanged = (): void => {
setFormatterSpec(
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(
quantityType
)
);
setParserSpec(
IModelApp.quantityFormatter.findParserSpecByQuantityType(quantityType)
);
};
IModelApp.quantityFormatter.onActiveFormattingUnitSystemChanged.addListener(
handleUnitSystemChanged
);
return () => {
IModelApp.quantityFormatter.onActiveFormattingUnitSystemChanged.removeListener(
handleUnitSystemChanged
);
};
}, [quantityType]);
React.useEffect(() => {
const handleUnitSystemChanged = (
args: QuantityFormatsChangedArgs
): void => {
const quantityKey =
IModelApp.quantityFormatter.getQuantityTypeKey(quantityType);
// istanbul ignore else
if (args.quantityType === quantityKey) {
setFormatterSpec(
IModelApp.quantityFormatter.findFormatterSpecByQuantityType(
quantityType
)
);
setParserSpec(
IModelApp.quantityFormatter.findParserSpecByQuantityType(quantityType)
);
}
};
IModelApp.quantityFormatter.onQuantityFormatsChanged.addListener(
handleUnitSystemChanged
);
return () => {
IModelApp.quantityFormatter.onQuantityFormatsChanged.removeListener(
handleUnitSystemChanged
);
};
}, [quantityType]);
return (
<ParsedInput
data-testid="components-quantity-input"
ref={ref}
style={style}
className={classNames}
onChange={onQuantityChange}
initialValue={initialValue}
readonly={readonly}
formatValue={formatValue}
parseString={parseString}
/>
);
}