Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
let updater: Node[];
const init = this.get_init(block, value);

// Set inputs value default to '' if undefined
if (name == 'value') {
block.chunks.mount.push(b`@set_input_value(${element.var}, ${value});`);
}

if (is_legacy_input_type) {
block.chunks.hydrate.push(
b`@set_input_type(${element.var}, ${init});`
Expand All @@ -149,6 +154,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
updater = b`@select_option(${element.var}, ${value});`;
}


block.chunks.mount.push(b`
${updater}
`);
Expand Down
85 changes: 85 additions & 0 deletions test/js/samples/input-default-value/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
append,
detach,
element,
init,
insert,
listen,
noop,
safe_not_equal,
set_data,
set_input_value,
space,
text
} from "svelte/internal";

function create_fragment(ctx) {
let input;
let t0;
let h1;
let t1;
let t2;
let mounted;
let dispose;

return {
c() {
input = element("input");
t0 = space();
h1 = element("h1");
t1 = text(/*name*/ ctx[0]);
t2 = text("!");
input.value = /*name*/ ctx[0];
},
m(target, anchor) {
insert(target, input, anchor);
set_input_value(input, /*name*/ ctx[0]);
insert(target, t0, anchor);
insert(target, h1, anchor);
append(h1, t1);
append(h1, t2);

if (!mounted) {
dispose = listen(input, "input", /*onInput*/ ctx[1]);
mounted = true;
}
},
p(ctx, [dirty]) {
if (dirty & /*name*/ 1 && input.value !== /*name*/ ctx[0]) {
input.value = /*name*/ ctx[0];
}

if (dirty & /*name*/ 1) set_data(t1, /*name*/ ctx[0]);
},
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(input);
if (detaching) detach(t0);
if (detaching) detach(h1);
mounted = false;
dispose();
}
};
}

function instance($$self, $$props, $$invalidate) {
let name;

function onInput(event) {
$$invalidate(0, name = event.target.value);
}

return [name, onInput];
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}

export default Component;
11 changes: 11 additions & 0 deletions test/js/samples/input-default-value/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
let name ;

function onInput(event) {
name = event.target.value
}
</script>

<input value={name} on:input={onInput}>

<h1>{name}!</h1>
2 changes: 2 additions & 0 deletions test/js/samples/input-value/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
run_all,
safe_not_equal,
set_data,
set_input_value,
space,
text
} from "svelte/internal";
Expand Down Expand Up @@ -169,6 +170,7 @@ function create_fragment(ctx) {
},
m(target, anchor) {
insert(target, input0, anchor);
set_input_value(input0, /*name*/ ctx[0]);
insert(target, t0, anchor);
insert(target, input1, anchor);
insert(target, t1, anchor);
Expand Down
6 changes: 5 additions & 1 deletion test/js/samples/select-dynamic-value/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
insert,
noop,
safe_not_equal,
select_option
select_option,
set_input_value
} from "svelte/internal";

function create_fragment(ctx) {
Expand All @@ -31,7 +32,10 @@ function create_fragment(ctx) {
m(target, anchor) {
insert(target, select, anchor);
append(select, option0);
set_input_value(option0, "1");
append(select, option1);
set_input_value(option1, "2");
set_input_value(select, /*current*/ ctx[0]);
select_option(select, /*current*/ ctx[0]);
},
p(ctx, [dirty]) {
Expand Down