Skip to content

Commit

Permalink
optimize api for utils/form
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Apr 3, 2024
1 parent bca6b08 commit 19bbfc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions spx-gui/src/components/project/ProjectCreate.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<NForm v-bind="formBinds" :model="formValue">
<NForm v-bind="form.binds" :model="form.value">
<NFormItem :label="_t({ en: 'Project Name', zh: '项目名' })" path="name">
<NInput v-model:value="formValue.name" />
<NInput v-model:value="form.value.name" />
</NFormItem>
<NFormItem>
<NButton type="tertiary" @click="handleCancel">
Expand Down Expand Up @@ -29,7 +29,7 @@ const emit = defineEmits<{
const userStore = useUserStore()
const [formBinds, formValue, validateForm] = useForm({
const form = useForm({
name: ['', validateName]
})
Expand All @@ -44,10 +44,10 @@ const addProject = useMessageHandle(
)
async function handleSubmit() {
const errs = await validateForm()
const errs = await form.validate()
if (errs.length > 0) return
const projectData = await addProject({
name: formValue.value.name,
name: form.value.name,
isPublic: IsPublic.personal,
files: {}
})
Expand Down
8 changes: 4 additions & 4 deletions spx-gui/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @desc Helpers to deal with form validation
*/

import { ref } from 'vue'
import { reactive, ref } from 'vue'
import type { FormRules, FormInst, FormValidationError } from 'naive-ui'
import { useI18n, type LocaleMessage } from './i18n'

Expand All @@ -23,11 +23,11 @@ export function useForm(input: FormInput) {

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

const formValue = ref<{ [path: string]: any }>({})
const formValue = reactive<{ [path: string]: any }>({})
const formRules: FormRules = {}
Object.keys(input).forEach((path) => {
const [initialValue, validator] = input[path]
formValue.value[path] = initialValue
formValue[path] = initialValue
formRules[path] = {
async validator(_: unknown, v: unknown) {
const result = await validator(v)
Expand Down Expand Up @@ -58,5 +58,5 @@ export function useForm(input: FormInput) {
rules: formRules
}

return [binds, formValue, validate] as const
return { binds, value: formValue, validate }
}

0 comments on commit 19bbfc4

Please sign in to comment.