1
0
Fork 0
mirror of synced 2024-09-28 15:21:28 +12:00
budibase/packages/client/src/components/app/forms/AttachmentField.svelte

58 lines
1.2 KiB
Svelte
Raw Normal View History

2021-01-30 02:22:38 +13:00
<script>
import Field from "./Field.svelte"
import { CoreDropzone } from "@budibase/bbui"
import { getContext } from "svelte"
2021-01-30 02:22:38 +13:00
export let field
export let label
export let disabled = false
export let validation
2021-01-30 02:22:38 +13:00
let fieldState
let fieldApi
const { API, notificationStore } = getContext("sdk")
2021-06-08 23:50:58 +12:00
const formContext = getContext("form")
const BYTES_IN_MB = 1000000
2021-05-04 22:32:22 +12:00
const handleFileTooLarge = fileSizeLimit => {
notificationStore.actions.warning(
`Files cannot exceed ${
fileSizeLimit / BYTES_IN_MB
} MB. Please try again with smaller files.`
)
}
2021-05-04 22:32:22 +12:00
const processFiles = async fileList => {
let data = new FormData()
for (let i = 0; i < fileList.length; i++) {
data.append("file", fileList[i])
}
2021-06-08 23:50:58 +12:00
return await API.uploadAttachment(data, formContext?.dataSource?.tableId)
}
2021-01-30 02:22:38 +13:00
</script>
<Field
{label}
{field}
{disabled}
{validation}
type="attachment"
bind:fieldState
bind:fieldApi
defaultValue={[]}
>
{#if fieldState}
<CoreDropzone
value={fieldState.value}
disabled={fieldState.disabled}
error={fieldState.error}
2021-06-08 23:50:58 +12:00
on:change={e => {
fieldApi.setValue(e.detail)
}}
{processFiles}
{handleFileTooLarge}
/>
2021-01-30 02:22:38 +13:00
{/if}
</Field>