Skip to content

Conversation

@teemingc
Copy link
Member

@teemingc teemingc commented Aug 6, 2023

fixes #8316

This PR allows setting the files binding for file inputs to allow for common operations such as setting the files to a drag and drop file, or clearing the file input selection. Currently, this is not possible because bind:files is read-only in Svelte. The redundant alternative is to bind the input element instead and set the files property there.

<script>
  let files;
</script>

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

<!-- Reset file input -->
<button on:click={() => files = new DataTransfer().files}>Reset</button>

<!-- Set file input as the dragged file -->
<svelte:window
	on:drop|preventDefault={(event) => {
		const { dataTransfer } = event;
		if (!dataTransfer) return;

		files = dataTransfer.files;
	}}
	on:dragover|preventDefault
/>

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link

changeset-bot bot commented Aug 6, 2023

🦋 Changeset detected

Latest commit: 1b2dba6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann
Copy link
Member

@s3812497 it looks like this PR has a merge conflict

@teemingc
Copy link
Member Author

resolved the merge conflict

@teemingc teemingc changed the title fix: allow setting files binding for <input type="file" /> fix: allow setting files binding for <input type="file" /> (Svelte 4) Nov 15, 2023
@dummdidumm
Copy link
Member

#10032 revealed that this would actually be a breaking change, therefore closing

@dummdidumm dummdidumm closed this Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input with type="file" and two-way binding doesn't track empty fileList

3 participants