-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use <label>
HTML element when possible
#7609
Conversation
UUID are cryptographically secure unique identifiers and take time to get generated. It's better to use the useId hook which is made for that purpose, always returns the same value for the same hook instance, and works well with server-side rendering.
…isabling an eslint rule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This pull request enhances accessibility and performance in input components by implementing the <label>
HTML element and utilizing React's useId()
hook.
- Replaced
<span>
with<label>
inTextInput.tsx
for improved accessibility - Implemented
useId()
hook inCheckbox.tsx
,Radio.tsx
, andTextInputV2.tsx
for stable ID generation - Updated
Toggle.tsx
to use functional state updates inuseEffect
for more reliable state management - Improved label-input linking across components using
htmlFor
attribute andid
- Enhanced overall accessibility and adherence to best practices in React development
5 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile
@@ -65,12 +65,15 @@ const TextInput: React.FC<TextInputProps> = ({ | |||
placeholder, | |||
icon, | |||
}) => { | |||
const inputId = useId(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice I didn't know about this one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be our default way to generated Ids in the frontend (when we don't need uuids). What do you think @lucasbordeau?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The useId
hook remains a React hook and must follow the hooks' rules. It can't be called in response to an event. The useId
hook is excellent for generating identifiers scoped to a React component, like HTML IDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't say that it should be the default way for all ids but for HTML ids it's most indicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!
@@ -165,7 +164,7 @@ export const Checkbox = ({ | |||
setIsInternalChecked(event.target.checked ?? false); | |||
}; | |||
|
|||
const checkboxId = 'checkbox' + v4(); | |||
const checkboxId = React.useId(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import from react?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole component imports hooks from the React global export, so I did the same.
Log
|
This PR:
<label>
HTML elements when possible to represent labelsuseId()
React hook to get an identifier to link the label with its input; it's more suitable than generating a UUID at every renderFixes #7281