fix(form:cascader): set input id so <label for> resolves - #2038
Conversation
The CascaderWidget renders a `<label [attr.for]="id">` via sf-item-wrap, but never sets that id on the inner `<input>` rendered by nz-cascader. As a result the label's `for` attribute points to a non-existent element, failing the Lighthouse/axe 'Incorrect use of <label for=FORM_ELEMENT>' audit. Unlike the abc:se fix (61cbe73), the id cannot be set on the control's elementRef directly because the nz-cascader host element is not a labelable element — the id must go on the inner <input>. Fix: override afterViewInit() to set the id on the inner <input> after nz-cascader renders it (deferred one microtask), and re-sync after reset() since the input may be recreated when data loads.
There was a problem hiding this comment.
Code Review
This pull request improves accessibility for the cascader widget by dynamically syncing the ID of the inner input element with the widget's ID, ensuring that the associated label's 'for' attribute resolves correctly. The changes include querying the input element asynchronously during view initialization and after data resets, along with adding a unit test to verify this behavior. The review feedback suggests removing a redundant type assertion on 'this.host.nativeElement' and replacing 'document.querySelector' with 'page.getEl' in the unit test to avoid potential test pollution.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
|
||
| private syncInputId(): void { | ||
| if (!this.id) return; | ||
| const input = (this.host.nativeElement as HTMLElement).querySelector('input'); |
There was a problem hiding this comment.
The this.host property is already typed as ElementRef<HTMLElement>, so this.host.nativeElement is guaranteed to be of type HTMLElement. The type assertion as HTMLElement is redundant and can be safely removed to simplify the code.
| const input = (this.host.nativeElement as HTMLElement).querySelector('input'); | |
| const input = this.host.nativeElement.querySelector('input'); |
References
- Prefer type inference when the type is obvious. Since ElementRef is already typed as ElementRef, the type of nativeElement is inferred as HTMLElement, making the type assertion redundant. (link)
| const forId = labelEl.getAttribute('for')!; | ||
| expect(forId).toMatch(/^_sf-\d+$/); | ||
|
|
||
| const inputEl = document.querySelector('nz-cascader input') as HTMLInputElement; |
There was a problem hiding this comment.
Using document.querySelector in unit tests can query elements outside the current test fixture or lead to test pollution if multiple tests run. Since page.getEl is already used in this test suite (e.g., on line 150), it is safer and more consistent to use page.getEl('nz-cascader input') instead.
| const inputEl = document.querySelector('nz-cascader input') as HTMLInputElement; | |
| const inputEl = page.getEl('nz-cascader input') as HTMLInputElement; |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2038 +/- ##
========================================
Coverage 95.14% 95.15%
========================================
Files 270 270
Lines 8776 8785 +9
Branches 1811 1708 -103
========================================
+ Hits 8350 8359 +9
Misses 338 338
Partials 88 88 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| export class CascaderWidget extends ControlUIWidget<SFCascaderWidgetSchema> implements OnInit { | ||
| static readonly KEY = 'cascader'; | ||
|
|
||
| private readonly host = inject(ElementRef<HTMLElement>); |
There was a problem hiding this comment.
I think it would be more reliable to use:
private readonly comp = viewChild.required<NzCascaderComponent>('comp');and then access the input element via this.comp().input?.id.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The
CascaderWidgetrenders a<label [attr.for]="id">viasf-item-wrap, but never sets thatidon the inner<input>rendered bynz-cascader. As a result the label'sforattribute points to a non-existent element, which fails the Lighthouse/axe "Incorrect use of<label for=FORM_ELEMENT>" audit.Rendered DOM before the fix:
document.getElementById('_sf-0')returnsnull→ the<label for>is broken and screen readers cannot associate the label with the control.This is the same class of accessibility bug that was fixed for the
abc:sewidget in #1993 (commit 61cbe73, v21.1.0), but theform:cascaderwidget was not covered.Issue Number: N/A
What is the new behavior?
The widget overrides
afterViewInit()to set the widgetidon the inner<input>rendered bynz-cascader(deferred one microtask so the input exists), and re-syncs it afterreset()since the input may be recreated when data loads.Now the inner input carries the same id the
<label for>points to, and the Lighthouse audit passes.Does this PR introduce a breaking change?
The change only adds an
idattribute to an existing<input>; no public API, selector, or behavior changes.Other information
widget.spec.tsunder a new[accessibility]describe block, asserting that the inner<input>id matches the<label for>value.tsc -p packages/tsconfig.json --noEmitand ESLint both clean.