fix: id attribute to Input component in (React) demo.Formcomponents.tsx.ejs #444
main fromrefs/pull/444/head·opened Hello, I noticed a accessibility issue with the TanStack Form demo component, inputs where missing an id attribute while it did have a htmlFor on the label.
What I changed
- I added an id attribute using the label parameter to match the
htmlFor
Summary by CodeRabbit#
- Bug Fixes
- Improved form label and input field association for better accessibility and usability. Labels can now properly focus their corresponding input fields when clicked.
- 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs (1)
285-288: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick winAdd missing
idattribute toselectelement for label association.The non-shadcn
Selectcomponent has the same accessibility issue that this PR fixes for the shadcnTextField: the<label>on line 285 useshtmlFor={label}, but the<select>element on line 288 is missing the correspondingidattribute.♿ Proposed fix
</label> <select + id={label} name={field.name} value={field.state.value}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs` around lines 285 - 288, The <label> uses htmlFor={label} but the corresponding <select> is missing an id; update the <select> element in demo.FormComponents (the select rendered where label is used) to include id={label} so the label correctly associates with the select (ensure you use the same unique identifier variable "label" as used in the <label>).🧹 Nitpick comments (1)
packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs (1)
45-70: ⚡ Quick winConsider accepting explicit
idprop or sanitizing label for ID generation.All form components (TextField, TextArea, Slider, Switch) use the
labelprop directly as the DOMid. While this works for simple cases, it could cause issues if:- Labels contain spaces or special characters (e.g., "First Name" →
id="First Name") - Multiple fields share the same label text (would create duplicate IDs)
Consider one of these approaches:
- Accept an optional
idprop withlabelas fallback - Sanitize the label (e.g., slugify: "First Name" → "first-name")
💡 Example: accepting optional id prop
export function TextField({ label, + id, placeholder, }: { label: string + id?: string placeholder?: string }) { const field = useFieldContext<string>() const errors = useStore(field.store, (state) => state.meta.errors) + const inputId = id ?? label return ( <div> - <Label htmlFor={label} className="mb-2 text-xl font-bold"> + <Label htmlFor={inputId} className="mb-2 text-xl font-bold"> {label} </Label> <Input - id={label} + id={inputId} value={field.state.value}Apply similar changes to TextArea, Slider, and Switch components.
Also applies to: 72-97, 141-159, 161-179
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs` around lines 45 - 70, Update TextField (and similarly TextArea, Slider, Switch) to accept an optional id prop and use a sanitized fallback derived from label when id isn’t provided: add id?: string to the component props, compute const fieldId = id ?? slugified(label) (use a small sanitizer/slugify helper that lowercases, trims, replaces spaces/special chars with hyphens and deduplicates hyphens), then use fieldId for Label htmlFor and the control id to avoid spaces/special-chars and duplicate IDs; apply the same change and helper to the components named TextArea, Slider, and Switch so all label→id usage is consistent.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Outside diff comments: In `@packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs`: - Around line 285-288: The <label> uses htmlFor={label} but the corresponding <select> is missing an id; update the <select> element in demo.FormComponents (the select rendered where label is used) to include id={label} so the label correctly associates with the select (ensure you use the same unique identifier variable "label" as used in the <label>). --- Nitpick comments: In `@packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs`: - Around line 45-70: Update TextField (and similarly TextArea, Slider, Switch) to accept an optional id prop and use a sanitized fallback derived from label when id isn’t provided: add id?: string to the component props, compute const fieldId = id ?? slugified(label) (use a small sanitizer/slugify helper that lowercases, trims, replaces spaces/special chars with hyphens and deduplicates hyphens), then use fieldId for Label htmlFor and the control id to avoid spaces/special-chars and duplicate IDs; apply the same change and helper to the components named TextArea, Slider, and Switch so all label→id usage is consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
3c0601a5-1841-4235-891a-83d1adee9ac0📥 Commits
Reviewing files that changed from the base of the PR and between 8425b8032523d419d6abef93dd0903052254789f and 40d711487cdd61acd09fa1642b5b0236692881d1.
📒 Files selected for processing (1)
packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejs
- Labels contain spaces or special characters (e.g., "First Name" →
Konijnebeer(GitHub profile, opens in a new tab) changed the title
Thanks for catching the missing label association! This landed in #559 with your contribution credited. It also handles repeated labels and covers focus in the generated app smoke test.
tannerlinsley closed this
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
A single line is added to the shadcn TextField demo component, assigning
id={label}to the Input element to align with the existingLabel htmlFor={label}binding for proper form accessibility.Changes#
Form Accessibility Alignment
packages/create/src/frameworks/react/add-ons/form/assets/src/components/demo.FormComponents.tsx.ejsid={label}prop to establish label-input association viahtmlForbinding.Estimated code review effort#
🎯 1 (Trivial) | ⏱️ ~2 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip
💬 Introducing Slack Agent: Turn conversations into code.
Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack.
👉 Get your free trial and get 200 agent minutes per Slack user (a $50 value).
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share