Skip to content

Commit

Permalink
feat: support option render (#470)
Browse files Browse the repository at this point in the history
* feat: support option render

* feat: test

* feat: test

* feat: add option

* feat: 优化示例

* feat: 优化类型

* feat: review

* feat: review

* feat: 添加 level index

* feat: 删除 level index

* feat: disaled

* feat: opeion
  • Loading branch information
crazyair authored Mar 6, 2024
1 parent f31fa63 commit 1e95a21
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/demo/option-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: option-render
nav:
title: Demo
path: /demo
---

<code src="../../examples/option-render.tsx"></code>
74 changes: 74 additions & 0 deletions examples/option-render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import '../assets/index.less';
import Cascader from '../src';

const addressOptions = [
{
label: '福建',
title: '福建-fj',
value: 'fj',
children: [
{
label: '福州',
value: 'fuzhou',
children: [
{
label: '马尾',
value: 'mawei',
},
],
},
{
label: '泉州',
value: 'quanzhou',
},
],
},
{
label: '浙江',
value: 'zj',
title: '浙江-zj',
children: [
{
label: '杭州',
value: 'hangzhou',
children: [
{
label: '余杭',
value: 'yuhang',
},
],
},
],
},
{
label: '北京',
value: 'bj',
title: '北京-bj',
children: [
{
label: '朝阳区',
value: 'chaoyang',
},
{
label: '海淀区',
value: 'haidian',
},
],
},
];

const Demo = () => {
return (
<div>
<Cascader
options={addressOptions}
optionRender={option => {
return <label title={option.title}>{option.label}</label>;
}}
/>
</div>
);
};

export default Demo;
4 changes: 4 additions & 0 deletions src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionTyp
id?: string;
prefixCls?: string;
fieldNames?: FieldNames;
optionRender?: (option: OptionType) => React.ReactNode;
children?: React.ReactElement;

// Value
Expand Down Expand Up @@ -210,6 +211,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
children,
dropdownMatchSelectWidth = false,
showCheckedStrategy = SHOW_PARENT,
optionRender,
...restProps
} = props;

Expand Down Expand Up @@ -384,6 +386,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
expandIcon,
loadingIcon,
dropdownMenuColumnStyle,
optionRender,
}),
[
mergedOptions,
Expand All @@ -400,6 +403,7 @@ const Cascader = React.forwardRef<CascaderRef, InternalCascaderProps>((props, re
expandIcon,
loadingIcon,
dropdownMenuColumnStyle,
optionRender,
],
);

Expand Down
5 changes: 4 additions & 1 deletion src/OptionList/Column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function Column({
expandIcon,
loadingIcon,
dropdownMenuColumnStyle,
optionRender,
} = React.useContext(CascaderContext);

const hoverOpen = expandTrigger === 'hover';
Expand Down Expand Up @@ -197,7 +198,9 @@ export default function Column({
}}
/>
)}
<div className={`${menuItemPrefixCls}-content`}>{label}</div>
<div className={`${menuItemPrefixCls}-content`}>
{optionRender ? optionRender(option) : label}
</div>
{!isLoading && expandIcon && !isMergedLeaf && (
<div className={`${menuItemPrefixCls}-expand-icon`}>{expandIcon}</div>
)}
Expand Down
1 change: 1 addition & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface CascaderContextProps {
expandIcon?: React.ReactNode;
loadingIcon?: React.ReactNode;
dropdownMenuColumnStyle?: React.CSSProperties;
optionRender?: CascaderProps['optionRender'];
}

const CascaderContext = React.createContext<CascaderContextProps>(null);
Expand Down
22 changes: 22 additions & 0 deletions tests/search.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,26 @@ describe('Cascader.Search', () => {
'bamboo / little',
);
});
it('Should optionRender work', () => {
const { container, rerender } = render(
<Cascader
open
options={[{ label: 'bamboo', value: 'bamboo' }]}
optionRender={option => `${option.label} - test`}
/>,
);
expect(container.querySelector('.rc-cascader-menu-item-content').innerHTML).toEqual(
'bamboo - test',
);
rerender(
<Cascader
open
options={[{ label: 'bamboo', disabled: true, value: 'bamboo' }]}
optionRender={option => JSON.stringify(option)}
/>,
);
expect(container.querySelector('.rc-cascader-menu-item-content').innerHTML).toEqual(
'{"label":"bamboo","disabled":true,"value":"bamboo"}',
);
});
});

0 comments on commit 1e95a21

Please sign in to comment.