feat(file-uploader-button): support files prop

Closes #1116
This commit is contained in:
Eric Liu 2022-02-21 08:48:39 -08:00
commit bf85af2c81

View file

@ -9,6 +9,12 @@
*/ */
export let accept = []; export let accept = [];
/**
* Obtain the uploaded file names
* @type {File[]}
*/
export let files = [];
/** Set to `true` to allow multiple files */ /** Set to `true` to allow multiple files */
export let multiple = false; export let multiple = false;
@ -45,6 +51,13 @@
import { createEventDispatcher } from "svelte"; import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let initialLabelText = labelText;
$: if (ref && files.length === 0) {
labelText = initialLabelText;
ref.value = null;
}
</script> </script>
<label <label
@ -80,13 +93,13 @@
class:bx--visually-hidden="{true}" class:bx--visually-hidden="{true}"
{...$$restProps} {...$$restProps}
on:change|stopPropagation="{({ target }) => { on:change|stopPropagation="{({ target }) => {
const files = target.files; files = [...target.files];
const length = files.length;
if (files && !disableLabelChanges) { if (files && !disableLabelChanges) {
labelText = length > 1 ? `${length} files` : files[0].name; labelText = files.length > 1 ? `${files.length} files` : files[0].name;
} }
dispatch('change', [...files]); dispatch('change', files);
}}" }}"
on:click on:click
on:click="{({ target }) => { on:click="{({ target }) => {