Browse Source

fix: Tag.CheckableTag don't support onClick (#24743)

close #24733
pull/24741/head
偏右 5 years ago
committed by GitHub
parent
commit
6f36f4a877
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      components/tag/CheckableTag.tsx
  2. 24
      components/tag/__tests__/index.test.js

8
components/tag/CheckableTag.tsx

@ -8,15 +8,19 @@ export interface CheckableTagProps {
style?: React.CSSProperties;
checked: boolean;
onChange?: (checked: boolean) => void;
onClick?: (e: React.MouseEventHandler<HTMLElement>) => void;
}
const CheckableTag: React.FC<CheckableTagProps> = props => {
const { getPrefixCls } = React.useContext(ConfigContext);
const handleClick = () => {
const { checked, onChange } = props;
const handleClick = (e: React.MouseEventHandler) => {
const { checked, onChange, onClick } = props;
if (onChange) {
onChange(!checked);
}
if (onClick) {
onClick(e);
}
};
const { prefixCls: customizePrefixCls, className, checked, ...restProps } = props;

24
components/tag/__tests__/index.test.js

@ -42,6 +42,30 @@ describe('Tag', () => {
expect(wrapper.find('.ant-tag:not(.ant-tag-hidden)').length).toBe(1);
});
it('should trigger onClick', () => {
const onClick = jest.fn();
const wrapper = mount(<Tag onClick={onClick} />);
wrapper.find('.ant-tag').simulate('click');
expect(onClick).toHaveBeenCalledWith(
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
);
});
it('should trigger onClick on CheckableTag', () => {
const onClick = jest.fn();
const wrapper = mount(<Tag.CheckableTag onClick={onClick} />);
wrapper.find('.ant-tag').simulate('click');
expect(onClick).toHaveBeenCalledWith(
expect.objectContaining({
type: 'click',
preventDefault: expect.any(Function),
}),
);
});
// https://github.com/ant-design/ant-design/issues/20344
it('should not trigger onClick when click close icon', () => {
const onClose = jest.fn();

Loading…
Cancel
Save