Skip to content

Commit ab2120a

Browse files
committed
Fix cases where placeholder option was accidentally given an index
1 parent 0bca1a4 commit ab2120a

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

packages/antd/src/widgets/SelectWidget/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,20 @@ export default function SelectWidget<
7373

7474
const selectOptions: DefaultOptionType[] | undefined = useMemo(() => {
7575
if (Array.isArray(enumOptions)) {
76-
const options = [...enumOptions];
77-
if (showPlaceholderOption) {
78-
options.unshift({ value: '', label: placeholder || '' });
79-
}
80-
return options.map(({ value: optionValue, label: optionLabel }, index) => ({
76+
const options: DefaultOptionType[] = enumOptions.map(({ value: optionValue, label: optionLabel }, index) => ({
8177
disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(optionValue) !== -1,
8278
key: String(index),
8379
value: String(index),
8480
label: optionLabel,
8581
}));
82+
83+
if (showPlaceholderOption) {
84+
options.unshift({ value: '', label: placeholder || '' });
85+
}
86+
return options;
8687
}
8788
return undefined;
88-
}, [enumDisabled, enumOptions, multiple, placeholder, schema.default]);
89+
}, [enumDisabled, enumOptions, placeholder, showPlaceholderOption]);
8990

9091
return (
9192
<Select

packages/antd/test/__snapshots__/Form.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,9 +3639,9 @@ exports[`single fields select field single choice formData 1`] = `
36393639
</span>
36403640
<span
36413641
className="ant-select-selection-item"
3642-
title="foo"
3642+
title="bar"
36433643
>
3644-
foo
3644+
bar
36453645
</span>
36463646
</div>
36473647
<span

packages/chakra-ui/src/SelectWidget/SelectWidget.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,40 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
6262
onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));
6363

6464
const showPlaceholderOption = !multiple && schema.default === undefined;
65-
const _valueLabelMap: any = {};
66-
const displayEnumOptions: OptionsOrGroups<any, any> = useMemo(() => {
65+
const { valueLabelMap, displayEnumOptions } = useMemo((): {
66+
valueLabelMap: Record<string | number, string>;
67+
displayEnumOptions: OptionsOrGroups<any, any>;
68+
} => {
69+
const valueLabelMap: Record<string | number, string> = {};
70+
let displayEnumOptions: OptionsOrGroups<any, any> = [];
6771
if (Array.isArray(enumOptions)) {
68-
const options = [...enumOptions];
69-
if (showPlaceholderOption) {
70-
options.unshift({ value: '', label: placeholder || '' });
71-
}
72-
return options.map((option: EnumOptionsType<S>, index: number) => {
72+
displayEnumOptions = enumOptions.map((option: EnumOptionsType<S>, index: number) => {
7373
const { value, label } = option;
74-
_valueLabelMap[index] = label || String(value);
74+
valueLabelMap[index] = label || String(value);
7575
return {
7676
label,
7777
value: String(index),
7878
isDisabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1,
7979
};
8080
});
81+
if (showPlaceholderOption) {
82+
(displayEnumOptions as any[]).unshift({ value: '', label: placeholder || '' });
83+
}
8184
}
82-
return [];
83-
}, [_valueLabelMap, enumDisabled, enumOptions, multiple, placeholder, schema.default]);
85+
return { valueLabelMap: valueLabelMap, displayEnumOptions: displayEnumOptions };
86+
}, [enumDisabled, enumOptions, placeholder, showPlaceholderOption]);
8487

8588
const isMultiple = typeof multiple !== 'undefined' && multiple !== false && Boolean(enumOptions);
8689
const selectedIndex = enumOptionsIndexForValue<S>(value, enumOptions, isMultiple);
8790
const formValue: any = isMultiple
8891
? ((selectedIndex as string[]) || []).map((i: string) => {
8992
return {
90-
label: _valueLabelMap[i],
93+
label: valueLabelMap[i],
9194
value: i,
9295
};
9396
})
9497
: {
95-
label: _valueLabelMap[selectedIndex as string] || '',
98+
label: valueLabelMap[selectedIndex as string] || '',
9699
selectedIndex,
97100
};
98101

packages/chakra-ui/test/__snapshots__/Form.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5230,7 +5230,7 @@ exports[`single fields select field single choice formData 1`] = `
52305230
<div
52315231
className=" emotion-7"
52325232
>
5233-
foo
5233+
bar
52345234
</div>
52355235
<div
52365236
className=" emotion-8"

packages/semantic-ui/src/SelectWidget/SelectWidget.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@ function createDefaultValueOptionsForDropDown<S extends StrictRJSFSchema = RJSFS
3030
placeholder?: string
3131
) {
3232
const disabledOptions = enumDisabled || [];
33-
const options: DropdownItemProps[] = [];
33+
const options: DropdownItemProps[] = map(enumOptions, ({ label, value }, index) => ({
34+
disabled: disabledOptions.indexOf(value) !== -1,
35+
key: label,
36+
text: label,
37+
value: String(index),
38+
}));
3439
if (showPlaceholderOption) {
35-
options.push({ value: '', text: placeholder || '' });
40+
options.unshift({ value: '', text: placeholder || '' });
3641
}
37-
options.push(
38-
...map(enumOptions, ({ label, value }, index) => ({
39-
disabled: disabledOptions.indexOf(value) !== -1,
40-
key: label,
41-
text: label,
42-
value: String(index),
43-
}))
44-
);
4542
return options;
4643
}
4744

0 commit comments

Comments
 (0)