Skip to content

Commit 7ac3e60

Browse files
authored
use value check instead of guard for number inputs (#4689)
1 parent 467fc84 commit 7ac3e60

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/compiler/compile/render_dom/wrappers/Element/Binding.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default class BindingWrapper {
5757

5858
this.is_readonly = this.node.is_readonly;
5959

60-
this.needs_lock = this.node.name === 'currentTime' || (parent.node.name === 'input' && parent.node.get_static_attribute_value('type') === 'number'); // TODO others?
60+
this.needs_lock = this.node.name === 'currentTime'; // TODO others?
6161
}
6262

6363
get_dependencies() {
@@ -93,11 +93,23 @@ export default class BindingWrapper {
9393
update_conditions.push(block.renderer.dirty(dependency_array));
9494
}
9595

96-
if (parent.node.name === 'input') {
97-
const type = parent.node.get_static_attribute_value('type');
98-
99-
if (type === null || type === "" || type === "text" || type === "email" || type === "password") {
100-
update_conditions.push(x`${parent.var}.${this.node.name} !== ${this.snippet}`);
96+
if (parent.node.name === "input") {
97+
const type = parent.node.get_static_attribute_value("type");
98+
99+
if (
100+
type === null ||
101+
type === "" ||
102+
type === "text" ||
103+
type === "email" ||
104+
type === "password"
105+
) {
106+
update_conditions.push(
107+
x`${parent.var}.${this.node.name} !== ${this.snippet}`
108+
);
109+
} else if (type === "number") {
110+
update_conditions.push(
111+
x`@to_number(${parent.var}.${this.node.name}) !== ${this.snippet}`
112+
);
101113
}
102114
}
103115

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default {
2+
test({ assert, target, window, component }) {
3+
const input = target.querySelector("input");
4+
const inputEvent = new window.InputEvent("input");
5+
assert.equal(component.value, 5);
6+
assert.equal(input.value, "5");
7+
8+
input.value = "5.";
9+
input.dispatchEvent(inputEvent);
10+
11+
// input type number has value === "" if ends with dot/comma
12+
assert.equal(component.value, undefined);
13+
assert.equal(input.value, "");
14+
15+
input.value = "5.5";
16+
input.dispatchEvent(inputEvent);
17+
18+
assert.equal(component.value, 5.5);
19+
assert.equal(input.value, "5.5");
20+
21+
input.value = "5.50";
22+
input.dispatchEvent(inputEvent);
23+
24+
assert.equal(component.value, 5.5);
25+
assert.equal(input.value, "5.50");
26+
27+
component.value = 1;
28+
assert.equal(component.value, 1);
29+
assert.equal(input.value, "1");
30+
},
31+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value = 5;
3+
</script>
4+
5+
<input type="number" bind:value />

0 commit comments

Comments
 (0)