diff --git a/src/components/form-file/README.md b/src/components/form-file/README.md index 56f128d7839..ae908660fc3 100755 --- a/src/components/form-file/README.md +++ b/src/components/form-file/README.md @@ -145,24 +145,27 @@ assitive technologies. ## Clearing the file selection -Because of limitations in the value binding with `` elements, `v-model` -for `` is unidirectional, and cannot be used to set or clear the file(s) selection. -To get around this limitation, `` provides a `reset()` method that can be -called to clear the file input. +With inputs of type file, normally the `v-model` is uni-directional (meaning +you cannot pre-set the selected files). However, you can clear the file input's +selected files by setting the `v-model` to either `null`, an empty string, or an +empty array). -To take advantage of the `reset()` method, you will need to obtain a reference -to the `` component: +Alternatively, `` provides a `reset()` method that can be called to +clear the file input. To take advantage of the `reset()` method, you will need +to obtain a reference to the `` component. ```html -
- - Reset -
-``` + -```js -window.app = new Vue({ - el: '#app', + + + ``` +**Implementation note:** As not all browsers allow setting a value of a file +input (even to null or an empty string), `b-form-input` employs a technique that +works cross-browser that involves changing the input type to null and then back +to type file. + diff --git a/src/components/form-file/form-file.js b/src/components/form-file/form-file.js index c1d374fc724..3970e99441b 100644 --- a/src/components/form-file/form-file.js +++ b/src/components/form-file/form-file.js @@ -2,7 +2,7 @@ import idMixin from '../../mixins/id' import formMixin from '../../mixins/form' import formStateMixin from '../../mixins/form-state' import formCustomMixin from '../../mixins/form-custom' -import { from as arrayFrom } from '../../utils/array' +import { from as arrayFrom, isArray } from '../../utils/array' // temporary css until Bootstrap V4.2 is released import './form-file.css' @@ -36,7 +36,8 @@ export default { on: { change: this.onFileChange, focusin: this.focusHandler, - focusout: this.focusHandler + focusout: this.focusHandler, + reset: this.onReset } }) @@ -80,6 +81,9 @@ export default { } }, props: { + value: { + default: null + }, accept: { type: String, default: '' @@ -152,6 +156,11 @@ export default { } else { this.$emit('input', newVal) } + }, + value (newVal) { + if (!newVal || (isArray(newVal) && newVal.length === 0)) { + this.reset() + } } }, methods: { @@ -216,6 +225,10 @@ export default { this.selectedFile = files[0] } }, + onReset () { + // Triggered when the parent form (if any) is reset + this.selectedFile = this.multiple ? [] : null + }, onDragover (evt) { evt.preventDefault() evt.stopPropagation()