Skip to content

Commit cb8a623

Browse files
author
许玲玲
committed
fix: incorrect display when value change from with label to without label
1 parent 2535b87 commit cb8a623

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/hooks/useCache.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,36 @@ import type { RawValueType } from '../BaseSelect';
33
import type { DefaultOptionType, LabelInValueType } from '../Select';
44

55
/**
6-
* Cache `value` related LabeledValue & options.
6+
* Cache `options` related LabeledValue & options.
77
*/
88
export default (
99
labeledValues: LabelInValueType[],
1010
valueOptions: Map<RawValueType, DefaultOptionType>,
1111
): [LabelInValueType[], (val: RawValueType) => DefaultOptionType] => {
1212
const cacheRef = React.useRef({
13-
values: new Map<RawValueType, LabelInValueType>(),
1413
options: new Map<RawValueType, DefaultOptionType>(),
1514
});
1615

1716
const filledLabeledValues = React.useMemo(() => {
18-
const { values: prevValueCache, options: prevOptionCache } = cacheRef.current;
19-
17+
const { options: prevOptionCache } = cacheRef.current;
2018
// Fill label by cache
2119
const patchedValues = labeledValues.map((item) => {
2220
if (item.label === undefined) {
2321
return {
2422
...item,
25-
label: prevValueCache.get(item.value)?.label,
23+
label: prevOptionCache.get(item.value)?.label,
2624
};
2725
}
2826

2927
return item;
3028
});
3129

32-
// Refresh cache
33-
const valueCache = new Map<RawValueType, LabelInValueType>();
3430
const optionCache = new Map<RawValueType, DefaultOptionType>();
3531

3632
patchedValues.forEach((item) => {
37-
valueCache.set(item.value, item);
3833
optionCache.set(item.value, valueOptions.get(item.value) || prevOptionCache.get(item.value));
3934
});
4035

41-
cacheRef.current.values = valueCache;
4236
cacheRef.current.options = optionCache;
4337

4438
return patchedValues;

tests/Select.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,23 @@ describe('Select.Basic', () => {
18851885
expect(findSelection(wrapper).text()).toEqual('903');
18861886
});
18871887

1888+
it('value should be used as label When value does not have a label attribute and historical options do not contain value', () => {
1889+
const wrapper = mount(<Select value={{value: 903, label: 'light'}} options={[]} />);
1890+
expect(findSelection(wrapper).text()).toEqual('light');
1891+
1892+
wrapper.setProps({ value: 903 });
1893+
expect(findSelection(wrapper).text()).toEqual('903');
1894+
1895+
wrapper.setProps({ options: [{ value: 903, label: 'Bamboo' }] });
1896+
expect(findSelection(wrapper).text()).toEqual('Bamboo');
1897+
1898+
wrapper.setProps({ value:{ value: 903, label: 'light' },options:[]});
1899+
expect(findSelection(wrapper).text()).toEqual('light');
1900+
1901+
wrapper.setProps({ value: 903, options:[]});
1902+
expect(findSelection(wrapper).text()).toEqual('Bamboo');
1903+
});
1904+
18881905
// https://github.com/ant-design/ant-design/issues/24747
18891906
// This can not test function called with jest spy, coverage only
18901907
it('mouse enter to refresh', () => {

0 commit comments

Comments
 (0)