@larose-ui/forms 0.1.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/LICENSE +21 -0
- package/dist/index.css +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +164 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 laRose contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.css
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* src/Form.module.css */
|
|
2
|
+
.form {
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-direction: column;
|
|
5
|
+
gap: var(--lr-space-4);
|
|
6
|
+
max-width: 28rem;
|
|
7
|
+
}
|
|
8
|
+
.title {
|
|
9
|
+
margin: 0;
|
|
10
|
+
font-size: var(--lr-font-size-xl);
|
|
11
|
+
font-weight: var(--lr-font-weight-semibold);
|
|
12
|
+
}
|
|
13
|
+
.actions {
|
|
14
|
+
display: flex;
|
|
15
|
+
justify-content: flex-end;
|
|
16
|
+
margin-top: var(--lr-space-2);
|
|
17
|
+
}
|
|
18
|
+
.selectLabel {
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-direction: column;
|
|
21
|
+
gap: var(--lr-space-1);
|
|
22
|
+
font-size: var(--lr-font-size-sm);
|
|
23
|
+
font-weight: var(--lr-font-weight-medium);
|
|
24
|
+
}
|
|
25
|
+
.select,
|
|
26
|
+
.textarea {
|
|
27
|
+
font-family: var(--lr-font-family-sans);
|
|
28
|
+
font-size: var(--lr-font-size-md);
|
|
29
|
+
padding: var(--lr-space-2) var(--lr-space-3);
|
|
30
|
+
border: 1px solid var(--lr-color-border);
|
|
31
|
+
border-radius: var(--lr-radius-md);
|
|
32
|
+
background: var(--lr-color-background);
|
|
33
|
+
color: var(--lr-color-text);
|
|
34
|
+
}
|
|
35
|
+
.textarea {
|
|
36
|
+
min-height: 5rem;
|
|
37
|
+
resize: vertical;
|
|
38
|
+
}
|
|
39
|
+
.select:focus,
|
|
40
|
+
.textarea:focus {
|
|
41
|
+
outline: none;
|
|
42
|
+
border-color: var(--lr-color-primary);
|
|
43
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type FieldType = 'text' | 'email' | 'number' | 'select' | 'date' | 'textarea';
|
|
4
|
+
interface FieldCondition {
|
|
5
|
+
field: string;
|
|
6
|
+
equals?: unknown;
|
|
7
|
+
notEquals?: unknown;
|
|
8
|
+
}
|
|
9
|
+
interface SelectOption {
|
|
10
|
+
label: string;
|
|
11
|
+
value: string;
|
|
12
|
+
}
|
|
13
|
+
interface FormFieldSchema {
|
|
14
|
+
name: string;
|
|
15
|
+
type: FieldType;
|
|
16
|
+
label: string;
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
required?: boolean;
|
|
19
|
+
hint?: string;
|
|
20
|
+
showWhen?: FieldCondition;
|
|
21
|
+
dependsOn?: string;
|
|
22
|
+
options?: SelectOption[];
|
|
23
|
+
}
|
|
24
|
+
interface FormSchema {
|
|
25
|
+
id: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
fields: FormFieldSchema[];
|
|
28
|
+
}
|
|
29
|
+
type FormValues = Record<string, string>;
|
|
30
|
+
declare function evaluateCondition(condition: FieldCondition, values: FormValues): boolean;
|
|
31
|
+
declare function getVisibleFields(schema: FormSchema, values: FormValues): FormFieldSchema[];
|
|
32
|
+
declare function validateForm(schema: FormSchema, values: FormValues): Record<string, string>;
|
|
33
|
+
|
|
34
|
+
interface FormProps {
|
|
35
|
+
schema: FormSchema;
|
|
36
|
+
initialValues?: FormValues;
|
|
37
|
+
submitUrl?: string;
|
|
38
|
+
onSubmit?: (values: FormValues) => Promise<void> | void;
|
|
39
|
+
submitLabel?: string;
|
|
40
|
+
}
|
|
41
|
+
declare function Form({ schema, initialValues, submitUrl, onSubmit, submitLabel, }: FormProps): react.JSX.Element;
|
|
42
|
+
|
|
43
|
+
export { type FieldCondition, type FieldType, Form, type FormFieldSchema, type FormProps, type FormSchema, type FormValues, type SelectOption, evaluateCondition, getVisibleFields, validateForm };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// src/schema.ts
|
|
2
|
+
function evaluateCondition(condition, values) {
|
|
3
|
+
const current = values[condition.field];
|
|
4
|
+
if (condition.equals !== void 0) return current === condition.equals;
|
|
5
|
+
if (condition.notEquals !== void 0) return current !== condition.notEquals;
|
|
6
|
+
return Boolean(current);
|
|
7
|
+
}
|
|
8
|
+
function getVisibleFields(schema, values) {
|
|
9
|
+
return schema.fields.filter((field) => {
|
|
10
|
+
if (!field.showWhen) return true;
|
|
11
|
+
return evaluateCondition(field.showWhen, values);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function validateForm(schema, values) {
|
|
15
|
+
const errors = {};
|
|
16
|
+
const visible = getVisibleFields(schema, values);
|
|
17
|
+
for (const field of visible) {
|
|
18
|
+
const value = values[field.name]?.trim() ?? "";
|
|
19
|
+
if (field.required && !value) {
|
|
20
|
+
errors[field.name] = `${field.label} is required`;
|
|
21
|
+
}
|
|
22
|
+
if (field.type === "email" && value && !value.includes("@")) {
|
|
23
|
+
errors[field.name] = "Invalid email address";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return errors;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/Form.tsx
|
|
30
|
+
import { useCallback, useMemo, useState } from "react";
|
|
31
|
+
import { Input, Button, Alert, Textarea, Select } from "@larose-ui/react";
|
|
32
|
+
import { useMutation } from "@larose-ui/data";
|
|
33
|
+
import {
|
|
34
|
+
useOptionalObservability,
|
|
35
|
+
markFormSubmitted,
|
|
36
|
+
trackFormSuccess,
|
|
37
|
+
trackFormError,
|
|
38
|
+
trackFormValidationFailed
|
|
39
|
+
} from "@larose-ui/observability";
|
|
40
|
+
|
|
41
|
+
// src/Form.module.css
|
|
42
|
+
var Form_default = {};
|
|
43
|
+
|
|
44
|
+
// src/Form.tsx
|
|
45
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
46
|
+
function Form({
|
|
47
|
+
schema,
|
|
48
|
+
initialValues = {},
|
|
49
|
+
submitUrl,
|
|
50
|
+
onSubmit,
|
|
51
|
+
submitLabel = "Save"
|
|
52
|
+
}) {
|
|
53
|
+
const [values, setValues] = useState(initialValues);
|
|
54
|
+
const [errors, setErrors] = useState({});
|
|
55
|
+
const [touched, setTouched] = useState({});
|
|
56
|
+
const [localSubmitting, setLocalSubmitting] = useState(false);
|
|
57
|
+
const observability = useOptionalObservability();
|
|
58
|
+
const formName = schema.id;
|
|
59
|
+
const mutation = useMutation({
|
|
60
|
+
url: submitUrl ?? "/__larose_noop__",
|
|
61
|
+
method: "POST"
|
|
62
|
+
});
|
|
63
|
+
const visibleFields = useMemo(
|
|
64
|
+
() => getVisibleFields(schema, values),
|
|
65
|
+
[schema, values]
|
|
66
|
+
);
|
|
67
|
+
const handleChange = useCallback((name, value) => {
|
|
68
|
+
setValues((prev) => ({ ...prev, [name]: value }));
|
|
69
|
+
setTouched((prev) => ({ ...prev, [name]: true }));
|
|
70
|
+
}, []);
|
|
71
|
+
const handleSubmit = useCallback(
|
|
72
|
+
async (e) => {
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
const validationErrors = validateForm(schema, values);
|
|
75
|
+
setErrors(validationErrors);
|
|
76
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
77
|
+
if (observability) {
|
|
78
|
+
trackFormValidationFailed(
|
|
79
|
+
formName,
|
|
80
|
+
observability.track,
|
|
81
|
+
Object.keys(validationErrors)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (observability) {
|
|
87
|
+
markFormSubmitted(formName, observability.track);
|
|
88
|
+
}
|
|
89
|
+
if (onSubmit) {
|
|
90
|
+
setLocalSubmitting(true);
|
|
91
|
+
try {
|
|
92
|
+
await onSubmit(values);
|
|
93
|
+
if (observability) trackFormSuccess(formName, observability.track);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (observability) {
|
|
96
|
+
trackFormError(
|
|
97
|
+
formName,
|
|
98
|
+
observability.track,
|
|
99
|
+
err instanceof Error ? err.message : "Submit failed"
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
throw err;
|
|
103
|
+
} finally {
|
|
104
|
+
setLocalSubmitting(false);
|
|
105
|
+
}
|
|
106
|
+
} else if (submitUrl) {
|
|
107
|
+
const result = await mutation.mutate(values);
|
|
108
|
+
if (mutation.error && observability) {
|
|
109
|
+
trackFormError(formName, observability.track, mutation.error.message);
|
|
110
|
+
} else if (result !== void 0 && observability) {
|
|
111
|
+
trackFormSuccess(formName, observability.track);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
[schema, values, onSubmit, submitUrl, mutation, observability, formName]
|
|
116
|
+
);
|
|
117
|
+
const serverError = submitUrl ? mutation.error : null;
|
|
118
|
+
const isSubmitting = localSubmitting || mutation.status === "submitting";
|
|
119
|
+
return /* @__PURE__ */ jsxs("form", { className: Form_default.form, onSubmit: handleSubmit, "data-form-id": schema.id, children: [
|
|
120
|
+
schema.title && /* @__PURE__ */ jsx("h2", { className: Form_default.title, children: schema.title }),
|
|
121
|
+
serverError && /* @__PURE__ */ jsx(Alert, { variant: "error", title: "Save failed", children: serverError.message }),
|
|
122
|
+
visibleFields.map((field) => /* @__PURE__ */ jsx("div", { children: field.type === "select" ? /* @__PURE__ */ jsx(
|
|
123
|
+
Select,
|
|
124
|
+
{
|
|
125
|
+
label: field.label,
|
|
126
|
+
name: field.name,
|
|
127
|
+
value: values[field.name] ?? "",
|
|
128
|
+
onChange: (e) => handleChange(field.name, e.target.value),
|
|
129
|
+
options: field.options ?? [],
|
|
130
|
+
error: touched[field.name] ? errors[field.name] : void 0
|
|
131
|
+
}
|
|
132
|
+
) : field.type === "textarea" ? /* @__PURE__ */ jsx(
|
|
133
|
+
Textarea,
|
|
134
|
+
{
|
|
135
|
+
label: field.label,
|
|
136
|
+
name: field.name,
|
|
137
|
+
value: values[field.name] ?? "",
|
|
138
|
+
onChange: (e) => handleChange(field.name, e.target.value),
|
|
139
|
+
placeholder: field.placeholder,
|
|
140
|
+
error: touched[field.name] ? errors[field.name] : void 0,
|
|
141
|
+
hint: field.hint
|
|
142
|
+
}
|
|
143
|
+
) : /* @__PURE__ */ jsx(
|
|
144
|
+
Input,
|
|
145
|
+
{
|
|
146
|
+
label: field.label,
|
|
147
|
+
name: field.name,
|
|
148
|
+
type: field.type === "number" ? "number" : field.type,
|
|
149
|
+
value: values[field.name] ?? "",
|
|
150
|
+
onChange: (e) => handleChange(field.name, e.target.value),
|
|
151
|
+
hint: field.hint,
|
|
152
|
+
error: touched[field.name] ? errors[field.name] : void 0,
|
|
153
|
+
placeholder: field.placeholder
|
|
154
|
+
}
|
|
155
|
+
) }, field.name)),
|
|
156
|
+
/* @__PURE__ */ jsx("div", { className: Form_default.actions, children: /* @__PURE__ */ jsx(Button, { type: "submit", loading: isSubmitting, children: submitLabel }) })
|
|
157
|
+
] });
|
|
158
|
+
}
|
|
159
|
+
export {
|
|
160
|
+
Form,
|
|
161
|
+
evaluateCondition,
|
|
162
|
+
getVisibleFields,
|
|
163
|
+
validateForm
|
|
164
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@larose-ui/forms",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Schema-driven forms for laRose UI platform",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@larose-ui/core": "0.1.0",
|
|
20
|
+
"@larose-ui/data": "0.1.0",
|
|
21
|
+
"@larose-ui/observability": "0.1.0",
|
|
22
|
+
"@larose-ui/react": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
29
|
+
"@testing-library/react": "^16.1.0",
|
|
30
|
+
"@testing-library/user-event": "^14.5.2",
|
|
31
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
32
|
+
"jsdom": "^25.0.1",
|
|
33
|
+
"react": "^19.0.0",
|
|
34
|
+
"react-dom": "^19.0.0",
|
|
35
|
+
"tsup": "^8.3.5",
|
|
36
|
+
"typescript": "^5.7.2",
|
|
37
|
+
"vitest": "^2.1.8"
|
|
38
|
+
},
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/larose-ui/larose.git",
|
|
46
|
+
"directory": "packages/forms"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"larose",
|
|
50
|
+
"react",
|
|
51
|
+
"ui-platform",
|
|
52
|
+
"design-system",
|
|
53
|
+
"saas"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"dev": "tsup --watch",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"typecheck": "tsc --noEmit",
|
|
60
|
+
"clean": "rm -rf dist"
|
|
61
|
+
}
|
|
62
|
+
}
|