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

#2533 - allow useCopyToClipboard to receive options parameter #2542

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ export interface CopyToClipboardState {
error?: Error;
}

const useCopyToClipboard = (): [CopyToClipboardState, (value: string) => void] => {
const useCopyToClipboard = (): [
CopyToClipboardState,
(value: string, options?: object) => void
] => {
const isMounted = useMountedState();
const [state, setState] = useSetState<CopyToClipboardState>({
value: undefined,
error: undefined,
noUserInteraction: true,
});

const copyToClipboard = useCallback((value) => {
const copyToClipboard = useCallback((value, options = {}) => {
if (!isMounted()) {
return;
}
Expand Down Expand Up @@ -49,7 +52,7 @@ const useCopyToClipboard = (): [CopyToClipboardState, (value: string) => void] =
return;
}
normalizedValue = value.toString();
noUserInteraction = writeText(normalizedValue);
noUserInteraction = writeText(normalizedValue, options);
setState({
value: normalizedValue,
error: undefined,
Expand Down
15 changes: 14 additions & 1 deletion tests/useCopyToClipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ describe('useCopyToClipboard', () => {
expect(state.error).not.toBeDefined();
});

it('should pass given options to copy to clipboard', () => {
const testValue = 'test';
const testOptions = {
debug: true,
};
let [, copyToClipboard] = hook.result.current;
act(() => copyToClipboard(testValue, testOptions));
[, copyToClipboard] = hook.result.current;

expect(writeText).toBeCalled();
expect(writeText).toBeCalledWith(testValue, testOptions);
});

it('should not call writeText if passed an invalid input and set state', () => {
let testValue = {}; // invalid value
let [state, copyToClipboard] = hook.result.current;
Expand All @@ -67,7 +80,7 @@ describe('useCopyToClipboard', () => {
act(() => copyToClipboard(valueToRaiseMockException));
[state, copyToClipboard] = hook.result.current;

expect(writeText).toBeCalledWith(valueToRaiseMockException);
expect(writeText).toBeCalledWith(valueToRaiseMockException, {});
expect(state.value).toBe(valueToRaiseMockException);
expect(state.noUserInteraction).not.toBeDefined();
expect(state.error).toStrictEqual(new Error(valueToRaiseMockException));
Expand Down