@canard/schema-form
Version:
React-based component library that renders forms based on JSON Schema with plugin system support for validators and UI components
29 lines (28 loc) • 1.2 kB
TypeScript
import { type RefObject } from 'react';
import type { FormHandle } from '../components/Form';
import type { AllowedValue, InferValueType, JsonSchema } from '../types';
/**
* Hook for handling form submission with pending state management.
* Provides a submit function and pending state synchronized with the form's internal submit handler.
*
* @example
* ```typescript
* const formRef = useRef<FormHandle<typeof schema>>(null);
* const { submit, pending } = useFormSubmit(formRef);
*
* // In component
* <Form ref={formRef} jsonSchema={schema} onSubmit={handleSubmit} />
* <button onClick={submit} disabled={pending}>
* {pending ? 'Submitting...' : 'Submit'}
* </button>
* ```
*
* @param ref - React ref to FormHandle instance
* @returns Object containing submit function and pending state
* @returns {Function} submit - Async function that triggers form submission
* @returns {boolean | undefined} pending - Submission pending state
*/
export declare const useFormSubmit: <Schema extends JsonSchema, Value extends AllowedValue = InferValueType<Schema>>(ref: RefObject<FormHandle<Schema, Value> | null>) => {
submit: () => Promise<void>;
pending: boolean | undefined;
};