2025-07-17 17:18:44 +08:00
|
|
|
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, formSchemas: FormSchema[]) => {
|
|
|
|
|
const { checkValidated } = useCheckValidated(form, formSchemas)
|
|
|
|
|
|
|
|
|
|
const getFormValues = useCallback((
|
|
|
|
|
{
|
2025-11-12 17:59:37 +08:00
|
|
|
needCheckValidatedValues = true,
|
2025-07-17 17:18:44 +08:00
|
|
|
needTransformWhenSecretFieldIsPristine,
|
|
|
|
|
}: GetValuesOptions,
|
|
|
|
|
) => {
|
|
|
|
|
const values = form?.store.state.values || {}
|
|
|
|
|
if (!needCheckValidatedValues) {
|
|
|
|
|
return {
|
|
|
|
|
values,
|
2025-11-12 17:59:37 +08:00
|
|
|
isCheckValidated: true,
|
2025-07-17 17:18:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (checkValidated()) {
|
|
|
|
|
return {
|
|
|
|
|
values: needTransformWhenSecretFieldIsPristine ? getTransformedValuesWhenSecretInputPristine(formSchemas, form) : values,
|
|
|
|
|
isCheckValidated: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return {
|
|
|
|
|
values: {},
|
|
|
|
|
isCheckValidated: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [form, checkValidated, formSchemas])
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
getFormValues,
|
|
|
|
|
}
|
|
|
|
|
}
|