Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/@react-aria/dnd/src/useClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {useFocus} from '@react-aria/interactions';

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

e.preventDefault();
if (e.clipboardData) {
writeToDataTransfer(e.clipboardData, options.getItems());
writeToDataTransfer(e.clipboardData, options.getItems({type: 'copy'}));
options.onCopy?.();
}
});
Expand All @@ -106,7 +106,7 @@ export function useClipboard(options: ClipboardProps): ClipboardResult {

e.preventDefault();
if (e.clipboardData) {
writeToDataTransfer(e.clipboardData, options.getItems());
writeToDataTransfer(e.clipboardData, options.getItems({type: 'cut'}));
options.onCut();
}
});
Expand Down
37 changes: 37 additions & 0 deletions packages/@react-aria/dnd/test/useClipboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,42 @@ describe('useClipboard', () => {
expect(await onPaste.mock.calls[0][0][1].getText('test')).toBe('item 2');
expect(await onPaste.mock.calls[0][0][1].getText('text/plain')).toBe('item 2');
});

it('should show the type of the clipboard event if cutting', async () => {
let getItems = (details) => [{
[details.type]: 'test data'
}];

let onCut = jest.fn();
let tree = render(<Copyable getItems={getItems} onCut={onCut} />);
let button = tree.getByRole('button');

await user.tab();
expect(document.activeElement).toBe(button);

let clipboardData = new DataTransfer();
fireEvent(button, new ClipboardEvent('cut', {clipboardData}));
expect([...clipboardData.items]).toEqual([new DataTransferItem('cut', 'test data')]);
expect(onCut).toHaveBeenCalledTimes(1);
});

it('should show the type of the clipboard event if copying', async () => {
let getItems = (details) => [{
[details.type]: 'test data'
}];

let onCopy = jest.fn();
let tree = render(<Copyable getItems={getItems} onCopy={onCopy} />);
let button = tree.getByRole('button');

await user.tab();
expect(document.activeElement).toBe(button);

let clipboardData = new DataTransfer();
fireEvent(button, new ClipboardEvent('copy', {clipboardData}));
expect([...clipboardData.items]).toEqual([new DataTransferItem('copy', 'test data')]);
expect(onCopy).toHaveBeenCalledTimes(1);
});
});
});