Skip to content

Commit 881339e

Browse files
authored
fix: optionLabelProp (#691)
1 parent 0c90b15 commit 881339e

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/OptionList.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
4444
virtual,
4545
listHeight,
4646
listItemHeight,
47-
optionLabelProp,
4847
} = React.useContext(SelectContext);
4948

5049
const itemPrefixCls = `${prefixCls}-item`;
@@ -225,13 +224,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
225224

226225
const omitFieldNameList = Object.keys(fieldNames).map((key) => fieldNames[key]);
227226

228-
const getLabel = (item: Record<string, any>) => {
229-
if (optionLabelProp) {
230-
return item.data[optionLabelProp];
231-
}
232-
233-
return item.label;
234-
};
227+
const getLabel = (item: Record<string, any>) => item.label;
235228

236229
const renderItem = (index: number) => {
237230
const item = memoFlattenOptions[index];

src/Select.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ const Select = React.forwardRef(
276276
const option = valueOptions.get(rawValue);
277277
if (option) {
278278
// Fill missing props
279-
if (rawLabel === undefined) rawLabel = option?.[mergedFieldNames.label];
279+
if (rawLabel === undefined)
280+
rawLabel = option?.[optionLabelProp || mergedFieldNames.label];
280281
if (rawKey === undefined) rawKey = option?.key ?? rawValue;
281282
rawDisabled = option?.disabled;
282283

@@ -297,7 +298,7 @@ const Select = React.forwardRef(
297298
};
298299
});
299300
},
300-
[mergedFieldNames, valueOptions],
301+
[mergedFieldNames, optionLabelProp, valueOptions],
301302
);
302303

303304
// =========================== Values ===========================
@@ -594,7 +595,6 @@ const Select = React.forwardRef(
594595
listHeight,
595596
listItemHeight,
596597
childrenAsData,
597-
optionLabelProp,
598598
}),
599599
[
600600
parsedOptions,
@@ -609,7 +609,6 @@ const Select = React.forwardRef(
609609
listHeight,
610610
listItemHeight,
611611
childrenAsData,
612-
optionLabelProp,
613612
],
614613
);
615614

src/SelectContext.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface SelectContextProps {
1717
listHeight?: number;
1818
listItemHeight?: number;
1919
childrenAsData?: boolean;
20-
optionLabelProp?: string;
2120
}
2221

2322
const SelectContext = React.createContext<SelectContextProps>(null);

tests/Multiple.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,32 @@ describe('Select.Multiple', () => {
474474

475475
expect(wrapper.exists('.rc-select-selection-item-remove')).toBeFalsy();
476476
});
477+
478+
it('optionLabelProp', () => {
479+
const wrapper = mount(
480+
<Select
481+
mode="multiple"
482+
value={['bamboo', 'little']}
483+
open
484+
optionLabelProp="selector"
485+
options={[
486+
{
487+
label: 'Bamboo',
488+
value: 'bamboo',
489+
selector: 'BAMBOO',
490+
},
491+
{
492+
label: 'Little',
493+
value: 'little',
494+
selector: 'LITTLE',
495+
},
496+
]}
497+
/>,
498+
);
499+
500+
expect(findSelection(wrapper, 0).text()).toBe('BAMBOO');
501+
expect(findSelection(wrapper, 1).text()).toBe('LITTLE');
502+
expect(wrapper.find('div.rc-select-item-option-content').at(0).text()).toBe('Bamboo');
503+
expect(wrapper.find('div.rc-select-item-option-content').at(1).text()).toBe('Little');
504+
});
477505
});

0 commit comments

Comments
 (0)