Skip to content

remove guard for input number #4689

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

Merged
merged 5 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/compiler/compile/render_dom/wrappers/Element/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class BindingWrapper {

this.is_readonly = this.node.is_readonly;

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

get_dependencies() {
Expand Down Expand Up @@ -93,11 +93,23 @@ export default class BindingWrapper {
update_conditions.push(block.renderer.dirty(dependency_array));
}

if (parent.node.name === 'input') {
const type = parent.node.get_static_attribute_value('type');

if (type === null || type === "" || type === "text" || type === "email" || type === "password") {
update_conditions.push(x`${parent.var}.${this.node.name} !== ${this.snippet}`);
if (parent.node.name === "input") {
const type = parent.node.get_static_attribute_value("type");

if (
type === null ||
type === "" ||
type === "text" ||
type === "email" ||
type === "password"
) {
update_conditions.push(
x`${parent.var}.${this.node.name} !== ${this.snippet}`
);
} else if (type === "number") {
update_conditions.push(
x`@to_number(${parent.var}.${this.node.name}) !== ${this.snippet}`
);
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/runtime/samples/binding-input-number-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default {
test({ assert, target, window, component }) {
const input = target.querySelector("input");
const inputEvent = new window.InputEvent("input");
assert.equal(component.value, 5);
assert.equal(input.value, "5");

input.value = "5.";
input.dispatchEvent(inputEvent);

// input type number has value === "" if ends with dot/comma
assert.equal(component.value, undefined);
assert.equal(input.value, "");

input.value = "5.5";
input.dispatchEvent(inputEvent);

assert.equal(component.value, 5.5);
assert.equal(input.value, "5.5");

input.value = "5.50";
input.dispatchEvent(inputEvent);

assert.equal(component.value, 5.5);
assert.equal(input.value, "5.50");

component.value = 1;
assert.equal(component.value, 1);
assert.equal(input.value, "1");
},
};
5 changes: 5 additions & 0 deletions test/runtime/samples/binding-input-number-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value = 5;
</script>

<input type="number" bind:value />