From 769262666ecf03fb4ca4b1d2c7c7333f10dc0a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Wed, 16 Oct 2024 22:57:24 +0800 Subject: [PATCH 01/12] feat: add disabled api for panel --- src/OptionList/Column.tsx | 12 ++++++++---- src/OptionList/List.tsx | 27 +++++++++++++++++++++++---- src/Panel.tsx | 5 ++++- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/OptionList/Column.tsx b/src/OptionList/Column.tsx index 5805ab1c..1acaedfe 100644 --- a/src/OptionList/Column.tsx +++ b/src/OptionList/Column.tsx @@ -23,6 +23,7 @@ export interface ColumnProps; loadingKeys: React.Key[]; isSelectable: (option: DefaultOptionType) => boolean; + panelDisabled?: boolean; } export default function Column({ @@ -38,6 +39,7 @@ export default function Column) { const menuPrefixCls = `${prefixCls}-menu`; const menuItemPrefixCls = `${prefixCls}-menu-item`; @@ -54,6 +56,8 @@ export default function Column panelDisabled || optionDisabled; + // ============================ Option ============================ const optionInfoList = React.useMemo( () => @@ -115,7 +119,7 @@ export default function Column { // >>>>> Open const triggerOpenPath = () => { - if (disabled) { + if (isOptionDisabled(disabled)) { return; } const nextValueCells = [...fullPath]; @@ -127,7 +131,7 @@ export default function Column>>>> Selection const triggerSelect = () => { - if (isSelectable(option)) { + if (isSelectable(option) && !isOptionDisabled(disabled)) { onSelect(fullPath, isMergedLeaf); } }; @@ -148,7 +152,7 @@ export default function Column) => { if (disableCheckbox) { diff --git a/src/OptionList/List.tsx b/src/OptionList/List.tsx index 0850b016..3512c517 100644 --- a/src/OptionList/List.tsx +++ b/src/OptionList/List.tsx @@ -21,11 +21,27 @@ import useKeyboard from './useKeyboard'; export type RawOptionListProps = Pick< ReturnType, - 'prefixCls' | 'multiple' | 'searchValue' | 'toggleOpen' | 'notFoundContent' | 'direction' | 'open' + | 'prefixCls' + | 'multiple' + | 'searchValue' + | 'toggleOpen' + | 'notFoundContent' + | 'direction' + | 'open' + | 'disabled' >; const RawOptionList = React.forwardRef((props, ref) => { - const { prefixCls, multiple, searchValue, toggleOpen, notFoundContent, direction, open } = props; + const { + prefixCls, + multiple, + searchValue, + toggleOpen, + notFoundContent, + direction, + open, + disabled: panelDisabled, + } = props; const containerRef = React.useRef(null); const rtl = direction === 'rtl'; @@ -100,10 +116,11 @@ const RawOptionList = React.forwardRef(( }; const isSelectable = (option: DefaultOptionType) => { - const { disabled } = option; + if (panelDisabled) return false; + const { disabled: optionDisabled } = option; const isMergedLeaf = isLeaf(option, fieldNames); - return !disabled && (isMergedLeaf || changeOnSelect || multiple); + return !optionDisabled && (isMergedLeaf || changeOnSelect || multiple); }; const onPathSelect = (valuePath: SingleValueType, leaf: boolean, fromKeyboard = false) => { @@ -203,6 +220,7 @@ const RawOptionList = React.forwardRef(( halfCheckedSet, loadingKeys, isSelectable, + panelDisabled, }; // >>>>> Columns @@ -220,6 +238,7 @@ const RawOptionList = React.forwardRef(( options={col.options} prevValuePath={prevValuePath} activeValue={activeValue} + panelDisabled={panelDisabled} /> ); }); diff --git a/src/Panel.tsx b/src/Panel.tsx index 9fa7e6dd..15e36b83 100644 --- a/src/Panel.tsx +++ b/src/Panel.tsx @@ -35,7 +35,8 @@ export type PickType = | 'className' | 'style' | 'direction' - | 'notFoundContent'; + | 'notFoundContent' + | 'disabled'; export type PanelProps< OptionType extends DefaultOptionType = DefaultOptionType, @@ -68,6 +69,7 @@ export default function Panel< loadingIcon, direction, notFoundContent = 'Not Found', + disabled, } = props as Pick; // ======================== Multiple ======================== @@ -200,6 +202,7 @@ export default function Panel< toggleOpen={noop} open direction={direction} + disabled={disabled} /> )} From 75fdf3163cb6ac72dc308b510431c0c8e4aec586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Wed, 16 Oct 2024 23:20:31 +0800 Subject: [PATCH 02/12] feat: add test case --- tests/Panel.spec.tsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Panel.spec.tsx b/tests/Panel.spec.tsx index 452f1be4..be76d679 100644 --- a/tests/Panel.spec.tsx +++ b/tests/Panel.spec.tsx @@ -97,4 +97,17 @@ describe('Cascader.Panel', () => { const checkedLi = container.querySelector('[aria-checked="true"]'); expect(checkedLi?.textContent).toEqual('Little'); }); + + it('disabled', () => { + const onChange = jest.fn(); + const { container } = render( + , + ); + + expect(container.querySelector('.rc-cascader-panel-disabled')).toBeTruthy(); + + const selectOption = container.querySelector('.rc-cascader-menu-item')!; + fireEvent.click(selectOption); + expect(onChange).not.toHaveBeenCalled(); + }); }); From 3f41343824b96bfd6a55512a7dbe530862d1f10f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Wed, 16 Oct 2024 23:36:34 +0800 Subject: [PATCH 03/12] docs: add demo --- examples/panel.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/panel.tsx b/examples/panel.tsx index 1f985476..5adcb438 100644 --- a/examples/panel.tsx +++ b/examples/panel.tsx @@ -92,6 +92,8 @@ export default () => { + + ); From 62d5c1885a6d3c3f74b85e268fc31353d00f7ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Thu, 17 Oct 2024 10:15:38 +0800 Subject: [PATCH 04/12] fix: test case fixed --- tests/Panel.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Panel.spec.tsx b/tests/Panel.spec.tsx index be76d679..c830a9aa 100644 --- a/tests/Panel.spec.tsx +++ b/tests/Panel.spec.tsx @@ -104,7 +104,7 @@ describe('Cascader.Panel', () => { , ); - expect(container.querySelector('.rc-cascader-panel-disabled')).toBeTruthy(); + expect(container.querySelector('.rc-cascader-menu-item-disabled')).toBeTruthy(); const selectOption = container.querySelector('.rc-cascader-menu-item')!; fireEvent.click(selectOption); From cb771fe0b40390b7b36243f6a5c653a46b1de471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Thu, 17 Oct 2024 11:12:28 +0800 Subject: [PATCH 05/12] feat: adjust demo position --- examples/panel.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/panel.tsx b/examples/panel.tsx index 5adcb438..8bc8617b 100644 --- a/examples/panel.tsx +++ b/examples/panel.tsx @@ -90,10 +90,10 @@ export default () => { }} /> - - + + ); From 243125acee31979a3484c1b525df88601cda507f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Thu, 17 Oct 2024 11:15:33 +0800 Subject: [PATCH 06/12] feat: improve demo --- examples/panel.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/panel.tsx b/examples/panel.tsx index 8bc8617b..0e846099 100644 --- a/examples/panel.tsx +++ b/examples/panel.tsx @@ -61,6 +61,8 @@ export default () => { const [value2, setValue2] = React.useState([]); + const [disabled, setDisabled] = React.useState(false); + return ( <>

Panel

@@ -71,6 +73,13 @@ export default () => { > Set Value + { console.log('Change:', nextValue); setValue(nextValue); }} + disabled={disabled} /> { console.log('Change:', nextValue); setValue2(nextValue); }} + disabled={disabled} /> - - From ef96118c9c89985e62fc7fef9bec1ee658dc511d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= Date: Sat, 26 Oct 2024 14:14:35 +0800 Subject: [PATCH 07/12] chore: reorganize code for better readability --- examples/panel.tsx | 2 +- src/OptionList/Column.tsx | 6 +++--- src/OptionList/List.tsx | 7 +++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/panel.tsx b/examples/panel.tsx index 0e846099..c0c51353 100644 --- a/examples/panel.tsx +++ b/examples/panel.tsx @@ -101,7 +101,7 @@ export default () => { disabled={disabled} /> - + diff --git a/src/OptionList/Column.tsx b/src/OptionList/Column.tsx index 1acaedfe..37a2de8c 100644 --- a/src/OptionList/Column.tsx +++ b/src/OptionList/Column.tsx @@ -23,7 +23,7 @@ export interface ColumnProps; loadingKeys: React.Key[]; isSelectable: (option: DefaultOptionType) => boolean; - panelDisabled?: boolean; + disabled?: boolean; } export default function Column({ @@ -39,7 +39,7 @@ export default function Column) { const menuPrefixCls = `${prefixCls}-menu`; const menuItemPrefixCls = `${prefixCls}-menu-item`; @@ -56,7 +56,7 @@ export default function Column panelDisabled || optionDisabled; + const isOptionDisabled = (optionDisabled?: boolean) => disabled || optionDisabled; // ============================ Option ============================ const optionInfoList = React.useMemo( diff --git a/src/OptionList/List.tsx b/src/OptionList/List.tsx index 3512c517..676ecfcb 100644 --- a/src/OptionList/List.tsx +++ b/src/OptionList/List.tsx @@ -40,7 +40,7 @@ const RawOptionList = React.forwardRef(( notFoundContent, direction, open, - disabled: panelDisabled, + disabled, } = props; const containerRef = React.useRef(null); @@ -116,10 +116,11 @@ const RawOptionList = React.forwardRef(( }; const isSelectable = (option: DefaultOptionType) => { - if (panelDisabled) return false; + if (disabled) return false; const { disabled: optionDisabled } = option; const isMergedLeaf = isLeaf(option, fieldNames); + return !optionDisabled && (isMergedLeaf || changeOnSelect || multiple); }; @@ -220,7 +221,6 @@ const RawOptionList = React.forwardRef(( halfCheckedSet, loadingKeys, isSelectable, - panelDisabled, }; // >>>>> Columns @@ -238,7 +238,6 @@ const RawOptionList = React.forwardRef(( options={col.options} prevValuePath={prevValuePath} activeValue={activeValue} - panelDisabled={panelDisabled} /> ); }); From 9e1323286bfb5c89797615b705b6dc288743c536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= <1844749591@qq.com> Date: Sun, 27 Oct 2024 21:07:40 +0800 Subject: [PATCH 08/12] test: update snapshot --- tests/__snapshots__/search.spec.tsx.snap | 38 +++++++++++------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/tests/__snapshots__/search.spec.tsx.snap b/tests/__snapshots__/search.spec.tsx.snap index a08aaac7..8fa4d0e4 100644 --- a/tests/__snapshots__/search.spec.tsx.snap +++ b/tests/__snapshots__/search.spec.tsx.snap @@ -8,30 +8,26 @@ exports[`Cascader.Search should correct render Cascader with same field name of class="rc-cascader-selector" > - - - - +