Skip to main content
Glama
use-computed-field.ts858 B
import { useEffect } from "react" import type { FieldPath, FieldValues, Path, PathValue, UseFormReturn } from "react-hook-form" type UseComputedFieldProps<T extends FieldValues> = { form: UseFormReturn<T> sourceField: FieldPath<T> computedField: FieldPath<T> enabled?: boolean callback: (value: string) => string } export const useComputedField = <T extends FieldValues>({ form, sourceField, computedField, enabled = true, callback, }: UseComputedFieldProps<T>) => { const source = form.watch(sourceField) const state = form.getFieldState(computedField) useEffect(() => { if (enabled && !state.isTouched) { form.setValue(computedField, callback(source) as PathValue<T, Path<T>>, { shouldValidate: form.formState.isSubmitted, shouldDirty: form.formState.isDirty, }) } }, [source, enabled]) }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/metacode0602/open-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server