Skip to content

Commit

Permalink
fix: Cascade should not show checkbox when options is empty (#224)
Browse files Browse the repository at this point in the history
* fix: checkbox should hide when options is empty

* test: add test case
  • Loading branch information
afc163 authored Dec 15, 2021
1 parent 72da52c commit 197c81e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ const Cascader = React.forwardRef((props: CascaderProps, ref: React.Ref<Cascader
showSearch={mergedShowSearch}
onSearch={setMergedSearch}
labelRender={labelRender}
{...{
getRawInputElement: () => children,
}}
getRawInputElement={() => children}
/>
</CascaderContext.Provider>
);
Expand Down
4 changes: 3 additions & 1 deletion src/OptionList/Column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ColumnProps {
checkedSet: Set<React.Key>;
halfCheckedSet: Set<React.Key>;
loadingKeys: React.Key[];
isEmpty: boolean;
}

export default function Column({
Expand All @@ -32,6 +33,7 @@ export default function Column({
checkedSet,
halfCheckedSet,
loadingKeys,
isEmpty,
}: ColumnProps) {
const menuPrefixCls = `${prefixCls}-menu`;
const menuItemPrefixCls = `${prefixCls}-menu-item`;
Expand Down Expand Up @@ -107,7 +109,7 @@ export default function Column({
}
}}
>
{multiple && (
{multiple && !isEmpty && (
<Checkbox
prefixCls={`${prefixCls}-checkbox`}
checked={checked}
Expand Down
1 change: 1 addition & 0 deletions src/OptionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ const RefOptionList = React.forwardRef<RefOptionListProps, OptionListProps>((pro
key={index}
index={index}
{...columnProps}
isEmpty={isEmpty}
prefixCls={mergedPrefixCls}
options={col.options}
openKey={mergedOpenPath[index]}
Expand Down
7 changes: 3 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Cascader, { CascaderProps } from './Cascader';
import { FieldNames, DataNode, ShowSearchType } from './interface';

export { CascaderProps, FieldNames, DataNode, ShowSearchType };
import Cascader from './Cascader';

export type { CascaderProps } from './Cascader';
export type { FieldNames, DataNode, ShowSearchType } from './interface';
export default Cascader;
13 changes: 13 additions & 0 deletions tests/checkable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('Cascader.Checkable', () => {
],
);
});

it('click checkobx invoke one onChange', () => {
const onChange = jest.fn();
const wrapper = mount(<Cascader options={options} onChange={onChange} open checkable />);
Expand All @@ -89,4 +90,16 @@ describe('Cascader.Checkable', () => {
expect(wrapper.exists('.rc-cascader-checkbox-checked')).toBeTruthy();
expect(onChange).toHaveBeenCalledTimes(1);
});

// https://github.com/ant-design/ant-design/issues/33302
it('should not display checkbox when children is empty', () => {
const wrapper = mount(
<Cascader checkable options={[]}>
<input readOnly />
</Cascader>,
);
wrapper.find('input').simulate('click');
const menus = wrapper.find('.rc-cascader-menu');
expect(menus.find('.rc-cascader-checkbox').length).toBe(0);
});
});

1 comment on commit 197c81e

@vercel
Copy link

@vercel vercel bot commented on 197c81e Dec 15, 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.