Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CheckableTag support ref (#45070) #45164

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/tag/CheckableTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface CheckableTagProps {
onClick?: (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => void;
}

const CheckableTag: React.FC<CheckableTagProps> = (props) => {
const CheckableTag = React.forwardRef<HTMLSpanElement, CheckableTagProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
style,
Expand Down Expand Up @@ -54,6 +54,7 @@ const CheckableTag: React.FC<CheckableTagProps> = (props) => {
return wrapSSR(
<span
{...restProps}
ref={ref}
style={{
...style,
...tag?.style,
Expand All @@ -62,6 +63,6 @@ const CheckableTag: React.FC<CheckableTagProps> = (props) => {
onClick={handleClick}
/>,
);
};
});

export default CheckableTag;
15 changes: 15 additions & 0 deletions components/tag/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ describe('Tag', () => {
fireEvent.click(container.querySelectorAll('.ant-tag')[0]);
expect(onChange).toHaveBeenCalledWith(true);
});

it('should support ref', () => {
const ref = React.createRef<HTMLSpanElement>();
const { container } = render(
<Tag.CheckableTag checked={false} ref={ref}>
Tag Text
</Tag.CheckableTag>,
);
const refElement = ref.current;
const queryTarget = container.querySelector('.ant-tag');
expect(refElement instanceof HTMLSpanElement).toBe(true);
expect(refElement?.textContent).toBe('Tag Text');
expect(queryTarget?.textContent).toBe('Tag Text');
expect(refElement).toBe(queryTarget);
});
});
it('should onClick is undefined', async () => {
const { container } = render(<Tag onClick={undefined} />);
Expand Down