Skip to content

Commit e16918b

Browse files
BoyYangzai洋
andauthored
feat: add Option item disableCheckbox (#408)
* feat: add uncheckableItemValues props * feat: add Option item disableCheckbox * feat: change style with disable className * chore: remove useless * chore: remove init false * test: add disableCheckbox test * test: add check fet test --------- Co-authored-by: 洋 <[email protected]>
1 parent 0babdea commit e16918b

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

examples/multiple.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ const optionLists = [
99
value: 'zhejiang',
1010
label: 'Zhejiang',
1111
isLeaf: false,
12+
disableCheckbox: true,
1213
},
1314
{
1415
value: 'jiangsu',
1516
label: 'Jiangsu',
1617
isLeaf: false,
18+
disableCheckbox: false
1719
},
1820
];
1921

@@ -35,10 +37,12 @@ const Demo = () => {
3537
{
3638
label: `${targetOption.label} Dynamic 1`,
3739
value: 'dynamic1',
40+
disableCheckbox: false,
3841
},
3942
{
4043
label: `${targetOption.label} Dynamic 2`,
4144
value: 'dynamic2',
45+
disableCheckbox: true,
4246
},
4347
];
4448
setOptions([...options]);

src/Cascader.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export interface DefaultOptionType extends BaseOptionType {
5454
label: React.ReactNode;
5555
value?: string | number | null;
5656
children?: DefaultOptionType[];
57+
disableCheckbox?: boolean;
5758
}
5859

5960
interface BaseCascaderProps<OptionType extends BaseOptionType = DefaultOptionType>

src/OptionList/Checkbox.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface CheckboxProps {
88
halfChecked?: boolean;
99
disabled?: boolean;
1010
onClick?: React.MouseEventHandler;
11+
disableCheckbox: boolean;
1112
}
1213

1314
export default function Checkbox({
@@ -16,6 +17,7 @@ export default function Checkbox({
1617
halfChecked,
1718
disabled,
1819
onClick,
20+
disableCheckbox,
1921
}: CheckboxProps) {
2022
const { checkable } = React.useContext(CascaderContext);
2123

@@ -26,7 +28,7 @@ export default function Checkbox({
2628
className={classNames(`${prefixCls}`, {
2729
[`${prefixCls}-checked`]: checked,
2830
[`${prefixCls}-indeterminate`]: !checked && halfChecked,
29-
[`${prefixCls}-disabled`]: disabled,
31+
[`${prefixCls}-disabled`]: disabled || disableCheckbox,
3032
})}
3133
onClick={onClick}
3234
>

src/OptionList/Column.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default function Column({
5959
const optionInfoList = React.useMemo(
6060
() =>
6161
options.map(option => {
62-
const { disabled } = option;
62+
const { disabled, disableCheckbox } = option;
6363
const searchOptions = option[SEARCH_MARK];
6464
const label = option[FIX_LABEL] ?? option[fieldNames.label];
6565
const value = option[fieldNames.value];
@@ -89,6 +89,7 @@ export default function Column({
8989
checked,
9090
halfChecked,
9191
option,
92+
disableCheckbox,
9293
fullPath,
9394
fullPathKey,
9495
};
@@ -111,6 +112,7 @@ export default function Column({
111112
option,
112113
fullPath,
113114
fullPathKey,
115+
disableCheckbox,
114116
}) => {
115117
// >>>>> Open
116118
const triggerOpenPath = () => {
@@ -182,6 +184,7 @@ export default function Column({
182184
checked={checked}
183185
halfChecked={halfChecked}
184186
disabled={disabled}
187+
disableCheckbox={disableCheckbox}
185188
onClick={(e: React.MouseEvent<HTMLSpanElement>) => {
186189
e.stopPropagation();
187190
triggerSelect();

tests/checkable.spec.tsx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable react/jsx-no-bind */
22

33
import React from 'react';
4-
import { mount } from './enzyme';
54
import Cascader from '../src';
65
import { addressOptions } from './demoOptions';
6+
import { mount } from './enzyme';
77

88
describe('Cascader.Checkable', () => {
99
const options = [
@@ -171,11 +171,48 @@ describe('Cascader.Checkable', () => {
171171
it('should work with custom checkable', () => {
172172
const wrapper = mount(
173173
<Cascader
174-
checkable={<span className="my-custom-checkbox" >0</span>}
174+
checkable={<span className="my-custom-checkbox">0</span>}
175175
open
176176
options={addressOptions}
177177
/>,
178178
);
179179
expect(wrapper.find('.my-custom-checkbox')).toHaveLength(3);
180180
});
181+
182+
it('should be correct expression with disableCheckbox', () => {
183+
const wrapper = mount(
184+
<Cascader
185+
checkable={true}
186+
open
187+
options={[
188+
{
189+
label: '台湾',
190+
value: 'tw',
191+
192+
children: [
193+
{
194+
label: '福建',
195+
value: 'fj',
196+
disableCheckbox: true,
197+
},
198+
{
199+
label: '兰州',
200+
value: 'lz',
201+
},
202+
{ label: '北京', value: 'bj' },
203+
],
204+
},
205+
]}
206+
/>,
207+
);
208+
209+
// disabled className
210+
wrapper.find('.rc-cascader-menu-item').simulate('click');
211+
expect(wrapper.find('.rc-cascader-checkbox-disabled')).toHaveLength(1);
212+
213+
// Check all children except disableCheckbox When the parent checkbox is checked
214+
expect(wrapper.find('.rc-cascader-checkbox')).toHaveLength(4);
215+
wrapper.find('.rc-cascader-checkbox').first().simulate('click');
216+
expect(wrapper.find('.rc-cascader-checkbox-checked')).toHaveLength(3);
217+
});
181218
});

0 commit comments

Comments
 (0)