mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
parent
fd3698b49a
commit
85c4a14b2a
25 changed files with 546 additions and 28 deletions
85
src/components/FileUploader/FileUploaderDropContainer.svelte
Normal file
85
src/components/FileUploader/FileUploaderDropContainer.svelte
Normal file
|
@ -0,0 +1,85 @@
|
|||
<script>
|
||||
let className = undefined;
|
||||
export { className as class };
|
||||
export let name = '';
|
||||
export let role = 'button';
|
||||
export let id = Math.random();
|
||||
export let disabled = false;
|
||||
export let tabindex = '0';
|
||||
export let labelText = 'Add file';
|
||||
export let multiple = false;
|
||||
export let accept = [];
|
||||
export let validateFiles = files => files;
|
||||
export let style = undefined;
|
||||
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import SkeletonText from '../SkeletonText';
|
||||
import { ButtonSkeleton } from '../Button';
|
||||
import { cx } from '../../lib';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const _labelClass = cx('--file-browse-btn', disabled && '--file-browse-btn--disabled');
|
||||
|
||||
let over = false;
|
||||
let inputRef = undefined;
|
||||
|
||||
$: _class = cx('--file__drop-container', over && '--file__drop-container--drag-over', className);
|
||||
</script>
|
||||
|
||||
<div
|
||||
class={cx('--file')}
|
||||
on:dragover
|
||||
on:dragover|preventDefault|stopPropagation={({ dataTransfer }) => {
|
||||
if (!disabled) {
|
||||
over = true;
|
||||
dataTransfer.dropEffect = 'copy';
|
||||
}
|
||||
}}
|
||||
on:dragleave
|
||||
on:dragleave|preventDefault|stopPropagation={({ dataTransfer }) => {
|
||||
if (!disabled) {
|
||||
over = false;
|
||||
dataTransfer.dropEffect = 'move';
|
||||
}
|
||||
}}
|
||||
on:drop
|
||||
on:drop|preventDefault|stopPropagation={({ dataTransfer }) => {
|
||||
if (!disabled) {
|
||||
over = false;
|
||||
dispatch('add', validateFiles(dataTransfer.files));
|
||||
}
|
||||
}}
|
||||
{style}>
|
||||
<label
|
||||
class={_labelClass}
|
||||
for={id}
|
||||
on:keydown
|
||||
on:keydown={({ key }) => {
|
||||
if (key === ' ' || key === 'Enter') {
|
||||
inputRef.click();
|
||||
}
|
||||
}}
|
||||
{tabindex}>
|
||||
<div class={_class} {role}>
|
||||
{labelText}
|
||||
<input
|
||||
bind:this={inputRef}
|
||||
type="file"
|
||||
tabindex="-1"
|
||||
class={cx('--file-input')}
|
||||
on:change
|
||||
on:change={({ target }) => {
|
||||
dispatch('add', validateFiles(target.files));
|
||||
}}
|
||||
on:click
|
||||
on:click={({ target }) => {
|
||||
target.value = null;
|
||||
}}
|
||||
{id}
|
||||
{disabled}
|
||||
{accept}
|
||||
{name}
|
||||
{multiple} />
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
Loading…
Add table
Add a link
Reference in a new issue