Skip to content

Commit 7841c77

Browse files
authored
fix: tokenSeparators should work when has optionFilterProp or optionLabelProp (#754)
* fix: tokenSeparators should work when fieldNames.label is children * chore: update snapshot due to rc-trigger bumped * chore: improve code style * chore: improve code style
1 parent 941f52b commit 7841c77

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

src/Select.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ const Select = React.forwardRef(
221221
});
222222

223223
// =========================== Option ===========================
224-
const parsedOptions = useOptions(options, children, mergedFieldNames);
224+
const parsedOptions = useOptions(
225+
options,
226+
children,
227+
mergedFieldNames,
228+
optionFilterProp,
229+
optionLabelProp,
230+
);
225231
const { valueOptions, labelOptions, options: mergedOptions } = parsedOptions;
226232

227233
// ========================= Wrap Value =========================

src/hooks/useOptions.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export default function useOptions<OptionType>(
1010
options: OptionType[],
1111
children: React.ReactNode,
1212
fieldNames: FieldNames,
13+
optionFilterProp: string,
14+
optionLabelProp: string,
1315
) {
1416
return React.useMemo(() => {
1517
let mergedOptions = options;
@@ -22,13 +24,22 @@ export default function useOptions<OptionType>(
2224
const valueOptions = new Map<RawValueType, OptionType>();
2325
const labelOptions = new Map<React.ReactNode, OptionType>();
2426

27+
const setLabelOptions = (labelOptionsMap, option, key) => {
28+
if (key && typeof key === 'string') {
29+
labelOptionsMap.set(option[key], option);
30+
}
31+
};
32+
2533
function dig(optionList: OptionType[], isChildren = false) {
2634
// for loop to speed up collection speed
2735
for (let i = 0; i < optionList.length; i += 1) {
2836
const option = optionList[i];
2937
if (!option[fieldNames.options] || isChildren) {
3038
valueOptions.set(option[fieldNames.value], option);
31-
labelOptions.set(option[fieldNames.label], option);
39+
setLabelOptions(labelOptions, option, fieldNames.label);
40+
// https://github.com/ant-design/ant-design/issues/35304
41+
setLabelOptions(labelOptions, option, optionFilterProp);
42+
setLabelOptions(labelOptions, option, optionLabelProp);
3243
} else {
3344
dig(option[fieldNames.options], true);
3445
}
@@ -41,5 +52,5 @@ export default function useOptions<OptionType>(
4152
valueOptions,
4253
labelOptions,
4354
};
44-
}, [options, children, fieldNames]);
55+
}, [options, children, fieldNames, optionFilterProp, optionLabelProp]);
4556
}

tests/Multiple.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,56 @@ describe('Select.Multiple', () => {
182182
expectOpen(wrapper, false);
183183
});
184184

185+
it('should separate words when optionLabelProp is not children', () => {
186+
const handleChange = jest.fn();
187+
const wrapper = mount(
188+
<Select
189+
mode="multiple"
190+
optionLabelProp="label"
191+
tokenSeparators={[',']}
192+
onChange={handleChange}
193+
>
194+
<Option value="1" label="One">
195+
<div>One</div>
196+
</Option>
197+
<Option value="2" label="Two">
198+
<div>Two</div>
199+
</Option>
200+
</Select>,
201+
);
202+
wrapper.find('input').simulate('change', {
203+
target: {
204+
value: 'One,Two',
205+
},
206+
});
207+
expect(handleChange).toHaveBeenCalledWith(['1', '2'], expect.anything());
208+
});
209+
210+
it('should separate words when optionFilterProp is not children', () => {
211+
const handleChange = jest.fn();
212+
const wrapper = mount(
213+
<Select
214+
mode="multiple"
215+
optionFilterProp="label"
216+
tokenSeparators={[',']}
217+
onChange={handleChange}
218+
>
219+
<Option value="1" label="One">
220+
<div>One</div>
221+
</Option>
222+
<Option value="2" label="Two">
223+
<div>Two</div>
224+
</Option>
225+
</Select>,
226+
);
227+
wrapper.find('input').simulate('change', {
228+
target: {
229+
value: 'One,Two',
230+
},
231+
});
232+
expect(handleChange).toHaveBeenCalledWith(['1', '2'], expect.anything());
233+
});
234+
185235
it('focus', () => {
186236
jest.useFakeTimers();
187237
const handleFocus = jest.fn();

tests/__snapshots__/Select.test.tsx.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports[`Select.Basic does not filter when filterOption value is false 1`] = `
3535
<div>
3636
<div
3737
class="rc-select-dropdown"
38-
style="opacity:0;pointer-events:none"
38+
style="opacity:0"
3939
>
4040
<div>
4141
<div
@@ -168,7 +168,7 @@ exports[`Select.Basic filterOption could be true as described in default value 1
168168
<div>
169169
<div
170170
class="rc-select-dropdown rc-select-dropdown-empty"
171-
style="opacity: 0; pointer-events: none; min-width: 0; width: 0px;"
171+
style="opacity: 0; min-width: 0; width: 0px;"
172172
>
173173
<div>
174174
<div
@@ -490,7 +490,7 @@ exports[`Select.Basic render renders dropdown correctly 1`] = `
490490
<div>
491491
<div
492492
class="antd-dropdown"
493-
style="opacity:0;pointer-events:none"
493+
style="opacity:0"
494494
>
495495
<div>
496496
<div
@@ -735,7 +735,7 @@ exports[`Select.Basic should contain falsy children 1`] = `
735735
<div>
736736
<div
737737
class="rc-select-dropdown"
738-
style="opacity:0;pointer-events:none"
738+
style="opacity:0"
739739
>
740740
<div>
741741
<div
@@ -872,7 +872,7 @@ exports[`Select.Basic should render custom dropdown correctly 1`] = `
872872
<div>
873873
<div
874874
class="rc-select-dropdown"
875-
style="opacity: 0; pointer-events: none; min-width: 0; width: 0px;"
875+
style="opacity: 0; min-width: 0; width: 0px;"
876876
>
877877
<div>
878878
<div>

tests/__snapshots__/Tags.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ exports[`Select.Tags OptGroup renders correctly 1`] = `
100100
<div>
101101
<div
102102
class="rc-select-dropdown"
103-
style="opacity: 0; pointer-events: none; min-width: 0; width: 0px;"
103+
style="opacity: 0; min-width: 0; width: 0px;"
104104
>
105105
<div>
106106
<div

0 commit comments

Comments
 (0)