@page-speed/forms 0.6.3 → 0.6.4
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.
- package/dist/{FormContext-Db0L3Kwv.d.ts → FormContext-C6kWSQuv.d.ts} +1 -1
- package/dist/{FormContext-kKZxeb7G.d.cts → FormContext-DLG3GzgL.d.cts} +1 -1
- package/dist/chunk-A7MVZ757.js +701 -0
- package/dist/chunk-A7MVZ757.js.map +1 -0
- package/dist/chunk-DCGSRMZT.cjs +728 -0
- package/dist/chunk-DCGSRMZT.cjs.map +1 -0
- package/dist/chunk-UBDA7CS5.js +3 -0
- package/dist/chunk-UBDA7CS5.js.map +1 -0
- package/dist/chunk-V7JSGFCI.cjs +4 -0
- package/dist/chunk-V7JSGFCI.cjs.map +1 -0
- package/dist/core.cjs +13 -13
- package/dist/core.d.cts +10 -8
- package/dist/core.d.ts +10 -8
- package/dist/core.js +2 -2
- package/dist/index.cjs +12 -12
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/inputs.d.cts +1 -1
- package/dist/inputs.d.ts +1 -1
- package/dist/integration.cjs +101 -3
- package/dist/integration.cjs.map +1 -1
- package/dist/integration.d.cts +103 -18
- package/dist/integration.d.ts +103 -18
- package/dist/integration.js +100 -3
- package/dist/integration.js.map +1 -1
- package/dist/{types-BPxsUGm_.d.ts → types-BhcLAcZe.d.cts} +31 -22
- package/dist/{types-BPxsUGm_.d.cts → types-BhcLAcZe.d.ts} +31 -22
- package/dist/validation-rules.d.cts +1 -1
- package/dist/validation-rules.d.ts +1 -1
- package/dist/validation-utils.d.cts +1 -1
- package/dist/validation-utils.d.ts +1 -1
- package/dist/validation-valibot.d.cts +1 -1
- package/dist/validation-valibot.d.ts +1 -1
- package/dist/validation.d.cts +1 -1
- package/dist/validation.d.ts +1 -1
- package/package.json +1 -1
- package/dist/chunk-3ED2FKXF.cjs +0 -350
- package/dist/chunk-3ED2FKXF.cjs.map +0 -1
- package/dist/chunk-H3YJRLVO.js +0 -326
- package/dist/chunk-H3YJRLVO.js.map +0 -1
- package/dist/chunk-SNSK3TMG.js +0 -373
- package/dist/chunk-SNSK3TMG.js.map +0 -1
- package/dist/chunk-V545YJFP.cjs +0 -397
- package/dist/chunk-V545YJFP.cjs.map +0 -1
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import { cn, FieldLabel, TextInput, Button, FieldDescription, Field, LabelGroup, FieldError } from './chunk-J37BGNM6.js';
|
|
2
|
+
import * as React5 from 'react';
|
|
3
|
+
import { useRef, useCallback, useContext } from 'react';
|
|
4
|
+
import { useObservable, useSelector } from '@legendapp/state/react';
|
|
5
|
+
import { useMap } from '@opensite/hooks/useMap';
|
|
6
|
+
import { cva } from 'class-variance-authority';
|
|
7
|
+
import 'radix-ui';
|
|
8
|
+
import { Icon } from '@page-speed/icon';
|
|
9
|
+
|
|
10
|
+
function useForm(options) {
|
|
11
|
+
const {
|
|
12
|
+
initialValues,
|
|
13
|
+
validationSchema,
|
|
14
|
+
validateOn = "onBlur",
|
|
15
|
+
revalidateOn = "onChange",
|
|
16
|
+
onSubmit,
|
|
17
|
+
onError,
|
|
18
|
+
debug = false
|
|
19
|
+
} = options;
|
|
20
|
+
const state$ = useObservable({
|
|
21
|
+
values: initialValues,
|
|
22
|
+
errors: {},
|
|
23
|
+
touched: {},
|
|
24
|
+
isSubmitting: false,
|
|
25
|
+
status: "idle",
|
|
26
|
+
initialValues: { ...initialValues },
|
|
27
|
+
// Create a copy to prevent reference sharing
|
|
28
|
+
hasValidated: {}
|
|
29
|
+
});
|
|
30
|
+
const validationInProgress = useRef(/* @__PURE__ */ new Set());
|
|
31
|
+
const [, fieldMetadataActions] = useMap();
|
|
32
|
+
const validateField = useCallback(
|
|
33
|
+
async (field) => {
|
|
34
|
+
const validators = validationSchema?.[field];
|
|
35
|
+
if (!validators) return void 0;
|
|
36
|
+
const fieldKey = String(field);
|
|
37
|
+
validationInProgress.current.add(fieldKey);
|
|
38
|
+
const currentMeta = fieldMetadataActions.get(fieldKey) || {
|
|
39
|
+
validationCount: 0
|
|
40
|
+
};
|
|
41
|
+
fieldMetadataActions.set(fieldKey, {
|
|
42
|
+
lastValidated: Date.now(),
|
|
43
|
+
validationCount: currentMeta.validationCount + 1
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
const value = state$.values[field].get();
|
|
47
|
+
const allValues = state$.values.get();
|
|
48
|
+
const validatorArray = Array.isArray(validators) ? validators : [validators];
|
|
49
|
+
for (const validator of validatorArray) {
|
|
50
|
+
const error = await validator(value, allValues);
|
|
51
|
+
if (error) {
|
|
52
|
+
state$.errors[field].set(error);
|
|
53
|
+
validationInProgress.current.delete(fieldKey);
|
|
54
|
+
return error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
state$.errors[field].set(void 0);
|
|
58
|
+
validationInProgress.current.delete(fieldKey);
|
|
59
|
+
return void 0;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
validationInProgress.current.delete(fieldKey);
|
|
62
|
+
const errorMessage = error instanceof Error ? error.message : "Validation error";
|
|
63
|
+
state$.errors[field].set(errorMessage);
|
|
64
|
+
return errorMessage;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
[validationSchema, state$, fieldMetadataActions]
|
|
68
|
+
);
|
|
69
|
+
const validateForm = useCallback(async () => {
|
|
70
|
+
if (!validationSchema) return {};
|
|
71
|
+
const fields = Object.keys(validationSchema);
|
|
72
|
+
const errors2 = {};
|
|
73
|
+
await Promise.all(
|
|
74
|
+
fields.map(async (field) => {
|
|
75
|
+
const error = await validateField(field);
|
|
76
|
+
if (error) {
|
|
77
|
+
errors2[field] = error;
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
);
|
|
81
|
+
state$.errors.set(errors2);
|
|
82
|
+
return errors2;
|
|
83
|
+
}, [validationSchema, validateField, state$]);
|
|
84
|
+
const setFieldValue = useCallback(
|
|
85
|
+
(field, value) => {
|
|
86
|
+
state$.values[field].set(value);
|
|
87
|
+
const shouldRevalidate = revalidateOn === "onChange" && state$.hasValidated[String(field)].get();
|
|
88
|
+
if (shouldRevalidate && validationSchema?.[field]) {
|
|
89
|
+
validateField(field);
|
|
90
|
+
}
|
|
91
|
+
if (debug) {
|
|
92
|
+
console.log("[useForm] setFieldValue:", { field, value });
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
[state$, revalidateOn, validationSchema, validateField, debug]
|
|
96
|
+
);
|
|
97
|
+
const setFieldTouched = useCallback(
|
|
98
|
+
(field, touched2) => {
|
|
99
|
+
state$.touched[field].set(touched2);
|
|
100
|
+
if (touched2 && validateOn === "onBlur" && validationSchema?.[field]) {
|
|
101
|
+
state$.hasValidated[String(field)].set(true);
|
|
102
|
+
validateField(field);
|
|
103
|
+
}
|
|
104
|
+
if (debug) {
|
|
105
|
+
console.log("[useForm] setFieldTouched:", { field, touched: touched2 });
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
[state$, validateOn, validationSchema, validateField, debug]
|
|
109
|
+
);
|
|
110
|
+
const resetForm = useCallback(() => {
|
|
111
|
+
state$.values.set(state$.initialValues.get());
|
|
112
|
+
state$.errors.set({});
|
|
113
|
+
state$.touched.set({});
|
|
114
|
+
state$.isSubmitting.set(false);
|
|
115
|
+
state$.status.set("idle");
|
|
116
|
+
state$.hasValidated.set({});
|
|
117
|
+
fieldMetadataActions.clear();
|
|
118
|
+
if (debug) {
|
|
119
|
+
console.log("[useForm] Form reset");
|
|
120
|
+
}
|
|
121
|
+
}, [state$, fieldMetadataActions, debug]);
|
|
122
|
+
const handleSubmit = useCallback(
|
|
123
|
+
async (e) => {
|
|
124
|
+
e?.preventDefault();
|
|
125
|
+
if (debug) {
|
|
126
|
+
console.log("[useForm] handleSubmit started");
|
|
127
|
+
}
|
|
128
|
+
state$.isSubmitting.set(true);
|
|
129
|
+
state$.status.set("submitting");
|
|
130
|
+
try {
|
|
131
|
+
const errors2 = await validateForm();
|
|
132
|
+
const hasErrors = Object.keys(errors2).length > 0;
|
|
133
|
+
if (hasErrors) {
|
|
134
|
+
state$.status.set("error");
|
|
135
|
+
onError?.(errors2);
|
|
136
|
+
if (debug) {
|
|
137
|
+
console.log("[useForm] Validation errors:", errors2);
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const helpers = {
|
|
142
|
+
setValues: (values2) => {
|
|
143
|
+
if (typeof values2 === "function") {
|
|
144
|
+
state$.values.set(values2(state$.values.get()));
|
|
145
|
+
} else {
|
|
146
|
+
state$.values.set(values2);
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
setFieldValue,
|
|
150
|
+
setErrors: (errors3) => state$.errors.set(errors3),
|
|
151
|
+
setFieldError: (field, error) => state$.errors[field].set(error),
|
|
152
|
+
setTouched: (touched2) => state$.touched.set(touched2),
|
|
153
|
+
setFieldTouched,
|
|
154
|
+
setSubmitting: (submitting) => state$.isSubmitting.set(submitting),
|
|
155
|
+
resetForm
|
|
156
|
+
};
|
|
157
|
+
await onSubmit(state$.values.get(), helpers);
|
|
158
|
+
state$.status.set("success");
|
|
159
|
+
if (debug) {
|
|
160
|
+
console.log("[useForm] Submit successful");
|
|
161
|
+
}
|
|
162
|
+
} catch (error) {
|
|
163
|
+
state$.status.set("error");
|
|
164
|
+
if (debug) {
|
|
165
|
+
console.error("[useForm] Submit error:", error);
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
} finally {
|
|
169
|
+
state$.isSubmitting.set(false);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
[
|
|
173
|
+
state$,
|
|
174
|
+
validateForm,
|
|
175
|
+
onSubmit,
|
|
176
|
+
onError,
|
|
177
|
+
setFieldValue,
|
|
178
|
+
setFieldTouched,
|
|
179
|
+
resetForm,
|
|
180
|
+
debug
|
|
181
|
+
]
|
|
182
|
+
);
|
|
183
|
+
const getFieldProps = useCallback(
|
|
184
|
+
(field) => {
|
|
185
|
+
return {
|
|
186
|
+
name: String(field),
|
|
187
|
+
value: state$.values[field].get(),
|
|
188
|
+
onChange: (value) => setFieldValue(field, value),
|
|
189
|
+
onBlur: () => setFieldTouched(field, true)
|
|
190
|
+
};
|
|
191
|
+
},
|
|
192
|
+
[state$, setFieldValue, setFieldTouched]
|
|
193
|
+
);
|
|
194
|
+
const getFieldMeta = useCallback(
|
|
195
|
+
(field) => {
|
|
196
|
+
const fieldKey = String(field);
|
|
197
|
+
const metadata = fieldMetadataActions.get(fieldKey);
|
|
198
|
+
return {
|
|
199
|
+
error: state$.errors[field].get(),
|
|
200
|
+
touched: state$.touched[field].get() ?? false,
|
|
201
|
+
isDirty: state$.values[field].get() !== state$.initialValues[field].get(),
|
|
202
|
+
isValidating: validationInProgress.current.has(fieldKey),
|
|
203
|
+
// Additional metadata from @opensite/hooks
|
|
204
|
+
validationCount: metadata?.validationCount,
|
|
205
|
+
lastValidated: metadata?.lastValidated
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
[state$, fieldMetadataActions]
|
|
209
|
+
);
|
|
210
|
+
const values = useSelector(() => state$.values.get());
|
|
211
|
+
const errors = useSelector(() => state$.errors.get());
|
|
212
|
+
const touched = useSelector(() => state$.touched.get());
|
|
213
|
+
const isSubmitting = useSelector(() => state$.isSubmitting.get());
|
|
214
|
+
const status = useSelector(() => state$.status.get());
|
|
215
|
+
const isValid = useSelector(() => Object.keys(state$.errors.get()).length === 0);
|
|
216
|
+
const isDirty = useSelector(() => {
|
|
217
|
+
const currentValues = state$.values.get();
|
|
218
|
+
const initialValues2 = state$.initialValues.get();
|
|
219
|
+
return Object.keys(currentValues).some(
|
|
220
|
+
(key) => currentValues[key] !== initialValues2[key]
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
return {
|
|
224
|
+
// State
|
|
225
|
+
values,
|
|
226
|
+
errors,
|
|
227
|
+
touched,
|
|
228
|
+
isSubmitting,
|
|
229
|
+
isValid,
|
|
230
|
+
isDirty,
|
|
231
|
+
status,
|
|
232
|
+
// Actions
|
|
233
|
+
handleSubmit,
|
|
234
|
+
setValues: (values2) => {
|
|
235
|
+
if (typeof values2 === "function") {
|
|
236
|
+
state$.values.set(values2(state$.values.get()));
|
|
237
|
+
} else {
|
|
238
|
+
state$.values.set(values2);
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
setFieldValue,
|
|
242
|
+
setErrors: (errors2) => state$.errors.set(errors2),
|
|
243
|
+
setFieldError: (field, error) => state$.errors[field].set(error),
|
|
244
|
+
setTouched: (touched2) => state$.touched.set(touched2),
|
|
245
|
+
setFieldTouched,
|
|
246
|
+
validateForm,
|
|
247
|
+
validateField,
|
|
248
|
+
resetForm,
|
|
249
|
+
getFieldProps,
|
|
250
|
+
getFieldMeta
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
var FormContext = React5.createContext(null);
|
|
254
|
+
FormContext.displayName = "FormContext";
|
|
255
|
+
function useField(options) {
|
|
256
|
+
const { name, validate, transform } = options;
|
|
257
|
+
const form = useContext(FormContext);
|
|
258
|
+
if (!form) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
"useField must be used within a FormContext. Wrap your component with <Form> or use useForm's getFieldProps instead."
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
const baseFieldProps = form.getFieldProps(name);
|
|
264
|
+
const field = {
|
|
265
|
+
...baseFieldProps,
|
|
266
|
+
value: baseFieldProps.value,
|
|
267
|
+
onChange: (value) => {
|
|
268
|
+
const transformedValue = transform ? transform(value) : value;
|
|
269
|
+
baseFieldProps.onChange(transformedValue);
|
|
270
|
+
if (validate) {
|
|
271
|
+
const result = validate(transformedValue, form.values);
|
|
272
|
+
if (result instanceof Promise) {
|
|
273
|
+
result.then((error) => {
|
|
274
|
+
if (error !== void 0) {
|
|
275
|
+
form.setFieldError(name, error);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
} else if (result !== void 0) {
|
|
279
|
+
form.setFieldError(name, result);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
const meta = form.getFieldMeta(name);
|
|
285
|
+
const helpers = {
|
|
286
|
+
setValue: useCallback(
|
|
287
|
+
(value) => {
|
|
288
|
+
const transformedValue = transform ? transform(value) : value;
|
|
289
|
+
form.setFieldValue(name, transformedValue);
|
|
290
|
+
},
|
|
291
|
+
[name, transform, form]
|
|
292
|
+
),
|
|
293
|
+
setTouched: useCallback(
|
|
294
|
+
(touched) => {
|
|
295
|
+
form.setFieldTouched(name, touched);
|
|
296
|
+
},
|
|
297
|
+
[name, form]
|
|
298
|
+
),
|
|
299
|
+
setError: useCallback(
|
|
300
|
+
(error) => {
|
|
301
|
+
form.setFieldError(name, error);
|
|
302
|
+
},
|
|
303
|
+
[name, form]
|
|
304
|
+
)
|
|
305
|
+
};
|
|
306
|
+
return {
|
|
307
|
+
field,
|
|
308
|
+
meta,
|
|
309
|
+
helpers
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function renderMessage(message, fallbackClassName, className) {
|
|
313
|
+
if (typeof message === "string") {
|
|
314
|
+
return /* @__PURE__ */ React5.createElement(
|
|
315
|
+
"p",
|
|
316
|
+
{
|
|
317
|
+
className: cn(
|
|
318
|
+
"text-sm font-medium text-center text-balance",
|
|
319
|
+
className
|
|
320
|
+
)
|
|
321
|
+
},
|
|
322
|
+
message
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
return /* @__PURE__ */ React5.createElement("div", { className: cn(fallbackClassName, className) }, message);
|
|
326
|
+
}
|
|
327
|
+
function FormFeedback({
|
|
328
|
+
successMessage,
|
|
329
|
+
submissionError,
|
|
330
|
+
successMessageClassName,
|
|
331
|
+
errorMessageClassName
|
|
332
|
+
}) {
|
|
333
|
+
if (!successMessage && !submissionError) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
return /* @__PURE__ */ React5.createElement(React5.Fragment, null, successMessage ? /* @__PURE__ */ React5.createElement(
|
|
337
|
+
"div",
|
|
338
|
+
{
|
|
339
|
+
className: cn(
|
|
340
|
+
"rounded-md border border-primary bg-primary px-4 py-3 shadow-sm",
|
|
341
|
+
successMessageClassName
|
|
342
|
+
),
|
|
343
|
+
role: "status",
|
|
344
|
+
"aria-live": "polite"
|
|
345
|
+
},
|
|
346
|
+
renderMessage(
|
|
347
|
+
successMessage,
|
|
348
|
+
"text-primary-foreground",
|
|
349
|
+
"text-primary-foreground"
|
|
350
|
+
)
|
|
351
|
+
) : null, submissionError ? /* @__PURE__ */ React5.createElement(
|
|
352
|
+
"div",
|
|
353
|
+
{
|
|
354
|
+
className: cn(
|
|
355
|
+
"rounded-md border border-destructive bg-destructive px-4 py-3 shadow-sm",
|
|
356
|
+
errorMessageClassName
|
|
357
|
+
),
|
|
358
|
+
role: "alert",
|
|
359
|
+
"aria-live": "assertive"
|
|
360
|
+
},
|
|
361
|
+
renderMessage(
|
|
362
|
+
submissionError,
|
|
363
|
+
"text-destructive-foreground",
|
|
364
|
+
"text-destructive-foreground"
|
|
365
|
+
)
|
|
366
|
+
) : null);
|
|
367
|
+
}
|
|
368
|
+
FormFeedback.displayName = "FormFeedback";
|
|
369
|
+
var buttonGroupVariants = cva(
|
|
370
|
+
"flex w-fit items-stretch [&>*]:focus-visible:z-10 [&>*]:focus-visible:relative [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1 has-[select[aria-hidden=true]:last-child]:[&>[data-slot=select-trigger]:last-of-type]:rounded-r-md has-[>[data-slot=button-group]]:gap-2",
|
|
371
|
+
{
|
|
372
|
+
variants: {
|
|
373
|
+
orientation: {
|
|
374
|
+
horizontal: "[&>*:not(:first-child)]:rounded-l-none [&>*:not(:first-child)]:border-l-0 [&>*:not(:last-child)]:rounded-r-none",
|
|
375
|
+
vertical: "flex-col [&>*:not(:first-child)]:rounded-t-none [&>*:not(:first-child)]:border-t-0 [&>*:not(:last-child)]:rounded-b-none"
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
defaultVariants: {
|
|
379
|
+
orientation: "horizontal"
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
);
|
|
383
|
+
function ButtonGroup({
|
|
384
|
+
className,
|
|
385
|
+
orientation,
|
|
386
|
+
...props
|
|
387
|
+
}) {
|
|
388
|
+
return /* @__PURE__ */ React5.createElement(
|
|
389
|
+
"div",
|
|
390
|
+
{
|
|
391
|
+
role: "group",
|
|
392
|
+
"data-slot": "button-group",
|
|
393
|
+
"data-orientation": orientation,
|
|
394
|
+
className: cn(buttonGroupVariants({ orientation }), className),
|
|
395
|
+
...props
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
var FieldFeedback = ({
|
|
400
|
+
errorId,
|
|
401
|
+
errorClassName,
|
|
402
|
+
error,
|
|
403
|
+
shouldRenderError
|
|
404
|
+
}) => {
|
|
405
|
+
const errorText = Array.isArray(error) ? error.join(", ") : error;
|
|
406
|
+
if (!errorText || !shouldRenderError) return null;
|
|
407
|
+
return /* @__PURE__ */ React5.createElement(FieldError, { id: errorId, className: errorClassName }, errorText);
|
|
408
|
+
};
|
|
409
|
+
var DEFAULT_ICON_API_KEY = "au382bi7fsh96w9h9xlrnat2jglx";
|
|
410
|
+
var INPUT_SIZE_CLASSES = {
|
|
411
|
+
xs: "h-6 text-xs px-3",
|
|
412
|
+
// button: h-6 → match
|
|
413
|
+
sm: "text-sm px-3",
|
|
414
|
+
// button: h-8 overridden to h-9 below → match
|
|
415
|
+
default: "text-base px-4",
|
|
416
|
+
// button: h-9 (no override needed)
|
|
417
|
+
lg: "h-10 text-md px-6"
|
|
418
|
+
// button: h-10 → match
|
|
419
|
+
};
|
|
420
|
+
function ButtonGroupForm({
|
|
421
|
+
name,
|
|
422
|
+
label,
|
|
423
|
+
description,
|
|
424
|
+
inputProps,
|
|
425
|
+
submitLabel = "Submit",
|
|
426
|
+
submitVariant = "default",
|
|
427
|
+
size = "default",
|
|
428
|
+
isSubmitting = false,
|
|
429
|
+
submitIconName,
|
|
430
|
+
submitIconComponent,
|
|
431
|
+
errorText,
|
|
432
|
+
className,
|
|
433
|
+
labelClassName
|
|
434
|
+
}) {
|
|
435
|
+
const inputId = React5.useMemo(() => {
|
|
436
|
+
return `button-group-input-${name}`;
|
|
437
|
+
}, [name]);
|
|
438
|
+
const errorId = `${inputId}-error`;
|
|
439
|
+
const hasValue = React5.useMemo(() => {
|
|
440
|
+
return String(inputProps.value ?? "").trim().length > 0;
|
|
441
|
+
}, [inputProps.value]);
|
|
442
|
+
const hasError = React5.useMemo(() => {
|
|
443
|
+
return !!inputProps.error;
|
|
444
|
+
}, [inputProps.error]);
|
|
445
|
+
const buttonSize = React5.useMemo(() => {
|
|
446
|
+
if (submitIconName || submitIconComponent) {
|
|
447
|
+
return size === "default" || size === "sm" ? "icon" : `icon-${size}`;
|
|
448
|
+
}
|
|
449
|
+
return size;
|
|
450
|
+
}, [submitIconName, size, submitIconComponent]);
|
|
451
|
+
const labelElement = React5.useMemo(() => {
|
|
452
|
+
if (submitIconName) {
|
|
453
|
+
return /* @__PURE__ */ React5.createElement(Icon, { name: submitIconName, apiKey: DEFAULT_ICON_API_KEY });
|
|
454
|
+
} else if (submitIconComponent) {
|
|
455
|
+
return submitIconComponent;
|
|
456
|
+
} else if (submitLabel) {
|
|
457
|
+
return submitLabel;
|
|
458
|
+
} else {
|
|
459
|
+
return "Submit";
|
|
460
|
+
}
|
|
461
|
+
}, [submitIconComponent, submitIconName, submitLabel]);
|
|
462
|
+
return /* @__PURE__ */ React5.createElement("div", { className: cn("space-y-2", className) }, label && /* @__PURE__ */ React5.createElement(FieldLabel, { htmlFor: inputId, className: labelClassName }, label), /* @__PURE__ */ React5.createElement(
|
|
463
|
+
ButtonGroup,
|
|
464
|
+
{
|
|
465
|
+
className: cn(
|
|
466
|
+
"rounded-md",
|
|
467
|
+
!hasError && hasValue && "ring-2 ring-ring",
|
|
468
|
+
hasError && "ring-1 ring-destructive"
|
|
469
|
+
)
|
|
470
|
+
},
|
|
471
|
+
/* @__PURE__ */ React5.createElement(
|
|
472
|
+
TextInput,
|
|
473
|
+
{
|
|
474
|
+
...inputProps,
|
|
475
|
+
id: inputId,
|
|
476
|
+
suppressValueRing: true,
|
|
477
|
+
"aria-describedby": hasError ? errorId : void 0,
|
|
478
|
+
className: cn(
|
|
479
|
+
INPUT_SIZE_CLASSES[size],
|
|
480
|
+
"border-r-0 rounded-r-none focus-visible:z-10",
|
|
481
|
+
inputProps.className
|
|
482
|
+
)
|
|
483
|
+
}
|
|
484
|
+
),
|
|
485
|
+
/* @__PURE__ */ React5.createElement(
|
|
486
|
+
Button,
|
|
487
|
+
{
|
|
488
|
+
size: buttonSize,
|
|
489
|
+
type: "submit",
|
|
490
|
+
variant: submitVariant,
|
|
491
|
+
disabled: isSubmitting,
|
|
492
|
+
className: cn(
|
|
493
|
+
"relative rounded-l-none ring-0",
|
|
494
|
+
// 'sm' button variant is h-8; override to h-9 to align with input
|
|
495
|
+
size === "sm" && "h-9"
|
|
496
|
+
)
|
|
497
|
+
},
|
|
498
|
+
isSubmitting ? /* @__PURE__ */ React5.createElement("span", { className: "absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2" }, /* @__PURE__ */ React5.createElement(
|
|
499
|
+
Icon,
|
|
500
|
+
{
|
|
501
|
+
name: "line-md/loading-twotone-loop",
|
|
502
|
+
apiKey: DEFAULT_ICON_API_KEY
|
|
503
|
+
}
|
|
504
|
+
)) : null,
|
|
505
|
+
/* @__PURE__ */ React5.createElement(
|
|
506
|
+
"span",
|
|
507
|
+
{
|
|
508
|
+
className: cn(
|
|
509
|
+
"transition-opacity duration-200",
|
|
510
|
+
isSubmitting ? "opacity-0" : "opacity-100"
|
|
511
|
+
)
|
|
512
|
+
},
|
|
513
|
+
labelElement
|
|
514
|
+
)
|
|
515
|
+
)
|
|
516
|
+
), description && /* @__PURE__ */ React5.createElement(FieldDescription, null, description), /* @__PURE__ */ React5.createElement(
|
|
517
|
+
FieldFeedback,
|
|
518
|
+
{
|
|
519
|
+
error: errorText,
|
|
520
|
+
shouldRenderError: hasError,
|
|
521
|
+
errorId
|
|
522
|
+
}
|
|
523
|
+
));
|
|
524
|
+
}
|
|
525
|
+
ButtonGroupForm.displayName = "ButtonGroupForm";
|
|
526
|
+
function Form({
|
|
527
|
+
form,
|
|
528
|
+
children,
|
|
529
|
+
fields,
|
|
530
|
+
className,
|
|
531
|
+
action,
|
|
532
|
+
method,
|
|
533
|
+
noValidate = true,
|
|
534
|
+
submissionConfig,
|
|
535
|
+
successMessage,
|
|
536
|
+
submissionError,
|
|
537
|
+
successMessageClassName,
|
|
538
|
+
errorMessageClassName,
|
|
539
|
+
onNewSubmission,
|
|
540
|
+
notificationConfig,
|
|
541
|
+
styleConfig,
|
|
542
|
+
formConfig,
|
|
543
|
+
...props
|
|
544
|
+
}) {
|
|
545
|
+
const handleFormSubmit = React5.useCallback(
|
|
546
|
+
async (e) => {
|
|
547
|
+
try {
|
|
548
|
+
await form.handleSubmit(e);
|
|
549
|
+
} catch {
|
|
550
|
+
}
|
|
551
|
+
},
|
|
552
|
+
[form]
|
|
553
|
+
);
|
|
554
|
+
const resolvedClassName = className ?? styleConfig?.formClassName;
|
|
555
|
+
const resolvedAction = action ?? formConfig?.endpoint;
|
|
556
|
+
const resolvedMethod = method ?? formConfig?.method ?? "post";
|
|
557
|
+
const resolvedSubmissionConfig = submissionConfig ?? formConfig?.submissionConfig;
|
|
558
|
+
const resolvedSuccessMessage = successMessage ?? notificationConfig?.successMessage;
|
|
559
|
+
const resolvedSubmissionError = submissionError ?? notificationConfig?.submissionError;
|
|
560
|
+
const resolvedSuccessMessageClassName = successMessageClassName ?? styleConfig?.successMessageClassName;
|
|
561
|
+
const resolvedErrorMessageClassName = errorMessageClassName ?? styleConfig?.errorMessageClassName;
|
|
562
|
+
const behavior = resolvedSubmissionConfig?.behavior || "showConfirmation";
|
|
563
|
+
const shouldManageSubmissionUi = resolvedSubmissionConfig !== void 0 || resolvedSuccessMessage !== void 0 || resolvedSuccessMessageClassName !== void 0 || resolvedErrorMessageClassName !== void 0 || resolvedSubmissionError != null || onNewSubmission !== void 0;
|
|
564
|
+
const hasSubmissionError = Boolean(resolvedSubmissionError);
|
|
565
|
+
const isSubmissionSuccessful = shouldManageSubmissionUi && form.status === "success" && !hasSubmissionError;
|
|
566
|
+
const defaultSuccessMessage = behavior === "redirect" ? "Form submitted successfully. Redirecting..." : "Thank you. Your form has been submitted successfully.";
|
|
567
|
+
const finalSuccessMessage = resolvedSuccessMessage ?? defaultSuccessMessage;
|
|
568
|
+
const shouldRenderCustomComponent = isSubmissionSuccessful && behavior === "renderCustomComponent" && Boolean(resolvedSubmissionConfig?.customComponent);
|
|
569
|
+
const newSubmissionAction = resolvedSubmissionConfig?.newFormSubmissionAction;
|
|
570
|
+
const showNewSubmissionAction = isSubmissionSuccessful && (typeof newSubmissionAction?.enable === "boolean" ? newSubmissionAction.enable : Boolean(newSubmissionAction?.label));
|
|
571
|
+
const newSubmissionLabel = newSubmissionAction?.label ?? "Submit another response";
|
|
572
|
+
const handleNewSubmission = React5.useCallback(() => {
|
|
573
|
+
form.resetForm();
|
|
574
|
+
onNewSubmission?.();
|
|
575
|
+
}, [form, onNewSubmission]);
|
|
576
|
+
const formLayout = formConfig?.formLayout ?? "standard";
|
|
577
|
+
const isButtonGroupLayout = formLayout === "button-group";
|
|
578
|
+
const hasTextField = fields && fields.length === 1 && fields[0] && ["text", "email", "password", "url", "tel", "search"].includes(
|
|
579
|
+
fields[0].type
|
|
580
|
+
);
|
|
581
|
+
const shouldUseButtonGroup = isButtonGroupLayout && hasTextField;
|
|
582
|
+
const buttonGroupContent = React5.useMemo(() => {
|
|
583
|
+
if (!shouldUseButtonGroup || !fields || fields.length === 0) return null;
|
|
584
|
+
const field = fields[0];
|
|
585
|
+
const fieldProps = form.getFieldProps(field.name);
|
|
586
|
+
const fieldMeta = form.getFieldMeta(field.name);
|
|
587
|
+
return /* @__PURE__ */ React5.createElement(
|
|
588
|
+
ButtonGroupForm,
|
|
589
|
+
{
|
|
590
|
+
name: field.name,
|
|
591
|
+
label: field.label,
|
|
592
|
+
description: field.description,
|
|
593
|
+
className: field.className,
|
|
594
|
+
inputProps: {
|
|
595
|
+
name: fieldProps.name,
|
|
596
|
+
value: fieldProps.value,
|
|
597
|
+
onChange: fieldProps.onChange,
|
|
598
|
+
onBlur: fieldProps.onBlur,
|
|
599
|
+
type: field.type,
|
|
600
|
+
placeholder: field.placeholder,
|
|
601
|
+
required: field.required,
|
|
602
|
+
disabled: field.disabled,
|
|
603
|
+
error: fieldMeta.touched && !!fieldMeta.error
|
|
604
|
+
},
|
|
605
|
+
errorText: fieldMeta.touched ? fieldMeta.error : void 0,
|
|
606
|
+
submitLabel: formConfig?.submitLabel,
|
|
607
|
+
submitVariant: formConfig?.submitVariant,
|
|
608
|
+
submitIconName: formConfig?.submitIconName,
|
|
609
|
+
submitIconComponent: formConfig?.submitIconComponent,
|
|
610
|
+
size: formConfig?.buttonGroupSize,
|
|
611
|
+
isSubmitting: form.isSubmitting
|
|
612
|
+
}
|
|
613
|
+
);
|
|
614
|
+
}, [shouldUseButtonGroup, fields, form, formConfig]);
|
|
615
|
+
return /* @__PURE__ */ React5.createElement(FormContext.Provider, { value: form }, /* @__PURE__ */ React5.createElement(
|
|
616
|
+
"form",
|
|
617
|
+
{
|
|
618
|
+
onSubmit: handleFormSubmit,
|
|
619
|
+
action: resolvedAction,
|
|
620
|
+
method: resolvedMethod,
|
|
621
|
+
noValidate,
|
|
622
|
+
className: resolvedClassName,
|
|
623
|
+
...props
|
|
624
|
+
},
|
|
625
|
+
isSubmissionSuccessful ? /* @__PURE__ */ React5.createElement("div", { className: "space-y-4" }, shouldRenderCustomComponent ? resolvedSubmissionConfig?.customComponent : /* @__PURE__ */ React5.createElement(
|
|
626
|
+
FormFeedback,
|
|
627
|
+
{
|
|
628
|
+
successMessage: finalSuccessMessage,
|
|
629
|
+
successMessageClassName: resolvedSuccessMessageClassName
|
|
630
|
+
}
|
|
631
|
+
), showNewSubmissionAction ? /* @__PURE__ */ React5.createElement(
|
|
632
|
+
Button,
|
|
633
|
+
{
|
|
634
|
+
type: "button",
|
|
635
|
+
variant: "outline",
|
|
636
|
+
onClick: handleNewSubmission
|
|
637
|
+
},
|
|
638
|
+
newSubmissionLabel
|
|
639
|
+
) : null) : /* @__PURE__ */ React5.createElement(React5.Fragment, null, shouldUseButtonGroup ? buttonGroupContent : children, resolvedSubmissionError ? /* @__PURE__ */ React5.createElement("div", { className: "mt-4" }, /* @__PURE__ */ React5.createElement(
|
|
640
|
+
FormFeedback,
|
|
641
|
+
{
|
|
642
|
+
submissionError: resolvedSubmissionError,
|
|
643
|
+
errorMessageClassName: resolvedErrorMessageClassName
|
|
644
|
+
}
|
|
645
|
+
)) : null)
|
|
646
|
+
));
|
|
647
|
+
}
|
|
648
|
+
Form.displayName = "Form";
|
|
649
|
+
function Field2({
|
|
650
|
+
name,
|
|
651
|
+
label,
|
|
652
|
+
description,
|
|
653
|
+
children,
|
|
654
|
+
showError = true,
|
|
655
|
+
className,
|
|
656
|
+
errorClassName,
|
|
657
|
+
required = false,
|
|
658
|
+
validate
|
|
659
|
+
}) {
|
|
660
|
+
const fieldState = useField({ name, validate });
|
|
661
|
+
const { meta } = fieldState;
|
|
662
|
+
const hasError = React5.useMemo(() => {
|
|
663
|
+
return showError && meta.touched && meta.error ? true : false;
|
|
664
|
+
}, [meta?.touched, meta?.error, showError]);
|
|
665
|
+
const errorId = `${name}-error`;
|
|
666
|
+
const descriptionId = `${name}-description`;
|
|
667
|
+
return /* @__PURE__ */ React5.createElement(
|
|
668
|
+
Field,
|
|
669
|
+
{
|
|
670
|
+
className,
|
|
671
|
+
"data-field": name,
|
|
672
|
+
invalid: hasError
|
|
673
|
+
},
|
|
674
|
+
/* @__PURE__ */ React5.createElement(
|
|
675
|
+
LabelGroup,
|
|
676
|
+
{
|
|
677
|
+
labelHtmlFor: name,
|
|
678
|
+
required,
|
|
679
|
+
variant: "label",
|
|
680
|
+
secondaryId: descriptionId,
|
|
681
|
+
secondary: description,
|
|
682
|
+
primary: label
|
|
683
|
+
}
|
|
684
|
+
),
|
|
685
|
+
/* @__PURE__ */ React5.createElement("div", { "data-slot": "field-control" }, typeof children === "function" ? children(fieldState) : children),
|
|
686
|
+
/* @__PURE__ */ React5.createElement(
|
|
687
|
+
FieldFeedback,
|
|
688
|
+
{
|
|
689
|
+
errorId,
|
|
690
|
+
errorClassName,
|
|
691
|
+
shouldRenderError: hasError,
|
|
692
|
+
error: meta.error
|
|
693
|
+
}
|
|
694
|
+
)
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
Field2.displayName = "Field";
|
|
698
|
+
|
|
699
|
+
export { ButtonGroupForm, Field2 as Field, Form, FormContext, FormFeedback, useField, useForm };
|
|
700
|
+
//# sourceMappingURL=chunk-A7MVZ757.js.map
|
|
701
|
+
//# sourceMappingURL=chunk-A7MVZ757.js.map
|