Skip to content

Commit 19cf778

Browse files
grahamsmtrolandjitsu
authored andcommitted
fix(examples): rejectedFiles to fileRejections
1 parent 08a89cf commit 19cf778

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

examples/accept/README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,44 @@ import React from 'react';
1414
import {useDropzone} from 'react-dropzone';
1515

1616
function Accept(props) {
17-
const {acceptedFiles, rejectedFiles, getRootProps, getInputProps} = useDropzone({
17+
const {
18+
acceptedFiles,
19+
fileRejections,
20+
getRootProps,
21+
getInputProps
22+
} = useDropzone({
1823
accept: 'image/jpeg, image/png'
1924
});
20-
21-
const acceptedFilesItems = acceptedFiles.map(file => (
25+
26+
const acceptedFileItems = acceptedFiles.map(file => (
2227
<li key={file.path}>
2328
{file.path} - {file.size} bytes
2429
</li>
2530
));
2631

27-
const rejectedFilesItems = rejectedFiles.map(file => (
32+
const fileRejectionItems = fileRejections.map({ file, errors}) => (
2833
<li key={file.path}>
2934
{file.path} - {file.size} bytes
35+
<ul>
36+
{errors.map(e => (
37+
<li key={e.code}>{e.message}</li>
38+
)}
39+
</ul>
3040
</li>
3141
));
3242

3343
return (
3444
<section className="container">
35-
<div {...getRootProps({className: 'dropzone'})}>
45+
<div {...getRootProps({ className: 'dropzone' })}>
3646
<input {...getInputProps()} />
3747
<p>Drag 'n' drop some files here, or click to select files</p>
3848
<em>(Only *.jpeg and *.png images will be accepted)</em>
3949
</div>
4050
<aside>
4151
<h4>Accepted files</h4>
42-
<ul>
43-
{acceptedFilesItems}
44-
</ul>
52+
<ul>{acceptedFileItems}</ul>
4553
<h4>Rejected files</h4>
46-
<ul>
47-
{rejectedFilesItems}
48-
</ul>
54+
<ul>{fileRejectionItems}</ul>
4955
</aside>
5056
</section>
5157
);

src/index.spec.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,29 +1920,49 @@ describe('useDropzone() hook', () => {
19201920
expect(onDropSpy).toHaveBeenCalledWith(files, [], expect.anything())
19211921
})
19221922

1923-
it('sets {acceptedFiles, rejectedFiles}', async () => {
1924-
const FileList = (props = { files: [] }) => (
1923+
it('sets {acceptedFiles, fileRejections}', async () => {
1924+
const FileList = ({ files = [] }) => (
19251925
<ul>
1926-
{props.files.map(file => (
1927-
<li key={file.name} data-type={props.type}>
1926+
{files.map(file => (
1927+
<li key={file.name} data-type={'accepted'}>
19281928
{file.name}
19291929
</li>
19301930
))}
19311931
</ul>
19321932
)
19331933

1934+
const RejectedFileList = ({ fileRejections = [] }) => (
1935+
<ul>
1936+
{fileRejections.map(({ file, errors }) => (
1937+
<li key={file.name}>
1938+
<span data-type={"rejected"}>{file.name}</span>
1939+
<ul>
1940+
{errors.map(e => (
1941+
<li key={e.code} data-type={"error"}>
1942+
{e.code}
1943+
</li>
1944+
))}
1945+
</ul>
1946+
</li>
1947+
))}
1948+
</ul>
1949+
)
1950+
19341951
const getAcceptedFiles = node => node.querySelectorAll(`[data-type="accepted"]`)
19351952
const getRejectedFiles = node => node.querySelectorAll(`[data-type="rejected"]`)
1953+
const getRejectedFilesErrors = node => node.querySelectorAll(`[data-type="error"]`)
19361954
const matchToFiles = (fileList, files) =>
19371955
Array.from(fileList).every(item => !!files.find(file => file.name === item.textContent))
1956+
const matchToErrorCode = (errorList, code) =>
1957+
Array.from(errorList).every(item => item.textContent === code)
19381958

19391959
const ui = (
19401960
<Dropzone accept="image/*">
19411961
{({ getRootProps, getInputProps, acceptedFiles, fileRejections }) => (
19421962
<div {...getRootProps()}>
19431963
<input {...getInputProps()} />
1944-
<FileList files={acceptedFiles} type="accepted" />
1945-
<FileList files={fileRejections.map(rejection => rejection.file)} type="rejected" />
1964+
<FileList files={acceptedFiles} />
1965+
<RejectedFileList fileRejections={fileRejections} />
19461966
</div>
19471967
)}
19481968
</Dropzone>
@@ -1961,8 +1981,12 @@ describe('useDropzone() hook', () => {
19611981
const rejectedFileList = getRejectedFiles(dropzone)
19621982
expect(rejectedFileList).toHaveLength(files.length)
19631983
expect(matchToFiles(rejectedFileList, files)).toBe(true)
1984+
const rejectedFileErrorList = getRejectedFilesErrors(dropzone)
1985+
expect(rejectedFileErrorList).toHaveLength(files.length)
1986+
expect(matchToErrorCode(rejectedFileErrorList, 'file-invalid-type')).toBe(true)
19641987
})
19651988

1989+
19661990
it('resets {isDragActive, isDragAccept, isDragReject}', async () => {
19671991
const ui = (
19681992
<Dropzone>

typings/tests/all.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export default class Test extends React.Component {
66
return (
77
<div>
88
<Dropzone
9-
onDrop={(acceptedFiles, rejectedFiles, event) =>
10-
console.log(acceptedFiles, rejectedFiles, event)}
9+
onDrop={(acceptedFiles, fileRejections, event) =>
10+
console.log(acceptedFiles, fileRejections, event)}
1111
onDragEnter={event => console.log(event)}
1212
onDragOver={event => console.log(event)}
1313
onDragLeave={event => console.log(event)}

typings/tests/events.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export class Events extends React.Component {
77
<section>
88
<div className="dropzone">
99
<Dropzone
10-
onDrop={(acceptedFiles, rejectedFiles, event) =>
11-
console.log(acceptedFiles, rejectedFiles, event)}
10+
onDrop={(acceptedFiles, fileRejections, event) =>
11+
console.log(acceptedFiles, fileRejections, event)}
1212
onDragEnter={event => console.log(event)}
1313
onDragOver={event => console.log(event)}
1414
onDragLeave={event => console.log(event)}

0 commit comments

Comments
 (0)