Skip to content

Commit

Permalink
feat(ui-file-drop): add inputRef prop to make FileDrop focusable
Browse files Browse the repository at this point in the history
Closes: INSTUI-4441
  • Loading branch information
ToMESSKa committed Feb 11, 2025
1 parent 404724c commit b3acc38
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 4 deletions.
57 changes: 56 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/ui-file-drop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
"@instructure/ui-babel-preset": "9.10.2",
"@instructure/ui-test-locator": "9.10.2",
"@instructure/ui-test-utils": "9.10.2",
"@instructure/ui-themes": "9.10.2"
"@instructure/ui-themes": "9.10.2",
"@testing-library/jest-dom": "^6.4.6",
"@testing-library/react": "^15.0.7",
"vitest": "^2.0.2"
},
"peerDependencies": {
"react": ">=16.8 <=18"
Expand Down
57 changes: 57 additions & 0 deletions packages/ui-file-drop/src/FileDrop/__new-tests__/FileDrop.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React from 'react'
import { render } from '@testing-library/react'
import { vi } from 'vitest'
import '@testing-library/jest-dom'

import { FileDrop } from '../index'

describe('<FileDrop/>', () => {
it('should focus the input when focus is called', async () => {
let inputEl: any
render(
<FileDrop
renderLabel="filedrop"
inputRef={(el: HTMLInputElement | null) => {
inputEl = el
}}
/>
)
const input = document.querySelector('input[class$="-fileDrop__input"]')

inputEl.focus()

expect(input).toHaveFocus()
})

it('should provide an inputRef prop', async () => {
const inputRef = vi.fn()
render(<FileDrop renderLabel="filedrop" inputRef={inputRef} />)
const input = document.querySelector('input[class$="-fileDrop__input"]')

expect(inputRef).toHaveBeenCalledWith(input)
})
})
3 changes: 3 additions & 0 deletions packages/ui-file-drop/src/FileDrop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ class FileDrop extends Component<FileDropProps, FileDropState> {

handleRef = (el: HTMLInputElement) => {
this.fileInputEl = el
if (typeof this.props.inputRef === 'function') {
this.props.inputRef(el)
}
}

handleBlur = () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/ui-file-drop/src/FileDrop/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ type FileDropOwnProps = {
* CSS-like shorthand. For example: margin="small auto large".
*/
margin?: Spacing
/**
* A function that provides a reference to the actual input element
*/
inputRef?: (inputElement: HTMLInputElement | null) => void
}

type FileDropState = {
Expand Down Expand Up @@ -210,7 +214,8 @@ const propTypes: PropValidators<PropKeys> = {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
maxWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
minWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
margin: ThemeablePropTypes.spacing
margin: ThemeablePropTypes.spacing,
inputRef: PropTypes.func
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -236,7 +241,8 @@ const allowedProps: AllowedPropKeys = [
'width',
'maxWidth',
'minWidth',
'margin'
'margin',
'inputRef'
]

export type { FileDropProps, FileDropState, FileDropStyleProps, FileDropStyle }
Expand Down

0 comments on commit b3acc38

Please sign in to comment.