form
parent
fd0a8d5834
commit
2572e99a4b
@ -0,0 +1,2 @@
|
||||
export * from './use-check-validated'
|
||||
export * from './use-get-form-values'
|
||||
@ -0,0 +1,36 @@
|
||||
import { useCallback } from 'react'
|
||||
import type { AnyFormApi } from '@tanstack/react-form'
|
||||
import { useToastContext } from '@/app/components/base/toast'
|
||||
|
||||
export const useCheckValidated = (form: AnyFormApi) => {
|
||||
const { notify } = useToastContext()
|
||||
|
||||
const checkValidated = useCallback(() => {
|
||||
const allError = form?.getAllErrors()
|
||||
|
||||
if (allError) {
|
||||
const fields = allError.fields
|
||||
const errorArray = Object.keys(fields).reduce((acc: string[], key: string) => {
|
||||
const errors: any[] = fields[key].errors
|
||||
|
||||
return [...acc, ...errors]
|
||||
}, [] as string[])
|
||||
|
||||
if (errorArray.length) {
|
||||
notify({
|
||||
type: 'error',
|
||||
message: errorArray[0],
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return true
|
||||
}, [form, notify])
|
||||
|
||||
return {
|
||||
checkValidated,
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
import { useCallback } from 'react'
|
||||
import type { AnyFormApi } from '@tanstack/react-form'
|
||||
import { useCheckValidated } from './use-check-validated'
|
||||
import type {
|
||||
FormSchema,
|
||||
GetValuesOptions,
|
||||
} from '../types'
|
||||
import { getTransformedValuesWhenSecretInputPristine } from '../utils'
|
||||
|
||||
export const useGetFormValues = (form: AnyFormApi) => {
|
||||
const { checkValidated } = useCheckValidated(form)
|
||||
|
||||
const getFormValues = useCallback((
|
||||
formSchemas: FormSchema[],
|
||||
{
|
||||
needCheckValidatedValues,
|
||||
needTransformWhenSecretFieldIsPristine,
|
||||
}: GetValuesOptions,
|
||||
) => {
|
||||
const values = form?.store.state.values || {}
|
||||
if (!needCheckValidatedValues) {
|
||||
return {
|
||||
values,
|
||||
isCheckValidated: false,
|
||||
}
|
||||
}
|
||||
|
||||
if (checkValidated()) {
|
||||
return {
|
||||
values: needTransformWhenSecretFieldIsPristine ? getTransformedValuesWhenSecretInputPristine(formSchemas, form) : values,
|
||||
isCheckValidated: true,
|
||||
}
|
||||
}
|
||||
else {
|
||||
return {
|
||||
values: {},
|
||||
isCheckValidated: false,
|
||||
}
|
||||
}
|
||||
}, [form, checkValidated])
|
||||
|
||||
return {
|
||||
getFormValues,
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
export * from './secret-input'
|
||||
@ -0,0 +1,29 @@
|
||||
import type { AnyFormApi } from '@tanstack/react-form'
|
||||
import type { FormSchema } from '@/app/components/base/form/types'
|
||||
import { FormTypeEnum } from '@/app/components/base/form/types'
|
||||
|
||||
export const transformFormSchemasSecretInput = (isPristineSecretInputNames: string[], values: Record<string, any>) => {
|
||||
const transformedValues: Record<string, any> = { ...values }
|
||||
|
||||
isPristineSecretInputNames.forEach((name) => {
|
||||
if (transformedValues[name])
|
||||
transformedValues[name] = '[__HIDDEN__]'
|
||||
})
|
||||
|
||||
return transformedValues
|
||||
}
|
||||
|
||||
export const getTransformedValuesWhenSecretInputPristine = (formSchemas: FormSchema[], form: AnyFormApi) => {
|
||||
const values = form?.store.state.values || {}
|
||||
const isPristineSecretInputNames: string[] = []
|
||||
for (let i = 0; i < formSchemas.length; i++) {
|
||||
const schema = formSchemas[i]
|
||||
if (schema.type === FormTypeEnum.secretInput) {
|
||||
const fieldMeta = form?.getFieldMeta(schema.name)
|
||||
if (fieldMeta?.isPristine)
|
||||
isPristineSecretInputNames.push(schema.name)
|
||||
}
|
||||
}
|
||||
|
||||
return transformFormSchemasSecretInput(isPristineSecretInputNames, values)
|
||||
}
|
||||
Loading…
Reference in New Issue