Skip to content

Commit

Permalink
Use HTML autofocus attr w/ React’s autoFocus prop
Browse files Browse the repository at this point in the history
trying to set the “autofocus” attribute (instead of “autoFocus”) creates a console warning and doesn’t actually work consistently
  • Loading branch information
acusti committed Feb 12, 2024
1 parent 46e9d52 commit a10dfb3
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/input-text/src/InputText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ export default React.forwardRef<HTMLInputElement, Props>(function InputText(
const setInputElement = useCallback((element: InputRef) => {
inputRef.current = element;
_setInputElement(element);
if (element && autoFocus) {
element.setAttribute('autofocus', 'autofocus');
}
}, []);

Check warning on line 117 in packages/input-text/src/InputText.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

React Hook useCallback has a missing dependency: 'autoFocus'. Either include it or remove the dependency array

Check warning on line 117 in packages/input-text/src/InputText.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

React Hook useCallback has a missing dependency: 'autoFocus'. Either include it or remove the dependency array

// If props.initialValue changes, override input value from it
Expand Down Expand Up @@ -226,8 +229,7 @@ export default React.forwardRef<HTMLInputElement, Props>(function InputText(
<Element
autoCapitalize={autoCapitalize}
autoComplete={autoComplete}
// @ts-expect-error using HTML’s autofocus attribute
autofocus={autoFocus}
autoFocus={autoFocus} // eslint-disable-line jsx-a11y/no-autofocus
className={className}
defaultValue={initialValue ?? ''}
disabled={disabled}
Expand Down

0 comments on commit a10dfb3

Please sign in to comment.