UNPKG

ai-native-form

Version:

AI-powered React form assistant with GPT-4, speech input, and schema auto-generation.

19 lines (18 loc) 905 B
import { jsx as _jsx } from "react/jsx-runtime"; import { createContext, useContext, useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; const AIFormContext = createContext(undefined); export function useAIFormContext() { const ctx = useContext(AIFormContext); if (!ctx) throw new Error("❌ useAIFormContext must be used within <AIFormProvider>"); return ctx; } export function AIFormProvider({ children, apiKey: initialKey = "", defaultPrompt, methods, }) { // local state to hold key (either provided or entered) const [apiKey, setApiKey] = useState(initialKey); // react-hook-form methods fallback const fallback = useForm(); const formMethods = methods ?? fallback; return (_jsx(AIFormContext.Provider, { value: { apiKey, setApiKey, defaultPrompt }, children: _jsx(FormProvider, { ...formMethods, children: children }) })); }