Form-associated mixins/controllers #2344
Replies: 6 comments 20 replies
-
I second this kind of thing. I think that the UI world has long needed some sort of "standard library" for stuff like this, because every time we make a form element component, we end up re-writing these kinds of logic controllers different ways. IMO, the Lit ecosystem makes perfect sense to publish "the Lit version" of a lib that could aim to be a JS UI standard lib so that we dont have to re-invent the form controller wheel every time. I've also got some other ideas for similar things, but on the "build tools" side of the house for folks that want to make icon components from a pile of SVGs, or turn a .scss/.css file into a compiled Typescript Id love for open-wc to be the home for these kinds of efforts instead of caleb and I working on spinning up another standalone UI library |
Beta Was this translation helpful? Give feedback.
-
I rather like the idea here! Questions:
|
Beta Was this translation helpful? Give feedback.
-
@Westbrook my two cents on your questions:
|
Beta Was this translation helpful? Give feedback.
-
I think Michael summed it up pretty well.
Forward behaviorA class DesignSystemInput extends FormControl(LitElement) {
static styles = css`/* styles */`;
@property({ type: String, reflect: true })
label = '';
@query('input')
input: HTMLInputElement;
firstUpdated() {
this.forwardControl(this.input);
}
render() {
return html`<label for="input">${this.label}</label>
<input id="input" name="${this.name}" ?required="${live(this.required)}" ?disabled="${live(this.disabled}" @input="${this.onInput}">`;
}
private onInput(event: KeyboardEvent) {
this.value = (event.target as HTMLInputElement).value;
}
} In the above we could ostensibly forward the value and full validity state from We know from experience that these things are likely to be common patterns that will need to be implemented repeatedly. I suppose the linking function could fall to a directive, but I don't have a ton of experience in that space, so I can't speak to it well. CompositionWe could also potentially compose these behaviors by using a series of controllers like the function FormControl(SuperClass) {
return class extends SuperClass {
static formAssociated = true;
internals = this.attachInternals();
valueController = new ValueController(this, this.internals);
requiredController = new RequiredController(this, this.internals, 'valueKeyToEvaluate');
/** etc. this is a naive example, hopefully the idea stands */
}
} |
Beta Was this translation helpful? Give feedback.
-
This all sounds pretty exciting. With a focus on these form association controllers, how should we get started? Should we start marking out APIs as a whole? |
Beta Was this translation helpful? Give feedback.
-
OK, I've been playing with this quite a bit lately and I have a proof of concept ready that I'd love feedback on if you have a chance. This also includes a link to a demo on CodePen. Limitations
|
Beta Was this translation helpful? Give feedback.
-
Would Open-WC be interested in publishing a set of utilities designed to help ease integrations with the
ElementInternals
spec to build form-associated custom elements? I have some ideas here that I've worked with @michaelwarren1106 on in the past. We're interested in making some of those concepts open source, but want don't really know of a great platform for this. Some core ideas:FormControlMixin
that takes a super class (probablyLitElement
, but potentially more generic) that has simple utilities or get/set patterns for setting the form-associated element's value and validity state based on a built-in element likeHTMLInputElement
,HTMLSelectElement
, etc.If this isn't something open-wc is interested in supporting, I completely get that, but would be really interested in working with this group to begin filling this gap in the dev community.
Beta Was this translation helpful? Give feedback.
All reactions