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

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino 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

@eltigerchino
Copy link
Member Author

resolved the merge conflict

@eltigerchino eltigerchino 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