Skip to content

Simplified "getAssociatedLabels" with custom elements support - #43997

Open
Konstantin Roman (nihil-pro) wants to merge 2 commits into
microsoft:mainfrom
nihil-pro:update-fix-43984
Open

Konstantin Roman (nihil-pro) wants to merge 2 commits into
microsoft:mainfrom
nihil-pro:update-fix-43984

Conversation

@nihil-pro

Copy link
Copy Markdown

fix(selectors): support labels for any correctly form-associated custom elements

"labels" property exists only for INPUT, BUTTON, METER, OUTPUT, PROGRESS, SELECT and TEXTAREA; but hardcoding these names limits custom elements.

Any custom element that wants to be a real form element must also implement a "labels" getter:

class CustomFormElement extends HTMLElement {
  static formAssociated = true;
  #internals;

  constructor() {
    super();
    this.#internals = this.attachInternals();
  }

  get labels() {
    return this.#internals.labels;
  }
}

Also, for a hidden input, the "labels" getter will return null. No reasons to double check.
So this function can be simplified dramatically: return Array.from(Reflect.get(element, "labels") || []);

Fixes #43898

@nihil-pro

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: getByLabel (and accessible name) ignore <label> for form-associated custom elements

1 participant