@schandlergarcia/sf-web-components 1.7.0 → 1.8.0
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/components/library/cards/ActionList.d.ts +10 -10
- package/dist/components/library/cards/ActionList.js +2 -3
- package/dist/components/library/cards/ActionList.js.map +1 -1
- package/dist/components/library/cards/ActivityCard.d.ts +18 -5
- package/dist/components/library/cards/ActivityCard.js +3 -4
- package/dist/components/library/cards/ActivityCard.js.map +1 -1
- package/dist/components/library/cards/BaseCard.d.ts +30 -24
- package/dist/components/library/cards/BaseCard.js +2 -3
- package/dist/components/library/cards/BaseCard.js.map +1 -1
- package/dist/components/library/cards/CalloutCard.d.ts +11 -9
- package/dist/components/library/cards/CalloutCard.js +2 -3
- package/dist/components/library/cards/CalloutCard.js.map +1 -1
- package/dist/components/library/cards/ChartCard.d.ts +29 -17
- package/dist/components/library/cards/ChartCard.js +13 -14
- package/dist/components/library/cards/ChartCard.js.map +1 -1
- package/dist/components/library/cards/FeedPanel.d.ts +12 -11
- package/dist/components/library/cards/FeedPanel.js +3 -4
- package/dist/components/library/cards/FeedPanel.js.map +1 -1
- package/dist/components/library/cards/ListCard.d.ts +33 -20
- package/dist/components/library/cards/ListCard.js +35 -35
- package/dist/components/library/cards/ListCard.js.map +1 -1
- package/dist/components/library/cards/MetricCard.d.ts +23 -17
- package/dist/components/library/cards/MetricCard.js +10 -11
- package/dist/components/library/cards/MetricCard.js.map +1 -1
- package/dist/components/library/cards/MetricsStrip.d.ts +11 -11
- package/dist/components/library/cards/MetricsStrip.js +1 -1
- package/dist/components/library/cards/MetricsStrip.js.map +1 -1
- package/dist/components/library/cards/SectionCard.d.ts +17 -12
- package/dist/components/library/cards/SectionCard.js +18 -19
- package/dist/components/library/cards/SectionCard.js.map +1 -1
- package/dist/components/library/cards/SemanticMetricCard.d.ts +15 -20
- package/dist/components/library/cards/SemanticMetricCardWithLoading.d.ts +8 -7
- package/dist/components/library/cards/SemanticTableCard.d.ts +13 -18
- package/dist/components/library/cards/SemanticTableCardWithLoading.d.ts +8 -7
- package/dist/components/library/cards/StatusCard.d.ts +29 -15
- package/dist/components/library/cards/StatusCard.js +16 -17
- package/dist/components/library/cards/StatusCard.js.map +1 -1
- package/dist/components/library/cards/TableCard.d.ts +40 -23
- package/dist/components/library/cards/TableCard.js +59 -59
- package/dist/components/library/cards/TableCard.js.map +1 -1
- package/dist/components/library/cards/WidgetCard.d.ts +19 -11
- package/dist/components/library/cards/WidgetCard.js.map +1 -1
- package/dist/components/library/charts/D3Chart.d.ts +23 -16
- package/dist/components/library/charts/D3Chart.js.map +1 -1
- package/dist/components/library/charts/D3ChartTemplates.d.ts +33 -3
- package/dist/components/library/charts/D3ChartTemplates.js +7 -7
- package/dist/components/library/charts/D3ChartTemplates.js.map +1 -1
- package/dist/components/library/charts/GeoMap.d.ts +81 -18
- package/dist/components/library/charts/GeoMap.js +28 -26
- package/dist/components/library/charts/GeoMap.js.map +1 -1
- package/dist/components/library/filters/FilterBar.d.ts +18 -8
- package/dist/components/library/filters/FilterBar.js +2 -3
- package/dist/components/library/filters/FilterBar.js.map +1 -1
- package/dist/components/library/filters/SearchFilter.d.ts +7 -6
- package/dist/components/library/filters/SearchFilter.js +2 -3
- package/dist/components/library/filters/SearchFilter.js.map +1 -1
- package/dist/components/library/filters/SelectFilter.d.ts +13 -7
- package/dist/components/library/filters/SelectFilter.js +2 -3
- package/dist/components/library/filters/SelectFilter.js.map +1 -1
- package/dist/components/library/filters/ToggleFilter.d.ts +7 -5
- package/dist/components/library/filters/ToggleFilter.js +2 -3
- package/dist/components/library/filters/ToggleFilter.js.map +1 -1
- package/dist/components/library/forms/FormField.d.ts +10 -8
- package/dist/components/library/forms/FormField.js +3 -4
- package/dist/components/library/forms/FormField.js.map +1 -1
- package/dist/components/library/forms/FormModal.d.ts +23 -14
- package/dist/components/library/forms/FormModal.js.map +1 -1
- package/dist/components/library/forms/FormRenderer.d.ts +29 -9
- package/dist/components/library/forms/FormRenderer.js +6 -7
- package/dist/components/library/forms/FormRenderer.js.map +1 -1
- package/dist/components/library/forms/FormSection.d.ts +10 -8
- package/dist/components/library/forms/FormSection.js +2 -3
- package/dist/components/library/forms/FormSection.js.map +1 -1
- package/dist/components/library/forms/index.d.ts +5 -0
- package/dist/components/library/forms/useFormState.d.ts +23 -15
- package/dist/components/library/forms/useFormState.js +53 -47
- package/dist/components/library/forms/useFormState.js.map +1 -1
- package/dist/components/library/layout/PageContainer.d.ts +6 -4
- package/dist/components/library/layout/PageContainer.js +4 -5
- package/dist/components/library/layout/PageContainer.js.map +1 -1
- package/package.json +4 -1
- package/src/components/library/cards/{ActionList.jsx → ActionList.tsx} +13 -9
- package/src/components/library/cards/{ActivityCard.jsx → ActivityCard.tsx} +33 -4
- package/src/components/library/cards/{BaseCard.jsx → BaseCard.tsx} +33 -6
- package/src/components/library/cards/{CalloutCard.jsx → CalloutCard.tsx} +12 -10
- package/src/components/library/cards/{ChartCard.jsx → ChartCard.tsx} +32 -6
- package/src/components/library/cards/{FeedPanel.jsx → FeedPanel.tsx} +13 -2
- package/src/components/library/cards/{ListCard.jsx → ListCard.tsx} +43 -7
- package/src/components/library/cards/{MetricCard.jsx → MetricCard.tsx} +25 -6
- package/src/components/library/cards/{MetricsStrip.jsx → MetricsStrip.tsx} +22 -12
- package/src/components/library/cards/{SectionCard.jsx → SectionCard.tsx} +27 -8
- package/src/components/library/cards/{SemanticMetricCard.jsx → SemanticMetricCard.tsx} +17 -5
- package/src/components/library/cards/{SemanticMetricCardWithLoading.jsx → SemanticMetricCardWithLoading.tsx} +9 -3
- package/src/components/library/cards/{SemanticTableCard.jsx → SemanticTableCard.tsx} +14 -3
- package/src/components/library/cards/{SemanticTableCardWithLoading.jsx → SemanticTableCardWithLoading.tsx} +9 -5
- package/src/components/library/cards/{StatusCard.jsx → StatusCard.tsx} +61 -12
- package/src/components/library/cards/{TableCard.jsx → TableCard.tsx} +51 -12
- package/src/components/library/cards/{WidgetCard.jsx → WidgetCard.tsx} +28 -5
- package/src/components/library/charts/{D3Chart.jsx → D3Chart.tsx} +27 -7
- package/src/components/library/charts/{D3ChartTemplates.jsx → D3ChartTemplates.tsx} +60 -28
- package/src/components/library/charts/{GeoMap.jsx → GeoMap.tsx} +106 -17
- package/src/components/library/filters/{FilterBar.jsx → FilterBar.tsx} +21 -11
- package/src/components/library/filters/{SearchFilter.jsx → SearchFilter.tsx} +8 -2
- package/src/components/library/filters/{SelectFilter.jsx → SelectFilter.tsx} +15 -8
- package/src/components/library/filters/{ToggleFilter.jsx → ToggleFilter.tsx} +7 -6
- package/src/components/library/forms/{FormField.jsx → FormField.tsx} +91 -45
- package/src/components/library/forms/{FormModal.jsx → FormModal.tsx} +21 -20
- package/src/components/library/forms/{FormRenderer.jsx → FormRenderer.tsx} +32 -10
- package/src/components/library/forms/{FormSection.jsx → FormSection.tsx} +13 -7
- package/src/components/library/forms/index.tsx +11 -0
- package/src/components/library/forms/{useFormState.jsx → useFormState.tsx} +43 -23
- package/src/components/library/layout/{PageContainer.jsx → PageContainer.tsx} +6 -3
- package/src/components/library/forms/index.jsx +0 -5
- /package/src/components/library/filters/{index.jsx → index.ts} +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useEffect, useCallback } from "react";
|
|
2
2
|
import { createPortal } from "react-dom";
|
|
3
3
|
import { motion, AnimatePresence } from "framer-motion";
|
|
4
4
|
import { XMarkIcon } from "@heroicons/react/24/outline";
|
|
5
5
|
import Spinner from "../ui/Spinner";
|
|
6
6
|
import FormRenderer from "./FormRenderer";
|
|
7
7
|
import useFormState from "./useFormState";
|
|
8
|
+
import type { FormSection } from "./FormRenderer";
|
|
8
9
|
|
|
9
10
|
const OVERLAY_VARIANTS = {
|
|
10
11
|
hidden: { opacity: 0 },
|
|
@@ -13,35 +14,35 @@ const OVERLAY_VARIANTS = {
|
|
|
13
14
|
|
|
14
15
|
const PANEL_VARIANTS = {
|
|
15
16
|
hidden: { opacity: 0, y: 24, scale: 0.97 },
|
|
16
|
-
visible: { opacity: 1, y: 0, scale: 1, transition: { type: "spring", damping: 25, stiffness: 350 } },
|
|
17
|
+
visible: { opacity: 1, y: 0, scale: 1, transition: { type: "spring" as const, damping: 25, stiffness: 350 } },
|
|
17
18
|
exit: { opacity: 0, y: 16, scale: 0.97, transition: { duration: 0.15 } },
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
/**
|
|
21
|
-
* Size → max-width mapping.
|
|
22
|
-
*/
|
|
23
21
|
const SIZE_CLASSES = {
|
|
24
22
|
sm: "max-w-md",
|
|
25
23
|
md: "max-w-xl",
|
|
26
24
|
lg: "max-w-2xl",
|
|
27
25
|
xl: "max-w-4xl",
|
|
28
|
-
};
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
export interface FormModalProps {
|
|
29
|
+
isOpen?: boolean;
|
|
30
|
+
onClose?: () => void;
|
|
31
|
+
title: string;
|
|
32
|
+
subtitle?: string;
|
|
33
|
+
sections?: FormSection[];
|
|
34
|
+
initialValues?: Record<string, unknown>;
|
|
35
|
+
onSubmit?: (values: Record<string, unknown>) => void | Promise<void>;
|
|
36
|
+
submitLabel?: string;
|
|
37
|
+
cancelLabel?: string;
|
|
38
|
+
size?: keyof typeof SIZE_CLASSES;
|
|
39
|
+
destructive?: boolean;
|
|
40
|
+
minSubmitMs?: number;
|
|
41
|
+
}
|
|
29
42
|
|
|
30
43
|
/**
|
|
31
44
|
* Modal dialog for creating or editing records.
|
|
32
45
|
* Wraps FormRenderer + useFormState in an animated overlay.
|
|
33
|
-
*
|
|
34
|
-
* @param {boolean} isOpen — whether the modal is visible
|
|
35
|
-
* @param {Function} onClose — close handler
|
|
36
|
-
* @param {string} title — modal title (e.g. "Edit Service", "New Incident")
|
|
37
|
-
* @param {string} subtitle — optional subtitle
|
|
38
|
-
* @param {Array} sections — form schema sections
|
|
39
|
-
* @param {Object} initialValues — prefill for editing (empty = create mode)
|
|
40
|
-
* @param {Function} onSubmit — async (values) => void, called on valid submit
|
|
41
|
-
* @param {string} submitLabel — submit button text (default: "Save")
|
|
42
|
-
* @param {string} cancelLabel — cancel button text (default: "Cancel")
|
|
43
|
-
* @param {string} size — "sm" | "md" | "lg" | "xl" (default: "lg")
|
|
44
|
-
* @param {boolean} destructive — if true, submit button is red (for delete confirmations)
|
|
45
46
|
*/
|
|
46
47
|
export default function FormModal({
|
|
47
48
|
isOpen = false,
|
|
@@ -56,7 +57,7 @@ export default function FormModal({
|
|
|
56
57
|
size = "lg",
|
|
57
58
|
destructive = false,
|
|
58
59
|
minSubmitMs,
|
|
59
|
-
}) {
|
|
60
|
+
}: FormModalProps) {
|
|
60
61
|
const form = useFormState({
|
|
61
62
|
initialValues,
|
|
62
63
|
sections,
|
|
@@ -73,7 +74,7 @@ export default function FormModal({
|
|
|
73
74
|
}, [isOpen]);
|
|
74
75
|
|
|
75
76
|
const onKeyDown = useCallback(
|
|
76
|
-
(e) => {
|
|
77
|
+
(e: KeyboardEvent) => {
|
|
77
78
|
if (e.key === "Escape") onClose?.();
|
|
78
79
|
},
|
|
79
80
|
[onClose]
|
|
@@ -1,17 +1,39 @@
|
|
|
1
|
-
import React from "react";
|
|
2
1
|
import FormSection from "./FormSection";
|
|
3
2
|
|
|
3
|
+
export interface FormSection {
|
|
4
|
+
id?: string;
|
|
5
|
+
title?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
fields: FormField[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface FormField {
|
|
11
|
+
id: string;
|
|
12
|
+
type: string;
|
|
13
|
+
label?: string;
|
|
14
|
+
placeholder?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
requiredMessage?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
readOnly?: boolean;
|
|
20
|
+
validate?: (value: unknown, values: Record<string, unknown>) => string | undefined;
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FormRendererProps {
|
|
25
|
+
sections?: FormSection[];
|
|
26
|
+
values?: Record<string, unknown>;
|
|
27
|
+
errors?: Record<string, string>;
|
|
28
|
+
touched?: Record<string, boolean>;
|
|
29
|
+
onFieldChange: (fieldId: string, value: unknown) => void;
|
|
30
|
+
onFieldBlur: (fieldId: string) => void;
|
|
31
|
+
formError?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
4
34
|
/**
|
|
5
35
|
* Renders a complete form from a schema definition.
|
|
6
36
|
* Pairs with useFormState for state management.
|
|
7
|
-
*
|
|
8
|
-
* @param {Array} sections — form schema sections
|
|
9
|
-
* @param {Object} values — current form values
|
|
10
|
-
* @param {Object} errors — current validation errors
|
|
11
|
-
* @param {Object} touched — which fields have been touched
|
|
12
|
-
* @param {Function} onFieldChange — (fieldId, value) => void
|
|
13
|
-
* @param {Function} onFieldBlur — (fieldId) => void
|
|
14
|
-
* @param {string} formError — top-level form error (e.g. submission failure)
|
|
15
37
|
*/
|
|
16
38
|
export default function FormRenderer({
|
|
17
39
|
sections = [],
|
|
@@ -21,7 +43,7 @@ export default function FormRenderer({
|
|
|
21
43
|
onFieldChange,
|
|
22
44
|
onFieldBlur,
|
|
23
45
|
formError,
|
|
24
|
-
}) {
|
|
46
|
+
}: FormRendererProps) {
|
|
25
47
|
return (
|
|
26
48
|
<div className="space-y-8">
|
|
27
49
|
{formError ? (
|
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import React from "react";
|
|
2
1
|
import FormField from "./FormField";
|
|
2
|
+
import type { FormSection as FormSectionType, FormField as FormFieldType } from "./FormRenderer";
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Fields that naturally span full width by default.
|
|
6
|
-
*/
|
|
7
4
|
const FULL_WIDTH_TYPES = new Set([
|
|
8
5
|
"textarea",
|
|
9
6
|
"radio",
|
|
10
7
|
"checkboxGroup",
|
|
11
8
|
]);
|
|
12
9
|
|
|
10
|
+
export interface FormSectionProps {
|
|
11
|
+
section: FormSectionType;
|
|
12
|
+
values: Record<string, unknown>;
|
|
13
|
+
errors: Record<string, string>;
|
|
14
|
+
touched: Record<string, boolean>;
|
|
15
|
+
onFieldChange: (fieldId: string, value: unknown) => void;
|
|
16
|
+
onFieldBlur: (fieldId: string) => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* A titled section of a form with a 2-column grid layout.
|
|
15
21
|
* Fields default to 1-column (half width) unless the type is naturally
|
|
@@ -22,7 +28,7 @@ export default function FormSection({
|
|
|
22
28
|
touched,
|
|
23
29
|
onFieldChange,
|
|
24
30
|
onFieldBlur,
|
|
25
|
-
}) {
|
|
31
|
+
}: FormSectionProps) {
|
|
26
32
|
const fields = section.fields ?? [];
|
|
27
33
|
if (!fields.length) return null;
|
|
28
34
|
|
|
@@ -42,7 +48,7 @@ export default function FormSection({
|
|
|
42
48
|
) : null}
|
|
43
49
|
|
|
44
50
|
<div className="grid grid-cols-1 gap-x-4 gap-y-5 sm:grid-cols-2">
|
|
45
|
-
{fields.map((field) => {
|
|
51
|
+
{fields.map((field: FormFieldType & { colSpan?: number }) => {
|
|
46
52
|
const span =
|
|
47
53
|
field.colSpan ??
|
|
48
54
|
(FULL_WIDTH_TYPES.has(field.type) ? 2 : 1);
|
|
@@ -57,7 +63,7 @@ export default function FormSection({
|
|
|
57
63
|
value={values[field.id]}
|
|
58
64
|
error={errors[field.id]}
|
|
59
65
|
touched={touched[field.id]}
|
|
60
|
-
onChange={(val) => onFieldChange(field.id, val)}
|
|
66
|
+
onChange={(val: unknown) => onFieldChange(field.id, val)}
|
|
61
67
|
onBlur={() => onFieldBlur(field.id)}
|
|
62
68
|
/>
|
|
63
69
|
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as FormModal } from "./FormModal";
|
|
2
|
+
export { default as FormRenderer } from "./FormRenderer";
|
|
3
|
+
export { default as FormSection } from "./FormSection";
|
|
4
|
+
export { default as FormField } from "./FormField";
|
|
5
|
+
export { default as useFormState } from "./useFormState";
|
|
6
|
+
|
|
7
|
+
export type { FormModalProps } from "./FormModal";
|
|
8
|
+
export type { FormRendererProps } from "./FormRenderer";
|
|
9
|
+
export type { FormSection as FormSectionType, FormField as FormFieldType } from "./FormRenderer";
|
|
10
|
+
export type { FormSectionProps } from "./FormSection";
|
|
11
|
+
export type { FormFieldProps } from "./FormField";
|
|
@@ -1,12 +1,35 @@
|
|
|
1
1
|
import { useState, useMemo, useCallback, useRef } from "react";
|
|
2
|
+
import type { FormSection } from "./FormRenderer";
|
|
2
3
|
|
|
3
4
|
const DEFAULT_MIN_SUBMIT_MS = 4000;
|
|
4
5
|
|
|
6
|
+
interface UseFormStateOptions {
|
|
7
|
+
initialValues?: Record<string, unknown>;
|
|
8
|
+
sections?: FormSection[];
|
|
9
|
+
onSubmit?: (values: Record<string, unknown>) => void | Promise<void>;
|
|
10
|
+
minSubmitMs?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface UseFormStateReturn {
|
|
14
|
+
values: Record<string, unknown>;
|
|
15
|
+
errors: Record<string, string>;
|
|
16
|
+
touched: Record<string, boolean>;
|
|
17
|
+
isDirty: boolean;
|
|
18
|
+
isValid: boolean;
|
|
19
|
+
isSubmitting: boolean;
|
|
20
|
+
setValue: (id: string, value: unknown) => void;
|
|
21
|
+
setValues: (vals: Record<string, unknown>) => void;
|
|
22
|
+
setTouched: (id: string) => void;
|
|
23
|
+
validate: () => boolean;
|
|
24
|
+
reset: () => void;
|
|
25
|
+
handleSubmit: (e?: React.FormEvent) => Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
|
|
5
28
|
/**
|
|
6
29
|
* Extracts all field ids and their default values from a form schema.
|
|
7
30
|
*/
|
|
8
|
-
function buildDefaults(sections) {
|
|
9
|
-
const defaults = {};
|
|
31
|
+
function buildDefaults(sections: FormSection[]): Record<string, unknown> {
|
|
32
|
+
const defaults: Record<string, unknown> = {};
|
|
10
33
|
for (const section of sections) {
|
|
11
34
|
for (const field of section.fields ?? []) {
|
|
12
35
|
if (field.type === "checkboxGroup") {
|
|
@@ -25,8 +48,8 @@ function buildDefaults(sections) {
|
|
|
25
48
|
* Runs validation for all fields.
|
|
26
49
|
* Returns an object of { fieldId: errorMessage } (empty object = valid).
|
|
27
50
|
*/
|
|
28
|
-
function runValidation(values, sections) {
|
|
29
|
-
const errors = {};
|
|
51
|
+
function runValidation(values: Record<string, unknown>, sections: FormSection[]): Record<string, string> {
|
|
52
|
+
const errors: Record<string, string> = {};
|
|
30
53
|
for (const section of sections) {
|
|
31
54
|
for (const field of section.fields ?? []) {
|
|
32
55
|
const val = values[field.id];
|
|
@@ -54,23 +77,19 @@ function runValidation(values, sections) {
|
|
|
54
77
|
|
|
55
78
|
/**
|
|
56
79
|
* Form state management hook.
|
|
57
|
-
*
|
|
58
|
-
* @param {Object} options
|
|
59
|
-
* @param {Object} options.initialValues — prefill for editing (merged over field defaults)
|
|
60
|
-
* @param {Array} options.sections — form schema sections (used for defaults + validation)
|
|
61
|
-
* @param {Function} options.onSubmit — called with (values) when form is valid
|
|
62
|
-
* @param {number} options.minSubmitMs — minimum time the submit spinner shows (default 4000ms, set 0 to disable)
|
|
63
|
-
*
|
|
64
|
-
* @returns {Object} { values, errors, touched, isDirty, isValid, isSubmitting,
|
|
65
|
-
* setValue, setValues, setTouched, validate, reset, handleSubmit }
|
|
66
80
|
*/
|
|
67
|
-
export default function useFormState({
|
|
81
|
+
export default function useFormState({
|
|
82
|
+
initialValues = {},
|
|
83
|
+
sections = [],
|
|
84
|
+
onSubmit,
|
|
85
|
+
minSubmitMs = DEFAULT_MIN_SUBMIT_MS
|
|
86
|
+
}: UseFormStateOptions = {}): UseFormStateReturn {
|
|
68
87
|
const defaults = useMemo(() => buildDefaults(sections), [sections]);
|
|
69
88
|
const merged = useMemo(() => ({ ...defaults, ...initialValues }), [defaults, initialValues]);
|
|
70
89
|
|
|
71
|
-
const [values, setValuesState] = useState(merged);
|
|
72
|
-
const [errors, setErrors] = useState({});
|
|
73
|
-
const [touched, setTouchedState] = useState({});
|
|
90
|
+
const [values, setValuesState] = useState<Record<string, unknown>>(merged);
|
|
91
|
+
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
92
|
+
const [touched, setTouchedState] = useState<Record<string, boolean>>({});
|
|
74
93
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
75
94
|
const submitRef = useRef(onSubmit);
|
|
76
95
|
submitRef.current = onSubmit;
|
|
@@ -86,7 +105,7 @@ export default function useFormState({ initialValues = {}, sections = [], onSubm
|
|
|
86
105
|
});
|
|
87
106
|
}, [merged, values]);
|
|
88
107
|
|
|
89
|
-
const setValue = useCallback((id, value) => {
|
|
108
|
+
const setValue = useCallback((id: string, value: unknown) => {
|
|
90
109
|
setValuesState((prev) => ({ ...prev, [id]: value }));
|
|
91
110
|
setErrors((prev) => {
|
|
92
111
|
if (!prev[id]) return prev;
|
|
@@ -96,18 +115,18 @@ export default function useFormState({ initialValues = {}, sections = [], onSubm
|
|
|
96
115
|
});
|
|
97
116
|
}, []);
|
|
98
117
|
|
|
99
|
-
const setValues = useCallback((vals) => {
|
|
118
|
+
const setValues = useCallback((vals: Record<string, unknown>) => {
|
|
100
119
|
setValuesState((prev) => ({ ...prev, ...vals }));
|
|
101
120
|
}, []);
|
|
102
121
|
|
|
103
|
-
const setTouched = useCallback((id) => {
|
|
122
|
+
const setTouched = useCallback((id: string) => {
|
|
104
123
|
setTouchedState((prev) => (prev[id] ? prev : { ...prev, [id]: true }));
|
|
105
124
|
}, []);
|
|
106
125
|
|
|
107
126
|
const validate = useCallback(() => {
|
|
108
127
|
const errs = runValidation(values, sections);
|
|
109
128
|
setErrors(errs);
|
|
110
|
-
const allTouched = {};
|
|
129
|
+
const allTouched: Record<string, boolean> = {};
|
|
111
130
|
for (const section of sections) {
|
|
112
131
|
for (const field of section.fields ?? []) {
|
|
113
132
|
allTouched[field.id] = true;
|
|
@@ -124,7 +143,7 @@ export default function useFormState({ initialValues = {}, sections = [], onSubm
|
|
|
124
143
|
}, [merged]);
|
|
125
144
|
|
|
126
145
|
const handleSubmit = useCallback(
|
|
127
|
-
async (e) => {
|
|
146
|
+
async (e?: React.FormEvent) => {
|
|
128
147
|
e?.preventDefault?.();
|
|
129
148
|
const valid = validate();
|
|
130
149
|
if (!valid) return false;
|
|
@@ -137,7 +156,8 @@ export default function useFormState({ initialValues = {}, sections = [], onSubm
|
|
|
137
156
|
await Promise.all([submitRef.current?.(values), delay]);
|
|
138
157
|
return true;
|
|
139
158
|
} catch (err) {
|
|
140
|
-
|
|
159
|
+
const errorMessage = err instanceof Error ? err.message : "Submission failed";
|
|
160
|
+
setErrors((prev) => ({ ...prev, _form: errorMessage }));
|
|
141
161
|
return false;
|
|
142
162
|
} finally {
|
|
143
163
|
setIsSubmitting(false);
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export interface PageContainerProps {
|
|
4
|
+
className?: string;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default function PageContainer({ className = "", children }: PageContainerProps) {
|
|
4
9
|
return (
|
|
5
10
|
<div className={["mx-auto w-full max-w-6xl px-4 sm:px-6", className].filter(Boolean).join(" ")}>
|
|
6
11
|
{children}
|
|
7
12
|
</div>
|
|
8
13
|
);
|
|
9
14
|
}
|
|
10
|
-
|
|
11
|
-
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { default as FormModal } from "./FormModal";
|
|
2
|
-
export { default as FormRenderer } from "./FormRenderer";
|
|
3
|
-
export { default as FormSection } from "./FormSection";
|
|
4
|
-
export { default as FormField } from "./FormField";
|
|
5
|
-
export { default as useFormState } from "./useFormState";
|
|
File without changes
|