Skip to content

Commit 588013b

Browse files
Updated Picker to treat empty string as a non value (#594)
+ removed placeholder as selectable value Co-authored-by: Brian Luerssen <[email protected]>
1 parent 936184b commit 588013b

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

packages/core/src/components/Picker/Picker.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const unstyledColor = "rgba(165, 173, 183, 1)";
9292
const disabledColor = "rgb(240, 240, 240)";
9393
const errorColor = "rgba(255, 69, 100, 1)";
9494

95+
//Empty string for 'value' is treated as a non-value
96+
//reason: Draftbit uses empty string as initial value for string state*/
9597
const Picker: React.FC<PickerProps> = ({
9698
error,
9799
options = [],
@@ -126,13 +128,13 @@ const Picker: React.FC<PickerProps> = ({
126128
};
127129

128130
React.useEffect(() => {
129-
if (value != null) {
131+
if (value != null && value !== "") {
130132
setInternalValue(value);
131133
}
132134
}, [value]);
133135

134136
React.useEffect(() => {
135-
if (defaultValue != null) {
137+
if (defaultValue != null && defaultValue !== "") {
136138
setInternalValue(defaultValue);
137139
}
138140
}, [defaultValue]);
@@ -149,11 +151,7 @@ const Picker: React.FC<PickerProps> = ({
149151
}
150152
}, [pickerVisible, autoDismissKeyboard]);
151153

152-
const normalizedOptions = normalizeOptions(options);
153-
154-
const pickerOptions = placeholder
155-
? [{ value: placeholder, label: placeholder }, ...normalizedOptions]
156-
: normalizedOptions;
154+
const pickerOptions = normalizeOptions(options);
157155

158156
const { viewStyles, textStyles } = extractStyles(style);
159157

@@ -304,10 +302,10 @@ const Picker: React.FC<PickerProps> = ({
304302
};
305303

306304
const handleValueChange = (newValue: string, itemIndex: number) => {
307-
if (!placeholder || itemIndex > 0) {
305+
if (newValue !== "") {
308306
onValueChange?.(newValue, itemIndex);
307+
setInternalValue(newValue);
309308
}
310-
setInternalValue(newValue);
311309
};
312310

313311
return (

0 commit comments

Comments
 (0)