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

Closes #1116
This commit is contained in:
metonym 2022-02-21 13:32:05 -08:00 committed by GitHub
commit 7586b2a10f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 8 deletions

View file

@ -1263,7 +1263,7 @@ None.
| Prop name | Kind | Reactive | Type | Default value | Description | | Prop name | Kind | Reactive | Type | Default value | Description |
| :--------------- | :----------------- | :------- | :----------------------------------------------------------------------------------------- | --------------------------------------- | ----------------------------------------------------------- | | :--------------- | :----------------- | :------- | :----------------------------------------------------------------------------------------- | --------------------------------------- | ----------------------------------------------------------- |
| files | <code>let</code> | Yes | <code>File[]</code> | <code>[]</code> | Obtain the uploaded file names | | files | <code>let</code> | Yes | <code>File[]</code> | <code>[]</code> | Obtain a reference to the uploaded files |
| status | <code>let</code> | No | <code>"uploading" &#124; "edit" &#124; "complete"</code> | <code>"uploading"</code> | Specify the file uploader status | | status | <code>let</code> | No | <code>"uploading" &#124; "edit" &#124; "complete"</code> | <code>"uploading"</code> | Specify the file uploader status |
| accept | <code>let</code> | No | <code>string[]</code> | <code>[]</code> | Specify the accepted file types | | accept | <code>let</code> | No | <code>string[]</code> | <code>[]</code> | Specify the accepted file types |
| multiple | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to allow multiple files | | multiple | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to allow multiple files |
@ -1300,6 +1300,7 @@ None.
| :------------------ | :--------------- | :------- | :----------------------------------------------------------------------------------------- | ------------------------------------------------ | -------------------------------------------- | | :------------------ | :--------------- | :------- | :----------------------------------------------------------------------------------------- | ------------------------------------------------ | -------------------------------------------- |
| ref | <code>let</code> | Yes | <code>null &#124; HTMLInputElement</code> | <code>null</code> | Obtain a reference to the input HTML element | | ref | <code>let</code> | Yes | <code>null &#124; HTMLInputElement</code> | <code>null</code> | Obtain a reference to the input HTML element |
| labelText | <code>let</code> | Yes | <code>string</code> | <code>"Add file"</code> | Specify the label text | | labelText | <code>let</code> | Yes | <code>string</code> | <code>"Add file"</code> | Specify the label text |
| files | <code>let</code> | Yes | <code>File[]</code> | <code>[]</code> | Obtain a reference to the uploaded files |
| accept | <code>let</code> | No | <code>string[]</code> | <code>[]</code> | Specify the accepted file types | | accept | <code>let</code> | No | <code>string[]</code> | <code>[]</code> | Specify the accepted file types |
| multiple | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to allow multiple files | | multiple | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to allow multiple files |
| disabled | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the input | | disabled | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the input |

View file

@ -3481,7 +3481,7 @@
{ {
"name": "files", "name": "files",
"kind": "let", "kind": "let",
"description": "Obtain the uploaded file names", "description": "Obtain a reference to the uploaded files",
"type": "File[]", "type": "File[]",
"value": "[]", "value": "[]",
"isFunction": false, "isFunction": false,
@ -3607,6 +3607,17 @@
"constant": false, "constant": false,
"reactive": false "reactive": false
}, },
{
"name": "files",
"kind": "let",
"description": "Obtain a reference to the uploaded files",
"type": "File[]",
"value": "[]",
"isFunction": false,
"isFunctionDeclaration": false,
"constant": false,
"reactive": true
},
{ {
"name": "multiple", "name": "multiple",
"kind": "let", "kind": "let",

View file

@ -18,7 +18,7 @@
export let accept = []; export let accept = [];
/** /**
* Obtain the uploaded file names * Obtain a reference to the uploaded files
* @type {File[]} * @type {File[]}
*/ */
export let files = []; export let files = [];

View file

@ -9,6 +9,12 @@
*/ */
export let accept = []; export let accept = [];
/**
* Obtain a reference to the uploaded files
* @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 }) => {

View file

@ -16,7 +16,7 @@ export interface FileUploaderProps
accept?: string[]; accept?: string[];
/** /**
* Obtain the uploaded file names * Obtain a reference to the uploaded files
* @default [] * @default []
*/ */
files?: File[]; files?: File[];

View file

@ -9,6 +9,12 @@ export interface FileUploaderButtonProps
*/ */
accept?: string[]; accept?: string[];
/**
* Obtain a reference to the uploaded files
* @default []
*/
files?: File[];
/** /**
* Set to `true` to allow multiple files * Set to `true` to allow multiple files
* @default false * @default false