Skip to content

Commit

Permalink
allow setting files (#9463)
Browse files Browse the repository at this point in the history
  • Loading branch information
eltigerchino authored Nov 16, 2023
1 parent 755fe33 commit 37f2493
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-geese-learn.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" />`
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/phases/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const binding_properties = {
},
files: {
event: 'change',
type: 'set',
valid_elements: ['input'],
omit_in_ssr: true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from '../../assert';

export default test({
async test({ assert, window }) {
const input = window.document.querySelector('input');
await new Promise((r) => setTimeout(r, 100));
assert.equal(input?.files?.length, 1);
window.document.querySelector('button')?.click();
await new Promise((r) => setTimeout(r, 100));
assert.equal(input?.files?.length, 0);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { onMount } from "svelte";
let files;
onMount(() => {
let list = new DataTransfer();
let file = new File(["content"], "filename.jpg");
list.items.add(file);
files = list.files;
})
</script>

<input type="file" bind:files />

<button onclick={() => files = new DataTransfer().files}>Reset</button>

1 comment on commit 37f2493

@vercel
Copy link

@vercel vercel bot commented on 37f2493 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-svelte.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.