diff --git a/src/Cascader.tsx b/src/Cascader.tsx index a73c4d09..7badc14d 100644 --- a/src/Cascader.tsx +++ b/src/Cascader.tsx @@ -38,6 +38,10 @@ const RefCascader = generate({ optionList: OptionList, }); +function defaultDisplayRender(labels: React.ReactNode[]) { + return labels.join(' / '); +} + // ====================================== Wrap ====================================== interface BaseCascaderProps extends Omit< @@ -73,6 +77,9 @@ interface BaseCascaderProps fieldNames?: FieldNames; + // Display + displayRender?: (label: React.ReactNode[], selectedOptions: DataNode[]) => React.ReactNode; + // Search showSearch?: boolean | ShowSearchType; searchValue?: string; @@ -142,6 +149,8 @@ const Cascader = React.forwardRef((props: CascaderProps, ref: React.Ref opt[fieldLabel]) - .join('>'); + const { options: selectedOptions } = restoreCompatibleValue(entity, mergedFieldNames); + const rawOptions = selectedOptions.map(opt => opt.node); + const labelList = rawOptions.map(opt => opt[fieldLabel]); + + return displayRender(labelList, rawOptions); }; // =========================== Change =========================== diff --git a/src/index.tsx b/src/index.tsx index 05bd1609..8ad9be75 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,5 @@ import Cascader, { CascaderProps } from './Cascader'; -import type { FieldNames, DataNode, ShowSearchType } from './interface'; +import { FieldNames, DataNode, ShowSearchType } from './interface'; export { CascaderProps, FieldNames, DataNode, ShowSearchType }; diff --git a/tests/fieldNames.spec.tsx b/tests/fieldNames.spec.tsx index 87274a20..73c83eb2 100644 --- a/tests/fieldNames.spec.tsx +++ b/tests/fieldNames.spec.tsx @@ -78,10 +78,29 @@ describe('Cascader.FieldNames', () => { />, ); + expect(wrapper.find('.rc-cascader-selection-item').text()).toEqual('Bamboo / Little / Toy'); + expect(wrapper.find('.rc-cascader-menu')).toHaveLength(3); expect(wrapper.find('.rc-cascader-menu-item-active')).toHaveLength(3); expect(wrapper.find('.rc-cascader-menu-item-active').at(0).text()).toEqual('Bamboo'); expect(wrapper.find('.rc-cascader-menu-item-active').at(1).text()).toEqual('Little'); expect(wrapper.find('.rc-cascader-menu-item-active').at(2).text()).toEqual('Toy'); }); + + it('displayRender', () => { + const wrapper = mount( + + `${labels.join('->')} & ${selectOptions.map((opt: any) => opt.customValue).join('>>')}` + } + />, + ); + + expect(wrapper.find('.rc-cascader-selection-item').text()).toEqual( + 'Bamboo->Little->Toy & bamboo>>little>>toy', + ); + }); });