@verifiedinc-public/shared-ui-elements 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
@@ -18,6 +18,7 @@ interface SelectInputProps {
|
|
18
18
|
onChange?: (value: Option | null) => void;
|
19
19
|
onClear?: () => void;
|
20
20
|
options: Option[];
|
21
|
+
value?: Option | null; // Controlled value
|
21
22
|
defaultOption?: Option;
|
22
23
|
InputProps?: TextFieldProps;
|
23
24
|
}
|
@@ -29,13 +30,23 @@ interface SelectInputProps {
|
|
29
30
|
export function SelectInput({
|
30
31
|
options,
|
31
32
|
defaultOption,
|
33
|
+
value: controlledValue,
|
32
34
|
onChange,
|
33
35
|
onClear,
|
34
36
|
...props
|
35
37
|
}: SelectInputProps): React.JSX.Element {
|
36
|
-
const [
|
38
|
+
const [internalValue, setInternalValue] = useState<Option | null>(
|
39
|
+
defaultOption ?? null,
|
40
|
+
);
|
41
|
+
|
42
|
+
// Determine the value to display
|
43
|
+
const isControlled = controlledValue !== undefined;
|
44
|
+
const value = isControlled ? controlledValue : internalValue;
|
45
|
+
|
37
46
|
const handleChange = (option: Option | null): void => {
|
38
|
-
|
47
|
+
if (!isControlled) {
|
48
|
+
setInternalValue(option); // Update internal state only if uncontrolled
|
49
|
+
}
|
39
50
|
if (onChange) {
|
40
51
|
onChange(option);
|
41
52
|
}
|