Skip to content

Commit 42cfe68

Browse files
authored
feat: implemented details prop to the getItems function in the useClipboard hook (#7727)
* feat: implemented details prop to the getItems function in the useClipboard hook * fix: linting * trigger build with correct email
1 parent c78b248 commit 42cfe68

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/@react-aria/dnd/src/useClipboard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {useFocus} from '@react-aria/interactions';
1818

1919
export interface ClipboardProps {
2020
/** A function that returns the items to copy. */
21-
getItems?: () => DragItem[],
21+
getItems?: (details: {type: 'cut' | 'copy'}) => DragItem[],
2222
/** Handler that is called when the user triggers a copy interaction. */
2323
onCopy?: () => void,
2424
/** Handler that is called when the user triggers a cut interaction. */
@@ -88,7 +88,7 @@ export function useClipboard(options: ClipboardProps): ClipboardResult {
8888

8989
e.preventDefault();
9090
if (e.clipboardData) {
91-
writeToDataTransfer(e.clipboardData, options.getItems());
91+
writeToDataTransfer(e.clipboardData, options.getItems({type: 'copy'}));
9292
options.onCopy?.();
9393
}
9494
});
@@ -106,7 +106,7 @@ export function useClipboard(options: ClipboardProps): ClipboardResult {
106106

107107
e.preventDefault();
108108
if (e.clipboardData) {
109-
writeToDataTransfer(e.clipboardData, options.getItems());
109+
writeToDataTransfer(e.clipboardData, options.getItems({type: 'cut'}));
110110
options.onCut();
111111
}
112112
});

packages/@react-aria/dnd/test/useClipboard.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,42 @@ describe('useClipboard', () => {
363363
expect(await onPaste.mock.calls[0][0][1].getText('test')).toBe('item 2');
364364
expect(await onPaste.mock.calls[0][0][1].getText('text/plain')).toBe('item 2');
365365
});
366+
367+
it('should show the type of the clipboard event if cutting', async () => {
368+
let getItems = (details) => [{
369+
[details.type]: 'test data'
370+
}];
371+
372+
let onCut = jest.fn();
373+
let tree = render(<Copyable getItems={getItems} onCut={onCut} />);
374+
let button = tree.getByRole('button');
375+
376+
await user.tab();
377+
expect(document.activeElement).toBe(button);
378+
379+
let clipboardData = new DataTransfer();
380+
fireEvent(button, new ClipboardEvent('cut', {clipboardData}));
381+
expect([...clipboardData.items]).toEqual([new DataTransferItem('cut', 'test data')]);
382+
expect(onCut).toHaveBeenCalledTimes(1);
383+
});
384+
385+
it('should show the type of the clipboard event if copying', async () => {
386+
let getItems = (details) => [{
387+
[details.type]: 'test data'
388+
}];
389+
390+
let onCopy = jest.fn();
391+
let tree = render(<Copyable getItems={getItems} onCopy={onCopy} />);
392+
let button = tree.getByRole('button');
393+
394+
await user.tab();
395+
expect(document.activeElement).toBe(button);
396+
397+
let clipboardData = new DataTransfer();
398+
fireEvent(button, new ClipboardEvent('copy', {clipboardData}));
399+
expect([...clipboardData.items]).toEqual([new DataTransferItem('copy', 'test data')]);
400+
expect(onCopy).toHaveBeenCalledTimes(1);
401+
});
366402
});
367403
});
404+

0 commit comments

Comments
 (0)