feat: improve file upload

This commit is contained in:
zernonia 2024-04-20 09:32:48 +08:00
parent 9162c7464c
commit baa83ecec8
2 changed files with 31 additions and 0 deletions

View File

@ -145,6 +145,15 @@ function onSubmit(values: Record<string, any>) {
</div> </div>
</template> </template>
<template #customParent="slotProps">
<div class="flex items-end space-x-2">
<AutoFormField v-bind="slotProps" class="w-full" />
<Button type="button">
Check
</Button>
</div>
</template>
<Button type="submit"> <Button type="submit">
Submit Submit
</Button> </Button>

View File

@ -1,12 +1,16 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue'
import { TrashIcon } from '@radix-icons/vue'
import { beautifyObjectName } from '../utils' import { beautifyObjectName } from '../utils'
import type { FieldProps } from '../interface' import type { FieldProps } from '../interface'
import AutoFormLabel from '../AutoFormLabel.vue' import AutoFormLabel from '../AutoFormLabel.vue'
import { FormControl, FormDescription, FormField, FormItem, FormMessage } from '@/lib/registry/new-york/ui/form' import { FormControl, FormDescription, FormField, FormItem, FormMessage } from '@/lib/registry/new-york/ui/form'
import { Input } from '@/lib/registry/new-york/ui/input' import { Input } from '@/lib/registry/new-york/ui/input'
import { Button } from '@/lib/registry/new-york/ui/button'
defineProps<FieldProps>() defineProps<FieldProps>()
const inputFile = ref<File>()
async function parseFileAsString(file: File | undefined): Promise<string> { async function parseFileAsString(file: File | undefined): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (file) { if (file) {
@ -32,15 +36,33 @@ async function parseFileAsString(file: File | undefined): Promise<string> {
<FormControl> <FormControl>
<slot v-bind="slotProps"> <slot v-bind="slotProps">
<Input <Input
v-if="!inputFile"
type="file" type="file"
v-bind="{ ...config?.inputProps }" v-bind="{ ...config?.inputProps }"
:disabled="disabled" :disabled="disabled"
@change="async (ev: InputEvent) => { @change="async (ev: InputEvent) => {
const file = (ev.target as HTMLInputElement).files?.[0] const file = (ev.target as HTMLInputElement).files?.[0]
inputFile = file
const parsed = await parseFileAsString(file) const parsed = await parseFileAsString(file)
slotProps.componentField.onInput(parsed) slotProps.componentField.onInput(parsed)
}" }"
/> />
<div v-else class="flex h-9 w-full items-center justify-between rounded-md border border-input bg-transparent pl-3 pr-1 py-1 text-sm shadow-sm transition-colors">
<p>{{ inputFile?.name }}</p>
<Button
:size="'icon'"
:variant="'ghost'"
class="h-[26px] w-[26px]"
aria-label="Remove file"
type="button"
@click="() => {
inputFile = undefined
slotProps.componentField.onInput(undefined)
}"
>
<TrashIcon />
</Button>
</div>
</slot> </slot>
</FormControl> </FormControl>
<FormDescription v-if="config?.description"> <FormDescription v-if="config?.description">