Closed mmun closed 8 years ago
This makes me sooo happy. No more input components.
Lgtm
As a side note, even if you're probably aware, using native inputs is more than on order of magnitude faster and memory is at least 1/3 smaller.
Quick benchmark (without this fix): https://benchmark-inputs.pagefrontapp.com/input-helper
Check console.debug
s
People are using bound
<input>
s. For the most part everything works great.But not everything is rainbows and butterflies. Text inputs have an annoying wart. Whenever you update their
value
, the cursor is moved to the end of the input. This becomes a problem when you are trying to edit text in the middle of a text input. Curiously, this issue doesn't affect<textarea>
(confirmed on Firefox/Chrome/Safari).Here's an ember-twiddle that show cases the current state of affairs.
Why does this happen?
A little background first. When you edit a text input through the browser GUI the
oninput
event is triggered. When you change the input element'svalue
property, theoninput
event is not fired, but the cursor is set to the end of the text field.Now let's say you write
and
If you edit the text input the browser will internally update the
value
property on the input element and then fire theoninput
event. Our handler mutates the value ofuser.name
. This triggers thevalue={{user.name}}
binding to update, or in other words callsProposed fix
In order to fix this we can avoid setting .value when we don't need to. This will skirt the issue of resetting the cursor when
value
is mutated. Specifically, we add a guard toPropertyAttrMorph
so that we avoid callinginput.value = newValue
if the follow conditions are met:this.attrName === 'value'
this.element.tagName === 'INPUT'
this.element.value == newValue
Here is an ember-twiddle with the patch applied. Note how editing the Name text input works correctly now. Even dead keys work just fine! (Try entering
Option-e + e
in the text input. It should generate a "é" character.)Trade-offs
Making this change has a small trade off. It alters DOM semantics so that, when the conditions above are met, we treat the mutation as a no-op rather than a set-the-value-to-what-it-already-was. We believe that this is a very small price to pay for a massive improvement in developer ergonomics.