@fixture-inc/forms-react 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ import { Form as Form$1, SubmitResult, FormField, FormStatus, FieldType, FieldValueMap } from '@fixture-inc/forms-core';
2
+ export { FieldType, FormField, FormResponse, FormStatus, Form as FormType, NormalizedError, SubmitResult } from '@fixture-inc/forms-core';
3
+ import * as React$1 from 'react';
4
+ import React__default from 'react';
5
+ import { FormClient } from '@fixture-inc/forms-browser';
6
+ export { FormClient, FormClientConfig } from '@fixture-inc/forms-browser';
7
+
8
+ /**
9
+ * React hooks for Fixture Forms
10
+ */
11
+
12
+ /**
13
+ * Options for useFixtureForm hook
14
+ */
15
+ interface UseFixtureFormOptions {
16
+ /** API endpoint. Defaults to inferred from context or 'https://api.fixture.app' */
17
+ apiUrl?: string;
18
+ /** Disable auto-fetching. Useful for SSR or conditional rendering. Default: true */
19
+ enabled?: boolean;
20
+ /** Pre-populate with server-fetched data (for SSR hydration) */
21
+ initialData?: {
22
+ form: Form$1;
23
+ nonce: string;
24
+ };
25
+ /** Custom fetch function (for testing, SSR, or proxying) */
26
+ fetcher?: (formId: string) => Promise<{
27
+ form: Form$1;
28
+ nonce: string;
29
+ }>;
30
+ /** Enable debug logging */
31
+ debug?: boolean;
32
+ /** Called on successful submission */
33
+ onSuccess?: (result: SubmitResult) => void;
34
+ /** Called on any error */
35
+ onError?: (error: {
36
+ message: string;
37
+ }) => void;
38
+ }
39
+ /**
40
+ * Props for binding to form inputs
41
+ */
42
+ interface FieldInputProps {
43
+ id: string;
44
+ name: string;
45
+ value: string | boolean;
46
+ onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => void;
47
+ required?: boolean;
48
+ type?: string;
49
+ checked?: boolean;
50
+ }
51
+ /**
52
+ * Return type for useFixtureForm hook
53
+ */
54
+ interface UseFixtureFormReturn {
55
+ /** Form definition (null while loading) */
56
+ form: Form$1 | null;
57
+ /** Form fields array (empty while loading) */
58
+ fields: FormField[];
59
+ /** Current form status */
60
+ status: FormStatus;
61
+ /** Global error message */
62
+ error: string | null;
63
+ /** Submission result (available after success) */
64
+ result: SubmitResult | null;
65
+ /** Current field values. Use getFieldValue() for type-safe access. */
66
+ values: Record<string, unknown>;
67
+ /** Field validation errors */
68
+ errors: Record<string, string>;
69
+ /** Get field value with canonical type */
70
+ getFieldValue: <T extends FieldType>(fieldId: string) => FieldValueMap[T] | undefined;
71
+ /** Set a single field value */
72
+ setValue: (fieldId: string, value: unknown) => void;
73
+ /** Set multiple field values */
74
+ setValues: (values: Record<string, unknown>) => void;
75
+ /** Submit the form */
76
+ submit: () => Promise<void>;
77
+ /** Reset form to initial values */
78
+ reset: () => void;
79
+ /** Refetch form from server */
80
+ refetch: () => Promise<void>;
81
+ /** Get props for binding to an input element */
82
+ getFieldProps: (fieldId: string) => FieldInputProps;
83
+ }
84
+ /**
85
+ * Hook for using Fixture Forms in React
86
+ *
87
+ * @example
88
+ * ```tsx
89
+ * function ContactForm() {
90
+ * const { form, fields, values, setValue, submit, status } = useFixtureForm('contact-form')
91
+ *
92
+ * if (status === 'loading') return <div>Loading...</div>
93
+ * if (status === 'success') return <div>Thanks!</div>
94
+ *
95
+ * return (
96
+ * <form onSubmit={(e) => { e.preventDefault(); submit(); }}>
97
+ * {fields.map(field => (
98
+ * <input
99
+ * key={field.id}
100
+ * {...getFieldProps(field.id)}
101
+ * />
102
+ * ))}
103
+ * <button type="submit" disabled={status === 'submitting'}>
104
+ * Submit
105
+ * </button>
106
+ * </form>
107
+ * )
108
+ * }
109
+ * ```
110
+ */
111
+ declare function useFixtureForm(formId: string, options?: UseFixtureFormOptions): UseFixtureFormReturn;
112
+ /**
113
+ * Hook to get the visitor ID from the forms tracker
114
+ */
115
+ declare function useVisitorId(): string | null;
116
+
117
+ /**
118
+ * Context value for the forms provider
119
+ */
120
+ interface FixtureFormsContextValue {
121
+ /** Shared FormClient instance */
122
+ client: FormClient;
123
+ /** Default API URL */
124
+ apiUrl?: string;
125
+ /** Debug mode enabled */
126
+ debug?: boolean;
127
+ }
128
+ /**
129
+ * Context for sharing FormClient across components
130
+ */
131
+ declare const FixtureFormsContext: React$1.Context<FixtureFormsContextValue | null>;
132
+ /**
133
+ * Hook to access the forms context
134
+ *
135
+ * @returns Context value or null if not in provider
136
+ */
137
+ declare function useFixtureFormsContext(): FixtureFormsContextValue | null;
138
+
139
+ /**
140
+ * Pre-built Form component
141
+ *
142
+ * A complete, styled form component that handles loading, submission, and success states.
143
+ */
144
+
145
+ interface FormProps extends UseFixtureFormOptions {
146
+ /** Form ID to render */
147
+ formId: string;
148
+ /** Custom className for the form container */
149
+ className?: string;
150
+ /** Custom submit button text */
151
+ submitText?: string;
152
+ /** Custom submitting button text */
153
+ submittingText?: string;
154
+ /** Custom success title */
155
+ successTitle?: string;
156
+ /** Custom success message (overrides server response) */
157
+ successMessage?: string;
158
+ /** Custom loading message */
159
+ loadingMessage?: string;
160
+ /** Hide the "Powered by Fixture" badge */
161
+ hideBadge?: boolean;
162
+ /** Custom render function for the form */
163
+ children?: (props: {
164
+ form: Form$1;
165
+ fields: Form$1['fields'];
166
+ values: Record<string, unknown>;
167
+ errors: Record<string, string>;
168
+ setValue: (fieldId: string, value: unknown) => void;
169
+ getFieldProps: (fieldId: string) => FieldInputProps;
170
+ submit: () => Promise<void>;
171
+ status: string;
172
+ }) => React__default.ReactNode;
173
+ }
174
+ /**
175
+ * Pre-built form component with default styling
176
+ */
177
+ declare function Form({ formId, className, submitText, submittingText, successTitle, successMessage, loadingMessage, hideBadge, children, ...options }: FormProps): React__default.ReactElement;
178
+
179
+ /**
180
+ * Pre-built Field component
181
+ *
182
+ * Renders a single form field with appropriate input type and styling.
183
+ */
184
+
185
+ interface FieldProps {
186
+ /** Field definition */
187
+ field: FormField;
188
+ /** Current value */
189
+ value: unknown;
190
+ /** Validation error */
191
+ error?: string;
192
+ /** Called when value changes */
193
+ onChange: (value: unknown) => void;
194
+ }
195
+ /**
196
+ * Render a single form field
197
+ */
198
+ declare function Field({ field, value, error, onChange, }: FieldProps): React__default.ReactElement;
199
+
200
+ export { Field, type FieldInputProps, type FieldProps, FixtureFormsContext, type FixtureFormsContextValue, Form, type FormProps, type UseFixtureFormOptions, type UseFixtureFormReturn, useFixtureForm, useFixtureFormsContext, useVisitorId };
@@ -0,0 +1,200 @@
1
+ import { Form as Form$1, SubmitResult, FormField, FormStatus, FieldType, FieldValueMap } from '@fixture-inc/forms-core';
2
+ export { FieldType, FormField, FormResponse, FormStatus, Form as FormType, NormalizedError, SubmitResult } from '@fixture-inc/forms-core';
3
+ import * as React$1 from 'react';
4
+ import React__default from 'react';
5
+ import { FormClient } from '@fixture-inc/forms-browser';
6
+ export { FormClient, FormClientConfig } from '@fixture-inc/forms-browser';
7
+
8
+ /**
9
+ * React hooks for Fixture Forms
10
+ */
11
+
12
+ /**
13
+ * Options for useFixtureForm hook
14
+ */
15
+ interface UseFixtureFormOptions {
16
+ /** API endpoint. Defaults to inferred from context or 'https://api.fixture.app' */
17
+ apiUrl?: string;
18
+ /** Disable auto-fetching. Useful for SSR or conditional rendering. Default: true */
19
+ enabled?: boolean;
20
+ /** Pre-populate with server-fetched data (for SSR hydration) */
21
+ initialData?: {
22
+ form: Form$1;
23
+ nonce: string;
24
+ };
25
+ /** Custom fetch function (for testing, SSR, or proxying) */
26
+ fetcher?: (formId: string) => Promise<{
27
+ form: Form$1;
28
+ nonce: string;
29
+ }>;
30
+ /** Enable debug logging */
31
+ debug?: boolean;
32
+ /** Called on successful submission */
33
+ onSuccess?: (result: SubmitResult) => void;
34
+ /** Called on any error */
35
+ onError?: (error: {
36
+ message: string;
37
+ }) => void;
38
+ }
39
+ /**
40
+ * Props for binding to form inputs
41
+ */
42
+ interface FieldInputProps {
43
+ id: string;
44
+ name: string;
45
+ value: string | boolean;
46
+ onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => void;
47
+ required?: boolean;
48
+ type?: string;
49
+ checked?: boolean;
50
+ }
51
+ /**
52
+ * Return type for useFixtureForm hook
53
+ */
54
+ interface UseFixtureFormReturn {
55
+ /** Form definition (null while loading) */
56
+ form: Form$1 | null;
57
+ /** Form fields array (empty while loading) */
58
+ fields: FormField[];
59
+ /** Current form status */
60
+ status: FormStatus;
61
+ /** Global error message */
62
+ error: string | null;
63
+ /** Submission result (available after success) */
64
+ result: SubmitResult | null;
65
+ /** Current field values. Use getFieldValue() for type-safe access. */
66
+ values: Record<string, unknown>;
67
+ /** Field validation errors */
68
+ errors: Record<string, string>;
69
+ /** Get field value with canonical type */
70
+ getFieldValue: <T extends FieldType>(fieldId: string) => FieldValueMap[T] | undefined;
71
+ /** Set a single field value */
72
+ setValue: (fieldId: string, value: unknown) => void;
73
+ /** Set multiple field values */
74
+ setValues: (values: Record<string, unknown>) => void;
75
+ /** Submit the form */
76
+ submit: () => Promise<void>;
77
+ /** Reset form to initial values */
78
+ reset: () => void;
79
+ /** Refetch form from server */
80
+ refetch: () => Promise<void>;
81
+ /** Get props for binding to an input element */
82
+ getFieldProps: (fieldId: string) => FieldInputProps;
83
+ }
84
+ /**
85
+ * Hook for using Fixture Forms in React
86
+ *
87
+ * @example
88
+ * ```tsx
89
+ * function ContactForm() {
90
+ * const { form, fields, values, setValue, submit, status } = useFixtureForm('contact-form')
91
+ *
92
+ * if (status === 'loading') return <div>Loading...</div>
93
+ * if (status === 'success') return <div>Thanks!</div>
94
+ *
95
+ * return (
96
+ * <form onSubmit={(e) => { e.preventDefault(); submit(); }}>
97
+ * {fields.map(field => (
98
+ * <input
99
+ * key={field.id}
100
+ * {...getFieldProps(field.id)}
101
+ * />
102
+ * ))}
103
+ * <button type="submit" disabled={status === 'submitting'}>
104
+ * Submit
105
+ * </button>
106
+ * </form>
107
+ * )
108
+ * }
109
+ * ```
110
+ */
111
+ declare function useFixtureForm(formId: string, options?: UseFixtureFormOptions): UseFixtureFormReturn;
112
+ /**
113
+ * Hook to get the visitor ID from the forms tracker
114
+ */
115
+ declare function useVisitorId(): string | null;
116
+
117
+ /**
118
+ * Context value for the forms provider
119
+ */
120
+ interface FixtureFormsContextValue {
121
+ /** Shared FormClient instance */
122
+ client: FormClient;
123
+ /** Default API URL */
124
+ apiUrl?: string;
125
+ /** Debug mode enabled */
126
+ debug?: boolean;
127
+ }
128
+ /**
129
+ * Context for sharing FormClient across components
130
+ */
131
+ declare const FixtureFormsContext: React$1.Context<FixtureFormsContextValue | null>;
132
+ /**
133
+ * Hook to access the forms context
134
+ *
135
+ * @returns Context value or null if not in provider
136
+ */
137
+ declare function useFixtureFormsContext(): FixtureFormsContextValue | null;
138
+
139
+ /**
140
+ * Pre-built Form component
141
+ *
142
+ * A complete, styled form component that handles loading, submission, and success states.
143
+ */
144
+
145
+ interface FormProps extends UseFixtureFormOptions {
146
+ /** Form ID to render */
147
+ formId: string;
148
+ /** Custom className for the form container */
149
+ className?: string;
150
+ /** Custom submit button text */
151
+ submitText?: string;
152
+ /** Custom submitting button text */
153
+ submittingText?: string;
154
+ /** Custom success title */
155
+ successTitle?: string;
156
+ /** Custom success message (overrides server response) */
157
+ successMessage?: string;
158
+ /** Custom loading message */
159
+ loadingMessage?: string;
160
+ /** Hide the "Powered by Fixture" badge */
161
+ hideBadge?: boolean;
162
+ /** Custom render function for the form */
163
+ children?: (props: {
164
+ form: Form$1;
165
+ fields: Form$1['fields'];
166
+ values: Record<string, unknown>;
167
+ errors: Record<string, string>;
168
+ setValue: (fieldId: string, value: unknown) => void;
169
+ getFieldProps: (fieldId: string) => FieldInputProps;
170
+ submit: () => Promise<void>;
171
+ status: string;
172
+ }) => React__default.ReactNode;
173
+ }
174
+ /**
175
+ * Pre-built form component with default styling
176
+ */
177
+ declare function Form({ formId, className, submitText, submittingText, successTitle, successMessage, loadingMessage, hideBadge, children, ...options }: FormProps): React__default.ReactElement;
178
+
179
+ /**
180
+ * Pre-built Field component
181
+ *
182
+ * Renders a single form field with appropriate input type and styling.
183
+ */
184
+
185
+ interface FieldProps {
186
+ /** Field definition */
187
+ field: FormField;
188
+ /** Current value */
189
+ value: unknown;
190
+ /** Validation error */
191
+ error?: string;
192
+ /** Called when value changes */
193
+ onChange: (value: unknown) => void;
194
+ }
195
+ /**
196
+ * Render a single form field
197
+ */
198
+ declare function Field({ field, value, error, onChange, }: FieldProps): React__default.ReactElement;
199
+
200
+ export { Field, type FieldInputProps, type FieldProps, FixtureFormsContext, type FixtureFormsContextValue, Form, type FormProps, type UseFixtureFormOptions, type UseFixtureFormReturn, useFixtureForm, useFixtureFormsContext, useVisitorId };