Skip to content

Commit 1ad93d6

Browse files
committed
feat: allowPasteUpload
1 parent 4efc79a commit 1ad93d6

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ React.render(<Upload />, container);
8484
|customRequest | function | null | provide an override for the default xhr behavior for additional customization|
8585
|withCredentials | boolean | false | ajax upload with cookie send |
8686
|openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |
87+
|allowPasteUpload | boolean | false | support paste upload |
8788

8889
#### onError arguments
8990

src/AjaxUploader.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class AjaxUploader extends Component<UploadProps> {
2828

2929
private fileInput: HTMLInputElement;
3030

31-
private isMouseEnter: boolean;
32-
3331
private _isMounted: boolean;
3432

3533
onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -106,14 +104,21 @@ class AjaxUploader extends Component<UploadProps> {
106104
};
107105

108106
onPrePaste(e: ClipboardEvent) {
109-
if (this.isMouseEnter) {
107+
const { allowPasteUpload } = this.props;
108+
109+
if (allowPasteUpload) {
110110
this.onFileDropOrPaste(e);
111111
}
112112
}
113113

114114
componentDidMount() {
115115
this._isMounted = true;
116-
document.addEventListener('paste', this.onPrePaste.bind(this));
116+
117+
const { allowPasteUpload } = this.props;
118+
119+
if (allowPasteUpload) {
120+
document.addEventListener('paste', this.onPrePaste.bind(this));
121+
}
117122
}
118123

119124
componentWillUnmount() {
@@ -280,18 +285,6 @@ class AjaxUploader extends Component<UploadProps> {
280285
this.fileInput = node;
281286
};
282287

283-
handleMouseEnter = (e: React.MouseEvent<HTMLDivElement>) => {
284-
this.isMouseEnter = true;
285-
286-
this.props.onMouseEnter?.(e);
287-
};
288-
289-
handleMouseLeave = (e: React.MouseEvent<HTMLDivElement>) => {
290-
this.isMouseEnter = false;
291-
292-
this.props.onMouseLeave?.(e);
293-
};
294-
295288
render() {
296289
const {
297290
component: Tag,
@@ -309,6 +302,8 @@ class AjaxUploader extends Component<UploadProps> {
309302
children,
310303
directory,
311304
openFileDialogOnClick,
305+
onMouseEnter,
306+
onMouseLeave,
312307
hasControlInside,
313308
...otherProps
314309
} = this.props;
@@ -326,8 +321,8 @@ class AjaxUploader extends Component<UploadProps> {
326321
: {
327322
onClick: openFileDialogOnClick ? this.onClick : () => {},
328323
onKeyDown: openFileDialogOnClick ? this.onKeyDown : () => {},
329-
onMouseEnter: this.handleMouseEnter,
330-
onMouseLeave: this.handleMouseLeave,
324+
onMouseEnter,
325+
onMouseLeave,
331326
onDrop: this.onFileDropOrPaste,
332327
onDragOver: this.onFileDropOrPaste,
333328
tabIndex: hasControlInside ? undefined : '0',

src/interface.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface UploadProps
4444
input?: React.CSSProperties;
4545
};
4646
hasControlInside?: boolean;
47+
allowPasteUpload?: boolean;
4748
}
4849

4950
export interface UploadProgressEvent extends Partial<ProgressEvent> {

tests/uploader.spec.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ describe('uploader', () => {
350350
});
351351

352352
it('paste to upload', done => {
353-
const rcUpload = uploader.container.querySelector('.rc-upload')!;
354-
const input = uploader.container.querySelector('input')!;
353+
const { container } = render(<Upload {...props} allowPasteUpload />);
354+
const input = container.querySelector('input')!;
355355

356356
const files = [
357357
{
@@ -373,7 +373,6 @@ describe('uploader', () => {
373373
done(err);
374374
};
375375

376-
fireEvent.mouseEnter(rcUpload);
377376
fireEvent.paste(input, {
378377
clipboardData: { files },
379378
});
@@ -407,8 +406,7 @@ describe('uploader', () => {
407406
});
408407

409408
it('paste files with multiple false', done => {
410-
const { container } = render(<Upload {...props} multiple={false} />);
411-
const rcUpload = container.querySelector('.rc-upload')!;
409+
const { container } = render(<Upload {...props} multiple={false} allowPasteUpload />);
412410
const input = container.querySelector('input')!;
413411
const files = [
414412
new File([''], 'success.png', { type: 'image/png' }),
@@ -441,7 +439,6 @@ describe('uploader', () => {
441439
value: files,
442440
});
443441

444-
fireEvent.mouseEnter(rcUpload);
445442
fireEvent.paste(input, { clipboardData: { files } });
446443

447444
setTimeout(() => {
@@ -783,7 +780,8 @@ describe('uploader', () => {
783780
});
784781

785782
it('paste directory', done => {
786-
const rcUpload = uploader.container.querySelector('.rc-upload')!;
783+
const { container } = render(<Upload {...props} allowPasteUpload />);
784+
const rcUpload = container.querySelector('.rc-upload')!;
787785
const files = {
788786
name: 'foo',
789787
children: [

0 commit comments

Comments
 (0)