If you want your file input to look and behave just like a regular input:
...you can use a regular TextField
component and place a <input type="file"... />
inside its endAdornment
:
<TextField
name="file"
value={ value.name }
onChange={ handleFileChange }
error={ error }
readOnly
InputProps={{
endAdornment: (
<input
ref={ inputRef }
type="file"
accept="application/JSON"
onChange={ handleFileChange }
tabIndex={ -1 }
style={{
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
opacity: 0,
}} />
),
}} />
You can add an onKeyDown
listener to open the file picker or clear the file using the keyboard (when the text input is focused):
const handleKeyDow = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
const inputElement = inputRef.current
if (!inputElement) return
let preventDefault = true
if (e.key === ' ' || e.key === 'Enter') {
inputElement.click()
} else if (e.key === 'Delete' || e.key === 'Backspace') {
inputElement.value = ''
} else {
preventDefault = false
}
if (preventDefault) e.preventDefault()
}, [])