Skip to content
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

input[type=file].value = '' does not reset input.files after triggerEvent #920

Closed
nag5000 opened this issue Sep 11, 2020 · 4 comments · Fixed by #924
Closed

input[type=file].value = '' does not reset input.files after triggerEvent #920

nag5000 opened this issue Sep 11, 2020 · 4 comments · Fixed by #924

Comments

@nag5000
Copy link
Contributor

nag5000 commented Sep 11, 2020

const file = new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' });
await triggerEvent(fileInput, 'change', { files: [file] });
fileInput.files.length; // 1
fileInput.value = '';
fileInput.files.length; // Actual: 1, Expected: 0.

Tmp workaround:

const fileInputProto = Object.getPrototypeOf(fileInput);
const inputValueProp = Object.getOwnPropertyDescriptor(fileInputProto, 'value');
Object.defineProperty(fileInput, 'value', {
  configurable: true,
  get() {
    return inputValueProp.get.call(fileInput);
  },
  set(value) {
    inputValueProp.set.call(fileInput, value);

    Object.defineProperty(fileInput, 'files', {
      configurable: true,
      value: []
    });
  }
});
@rwjblue
Copy link
Member

rwjblue commented Oct 19, 2020

Huh. Are you supposed to be able to truncate it by just assigning value like that? I'm not opposed to the a PR adding the work around IFF that is a normal usage pattern for folks.

@nag5000
Copy link
Contributor Author

nag5000 commented Oct 19, 2020

It's by spec: https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename

On setting, if the new value is the empty string, empty the list of selected files; otherwise, throw an "InvalidStateError" DOMException.

And it's the most popular way to reset input[type=file] on stackoverflow ;)

@rwjblue
Copy link
Member

rwjblue commented Oct 19, 2020

Thank you! Sorry for not knowing, I just haven't used Files very much personally...

@rwjblue
Copy link
Member

rwjblue commented Oct 19, 2020

In order to fix, we need to:

  • add a failing test case (seems basically to be exactly what you have written above)
  • Add the work around that you mention here

if (Array.isArray(files)) {
Object.defineProperty(files, 'item', {
value(index: number) {
return typeof index === 'number' ? this[index] : null;
},
});
Object.defineProperty(element, 'files', {
value: files,
configurable: true,
});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants