-
Which package(s) are affected?Lit Core (lit / lit-html / lit-element / reactive-element) DescriptionII'm not sure it is a bug but I do have an unexpected behavior (in Firefox and Chromium) when making a webComponent that wraps a number input (in the full element we show some error messages). If you don't set the value and type I tried to make the component as small as possible, we do use an internal variable to prevent unneeded renders for example but in some cases we do trigger a My criteria:
The element: import { html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
@customElement('number-element')
export class NumberElementComponent extends LitElement {
@property({ attribute: false })
public accessor value: number;
public render() {
return html`
<input
type="number"
.valueAsNumber=${ifDefined(this.value != null ? live(this.value) : null)}
@input=${this.handleInput}
/>
`;
}
private handleInput(e: { currentTarget: HTMLInputElement, preventDefault() }): void {
const value = e.currentTarget.valueAsNumber;
this.value = isNaN(value) ? null : value;
}
} The usage: html`<number-element .value=${123}></number-element>` ReproductionThe input should not be cleared when you replace or append a WorkaroundWe couldn't find any workarounds Is this a regression?No or unsure. This never worked, or I haven't tried before. Affected versionsFails for lit@3.1.0 and lit@3.1.2 Browser/OS/Node environmentFirefox (123.0.1 (64-bit)) and Chrome (122.0.6261.129 (64-bit)) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Hmm.. is the idea to never modify what's being shown in the input from what the user is typing? In that case you maybe don't even want to do a value property binding to the element. You can use the If you don't want to adjust the input while the user is typing, but do want to modify it after the user clicks away, then you should handle the If you do keep the property binding, Because you're also coercing values resulting in |
Beta Was this translation helpful? Give feedback.
-
My criteria:
I tried your suggestion (playground) but it seems that the |
Beta Was this translation helpful? Give feedback.
-
Haha, we had similar issues. This is our solution. |
Beta Was this translation helpful? Give feedback.
-
A couple of notes:
I think one confusing think about this component is that right now the input is "controlled" (meaning it's always set from the outside) when the value is valid, and I think you're trying to make it "uncontrolled" (meaning it keeps it's own state) when the value is invalid. My guess is that's more difficult to reason about than keeping the input always controlled, but @Totati's suggestion works if you keep it that way. See this updated demo. If you make your component fully controlled, then you would alway set the value, and either keep the current string value around to set into the input, or add a keydown event handler to prevent entering non-valid characters in the first place. The fact that |
Beta Was this translation helpful? Give feedback.
-
Using |
Beta Was this translation helpful? Give feedback.
Haha, we had similar issues. This is our solution.
Base on the source