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: Typography copyable not sync #48347

Merged
merged 2 commits into from
Apr 9, 2024
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
18 changes: 18 additions & 0 deletions components/typography/__tests__/copy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,22 @@ describe('Typography copy', () => {
expect(result.current?.copyLoading).toBe(false);
});
});

it('not block copy text change', () => {
const spy = jest.spyOn(copyObj, 'default');

const renderDemo = (text: string) => (
<Base copyable={{ text }} component="p">
Text
</Base>
);

const { container, rerender } = render(renderDemo('Bamboo'));
rerender(renderDemo('Light'));

fireEvent.click(container.querySelector('.ant-typography-copy')!);
expect(spy.mock.calls[0][0]).toBe('Light');

spy.mockRestore();
});
});
52 changes: 28 additions & 24 deletions components/typography/hooks/useCopyClick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import copy from 'copy-to-clipboard';
import { useEvent } from 'rc-util';
zombieJ marked this conversation as resolved.
Show resolved Hide resolved

import type { CopyConfig } from '../Base';

Expand Down Expand Up @@ -29,33 +30,36 @@ const useCopyClick = ({

React.useEffect(() => cleanCopyId, []);

// Keep copy action up to date
const onClick = useEvent(async (e?: React.MouseEvent<HTMLDivElement>) => {
e?.preventDefault();
e?.stopPropagation();
setCopyLoading(true);
try {
const text =
typeof copyConfig.text === 'function' ? await copyConfig.text() : copyConfig.text;
copy(text || String(children) || '', copyOptions);
setCopyLoading(false);

setCopied(true);

// Trigger tips update
cleanCopyId();
copyIdRef.current = setTimeout(() => {
setCopied(false);
}, 3000);

copyConfig.onCopy?.(e);
} catch (error) {
setCopyLoading(false);
throw error;
}
});

return {
copied,
copyLoading,
onClick: async (e?: React.MouseEvent<HTMLDivElement>) => {
e?.preventDefault();
e?.stopPropagation();
setCopyLoading(true);
try {
const text =
typeof copyConfig.text === 'function' ? await copyConfig.text() : copyConfig.text;
copy(text || String(children) || '', copyOptions);
setCopyLoading(false);

setCopied(true);

// Trigger tips update
cleanCopyId();
copyIdRef.current = setTimeout(() => {
setCopied(false);
}, 3000);

copyConfig.onCopy?.(e);
} catch (error) {
setCopyLoading(false);
throw error;
}
},
onClick,
};
};

Expand Down