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

Add missing lifecycle methods for elements with Custom Element Form Association. #1645

Open
6 tasks done
christian-bromann opened this issue Nov 22, 2023 · 0 comments
Open
6 tasks done

Comments

@christian-bromann
Copy link

πŸ” Search Terms

formDisabledCallback, formResetCallback, formAssociatedCallback, formStateRestoreCallback

βœ… Viability Checklist

⭐ Suggestion

Web components that access the internal form control APIs e.g.:

// Form-associated custom elements must be autonomous custom elements--
// meaning they must extend HTMLElement, not one of its subclasses.
class MyCounter extends HTMLElement {

  // Identify the element as a form-associated custom element
  static formAssociated = true;

  constructor() {
    super();
    // Get access to the internal form control APIs
    this.internals_ = this.attachInternals();
    // internal value for this control
    this.value_ = 0;
  }
  // ...
}

have additional lifecycle events: formDisabledCallback, formResetCallback, formAssociatedCallback, formStateRestoreCallback. Adding these to the DOM types would help get type support for their parameters. I suggest the following interface:

  formDisabledCallback?: (disabled: boolean) => void
  formResetCallback?: () => void
  formAssociatedCallback?: (form: HTMLFormElement) => void
  formStateRestoreCallback?: (state: any, mode: 'restore' | 'autocomplete') => void

You can read more about form element internals in https://web.dev/articles/more-capable-form-controls.

πŸ“ƒ Motivating Example

Providing type safety for custom components.

πŸ’» Use Cases

  1. What do you want to use this for?
    I am working on a component framework library and would love to have type support for these lifecycle events.

  2. What shortcomings exist with current approaches?
    Errors could sneak in because these interfaces aren't documented.

  3. What workarounds are you using in the meantime?
    Extending existing DOM apis.

@andrewbranch andrewbranch transferred this issue from microsoft/TypeScript Nov 22, 2023
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

No branches or pull requests

1 participant