@instructure/ui-file-drop 11.6.0 → 11.6.1-snapshot-129
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -296
- package/es/FileDrop/{index.js → v1/index.js} +3 -3
- package/es/FileDrop/v2/index.js +371 -0
- package/es/FileDrop/v2/props.js +26 -0
- package/es/FileDrop/v2/styles.js +111 -0
- package/es/FileDrop/v2/utils/accepts.js +51 -0
- package/es/FileDrop/v2/utils/getEventFiles.js +41 -0
- package/es/{index.js → exports/a.js} +1 -1
- package/{src/index.ts → es/exports/b.js} +1 -3
- package/lib/FileDrop/v1/index.js +379 -0
- package/lib/FileDrop/{index.js → v2/index.js} +9 -9
- package/lib/FileDrop/v2/props.js +31 -0
- package/lib/FileDrop/v2/styles.js +117 -0
- package/lib/FileDrop/v2/utils/accepts.js +58 -0
- package/lib/FileDrop/v2/utils/getEventFiles.js +47 -0
- package/lib/{index.js → exports/a.js} +2 -2
- package/lib/exports/b.js +12 -0
- package/package.json +42 -20
- package/src/FileDrop/{index.tsx → v1/index.tsx} +4 -4
- package/src/FileDrop/{props.ts → v1/props.ts} +1 -1
- package/src/FileDrop/{styles.ts → v1/styles.ts} +2 -6
- package/src/FileDrop/v2/README.md +177 -0
- package/src/FileDrop/v2/index.tsx +419 -0
- package/src/FileDrop/v2/props.ts +211 -0
- package/src/FileDrop/v2/styles.ts +140 -0
- package/src/FileDrop/v2/utils/accepts.ts +54 -0
- package/src/FileDrop/v2/utils/getEventFiles.ts +47 -0
- package/src/exports/a.ts +26 -0
- package/src/exports/b.ts +26 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/FileDrop/{index.d.ts → v1/index.d.ts} +1 -1
- package/types/FileDrop/v1/index.d.ts.map +1 -0
- package/types/FileDrop/{props.d.ts → v1/props.d.ts} +1 -1
- package/types/FileDrop/v1/props.d.ts.map +1 -0
- package/types/FileDrop/v1/styles.d.ts.map +1 -0
- package/types/FileDrop/v1/theme.d.ts.map +1 -0
- package/types/FileDrop/v1/utils/accepts.d.ts.map +1 -0
- package/types/FileDrop/v1/utils/getEventFiles.d.ts.map +1 -0
- package/types/FileDrop/v2/index.d.ts +89 -0
- package/types/FileDrop/v2/index.d.ts.map +1 -0
- package/types/FileDrop/v2/props.d.ts +134 -0
- package/types/FileDrop/v2/props.d.ts.map +1 -0
- package/types/FileDrop/v2/styles.d.ts +17 -0
- package/types/FileDrop/v2/styles.d.ts.map +1 -0
- package/types/FileDrop/v2/utils/accepts.d.ts +5 -0
- package/types/FileDrop/v2/utils/accepts.d.ts.map +1 -0
- package/types/FileDrop/v2/utils/getEventFiles.d.ts +5 -0
- package/types/FileDrop/v2/utils/getEventFiles.d.ts.map +1 -0
- package/types/exports/a.d.ts +3 -0
- package/types/exports/a.d.ts.map +1 -0
- package/types/exports/b.d.ts +3 -0
- package/types/exports/b.d.ts.map +1 -0
- package/types/FileDrop/index.d.ts.map +0 -1
- package/types/FileDrop/props.d.ts.map +0 -1
- package/types/FileDrop/styles.d.ts.map +0 -1
- package/types/FileDrop/theme.d.ts.map +0 -1
- package/types/FileDrop/utils/accepts.d.ts.map +0 -1
- package/types/FileDrop/utils/getEventFiles.d.ts.map +0 -1
- package/types/index.d.ts +0 -3
- package/types/index.d.ts.map +0 -1
- /package/es/FileDrop/{props.js → v1/props.js} +0 -0
- /package/es/FileDrop/{styles.js → v1/styles.js} +0 -0
- /package/es/FileDrop/{theme.js → v1/theme.js} +0 -0
- /package/es/FileDrop/{utils → v1/utils}/accepts.js +0 -0
- /package/es/FileDrop/{utils → v1/utils}/getEventFiles.js +0 -0
- /package/lib/FileDrop/{props.js → v1/props.js} +0 -0
- /package/lib/FileDrop/{styles.js → v1/styles.js} +0 -0
- /package/lib/FileDrop/{theme.js → v1/theme.js} +0 -0
- /package/lib/FileDrop/{utils → v1/utils}/accepts.js +0 -0
- /package/lib/FileDrop/{utils → v1/utils}/getEventFiles.js +0 -0
- /package/src/FileDrop/{README.md → v1/README.md} +0 -0
- /package/src/FileDrop/{theme.ts → v1/theme.ts} +0 -0
- /package/src/FileDrop/{utils → v1/utils}/accepts.ts +0 -0
- /package/src/FileDrop/{utils → v1/utils}/getEventFiles.ts +0 -0
- /package/types/FileDrop/{styles.d.ts → v1/styles.d.ts} +0 -0
- /package/types/FileDrop/{theme.d.ts → v1/theme.d.ts} +0 -0
- /package/types/FileDrop/{utils → v1/utils}/accepts.d.ts +0 -0
- /package/types/FileDrop/{utils → v1/utils}/getEventFiles.d.ts +0 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { Component } from 'react'
|
|
26
|
+
import keycode from 'keycode'
|
|
27
|
+
import { FormFieldMessages } from '@instructure/ui-form-field/latest'
|
|
28
|
+
import type { FormMessage } from '@instructure/ui-form-field/latest'
|
|
29
|
+
import { View } from '@instructure/ui-view/latest'
|
|
30
|
+
import {
|
|
31
|
+
callRenderProp,
|
|
32
|
+
passthroughProps,
|
|
33
|
+
getInteraction,
|
|
34
|
+
withDeterministicId
|
|
35
|
+
} from '@instructure/ui-react-utils'
|
|
36
|
+
|
|
37
|
+
import { accepts, getAcceptList } from './utils/accepts'
|
|
38
|
+
import { getEventFiles } from './utils/getEventFiles'
|
|
39
|
+
|
|
40
|
+
import { withStyle } from '@instructure/emotion'
|
|
41
|
+
import generateStyle from './styles'
|
|
42
|
+
|
|
43
|
+
import { allowedProps } from './props'
|
|
44
|
+
import type { FileDropProps, FileDropState } from './props'
|
|
45
|
+
import type { StyleParams } from './styles'
|
|
46
|
+
function keyEventIsClickButton(e: React.KeyboardEvent) {
|
|
47
|
+
return e.keyCode === keycode.codes.space || e.keyCode === keycode.codes.enter
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
---
|
|
52
|
+
category: components
|
|
53
|
+
---
|
|
54
|
+
**/
|
|
55
|
+
@withDeterministicId()
|
|
56
|
+
@withStyle(generateStyle)
|
|
57
|
+
class FileDrop extends Component<FileDropProps, FileDropState> {
|
|
58
|
+
static readonly componentId = 'FileDrop'
|
|
59
|
+
|
|
60
|
+
static allowedProps = allowedProps
|
|
61
|
+
static defaultProps = {
|
|
62
|
+
onClick: function (_e: React.MouseEvent) {},
|
|
63
|
+
shouldEnablePreview: false,
|
|
64
|
+
shouldAllowMultiple: false,
|
|
65
|
+
shouldAllowRepeats: true,
|
|
66
|
+
maxSize: Infinity,
|
|
67
|
+
minSize: 0,
|
|
68
|
+
// Leave interaction default undefined so that `disabled` and `readOnly` can also be supplied
|
|
69
|
+
messages: [],
|
|
70
|
+
display: 'block'
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
constructor(props: FileDropProps) {
|
|
74
|
+
super(props)
|
|
75
|
+
this.defaultId = props.deterministicId!()
|
|
76
|
+
this.messagesId = props.deterministicId!('FileDrop-messages')
|
|
77
|
+
}
|
|
78
|
+
componentDidMount() {
|
|
79
|
+
this.props.makeStyles?.(this.makeStyleProps())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
componentDidUpdate() {
|
|
83
|
+
this.props.makeStyles?.(this.makeStyleProps())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
convertToFile(fileLikeItem: DataTransferItem | File) {
|
|
87
|
+
if (fileLikeItem instanceof DataTransferItem)
|
|
88
|
+
return fileLikeItem.getAsFile()
|
|
89
|
+
else return fileLikeItem
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
makeStyleProps = (): StyleParams => {
|
|
93
|
+
return {
|
|
94
|
+
...this.props,
|
|
95
|
+
functionallyDisabled: this.functionallyDisabled,
|
|
96
|
+
visuallyDisabled: this.interaction === 'disabled',
|
|
97
|
+
dragRejected: this.state.isDragRejected || this.invalid,
|
|
98
|
+
dragAccepted: this.state.isDragAccepted
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
state: FileDropState = {
|
|
103
|
+
isDragAccepted: false,
|
|
104
|
+
isDragRejected: false,
|
|
105
|
+
isFocused: false,
|
|
106
|
+
isFileBrowserDisplayed: false
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
enterCounter = 0
|
|
110
|
+
fileInputEl?: HTMLInputElement
|
|
111
|
+
defaultId: string | null = null
|
|
112
|
+
messagesId = ''
|
|
113
|
+
|
|
114
|
+
ref: Element | null = null
|
|
115
|
+
|
|
116
|
+
handleElementRef = (el: Element | null) => {
|
|
117
|
+
this.ref = el
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get functionallyDisabled() {
|
|
121
|
+
return this.interaction === 'disabled' || this.interaction === 'readonly'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get interaction() {
|
|
125
|
+
return getInteraction({ props: this.props })
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get hasMessages() {
|
|
129
|
+
return this.props.messages ? this.props.messages.length > 0 : false
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get invalid() {
|
|
133
|
+
if (this.hasMessages) {
|
|
134
|
+
return (
|
|
135
|
+
this.props.messages!.findIndex((message: FormMessage) => {
|
|
136
|
+
return message.type === 'error' || message.type === 'newError'
|
|
137
|
+
}) >= 0
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
getDataTransferItems(
|
|
144
|
+
e: React.ChangeEvent | React.DragEvent,
|
|
145
|
+
shouldEnablePreview?: boolean
|
|
146
|
+
): Array<File> {
|
|
147
|
+
let list: Array<File> = []
|
|
148
|
+
Array.from(getEventFiles(e, this.fileInputEl)).forEach((file) => {
|
|
149
|
+
const fileObj = this.convertToFile(file)
|
|
150
|
+
if (fileObj) list.push(fileObj)
|
|
151
|
+
})
|
|
152
|
+
if (list.length > 1) {
|
|
153
|
+
list = this.props.shouldAllowMultiple ? list : [list[0]]
|
|
154
|
+
}
|
|
155
|
+
if (shouldEnablePreview) {
|
|
156
|
+
return list.map((file: File) =>
|
|
157
|
+
Object.assign(file, { preview: window.URL.createObjectURL(file) })
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
return list
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
handleDragEnter = (e: React.DragEvent) => {
|
|
164
|
+
e.preventDefault()
|
|
165
|
+
// Count the dropzone and any children that are entered.
|
|
166
|
+
this.enterCounter += 1
|
|
167
|
+
// Don't trigger onDragEnter for each children after the first one
|
|
168
|
+
if (this.enterCounter > 1) {
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
const allFilesAccepted = this.allFilesAccepted(this.getDataTransferItems(e))
|
|
172
|
+
this.setState({
|
|
173
|
+
isDragAccepted: allFilesAccepted,
|
|
174
|
+
isDragRejected: !allFilesAccepted
|
|
175
|
+
})
|
|
176
|
+
this.props.onDragEnter?.(e)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
handleDragOver = (e: React.DragEvent) => {
|
|
180
|
+
e.preventDefault()
|
|
181
|
+
e.stopPropagation()
|
|
182
|
+
try {
|
|
183
|
+
const event = e
|
|
184
|
+
event.dataTransfer.dropEffect = 'copy'
|
|
185
|
+
} catch (err) {
|
|
186
|
+
// continue regardless of error
|
|
187
|
+
}
|
|
188
|
+
this.props.onDragOver?.(e)
|
|
189
|
+
return false
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
handleDragLeave = (e: React.DragEvent) => {
|
|
193
|
+
e.preventDefault()
|
|
194
|
+
this.enterCounter -= 1
|
|
195
|
+
// Only deactivate once the dropzone and all children was left
|
|
196
|
+
if (this.enterCounter > 0) {
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
this.setState({
|
|
200
|
+
isDragAccepted: false,
|
|
201
|
+
isDragRejected: false
|
|
202
|
+
})
|
|
203
|
+
this.props.onDragLeave?.(e)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
parseFiles(fileList: Array<DataTransferItem | File>) {
|
|
207
|
+
const accepted: typeof fileList = []
|
|
208
|
+
const rejected: typeof fileList = []
|
|
209
|
+
fileList.forEach((file: DataTransferItem | File) => {
|
|
210
|
+
const fileObj = this.convertToFile(file)
|
|
211
|
+
if (
|
|
212
|
+
fileObj &&
|
|
213
|
+
this.fileAccepted(fileObj) &&
|
|
214
|
+
this.fileMatchSize(fileObj)
|
|
215
|
+
) {
|
|
216
|
+
accepted.push(file)
|
|
217
|
+
} else {
|
|
218
|
+
rejected.push(file)
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
return [accepted, rejected]
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
handleChange = (e: React.ChangeEvent | React.DragEvent) => {
|
|
225
|
+
const { onDrop, onDropAccepted, onDropRejected, shouldEnablePreview } =
|
|
226
|
+
this.props
|
|
227
|
+
const fileList = this.getDataTransferItems(e, shouldEnablePreview)
|
|
228
|
+
const [accepted, rejected] = this.parseFiles(fileList)
|
|
229
|
+
e.preventDefault()
|
|
230
|
+
this.enterCounter = 0
|
|
231
|
+
// When dropping a file the browser does not populate the input's
|
|
232
|
+
// 'files' property, so we need to do it manually. This will also populate
|
|
233
|
+
// the input's 'value' prop, which contains the first file's path
|
|
234
|
+
if (accepted.length > 0 && 'dataTransfer' in e) {
|
|
235
|
+
this.fileInputEl!.files = e.dataTransfer!.files
|
|
236
|
+
}
|
|
237
|
+
onDrop && onDrop(accepted, rejected, e as React.DragEvent)
|
|
238
|
+
if (rejected.length > 0 && onDropRejected) {
|
|
239
|
+
onDropRejected(rejected, e)
|
|
240
|
+
}
|
|
241
|
+
if (accepted.length > 0 && onDropAccepted) {
|
|
242
|
+
onDropAccepted(accepted, e)
|
|
243
|
+
}
|
|
244
|
+
this.setState({
|
|
245
|
+
isDragAccepted: false,
|
|
246
|
+
isDragRejected: false,
|
|
247
|
+
isFileBrowserDisplayed: false
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
fileAccepted = (file: File) => {
|
|
252
|
+
if (this.props.accept) return accepts(file, this.props.accept)
|
|
253
|
+
else return true
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
fileMatchSize(file: File) {
|
|
257
|
+
return file.size <= this.props.maxSize! && file.size >= this.props.minSize!
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
allFilesAccepted(files: Array<File>) {
|
|
261
|
+
return files.every(this.fileAccepted)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
acceptStr() {
|
|
265
|
+
const { accept } = this.props
|
|
266
|
+
return accept ? getAcceptList(accept).join(',') : undefined
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
renderLabel() {
|
|
270
|
+
const { renderLabel } = this.props
|
|
271
|
+
return callRenderProp(renderLabel, {
|
|
272
|
+
isDragAccepted: this.state.isDragAccepted,
|
|
273
|
+
isDragRejected: this.state.isDragRejected,
|
|
274
|
+
interaction: this.interaction
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
handleRef = (el: HTMLInputElement) => {
|
|
279
|
+
this.fileInputEl = el
|
|
280
|
+
if (typeof this.props.inputRef === 'function') {
|
|
281
|
+
this.props.inputRef(el)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
handleBlur = () => {
|
|
286
|
+
this.setState({
|
|
287
|
+
isFocused: false,
|
|
288
|
+
isFileBrowserDisplayed: false
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
handleFocus = () => {
|
|
293
|
+
this.setState({
|
|
294
|
+
isFocused: true,
|
|
295
|
+
isFileBrowserDisplayed: false
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
handleClick = (e: React.MouseEvent) => {
|
|
300
|
+
if (this.fileInputEl!.value && this.props.shouldAllowRepeats) {
|
|
301
|
+
this.fileInputEl!.value = null as unknown as string
|
|
302
|
+
}
|
|
303
|
+
// focus the input (because FF won't)
|
|
304
|
+
this.fileInputEl?.focus()
|
|
305
|
+
this.props.onClick!(e)
|
|
306
|
+
this.setState({
|
|
307
|
+
isFileBrowserDisplayed: true
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
handleKeyDown = (e: React.KeyboardEvent) => {
|
|
312
|
+
if (this.state.isFocused && keyEventIsClickButton(e)) {
|
|
313
|
+
if (this.props.shouldAllowRepeats) {
|
|
314
|
+
this.fileInputEl!.value = null as unknown as string
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
handleKeyUp = (e: React.KeyboardEvent) => {
|
|
320
|
+
// This is to handle the case where ESC is pressed inside a Dialog so that
|
|
321
|
+
// closing the file browser dialog doesn't also close the Dialog.
|
|
322
|
+
if (e.keyCode === keycode.codes.esc && this.state.isFileBrowserDisplayed) {
|
|
323
|
+
e.stopPropagation()
|
|
324
|
+
e.nativeEvent.stopImmediatePropagation()
|
|
325
|
+
this.setState({
|
|
326
|
+
isFileBrowserDisplayed: false
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
render() {
|
|
332
|
+
const {
|
|
333
|
+
display,
|
|
334
|
+
height,
|
|
335
|
+
width,
|
|
336
|
+
minWidth,
|
|
337
|
+
maxWidth,
|
|
338
|
+
margin,
|
|
339
|
+
onDropAccepted,
|
|
340
|
+
onDropRejected,
|
|
341
|
+
styles,
|
|
342
|
+
...props
|
|
343
|
+
} = this.props
|
|
344
|
+
const id = this.props.id || this.defaultId!
|
|
345
|
+
const focusColor =
|
|
346
|
+
this.state.isDragRejected || this.invalid ? 'danger' : undefined
|
|
347
|
+
|
|
348
|
+
return (
|
|
349
|
+
<View
|
|
350
|
+
display={display}
|
|
351
|
+
position="relative" // contain visually hidden file input element
|
|
352
|
+
width={width}
|
|
353
|
+
minWidth={minWidth}
|
|
354
|
+
maxWidth={maxWidth}
|
|
355
|
+
margin={margin}
|
|
356
|
+
height={height}
|
|
357
|
+
elementRef={this.handleElementRef}
|
|
358
|
+
data-cid="FileDrop"
|
|
359
|
+
>
|
|
360
|
+
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
|
|
361
|
+
<label
|
|
362
|
+
css={this.props.styles?.fileDropLabel}
|
|
363
|
+
htmlFor={id}
|
|
364
|
+
onDragEnter={this.handleDragEnter}
|
|
365
|
+
onDragOver={this.handleDragOver}
|
|
366
|
+
onDragLeave={this.handleDragLeave}
|
|
367
|
+
onDrop={this.handleChange}
|
|
368
|
+
>
|
|
369
|
+
<View
|
|
370
|
+
borderRadius={String(
|
|
371
|
+
(styles?.fileDropLayout as { borderRadius?: string | number })
|
|
372
|
+
?.borderRadius
|
|
373
|
+
)}
|
|
374
|
+
display="block"
|
|
375
|
+
position="relative"
|
|
376
|
+
withFocusOutline={this.state.isFocused}
|
|
377
|
+
focusColor={focusColor}
|
|
378
|
+
height={height}
|
|
379
|
+
>
|
|
380
|
+
<span css={this.props.styles?.fileDropLabelContent}>
|
|
381
|
+
<span css={this.props.styles?.fileDropLayout}>
|
|
382
|
+
<View height={height}>{this.renderLabel()}</View>
|
|
383
|
+
</span>
|
|
384
|
+
</span>
|
|
385
|
+
</View>
|
|
386
|
+
</label>
|
|
387
|
+
<input
|
|
388
|
+
{...passthroughProps(props)}
|
|
389
|
+
onClick={this.handleClick}
|
|
390
|
+
type="file"
|
|
391
|
+
css={this.props.styles?.fileDropInput}
|
|
392
|
+
id={id}
|
|
393
|
+
ref={this.handleRef}
|
|
394
|
+
onFocus={this.handleFocus}
|
|
395
|
+
onBlur={this.handleBlur}
|
|
396
|
+
onKeyDown={this.handleKeyDown}
|
|
397
|
+
onKeyUp={this.handleKeyUp}
|
|
398
|
+
multiple={this.props.shouldAllowMultiple}
|
|
399
|
+
accept={this.acceptStr()}
|
|
400
|
+
onChange={this.handleChange}
|
|
401
|
+
aria-describedby={this.hasMessages ? this.messagesId : undefined}
|
|
402
|
+
aria-invalid={this.invalid}
|
|
403
|
+
disabled={this.functionallyDisabled}
|
|
404
|
+
/>
|
|
405
|
+
{this.hasMessages ? (
|
|
406
|
+
<View display="block" margin="small 0 0">
|
|
407
|
+
<FormFieldMessages
|
|
408
|
+
id={this.messagesId}
|
|
409
|
+
messages={this.props.messages}
|
|
410
|
+
/>
|
|
411
|
+
</View>
|
|
412
|
+
) : null}
|
|
413
|
+
</View>
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export default FileDrop
|
|
419
|
+
export { FileDrop }
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { FormMessage } from '@instructure/ui-form-field/latest'
|
|
26
|
+
import type {
|
|
27
|
+
Spacing,
|
|
28
|
+
WithStyleProps,
|
|
29
|
+
ComponentStyle
|
|
30
|
+
} from '@instructure/emotion'
|
|
31
|
+
import type {
|
|
32
|
+
FileDropTheme,
|
|
33
|
+
OtherHTMLAttributes
|
|
34
|
+
} from '@instructure/shared-types'
|
|
35
|
+
import type { WithDeterministicIdProps } from '@instructure/ui-react-utils'
|
|
36
|
+
import { Renderable } from '@instructure/shared-types'
|
|
37
|
+
type RenderLabelProps = {
|
|
38
|
+
isDragAccepted: boolean
|
|
39
|
+
isDragRejected: boolean
|
|
40
|
+
interaction: FileDropOwnProps['interaction']
|
|
41
|
+
}
|
|
42
|
+
type FileDropOwnProps = {
|
|
43
|
+
/**
|
|
44
|
+
* The id of the input (to link it to its label for a11y)
|
|
45
|
+
*/
|
|
46
|
+
id?: string
|
|
47
|
+
/**
|
|
48
|
+
* The content of FileDrop; can be a component or React node.
|
|
49
|
+
* Components receive `isDragAccepted` and `isDragRejected` as props.
|
|
50
|
+
*/
|
|
51
|
+
renderLabel: Renderable<RenderLabelProps>
|
|
52
|
+
/**
|
|
53
|
+
* The mime media type/s or file extension/s allowed to be dropped inside
|
|
54
|
+
*/
|
|
55
|
+
accept?: string | string[]
|
|
56
|
+
/**
|
|
57
|
+
* Displays informational and error messages, used for input validation,
|
|
58
|
+
* can also display screenreader-only messages.
|
|
59
|
+
* Also changes the border color of the component on success/error.
|
|
60
|
+
*/
|
|
61
|
+
messages?: FormMessage[]
|
|
62
|
+
/**
|
|
63
|
+
* Called when clicking on the drop area to select files to upload
|
|
64
|
+
*/
|
|
65
|
+
onClick?: (e: React.MouseEvent) => void
|
|
66
|
+
/**
|
|
67
|
+
* Called when dropping files or when file dialog window exits successfully
|
|
68
|
+
*/
|
|
69
|
+
onDrop?: (
|
|
70
|
+
accepted: ArrayLike<DataTransferItem | File>,
|
|
71
|
+
rejected: ArrayLike<DataTransferItem | File>,
|
|
72
|
+
e: React.DragEvent
|
|
73
|
+
) => void
|
|
74
|
+
/**
|
|
75
|
+
* Called when dropping allowed files
|
|
76
|
+
*/
|
|
77
|
+
onDropAccepted?: (
|
|
78
|
+
accepted: ArrayLike<DataTransferItem | File>,
|
|
79
|
+
e: React.DragEvent | React.ChangeEvent
|
|
80
|
+
) => void
|
|
81
|
+
/**
|
|
82
|
+
* Called when dropping rejected files
|
|
83
|
+
*/
|
|
84
|
+
onDropRejected?: (
|
|
85
|
+
rejected: ArrayLike<DataTransferItem | File>,
|
|
86
|
+
e: React.DragEvent | React.ChangeEvent
|
|
87
|
+
) => void
|
|
88
|
+
/**
|
|
89
|
+
* Called when dragging files
|
|
90
|
+
* and passing through FileDrop's content for the first time
|
|
91
|
+
*/
|
|
92
|
+
onDragEnter?: (e: React.DragEvent) => void
|
|
93
|
+
/**
|
|
94
|
+
* Called when dragging files and passing through FileDrop's content
|
|
95
|
+
*/
|
|
96
|
+
onDragOver?: (e: React.DragEvent) => void
|
|
97
|
+
/**
|
|
98
|
+
* Called when dragging files and leaving FileDrop's content
|
|
99
|
+
*/
|
|
100
|
+
onDragLeave?: (e: React.DragEvent) => void
|
|
101
|
+
/**
|
|
102
|
+
* Flag to use window.URL.createObjectURL for each dropped file and pass it through file.preview
|
|
103
|
+
*/
|
|
104
|
+
shouldEnablePreview?: boolean
|
|
105
|
+
/**
|
|
106
|
+
* Flag to allow multiple files to drop at once
|
|
107
|
+
*/
|
|
108
|
+
shouldAllowMultiple?: boolean
|
|
109
|
+
/**
|
|
110
|
+
* Flag to allow upload of the same file more than once
|
|
111
|
+
*/
|
|
112
|
+
shouldAllowRepeats?: boolean
|
|
113
|
+
/**
|
|
114
|
+
* the maximum file size allowed
|
|
115
|
+
*/
|
|
116
|
+
maxSize?: number
|
|
117
|
+
/**
|
|
118
|
+
* the minimum file size allowed
|
|
119
|
+
*/
|
|
120
|
+
minSize?: number
|
|
121
|
+
/**
|
|
122
|
+
* Specifies if interaction with the input is enabled, disabled, or readonly.
|
|
123
|
+
*/
|
|
124
|
+
interaction?: 'enabled' | 'disabled' | 'readonly'
|
|
125
|
+
/**
|
|
126
|
+
* Set the CSS `display` property on FileInput's outermost element
|
|
127
|
+
*/
|
|
128
|
+
display?: 'block' | 'inline-block'
|
|
129
|
+
/**
|
|
130
|
+
* Set the CSS `height` property on FileInput's outermost element
|
|
131
|
+
*/
|
|
132
|
+
height?: string | number
|
|
133
|
+
/**
|
|
134
|
+
* Set the CSS `width` property on FileInput's outermost element
|
|
135
|
+
*/
|
|
136
|
+
width?: string | number
|
|
137
|
+
/**
|
|
138
|
+
* Set the CSS `maxWidth` property on FileInput's outermost element
|
|
139
|
+
*/
|
|
140
|
+
maxWidth?: string | number
|
|
141
|
+
/**
|
|
142
|
+
* Set the CSS `minWidth` property on FileInput's outermost element
|
|
143
|
+
*/
|
|
144
|
+
minWidth?: string | number
|
|
145
|
+
/**
|
|
146
|
+
* Valid values are 0, none, auto, xxx-small, xx-small, x-small, small,
|
|
147
|
+
* medium, large, x-large, xx-large. Apply these values via familiar
|
|
148
|
+
* CSS-like shorthand. For example: margin="small auto large".
|
|
149
|
+
*/
|
|
150
|
+
margin?: Spacing
|
|
151
|
+
/**
|
|
152
|
+
* A function that provides a reference to the actual input element
|
|
153
|
+
*/
|
|
154
|
+
inputRef?: (inputElement: HTMLInputElement | null) => void
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
type FileDropState = {
|
|
158
|
+
isDragAccepted: boolean
|
|
159
|
+
isDragRejected: boolean
|
|
160
|
+
isFocused: boolean
|
|
161
|
+
isFileBrowserDisplayed: boolean
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type FileDropStyleProps = {
|
|
165
|
+
functionallyDisabled: boolean
|
|
166
|
+
visuallyDisabled: boolean
|
|
167
|
+
dragRejected: boolean
|
|
168
|
+
dragAccepted: boolean
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
type PropKeys = keyof FileDropOwnProps
|
|
172
|
+
|
|
173
|
+
type AllowedPropKeys = Readonly<Array<PropKeys>>
|
|
174
|
+
|
|
175
|
+
type FileDropProps = FileDropOwnProps &
|
|
176
|
+
WithStyleProps<FileDropTheme, FileDropStyle> &
|
|
177
|
+
OtherHTMLAttributes<FileDropOwnProps> &
|
|
178
|
+
WithDeterministicIdProps
|
|
179
|
+
|
|
180
|
+
type FileDropStyle = ComponentStyle<
|
|
181
|
+
'fileDropLabel' | 'fileDropInput' | 'fileDropLabelContent' | 'fileDropLayout'
|
|
182
|
+
>
|
|
183
|
+
const allowedProps: AllowedPropKeys = [
|
|
184
|
+
'id',
|
|
185
|
+
'renderLabel',
|
|
186
|
+
'accept',
|
|
187
|
+
'messages',
|
|
188
|
+
'onClick',
|
|
189
|
+
'onDrop',
|
|
190
|
+
'onDropAccepted',
|
|
191
|
+
'onDropRejected',
|
|
192
|
+
'onDragEnter',
|
|
193
|
+
'onDragOver',
|
|
194
|
+
'onDragLeave',
|
|
195
|
+
'shouldEnablePreview',
|
|
196
|
+
'shouldAllowMultiple',
|
|
197
|
+
'shouldAllowRepeats',
|
|
198
|
+
'maxSize',
|
|
199
|
+
'minSize',
|
|
200
|
+
'interaction',
|
|
201
|
+
'display',
|
|
202
|
+
'height',
|
|
203
|
+
'width',
|
|
204
|
+
'maxWidth',
|
|
205
|
+
'minWidth',
|
|
206
|
+
'margin',
|
|
207
|
+
'inputRef'
|
|
208
|
+
]
|
|
209
|
+
|
|
210
|
+
export type { FileDropProps, FileDropState, FileDropStyleProps, FileDropStyle }
|
|
211
|
+
export { allowedProps }
|