From be5121023d34dce232be43083fe229bb66f2dd36 Mon Sep 17 00:00:00 2001 From: jameslahm <1366463855@qq.com> Date: Thu, 24 Dec 2020 00:56:02 +0800 Subject: [PATCH] fix: correct update option's label when update value (equal label) in options --- src/hooks/useCacheDisplayValue.ts | 2 +- src/interface/generator.ts | 1 + src/utils/valueUtil.ts | 1 + tests/Select.test.tsx | 3 +++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hooks/useCacheDisplayValue.ts b/src/hooks/useCacheDisplayValue.ts index bd9e9d752..99fe5677f 100644 --- a/src/hooks/useCacheDisplayValue.ts +++ b/src/hooks/useCacheDisplayValue.ts @@ -17,7 +17,7 @@ export default function useCacheDisplayValue( const resultValues = values.map(item => { const cacheLabel = valueLabels.get(item.value); - if (item.value === item.label && cacheLabel) { + if (item.isCacheable && cacheLabel) { return { ...item, label: cacheLabel, diff --git a/src/interface/generator.ts b/src/interface/generator.ts index ccccea005..490636ce6 100644 --- a/src/interface/generator.ts +++ b/src/interface/generator.ts @@ -13,6 +13,7 @@ export interface LabelValueType { key?: Key; value?: RawValueType; label?: React.ReactNode; + isCacheable?: Boolean; } export type DefaultValueType = RawValueType | RawValueType[] | LabelValueType | LabelValueType[]; diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index ad663a480..07e131faf 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -153,6 +153,7 @@ export const getLabeledValue: GetLabeledValue = ( result.label = item[optionLabelProp]; } else { result.label = value; + result.isCacheable = true; } // Used for motion control diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index 2ee73f328..99a20bb84 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -1612,6 +1612,9 @@ describe('Select.Basic', () => { wrapper.setProps({ options: [] }); expect(findSelection(wrapper).text()).toEqual('Bamboo'); + + wrapper.setProps({ options: [{ value: 903, label: 903 }] }); + expect(findSelection(wrapper).text()).toEqual('903'); }); // https://github.com/ant-design/ant-design/issues/24747