Skip to content

Commit

Permalink
fix: incorrect display when value change from with label to without l…
Browse files Browse the repository at this point in the history
…abel
  • Loading branch information
xulingling0 committed Feb 26, 2024
1 parent 2535b87 commit 00cca92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/hooks/useCache.ts
Expand Up @@ -15,14 +15,13 @@ export default (
});

const filledLabeledValues = React.useMemo(() => {
const { values: prevValueCache, options: prevOptionCache } = cacheRef.current;

const {options: prevOptionCache } = cacheRef.current;
// Fill label by cache
const patchedValues = labeledValues.map((item) => {
if (item.label === undefined) {
return {
...item,
label: prevValueCache.get(item.value)?.label,
label: prevOptionCache.get(item.value)?.label,
};
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Select.test.tsx
Expand Up @@ -1885,6 +1885,23 @@ describe('Select.Basic', () => {
expect(findSelection(wrapper).text()).toEqual('903');
});

it('value should be used as label When value does not have a label attribute and historical options do not contain value', () => {
const wrapper = mount(<Select value={{value: 903, label: 'light'}} options={[]} />);
expect(findSelection(wrapper).text()).toEqual('light');

wrapper.setProps({ value: 903 });
expect(findSelection(wrapper).text()).toEqual('903');

wrapper.setProps({ options: [{ value: 903, label: 'Bamboo' }] });
expect(findSelection(wrapper).text()).toEqual('Bamboo');

wrapper.setProps({ value:{ value: 903, label: 'light' },options:[]});
expect(findSelection(wrapper).text()).toEqual('light');

wrapper.setProps({ value: 903, options:[]});
expect(findSelection(wrapper).text()).toEqual('Bamboo');
});

// https://github.com/ant-design/ant-design/issues/24747
// This can not test function called with jest spy, coverage only
it('mouse enter to refresh', () => {
Expand Down

0 comments on commit 00cca92

Please sign in to comment.