Skip to content

Commit

Permalink
feat(ui-filedrop): add inputRef prop to make FileDrop focusable
Browse files Browse the repository at this point in the history
  • Loading branch information
ToMESSKa authored and joyenjoyer committed Feb 3, 2025
1 parent 8341b37 commit a3a75e0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
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 @@ -274,6 +274,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 a3a75e0

Please sign in to comment.