Skip to content

Commit 7a5c51e

Browse files
authored
revert: "fix(upload): apply accept attribute" (#563)
This reverts commit d513d6e.
1 parent 985d1a7 commit 7a5c51e

File tree

2 files changed

+4
-78
lines changed

2 files changed

+4
-78
lines changed

src/__tests__/upload.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -163,55 +163,3 @@ test('should call onChange/input bubbling up the event when a file is selected',
163163
expect(onInputInput).toHaveBeenCalledTimes(1)
164164
expect(onInputForm).toHaveBeenCalledTimes(1)
165165
})
166-
167-
test('should upload file with accepted format', () => {
168-
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
169-
const {element} = setup('<input type="file" accept="image/png" />')
170-
171-
userEvent.upload(element, file)
172-
173-
expect(element.files).toHaveLength(1)
174-
})
175-
176-
test('should upload multiple files with accepted format', () => {
177-
const files = [
178-
new File(['hello'], 'hello.png', {type: 'image/png'}),
179-
new File(['there'], 'there.jpg', {type: 'audio/mp3'}),
180-
new File(['there'], 'there.csv', {type: 'text/csv'}),
181-
new File(['there'], 'there.jpg', {type: 'video/mp4'}),
182-
]
183-
const {element} = setup(`
184-
<input
185-
type="file"
186-
accept="video/*,audio/*,.png" multiple
187-
/>
188-
`)
189-
190-
userEvent.upload(element, files)
191-
192-
expect(element.files).toHaveLength(3)
193-
})
194-
195-
test('should not upload file with unaccepted format', () => {
196-
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
197-
const {element} = setup('<input type="file" accept="image/jpg" />')
198-
199-
userEvent.upload(element, file)
200-
201-
expect(element.files).toHaveLength(0)
202-
})
203-
204-
test('should not upload multiple files with unaccepted formats', () => {
205-
const files = [
206-
new File(['hello'], 'hello.txt', {type: 'text/plain'}),
207-
new File(['there'], 'there.pdf', {type: 'application/pdf'}),
208-
new File(['there'], 'there.png', {type: 'image/png'}),
209-
]
210-
const {element} = setup(`
211-
<input id="files" type="file" accept="video/*" multiple />
212-
`)
213-
214-
userEvent.upload(element, files)
215-
216-
expect(element.files).toHaveLength(0)
217-
})

src/upload.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ function upload(element, fileOrFiles, init) {
1010

1111
const input = element.tagName === 'LABEL' ? element.control : element
1212

13-
const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles])
14-
.filter(file => isAcceptableFile(file, element.accept))
15-
.slice(0, input.multiple ? undefined : 1)
13+
const files = (Array.isArray(fileOrFiles)
14+
? fileOrFiles
15+
: [fileOrFiles]
16+
).slice(0, input.multiple ? undefined : 1)
1617

1718
// blur fires when the file selector pops up
1819
blur(element, init)
1920
// focus fires when they make their selection
2021
focus(element, init)
2122

22-
// treat empty array as if the user just closed the file upload dialog
23-
if (files.length === 0) {
24-
return
25-
}
26-
2723
// the event fired in the browser isn't actually an "input" or "change" event
2824
// but a new Event with a type set to "input" and "change"
2925
// Kinda odd...
@@ -50,22 +46,4 @@ function upload(element, fileOrFiles, init) {
5046
})
5147
}
5248

53-
function isAcceptableFile(file, accept) {
54-
if (!accept) {
55-
return true
56-
}
57-
58-
const wildcards = ['audio/*', 'image/*', 'video/*']
59-
60-
return accept.split(',').some(acceptToken => {
61-
if (acceptToken[0] === '.') {
62-
// tokens starting with a dot represent a file extension
63-
return file.name.endsWith(acceptToken)
64-
} else if (wildcards.includes(acceptToken)) {
65-
return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1))
66-
}
67-
return file.type === acceptToken
68-
})
69-
}
70-
7149
export {upload}

0 commit comments

Comments
 (0)