in seatunnel-ui/src/views/datasource/create/use-form.ts [33:113]
export function useForm(type: string) {
const { t } = useI18n()
const router = useRouter()
const formStructuresStore = useFormStructuresStore()
const initialValues = {
pluginName: type,
datasourceName: '',
description: ''
}
const state = reactive({
detailForm: { ...initialValues },
formName: '',
formStructure: [] as StructureItem[],
locales: {},
rules: {
name: {
trigger: ['input'],
validator() {
if (!state.detailForm.datasourceName) {
return new Error(t('datasource.datasource_name_tips'))
}
}
}
} as FormRules
})
const getFormItems = async (value: string) => {
if (formStructuresStore.getItem(value)) {
state.formStructure = formStructuresStore.getItem(value) as StructureItem[]
return
}
const result: any = await dynamicFormItems(value)
try {
const res = JSON.parse(result)
res.forms = res.forms.map((form: any) => ({ ...form, span: 12 }))
Object.assign(state.detailForm, useFormField(res.forms))
Object.assign(
state.rules,
useFormValidate(res.forms, state.detailForm, t)
)
state.locales = res.locales
state.formStructure = useFormStructure(
res.apis ? useFormRequest(res.apis, res.forms) : res.forms
) as any
} finally {}
}
const changeType = (value: string) => {
router.replace({ name: 'datasource-create', query: { type: value } })
getFormItems(value)
}
const resetFieldsValue = () => {
state.detailForm = { ...initialValues }
}
const setFieldsValue = (values: any) => {
Object.assign(state.detailForm, values)
}
const getFieldsValue = () => state.detailForm
onMounted(() => {
if (type) {
getFormItems(type)
}
})
return {
state,
changeType,
resetFieldsValue,
getFieldsValue,
setFieldsValue,
getFormItems
}
}