Skip to content
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

Make sure labels and form inputs are properly associated when a custom ID is provided #2666

Merged
merged 3 commits into from
Mar 8, 2024

Conversation

camertron
Copy link
Contributor

@camertron camertron commented Mar 6, 2024

What are you trying to accomplish?

This PR fixes the association between <label>s and their form inputs when a custom ID is provided. Normally, Rails will generate an ID for a given input, and use the same ID for the label. However, creating the input with a custom ID that is not also passed to the label results in the label's for attribute pointing to a non-existent element. For example,

<%= render(Primer::Alpha::TextField(name: :foo, id: "bar", label: "Foobar")) %>

results in the following HTML:

<label for="foo">Foobar</label>
<input type="text" name="foo" id="bar" />

The forms framework (via Rails) appears to link the label to the input's name instead of its ID, which is incorrect.

This PR ensures the two elements are always properly associated.

Integration

No changes necessary in production.

Risk Assessment

  • Low risk the change is small, highly observable, and easily rolled back.
  • Medium risk changes that are isolated, reduced in scope or could impact few users. The change will not impact library availability.
  • High risk changes are those that could impact customers and SLOs, low or no test coverage, low observability, or slow to rollback.

What approach did you choose and why?

@kendallgassner and I discovered we need to pass the given ID to the label's for: argument. This effectively bypasses Rails' default ID generation code.

Accessibility

  • No new axe scan violation - This change does not introduce any new axe scan violations.

Copy link

changeset-bot bot commented Mar 6, 2024

🦋 Changeset detected

Latest commit: 1fe44fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/view-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@camertron camertron changed the title Make sure labels for form inputs use a custom ID if provided Make sure labels and form inputs are properly associated when a custom ID is provided Mar 6, 2024
@camertron camertron marked this pull request as ready for review March 6, 2024 23:31
@camertron camertron requested a review from a team as a code owner March 6, 2024 23:31
@camertron camertron requested a review from keithamus March 6, 2024 23:31
Copy link
Contributor

@kendallgassner kendallgassner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💖 thanks for adding a test too!

@camertron camertron merged commit 53a3390 into main Mar 8, 2024
30 checks passed
@camertron camertron deleted the fix_label_for_attribute branch March 8, 2024 18:41
This was referenced Mar 8, 2024
jonrohan pushed a commit that referenced this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants