偏右
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
30 additions and
2 deletions
-
components/tag/CheckableTag.tsx
-
components/tag/__tests__/index.test.js
|
|
@ -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; |
|
|
|
|
|
@ -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(); |
|
|
|