Topic: Custom attachment button cannot be reached with the keyboard
copperPixel
free
asked 1 week ago
We replaced the visible file input with a styled upload button so that it matches the other form controls. Clicking it opens the picker, but keyboard users cannot reach the upload action. Our CSS currently hides the native input with display: none.
There is a visible label, and selected filenames are announced elsewhere in the form. I would like to keep the native file picker and avoid adding a second keyboard interaction to the upload area.
Mateusz Lazaru
staff
answered 1 week ago
Use an actual button with type="button" for the custom trigger. Let its click handler call click() on the native file input through a ref. The button then provides the usual keyboard activation and focus behavior without a separate key handler.
Give the trigger a clear accessible name, retain a visible focus indicator, and connect any validation message to the input using aria-describedby. Please include the trigger JSX if focus is being removed by a surrounding form handler.
const inputRef = useRef(null);
<button type="button" onClick={() => inputRef.current?.click()}>
Choose attachment
</button>
<input ref={inputRef} type="file" hidden onChange={handleFiles} />
https://react.dev/reference/react-dom/components/input
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: MDB React
- MDB Version: MDB5 9.0.0
- Device: Desktop
- Browser: Chrome
- OS: macOS
- Provided sample code: No
- Provided link: No