Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.3 KB
Raw Blame
import type { AnyFormApi } from '@tanstack/react-form'
import type { FormSchema, GetValuesOptions } from '../types'
import { useCallback } from 'react'
import { getTransformedValuesWhenSecretInputPristine } from '../utils/secret-input'
import { useCheckValidated } from './use-check-validated'

export const useGetFormValues = (
  form: AnyFormApi,
  formSchemas: FormSchema[],
  onInvalidField?: (name: string) => void,
) => {
  const { checkValidated } = useCheckValidated(form, formSchemas, onInvalidField)

  const getFormValues = useCallback(
    ({
      needCheckValidatedValues = true,
      needTransformWhenSecretFieldIsPristine,
    }: GetValuesOptions) => {
      const values = form?.store.state.values || {}
      if (!needCheckValidatedValues) {
        return {
          values,
          isCheckValidated: true,
        }
      }

      if (checkValidated()) {
        return {
          values: needTransformWhenSecretFieldIsPristine
            ? getTransformedValuesWhenSecretInputPristine(formSchemas, form)
            : values,
          isCheckValidated: true,
        }
      } else {
        return {
          values: {},
          isCheckValidated: false,
        }
      }
    },
    [form, checkValidated, formSchemas],
  )

  return {
    getFormValues,
  }
}