Skip to content

Commit a3a75e0

Browse files
ToMESSKajoyenjoyer
authored andcommitted
feat(ui-filedrop): add inputRef prop to make FileDrop focusable
1 parent 8341b37 commit a3a75e0

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 React from 'react'
26+
import { render } from '@testing-library/react'
27+
import { vi } from 'vitest'
28+
import '@testing-library/jest-dom'
29+
30+
import { FileDrop } from '../index'
31+
32+
describe('<FileDrop/>', () => {
33+
it('should focus the input when focus is called', async () => {
34+
let inputEl: any
35+
render(
36+
<FileDrop
37+
renderLabel="filedrop"
38+
inputRef={(el: HTMLInputElement | null) => {
39+
inputEl = el
40+
}}
41+
/>
42+
)
43+
const input = document.querySelector('input[class$="-fileDrop__input"]')
44+
45+
inputEl.focus()
46+
47+
expect(input).toHaveFocus()
48+
})
49+
50+
it('should provide an inputRef prop', async () => {
51+
const inputRef = vi.fn()
52+
render(<FileDrop renderLabel="filedrop" inputRef={inputRef} />)
53+
const input = document.querySelector('input[class$="-fileDrop__input"]')
54+
55+
expect(inputRef).toHaveBeenCalledWith(input)
56+
})
57+
})

packages/ui-file-drop/src/FileDrop/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class FileDrop extends Component<FileDropProps, FileDropState> {
274274

275275
handleRef = (el: HTMLInputElement) => {
276276
this.fileInputEl = el
277+
if (typeof this.props.inputRef === 'function') {
278+
this.props.inputRef(el)
279+
}
277280
}
278281

279282
handleBlur = () => {

packages/ui-file-drop/src/FileDrop/props.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ type FileDropOwnProps = {
155155
* CSS-like shorthand. For example: margin="small auto large".
156156
*/
157157
margin?: Spacing
158+
/**
159+
* A function that provides a reference to the actual input element
160+
*/
161+
inputRef?: (inputElement: HTMLInputElement | null) => void
158162
}
159163

160164
type FileDropState = {
@@ -210,7 +214,8 @@ const propTypes: PropValidators<PropKeys> = {
210214
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
211215
maxWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
212216
minWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
213-
margin: ThemeablePropTypes.spacing
217+
margin: ThemeablePropTypes.spacing,
218+
inputRef: PropTypes.func
214219
}
215220

216221
const allowedProps: AllowedPropKeys = [
@@ -236,7 +241,8 @@ const allowedProps: AllowedPropKeys = [
236241
'width',
237242
'maxWidth',
238243
'minWidth',
239-
'margin'
244+
'margin',
245+
'inputRef'
240246
]
241247

242248
export type { FileDropProps, FileDropState, FileDropStyleProps, FileDropStyle }

0 commit comments

Comments
 (0)