Skip to content

Commit

Permalink
fix: label with ReactNode (#232)
Browse files Browse the repository at this point in the history
* fix: label with ReactNode

* fix: label with ReactNode

* test: coverage
  • Loading branch information
zombieJ authored Dec 29, 2021
1 parent 9fe8180 commit 60dc6e0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
11 changes: 8 additions & 3 deletions examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Cascader from '../src';

const addressOptions = [
{
label: '空孩子',
label: <span>空孩子</span>,
value: 'empty',
children: [],
},
Expand Down Expand Up @@ -91,7 +91,7 @@ const addressOptions = [
const defaultValue = ['not', 'exist'];

const Demo = () => {
const [multiple, setMultiple] = React.useState(false);
const [multiple, setMultiple] = React.useState(true);
const [, setInputValue] = React.useState('');

const onChange = (value: any, selectedOptions: any) => {
Expand Down Expand Up @@ -119,10 +119,15 @@ const Demo = () => {
checkable={multiple}
allowClear
// defaultValue={multiple ? [defaultValue] : defaultValue}
defaultValue={[['not', 'yet'], ['exist']]}
// defaultValue={[['not', 'yet'], ['exist']]}
defaultValue={[['empty']]}
showSearch
// showSearch={{ limit: 1 }}
open
// tagRender={props => {
// console.log(props);
// return props.label as any;
// }}
// direction="rtl"
// searchValue="福a"
// changeOnSelect
Expand Down
21 changes: 20 additions & 1 deletion src/hooks/useDisplayValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,26 @@ export default (
rawValues: SingleValueType[],
options: DefaultOptionType[],
fieldNames: InternalFieldNames,
displayRender: CascaderProps['displayRender'] = labels => labels.join(' / '),
displayRender: CascaderProps['displayRender'] = labels => {
const SPLIT = ' / ';

if (labels.every(label => ['string', 'number'].includes(typeof label))) {
return labels.join(SPLIT);
}

// If exist non-string value, use ReactNode instead
return labels.reduce((list, label, index) => {
const keyedLabel = React.isValidElement(label)
? React.cloneElement(label, { key: index })
: label;

if (index === 0) {
return [keyedLabel];
}

return [...list, SPLIT, keyedLabel];
}, []);
},
) => {
return React.useMemo(() => {
return rawValues.map(valueCells => {
Expand Down
31 changes: 31 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,35 @@ describe('Cascader.Basic', () => {
expect(onValueChange).toHaveBeenCalledWith(['parent'], expect.anything());
expect(wrapper.find('ul.rc-cascader-menu')).toHaveLength(1);
});

it('ReactNode label should not be [object]', () => {
const wrapper = mount(
<Cascader
options={[
{ label: <span>Parent</span>, value: 'parent' },
{
label: 'Normal',
value: 'normal',
children: [
{
label: <span>Child</span>,
value: 'child',
},
{
label: 'Child2',
value: 'child2',
},
],
},
]}
value={[['parent'], ['normal', 'child']]}
checkable
/>,
);

expect(wrapper.find('.rc-cascader-selection-item-content').first().text()).toEqual('Parent');
expect(wrapper.find('.rc-cascader-selection-item-content').last().text()).toEqual(
'Normal / Child',
);
});
});

1 comment on commit 60dc6e0

@vercel
Copy link

@vercel vercel bot commented on 60dc6e0 Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.