-
Notifications
You must be signed in to change notification settings - Fork 11
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 @lit-labs/forms and FormAssociated RFC #45
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall i appreciate the minimal impact on public APIs and the design of these APIs. It feels well thought out and has had quite a bit of time put into it. Overall I don't see any major blockers beyond attachInternals()
needing to be managed by Lit with no escape hatches for if it was already defined.
attachInternals
has to be managed by Lit. I don't know if there is a way around this, but many people call attachInternals()
on a base class so they can leverage CSS custom states on any element, not just form associated elements. Would there be room in the mixin for providing a custom function for grabbing an existing ElementInternals state and if the function is provided, this helper should not attempt to register via attachInternals()
Initial idea is accepting an optional function argument which will get evaluated when calling getInternals(el)
. Something like the following:
class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => getInternals(element) })
// "internals" is a "public" field on the base element. The author using Lit's form associated may not have control over this.
class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => element.internals })
// Some libraries like Material UI use symbols to "protect" element internals.
export const elementInternalsSymbol = new Symbol("elementInternals")
import { elementInternalsSymbol } from "./internal/element-internals.js"
class MyEl extends FormAssociated(BaseElement, { elementInternals: (element) => element[elementInternalsSymbol] })
rfcs/NNN-form-associated-mixin.md
Outdated
|
||
#### ElementInternals | ||
|
||
The `FormAssociated()` mixin needs access to `ElementInternals`, so it calls `this.attachInternals()` in the constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a user has already called attachInternals()
in another base class? Is there any escape hatch to provide it with an existing instance of ElementInternals?
The `FormAssociated()` mixin implements `formDisabledCallback()` to set the | ||
`ariaDisabled` state and request a reactive update in case the element renders | ||
differently when disabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this do so via ElementInternals.ariaDisabled
?
I know in my tests screen readers still didn't have support for this ~2months ago, but sprouting aria-disabled
on the host feels equally bad :/
I'm assuming we could just document this and hopefully it eventually gets fixed, and if needed a consumer could override, call super.formDisabledCallback()
and then add their own aria-disabled
attribute if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this do so via
ElementInternals.ariaDisabled
?
Yes
I know in my tests screen readers still didn't have support for this ~2months ago, but sprouting
aria-disabled
on the host feels equally bad :/I'm assuming we could just document this and hopefully it eventually gets fixed, and if needed a consumer could override, call
super.formDisabledCallback()
and then add their ownaria-disabled
attribute if needed.
I think documenting it is good. You could potentially implement a small polyfill that implements ElementInternals.ariaDisabled
by sprouting an attribute, and remove that over time.
I like the simplicity of this API, but this is only usable for native |
// Setting this field calls internals.setFormValue() | ||
@formValue() | ||
@property({reflect: true}) | ||
accessor value: string = ''; | ||
|
||
// When the form resets, the value is set to this field | ||
@formDefaultValue() | ||
accessor defaultValue: string = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this representative of how normal form controls operate? In my (limited) testing, value
is usually not reflected, and the current value for the value
attribute is used as default, so I expected:
// Setting this field calls internals.setFormValue()
@formValue()
@property({reflect: false, attribute: false})
accessor value: string = '';
// When the form resets, the value is set to this field
@formDefaultValue()
@property({attribute: 'value'})
accessor defaultValue: string = '';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point here is that you can use the decorators to construct the public API you want, including as you point out, one that matches the oddities of <input>
. Yes, you do not have to reflect any of these decorated fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw, I'd still change the example: it's looks a lot like <input>
and doesn't require much change to behave like <input>
. Either that or explicitly mention that this behaves differently from <input>
so nobody ever copies this blindly.
// Setting this field calls internals.setFormValue() | |
@formValue() | |
@property({reflect: true}) | |
accessor value: string = ''; | |
// When the form resets, the value is set to this field | |
@formDefaultValue() | |
accessor defaultValue: string = ''; | |
// Setting this field calls internals.setFormValue() | |
@formValue() | |
@property({attribute: false}) | |
accessor value: string = ''; | |
// When the form resets, the value is set to this field | |
@formDefaultValue() | |
@property({attribute: 'value', reflect: true}) | |
accessor defaultValue: string = ''; |
whenever the value is changed. This is implemented by the `@formValue()` | ||
decorator, which wrapps an accesor's setter to call `setValue()`. | ||
|
||
A form value must be of type `string | File | FormData | null`. To facilitiate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I take that you can mimic <select multiple>
by having a FormData
value containing multiple values for the same name, right? Is that something to spend a few words on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but this sentence is merely reflecting the platform API constraints. This library won't dictate or interfere with how form data is represented, so however you would do this manually is how you would do it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in the scope of this PR, but as a webcomponent author, a section in some docs about both points I mentioned would be very helpful. Very finicky to get right :).
throw new DOMException( | ||
`Failed to execute 'attachInternals' on 'HTMLElement': ` + | ||
`ElementInternals for the specified element was already attached.`, | ||
'NotSupportedError'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: how about just calling super.attachInternals()
here to let the native implementation throw?
// Setting this field calls internals.setFormValue() | ||
@formValue() | ||
@property({reflect: true}) | ||
accessor value: string = ''; | ||
|
||
// When the form resets, the value is set to this field | ||
@formDefaultValue() | ||
accessor defaultValue: string = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw, I'd still change the example: it's looks a lot like <input>
and doesn't require much change to behave like <input>
. Either that or explicitly mention that this behaves differently from <input>
so nobody ever copies this blindly.
// Setting this field calls internals.setFormValue() | |
@formValue() | |
@property({reflect: true}) | |
accessor value: string = ''; | |
// When the form resets, the value is set to this field | |
@formDefaultValue() | |
accessor defaultValue: string = ''; | |
// Setting this field calls internals.setFormValue() | |
@formValue() | |
@property({attribute: false}) | |
accessor value: string = ''; | |
// When the form resets, the value is set to this field | |
@formDefaultValue() | |
@property({attribute: 'value', reflect: true}) | |
accessor defaultValue: string = ''; |
accessor defaultValue: string = ''; | ||
|
||
render() { | ||
return html`<input .value=${this.value}> @input=${this.#onInput}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
return html`<input .value=${this.value}> @input=${this.#onInput}`; | |
return html`<input .value=${this.value} @input=${this.#onInput}>`; |
|
||
The default value and state can be designated by decorators, in one of two ways: | ||
|
||
- Decorators: The `@defaultValue()` and `@defaultState()` decorators let the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Decorators: The `@defaultValue()` and `@defaultState()` decorators let the | |
- Decorators: The `@formDefaultValue()` and `@formDefaultState()` decorators let the |
- A set of decorators for designating fields as the form value and state | ||
- `@formValue()` for the public form value field | ||
- `@formState()` for the fields that compose part of the form state | ||
- `@formDefaultValue()` for the public form default value field |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is lib-labs/form handling whether the form is "touched" in some form? Because native <input>
elements will have their value
and checked
change whenever defaultValue
and defaultChecked
are changed as long as the input hasn't been touched: https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-fe-dirty-2
If it's a non-goal then maybe be explicit about it (or is it already and I missed it?)
Would it be an option to extract the ElementInternals management into its own mixin? I feel like e.g. the assignment of the role via static property would be useful in other scenarios. I also liked the suggestion in lit/lit#4773 (comment), which also seems like a clean separation between implementers and consumers. |
RFC for a new
@lit-labs/forms
package based on the work I prototyped in lit/lit#4773