Skip to content

Commit

Permalink
extract form value initialization into utils/form
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Apr 2, 2024
1 parent 58838c2 commit bca6b08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
16 changes: 5 additions & 11 deletions spx-gui/src/components/project/ProjectCreate.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<NForm ref="formRef" :model="formValue" :rules="formRules">
<NForm v-bind="formBinds" :model="formValue">
<NFormItem :label="_t({ en: 'Project Name', zh: '项目名' })" path="name">
<NInput v-model:value="formValue.name" />
</NFormItem>
Expand All @@ -15,9 +15,8 @@
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { NForm, NFormItem, NInput, NButton } from 'naive-ui'
import { type ProjectData, getProject, addProject as rawAddProject, IsPublic } from '@/apis/project'
import { type ProjectData, getProject, addProject as apiAddProject, IsPublic } from '@/apis/project'
import { useForm, type ValidationResult } from '@/utils/form'
import { useMessageHandle } from '@/utils/exception'
import { useUserStore } from '@/stores/user'
Expand All @@ -30,26 +29,21 @@ const emit = defineEmits<{
const userStore = useUserStore()
const formValue = ref({
name: ''
})
const [formRef, formRules, validateForm] = useForm({
name: validateName
const [formBinds, formValue, validateForm] = useForm({
name: ['', validateName]
})
function handleCancel() {
emit('cancelled')
}
const addProject = useMessageHandle(
rawAddProject,
apiAddProject,
{ en: 'Failed to create project', zh: '创建失败' },
(project) => ({ en: `Project ${project.name} created`, zh: `项目 ${project.name} 创建成功` })
)
async function handleSubmit() {
if (formRef.value == null) return
const errs = await validateForm()
if (errs.length > 0) return
const projectData = await addProject({
Expand Down
26 changes: 18 additions & 8 deletions spx-gui/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ export type ValidatorReturned = ValidationResult | Promise<ValidationResult>

export type Validator<V> = (v: V) => ValidatorReturned

export type FormRulesInput = {
[path: string]: Validator<any>
export type FormInput = {
[path: string]: [initialValue: unknown, validator: Validator<any>]
}

export function useForm(rulesInput: FormRulesInput) {
// TODO: better type definition for better type-safety
export function useForm(input: FormInput) {
const { t } = useI18n()

const formRef = ref<FormInst | null>(null)

const rules: FormRules = {}
Object.keys(rulesInput).forEach((path) => {
const validator = rulesInput[path]
rules[path] = {
const formValue = ref<{ [path: string]: any }>({})
const formRules: FormRules = {}
Object.keys(input).forEach((path) => {
const [initialValue, validator] = input[path]
formValue.value[path] = initialValue
formRules[path] = {
async validator(_: unknown, v: unknown) {
const result = await validator(v)
if (result == null) return
Expand All @@ -48,5 +51,12 @@ export function useForm(rulesInput: FormRulesInput) {
return errs
}

return [formRef, rules, validate] as const
// for NForm v-bind
const binds = {
// TODO: model in binds, to avoid `:model="formValue"`
ref: formRef,
rules: formRules
}

return [binds, formValue, validate] as const
}

0 comments on commit bca6b08

Please sign in to comment.