From cb8a6235c0b6af5a1db9f6fb19e2fbef083c7f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E7=8E=B2=E7=8E=B2?= Date: Mon, 26 Feb 2024 19:10:15 +0800 Subject: [PATCH] fix: incorrect display when value change from with label to without label --- src/hooks/useCache.ts | 12 +++--------- tests/Select.test.tsx | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/hooks/useCache.ts b/src/hooks/useCache.ts index 732f864b9..2ebeb831b 100644 --- a/src/hooks/useCache.ts +++ b/src/hooks/useCache.ts @@ -3,42 +3,36 @@ import type { RawValueType } from '../BaseSelect'; import type { DefaultOptionType, LabelInValueType } from '../Select'; /** - * Cache `value` related LabeledValue & options. + * Cache `options` related LabeledValue & options. */ export default ( labeledValues: LabelInValueType[], valueOptions: Map, ): [LabelInValueType[], (val: RawValueType) => DefaultOptionType] => { const cacheRef = React.useRef({ - values: new Map(), options: new Map(), }); 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, }; } return item; }); - // Refresh cache - const valueCache = new Map(); const optionCache = new Map(); patchedValues.forEach((item) => { - valueCache.set(item.value, item); optionCache.set(item.value, valueOptions.get(item.value) || prevOptionCache.get(item.value)); }); - cacheRef.current.values = valueCache; cacheRef.current.options = optionCache; return patchedValues; diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index 413c80bf3..e9c32b1bc 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -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(