allow setting files (#9463)

pull/9493/head
Tee Ming 1 year ago committed by GitHub
parent 755fe33a57
commit 37f249350c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: allow setting files binding for `<input type="file" />`

@ -182,6 +182,7 @@ export const binding_properties = {
}, },
files: { files: {
event: 'change', event: 'change',
type: 'set',
valid_elements: ['input'], valid_elements: ['input'],
omit_in_ssr: true omit_in_ssr: true
} }

@ -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);
}
});

@ -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>
Loading…
Cancel
Save