Skip to content

fix: allow setting files binding for <input type="file" /> (Svelte 4) #9080

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

Closed
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
5 changes: 5 additions & 0 deletions .changeset/long-foxes-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: allow setting files binding for `<input type="file" />`
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Numeric input values are coerced; even though `input.value` is a string as far a
<input type="range" bind:value={num} />
```

On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). It is readonly.
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList).

```svelte
<label for="avatar">Upload a picture:</label>
Expand Down
5 changes: 2 additions & 3 deletions packages/svelte/src/compiler/compile/nodes/Binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ export default class Binding extends Node {
return;
}
}
const type = parent.get_static_attribute_value('type');
this.is_readonly =
regex_dimensions.test(this.name) ||
regex_box_size.test(this.name) ||
(is_element(parent) &&
((parent.is_media_node() && read_only_media_attributes.has(this.name)) ||
(parent.name === 'input' && type === 'file'))) /* TODO others? */;
parent.is_media_node() &&
read_only_media_attributes.has(this.name)) /* TODO others? */;
}
is_readonly_media_attribute() {
return read_only_media_attributes.has(this.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ export default function (node, renderer, options) {
node_contents = x`@escape(${snippet} || "")`;
} else if (binding.name === 'value' && node.name === 'select') {
// NOTE: do not add "value" attribute on <select />
} else if (
binding.name === 'value' &&
node.name === 'input' &&
node.get_static_attribute_value('type') === 'file'
) {
const value = node.get_static_attribute_value('value');
if (value !== '') {
// NOTE: do not add "value" attribute on <input type="file" />
}
} else {
const snippet = expression.node;
renderer.add_expression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ function create_fragment(ctx) {
},
m(target, anchor) {
insert(target, input0, anchor);
input0.files = /*files*/ ctx[0];
insert(target, t, anchor);
insert(target, input1, anchor);
input1.files = /*files*/ ctx[0];

if (!mounted) {
dispose = [
Expand All @@ -42,7 +44,15 @@ function create_fragment(ctx) {
mounted = true;
}
},
p: noop,
p(ctx, [dirty]) {
if (dirty & /*files*/ 1) {
input0.files = /*files*/ ctx[0];
}

if (dirty & /*files*/ 1) {
input1.files = /*files*/ ctx[0];
}
},
i: noop,
o: noop,
d(detaching) {
Expand Down
7 changes: 6 additions & 1 deletion packages/svelte/test/js/samples/input-files/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ function create_fragment(ctx) {
},
m(target, anchor) {
insert(target, input, anchor);
input.files = /*files*/ ctx[0];

if (!mounted) {
dispose = listen(input, "change", /*input_change_handler*/ ctx[1]);
mounted = true;
}
},
p: noop,
p(ctx, [dirty]) {
if (dirty & /*files*/ 1) {
input.files = /*files*/ ctx[0];
}
},
i: noop,
o: noop,
d(detaching) {
Expand Down