open-wc / form-participation

Helper, mixins, tools, and more for supporting your custom elements in participating in an ancestor form.
MIT License
53 stars 7 forks source link

feat/group validation #12

Closed calebdwilliams closed 2 years ago

calebdwilliams commented 2 years ago
michaelwarren1106 commented 2 years ago

my main question about this approach is how a single instance of a component is supposed to know that its in a group. for comps like checkboxes/radios where this logic will mostly apply, being in a group is just having the same name as a sibling control. So if we're implementing a web component to emulate a checkbox, in order to set the formControlValidationGroup static prop, we'd have to do the same querySelector that the validator would end up doing.

are you thinking something like:

connectedCallback() {
  const parent = this.getRootNode();
  const inAGroup = parent.querySelectorAll(`[name="${this.name}"]`).length > 0;
  if(inaGroup && this.wantGroupBehavior) {
   this.formControlValidationGroup = true;
  } 
}

wont radio buttons with the same name automatically have this behavior via internals?

calebdwilliams commented 2 years ago

The static prop really just says that if the component is in a group and it enters into a valid state, to mark all instances within that group (same name/localName within a root) as similarly valid. You should ever only set that once. If a control (like a checkbox proxy component) is a standalone it can potentially be marked as part of a group and used as a solo component, no extra work will be done.