120 lines
3.4 KiB
Vue
120 lines
3.4 KiB
Vue
<script setup lang="ts">
|
|
// Components
|
|
import Block from '~/components/pub/custom-ui/doc-entry/block.vue'
|
|
import Cell from '~/components/pub/custom-ui/doc-entry/cell.vue'
|
|
import Field from '~/components/pub/custom-ui/doc-entry/field.vue'
|
|
import Label from '~/components/pub/custom-ui/doc-entry/label.vue'
|
|
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
|
|
|
|
// Types
|
|
import type { SpecialistFormData } from '~/schemas/specialist.schema.ts'
|
|
|
|
// Helpers
|
|
import type z from 'zod'
|
|
import { toTypedSchema } from '@vee-validate/zod'
|
|
import { useForm } from 'vee-validate'
|
|
|
|
interface Props {
|
|
schema: z.ZodSchema<any>
|
|
units: any[]
|
|
values: any
|
|
isLoading?: boolean
|
|
isReadonly?: boolean
|
|
}
|
|
|
|
const props = defineProps<Props>()
|
|
const isLoading = props.isLoading !== undefined ? props.isLoading : false
|
|
const isReadonly = props.isReadonly !== undefined ? props.isReadonly : false
|
|
const emit = defineEmits<{
|
|
submit: [values: SpecialistFormData, resetForm: () => void]
|
|
cancel: [resetForm: () => void]
|
|
}>()
|
|
|
|
const { defineField, errors, meta } = useForm({
|
|
validationSchema: toTypedSchema(props.schema),
|
|
initialValues: {
|
|
code: '',
|
|
name: '',
|
|
unit_id: 0,
|
|
} as Partial<SpecialistFormData>,
|
|
})
|
|
|
|
const [code, codeAttrs] = defineField('code')
|
|
const [name, nameAttrs] = defineField('name')
|
|
const [unit, unitAttrs] = defineField('unit_id')
|
|
|
|
// Fill fields from props.values if provided
|
|
if (props.values) {
|
|
if (props.values.code !== undefined) code.value = props.values.code
|
|
if (props.values.name !== undefined) name.value = props.values.name
|
|
if (props.values.unit_id !== undefined) unit.value = props.values.unit_id
|
|
}
|
|
|
|
const resetForm = () => {
|
|
code.value = ''
|
|
name.value = ''
|
|
unit.value = ''
|
|
}
|
|
|
|
// Form submission handler
|
|
function onSubmitForm(values: any) {
|
|
const formData: SpecialistFormData = {
|
|
name: name.value || '',
|
|
code: code.value || '',
|
|
unit_id: unit.value || '',
|
|
}
|
|
emit('submit', formData, resetForm)
|
|
}
|
|
|
|
// Form cancel handler
|
|
function onCancelForm() {
|
|
emit('cancel', resetForm)
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<form id="form-specialist" @submit.prevent>
|
|
<Block labelSize="thin" class="!mb-2.5 !pt-0 xl:!mb-3" :colCount="1">
|
|
<Cell>
|
|
<Label height="compact">Kode</Label>
|
|
<Field :errMessage="errors.code">
|
|
<Input id="code" v-model="code" v-bind="codeAttrs" :disabled="isLoading || isReadonly" />
|
|
</Field>
|
|
</Cell>
|
|
<Cell>
|
|
<Label height="compact">Nama</Label>
|
|
<Field :errMessage="errors.name">
|
|
<Input id="name" v-model="name" v-bind="nameAttrs" :disabled="isLoading || isReadonly" />
|
|
</Field>
|
|
</Cell>
|
|
<Cell>
|
|
<Label height="compact">Unit</Label>
|
|
<Field :errMessage="errors.unit">
|
|
<Combobox
|
|
id="unit"
|
|
v-model="unit"
|
|
v-bind="unitAttrs"
|
|
:items="units"
|
|
:disabled="isLoading || isReadonly"
|
|
placeholder="Pilih Unit"
|
|
search-placeholder="Cari unit"
|
|
empty-message="Unit tidak ditemukan"
|
|
/>
|
|
</Field>
|
|
</Cell>
|
|
</Block>
|
|
<div class="my-2 flex justify-end gap-2 py-2">
|
|
<Button type="button" variant="secondary" class="w-[120px]" @click="onCancelForm"> Kembali </Button>
|
|
<Button
|
|
v-if="!isReadonly"
|
|
type="button"
|
|
class="w-[120px]"
|
|
:disabled="isLoading || !meta.valid"
|
|
@click="onSubmitForm"
|
|
>
|
|
Simpan
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</template>
|