1
0
Fork 0
mirror of synced 2024-07-05 22:40:39 +12:00

Clean components

This commit is contained in:
Adria Navarro 2024-03-27 10:21:26 +01:00
parent b99a51d48b
commit 39ac734edd
3 changed files with 27 additions and 20 deletions

View file

@ -1,7 +1,6 @@
<script> <script>
import { onMount, getContext } from "svelte" import { onMount, getContext } from "svelte"
import { Dropzone } from "@budibase/bbui" import { Dropzone } from "@budibase/bbui"
import { FieldType } from "@budibase/types"
export let value export let value
export let focused = false export let focused = false
@ -11,17 +10,13 @@
export let invertX = false export let invertX = false
export let invertY = false export let invertY = false
export let schema export let schema
export let maximum
const { API, notifications } = getContext("grid") const { API, notifications } = getContext("grid")
const imageExtensions = ["png", "tiff", "gif", "raw", "jpg", "jpeg"] const imageExtensions = ["png", "tiff", "gif", "raw", "jpg", "jpeg"]
let isOpen = false let isOpen = false
$: isSingle = schema?.type === FieldType.ATTACHMENT_SINGLE
$: arrayValue = (value && !Array.isArray(value) ? [value] : value) || []
$: maximum = isSingle ? 1 : schema.constraints?.length?.maximum
$: editable = focused && !readonly $: editable = focused && !readonly
$: { $: {
if (!focused) { if (!focused) {
@ -74,15 +69,6 @@
} }
} }
const onFileChange = e => {
let value = e.detail
if (isSingle) {
value = value[0] || null
}
onChange(value)
}
onMount(() => { onMount(() => {
api = { api = {
focus: () => open(), focus: () => open(),
@ -96,7 +82,7 @@
<!-- svelte-ignore a11y-no-static-element-interactions --> <!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events --> <!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="attachment-cell" class:editable on:click={editable ? open : null}> <div class="attachment-cell" class:editable on:click={editable ? open : null}>
{#each arrayValue as attachment} {#each value || [] as attachment}
{#if isImage(attachment.extension)} {#if isImage(attachment.extension)}
<img src={attachment.url} alt={attachment.extension} /> <img src={attachment.url} alt={attachment.extension} />
{:else} {:else}
@ -110,10 +96,10 @@
{#if isOpen} {#if isOpen}
<div class="dropzone" class:invertX class:invertY> <div class="dropzone" class:invertX class:invertY>
<Dropzone <Dropzone
value={arrayValue} {value}
compact compact
on:change={onFileChange} on:change={e => onChange(e.detail)}
{maximum} maximum={maximum || schema.constraints?.length?.maximum}
{processFiles} {processFiles}
{deleteAttachments} {deleteAttachments}
{handleFileTooLarge} {handleFileTooLarge}

View file

@ -0,0 +1,20 @@
<script>
import AttachmentCell from "./AttachmentCell.svelte"
export let value
export let onChange
$: arrayValue = (!Array.isArray(value) && value ? [value] : value) || []
$: onFileChange = value => {
value = value[0] || null
onChange(value)
}
</script>
<AttachmentCell
{...$$restProps}
maximum={1}
value={arrayValue}
onChange={onFileChange}
/>

View file

@ -11,6 +11,7 @@ import BooleanCell from "../cells/BooleanCell.svelte"
import FormulaCell from "../cells/FormulaCell.svelte" import FormulaCell from "../cells/FormulaCell.svelte"
import JSONCell from "../cells/JSONCell.svelte" import JSONCell from "../cells/JSONCell.svelte"
import AttachmentCell from "../cells/AttachmentCell.svelte" import AttachmentCell from "../cells/AttachmentCell.svelte"
import AttachmentSingleCell from "../cells/AttachmentSingleCell.svelte"
import BBReferenceCell from "../cells/BBReferenceCell.svelte" import BBReferenceCell from "../cells/BBReferenceCell.svelte"
const TypeComponentMap = { const TypeComponentMap = {
@ -23,7 +24,7 @@ const TypeComponentMap = {
[FieldType.NUMBER]: NumberCell, [FieldType.NUMBER]: NumberCell,
[FieldType.BOOLEAN]: BooleanCell, [FieldType.BOOLEAN]: BooleanCell,
[FieldType.ATTACHMENT]: AttachmentCell, [FieldType.ATTACHMENT]: AttachmentCell,
[FieldType.ATTACHMENT_SINGLE]: AttachmentCell, [FieldType.ATTACHMENT_SINGLE]: AttachmentSingleCell,
[FieldType.LINK]: RelationshipCell, [FieldType.LINK]: RelationshipCell,
[FieldType.FORMULA]: FormulaCell, [FieldType.FORMULA]: FormulaCell,
[FieldType.JSON]: JSONCell, [FieldType.JSON]: JSONCell,