@alepha/react 0.11.11 → 0.11.12
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/README.md +1 -183
- package/dist/auth/index.browser.js +1460 -0
- package/dist/auth/index.browser.js.map +1 -0
- package/dist/auth/index.cjs +3647 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/index.d.cts +564 -0
- package/dist/auth/index.d.cts.map +1 -0
- package/dist/auth/index.d.ts +564 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +3615 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/{index.browser.js → core/index.browser.js} +36 -35
- package/dist/core/index.browser.js.map +1 -0
- package/dist/{index.cjs → core/index.cjs} +141 -140
- package/dist/core/index.cjs.map +1 -0
- package/dist/{index.d.cts → core/index.d.cts} +68 -68
- package/dist/core/index.d.cts.map +1 -0
- package/dist/{index.d.ts → core/index.d.ts} +68 -68
- package/dist/core/index.d.ts.map +1 -0
- package/dist/{index.js → core/index.js} +39 -38
- package/dist/core/index.js.map +1 -0
- package/dist/form/index.cjs +2054 -0
- package/dist/form/index.cjs.map +1 -0
- package/dist/form/index.d.cts +211 -0
- package/dist/form/index.d.cts.map +1 -0
- package/dist/form/index.d.ts +211 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +2026 -0
- package/dist/form/index.js.map +1 -0
- package/dist/head/index.browser.js +1503 -0
- package/dist/head/index.browser.js.map +1 -0
- package/dist/head/index.cjs +1908 -0
- package/dist/head/index.cjs.map +1 -0
- package/dist/head/index.d.cts +595 -0
- package/dist/head/index.d.cts.map +1 -0
- package/dist/head/index.d.ts +601 -0
- package/dist/head/index.d.ts.map +1 -0
- package/dist/head/index.js +1880 -0
- package/dist/head/index.js.map +1 -0
- package/dist/i18n/index.cjs +1886 -0
- package/dist/i18n/index.cjs.map +1 -0
- package/dist/i18n/index.d.cts +168 -0
- package/dist/i18n/index.d.cts.map +1 -0
- package/dist/i18n/index.d.ts +168 -0
- package/dist/i18n/index.d.ts.map +1 -0
- package/dist/i18n/index.js +1857 -0
- package/dist/i18n/index.js.map +1 -0
- package/dist/websocket/index.cjs +1774 -0
- package/dist/websocket/index.cjs.map +1 -0
- package/dist/websocket/index.d.cts +118 -0
- package/dist/websocket/index.d.cts.map +1 -0
- package/dist/websocket/index.d.ts +118 -0
- package/dist/websocket/index.d.ts.map +1 -0
- package/dist/websocket/index.js +1750 -0
- package/dist/websocket/index.js.map +1 -0
- package/package.json +89 -67
- package/src/auth/descriptors/$auth.ts +436 -0
- package/src/auth/descriptors/$authApple.ts +8 -0
- package/src/auth/descriptors/$authGithub.ts +81 -0
- package/src/auth/descriptors/$authGoogle.ts +38 -0
- package/src/auth/errors/SessionExpiredError.ts +6 -0
- package/src/auth/hooks/useAuth.ts +31 -0
- package/src/auth/index.browser.ts +16 -0
- package/src/auth/index.shared.ts +3 -0
- package/src/auth/index.ts +47 -0
- package/src/auth/providers/ReactAuthProvider.ts +629 -0
- package/src/auth/schemas/tokenResponseSchema.ts +11 -0
- package/src/auth/schemas/tokensSchema.ts +21 -0
- package/src/auth/schemas/userinfoResponseSchema.ts +10 -0
- package/src/auth/services/ReactAuth.ts +124 -0
- package/src/{components → core/components}/ErrorViewer.tsx +3 -2
- package/src/{components → core/components}/NestedView.tsx +1 -1
- package/src/{contexts → core/contexts}/AlephaContext.ts +1 -1
- package/src/{descriptors → core/descriptors}/$page.ts +4 -4
- package/src/{hooks → core/hooks}/useAction.ts +1 -1
- package/src/{hooks → core/hooks}/useAlepha.ts +1 -1
- package/src/{hooks → core/hooks}/useClient.ts +1 -1
- package/src/{hooks → core/hooks}/useEvents.ts +1 -1
- package/src/{hooks → core/hooks}/useInject.ts +1 -1
- package/src/{hooks → core/hooks}/useQueryParams.ts +1 -1
- package/src/{hooks → core/hooks}/useRouterState.ts +1 -1
- package/src/{hooks → core/hooks}/useSchema.ts +3 -3
- package/src/{hooks → core/hooks}/useStore.ts +2 -2
- package/src/{index.browser.ts → core/index.browser.ts} +4 -4
- package/src/{index.ts → core/index.ts} +6 -6
- package/src/{providers → core/providers}/ReactBrowserProvider.ts +6 -6
- package/src/{providers → core/providers}/ReactBrowserRendererProvider.ts +2 -2
- package/src/{providers → core/providers}/ReactBrowserRouterProvider.ts +3 -3
- package/src/{providers → core/providers}/ReactPageProvider.ts +3 -3
- package/src/{providers → core/providers}/ReactServerProvider.ts +7 -7
- package/src/{services → core/services}/ReactPageServerService.ts +2 -2
- package/src/{services → core/services}/ReactPageService.ts +1 -1
- package/src/{services → core/services}/ReactRouter.ts +1 -1
- package/src/form/components/FormState.tsx +17 -0
- package/src/form/hooks/useForm.ts +47 -0
- package/src/form/hooks/useFormState.ts +130 -0
- package/src/form/index.ts +38 -0
- package/src/form/services/FormModel.ts +548 -0
- package/src/head/descriptors/$head.ts +25 -0
- package/src/head/hooks/useHead.ts +62 -0
- package/src/head/index.browser.ts +25 -0
- package/src/head/index.ts +47 -0
- package/src/head/interfaces/Head.ts +46 -0
- package/src/head/providers/BrowserHeadProvider.ts +105 -0
- package/src/head/providers/HeadProvider.ts +73 -0
- package/src/head/providers/ServerHeadProvider.ts +109 -0
- package/src/i18n/README.md +76 -0
- package/src/i18n/components/Localize.tsx +35 -0
- package/src/i18n/descriptors/$dictionary.ts +65 -0
- package/src/i18n/hooks/useI18n.ts +18 -0
- package/src/i18n/index.ts +34 -0
- package/src/i18n/providers/I18nProvider.ts +277 -0
- package/src/websocket/hooks/useRoom.tsx +223 -0
- package/src/websocket/index.ts +7 -0
- package/dist/index.browser.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- /package/src/{components → core/components}/ClientOnly.tsx +0 -0
- /package/src/{components → core/components}/ErrorBoundary.tsx +0 -0
- /package/src/{components → core/components}/Link.tsx +0 -0
- /package/src/{components → core/components}/NotFound.tsx +0 -0
- /package/src/{contexts → core/contexts}/RouterLayerContext.ts +0 -0
- /package/src/{errors → core/errors}/Redirection.ts +0 -0
- /package/src/{hooks → core/hooks}/useActive.ts +0 -0
- /package/src/{hooks → core/hooks}/useRouter.ts +0 -0
- /package/src/{index.shared.ts → core/index.shared.ts} +0 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { $module } from "alepha";
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
export { default as FormState } from "./components/FormState.tsx";
|
|
6
|
+
export * from "./hooks/useForm.ts";
|
|
7
|
+
export * from "./hooks/useFormState.ts";
|
|
8
|
+
export * from "./services/FormModel.ts";
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
declare module "alepha" {
|
|
13
|
+
interface Hooks {
|
|
14
|
+
"form:change": { id: string; path: string; value: any };
|
|
15
|
+
"form:reset": { id: string; values: Record<string, any> };
|
|
16
|
+
"form:submit:begin": { id: string };
|
|
17
|
+
"form:submit:success": { id: string; values: Record<string, any> };
|
|
18
|
+
"form:submit:error": { id: string; error: Error };
|
|
19
|
+
"form:submit:end": { id: string };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* React hooks for managing forms in Alepha applications.
|
|
27
|
+
*
|
|
28
|
+
* This module provides a set of hooks to simplify form handling, validation, and submission in React applications built with Alepha.
|
|
29
|
+
*
|
|
30
|
+
* It includes:
|
|
31
|
+
* - `useForm`: A hook for managing form state, validation, and submission.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link useForm}
|
|
34
|
+
* @module alepha.react.form
|
|
35
|
+
*/
|
|
36
|
+
export const AlephaReactForm = $module({
|
|
37
|
+
name: "alepha.react.form",
|
|
38
|
+
});
|
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import {
|
|
2
|
+
$inject,
|
|
3
|
+
Alepha,
|
|
4
|
+
type Static,
|
|
5
|
+
type TObject,
|
|
6
|
+
type TSchema,
|
|
7
|
+
t,
|
|
8
|
+
} from "alepha";
|
|
9
|
+
import { $logger } from "alepha/logger";
|
|
10
|
+
import type { ChangeEvent, InputHTMLAttributes } from "react";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* FormModel is a dynamic form handler that generates form inputs based on a provided TypeBox schema.
|
|
14
|
+
* It manages form state, handles input changes, and processes form submissions with validation.
|
|
15
|
+
*
|
|
16
|
+
* It means to be injected and used within React components to provide a structured way to create and manage forms.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link useForm}
|
|
19
|
+
*/
|
|
20
|
+
export class FormModel<T extends TObject> {
|
|
21
|
+
protected readonly log = $logger();
|
|
22
|
+
protected readonly alepha = $inject(Alepha);
|
|
23
|
+
protected readonly values: Record<string, any> = {};
|
|
24
|
+
protected submitInProgress = false;
|
|
25
|
+
|
|
26
|
+
public input: SchemaToInput<T>;
|
|
27
|
+
|
|
28
|
+
public get submitting(): boolean {
|
|
29
|
+
return this.submitInProgress;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
public readonly id: string,
|
|
34
|
+
public readonly options: FormCtrlOptions<T>,
|
|
35
|
+
) {
|
|
36
|
+
this.options = options;
|
|
37
|
+
|
|
38
|
+
if (options.initialValues) {
|
|
39
|
+
this.values = this.alepha.codec.decode(
|
|
40
|
+
options.schema,
|
|
41
|
+
options.initialValues,
|
|
42
|
+
) as Record<string, any>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.input = this.createProxyFromSchema(options, options.schema, {
|
|
46
|
+
store: this.values,
|
|
47
|
+
parent: "",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public get element(): HTMLFormElement {
|
|
52
|
+
return window.document.getElementById(this.id)! as HTMLFormElement;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public get currentValues(): Record<string, any> {
|
|
56
|
+
return this.restructureValues(this.values);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public get props() {
|
|
60
|
+
return {
|
|
61
|
+
id: this.id,
|
|
62
|
+
noValidate: true,
|
|
63
|
+
onSubmit: (ev?: FormEventLike) => {
|
|
64
|
+
ev?.preventDefault?.();
|
|
65
|
+
this.submit();
|
|
66
|
+
},
|
|
67
|
+
onReset: (event: FormEventLike) => this.reset(event),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public readonly reset = (event: FormEventLike) => {
|
|
72
|
+
// clear values in place to maintain proxy reference
|
|
73
|
+
for (const key in this.values) {
|
|
74
|
+
delete this.values[key];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this.options.onReset?.();
|
|
78
|
+
|
|
79
|
+
return this.alepha.events.emit(
|
|
80
|
+
"form:reset",
|
|
81
|
+
{
|
|
82
|
+
id: this.id,
|
|
83
|
+
values: this.values,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
catch: true,
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
public readonly submit = async () => {
|
|
92
|
+
if (this.submitInProgress) {
|
|
93
|
+
this.log.warn(
|
|
94
|
+
"Form submission already in progress, ignoring duplicate submit.",
|
|
95
|
+
);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// emit both action and form events
|
|
100
|
+
await this.alepha.events.emit("react:action:begin", {
|
|
101
|
+
type: "form",
|
|
102
|
+
id: this.id,
|
|
103
|
+
});
|
|
104
|
+
await this.alepha.events.emit("form:submit:begin", {
|
|
105
|
+
id: this.id,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
this.submitInProgress = true;
|
|
109
|
+
|
|
110
|
+
const options = this.options;
|
|
111
|
+
const form = this.element;
|
|
112
|
+
const args = {
|
|
113
|
+
form,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
let values: Record<string, any> = this.restructureValues(this.values);
|
|
118
|
+
|
|
119
|
+
if (t.schema.isSchema(options.schema)) {
|
|
120
|
+
values = this.alepha.codec.decode(options.schema, values) as Record<
|
|
121
|
+
string,
|
|
122
|
+
any
|
|
123
|
+
>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
await options.handler(values as any, args);
|
|
127
|
+
|
|
128
|
+
await this.alepha.events.emit("react:action:success", {
|
|
129
|
+
type: "form",
|
|
130
|
+
id: this.id,
|
|
131
|
+
});
|
|
132
|
+
await this.alepha.events.emit("form:submit:success", {
|
|
133
|
+
id: this.id,
|
|
134
|
+
values,
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
this.log.error("Form submission error:", error);
|
|
138
|
+
|
|
139
|
+
options.onError?.(error as Error, args);
|
|
140
|
+
|
|
141
|
+
await this.alepha.events.emit("react:action:error", {
|
|
142
|
+
type: "form",
|
|
143
|
+
id: this.id,
|
|
144
|
+
error: error as Error,
|
|
145
|
+
});
|
|
146
|
+
await this.alepha.events.emit("form:submit:error", {
|
|
147
|
+
error: error as Error,
|
|
148
|
+
id: this.id,
|
|
149
|
+
});
|
|
150
|
+
} finally {
|
|
151
|
+
this.submitInProgress = false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
await this.alepha.events.emit("react:action:end", {
|
|
155
|
+
type: "form",
|
|
156
|
+
id: this.id,
|
|
157
|
+
});
|
|
158
|
+
await this.alepha.events.emit("form:submit:end", {
|
|
159
|
+
id: this.id,
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Restructures flat keys like "address.city" into nested objects like { address: { city: ... } }
|
|
165
|
+
* Values are already typed from onChange, so no conversion is needed.
|
|
166
|
+
*/
|
|
167
|
+
protected restructureValues(store: Record<string, any>): Record<string, any> {
|
|
168
|
+
const values: Record<string, any> = {};
|
|
169
|
+
|
|
170
|
+
for (const [key, value] of Object.entries(store)) {
|
|
171
|
+
if (key.includes(".")) {
|
|
172
|
+
// nested object: restructure flat key to nested structure
|
|
173
|
+
this.restructureNestedValue(values, key, value);
|
|
174
|
+
} else {
|
|
175
|
+
// value is already typed, just copy it
|
|
176
|
+
values[key] = value;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return values;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Helper to restructure a flat key like "address.city" into nested object structure.
|
|
185
|
+
* The value is already typed, so we just assign it to the nested path.
|
|
186
|
+
*/
|
|
187
|
+
protected restructureNestedValue(
|
|
188
|
+
values: Record<string, any>,
|
|
189
|
+
key: string,
|
|
190
|
+
value: any,
|
|
191
|
+
) {
|
|
192
|
+
const pathSegments = key.split(".");
|
|
193
|
+
const finalPropertyKey = pathSegments.pop();
|
|
194
|
+
if (!finalPropertyKey) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let currentObjectLevel = values;
|
|
199
|
+
|
|
200
|
+
// traverse/create the nested structure
|
|
201
|
+
for (const segment of pathSegments) {
|
|
202
|
+
currentObjectLevel[segment] ??= {};
|
|
203
|
+
currentObjectLevel = currentObjectLevel[segment];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// value is already typed from onChange, just assign it
|
|
207
|
+
currentObjectLevel[finalPropertyKey] = value;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
protected createProxyFromSchema<T extends TObject>(
|
|
211
|
+
options: FormCtrlOptions<T>,
|
|
212
|
+
schema: TSchema,
|
|
213
|
+
context: {
|
|
214
|
+
parent: string;
|
|
215
|
+
store: Record<string, any>;
|
|
216
|
+
},
|
|
217
|
+
): SchemaToInput<T> {
|
|
218
|
+
const parent = context.parent || "";
|
|
219
|
+
return new Proxy<SchemaToInput<T>>({} as SchemaToInput<T>, {
|
|
220
|
+
get: (_, prop: string) => {
|
|
221
|
+
if (!options.schema || !t.schema.isObject(schema)) {
|
|
222
|
+
return {};
|
|
223
|
+
}
|
|
224
|
+
if (prop in schema.properties) {
|
|
225
|
+
if (t.schema.isObject(schema.properties[prop])) {
|
|
226
|
+
return this.createProxyFromSchema(
|
|
227
|
+
options,
|
|
228
|
+
schema.properties[prop],
|
|
229
|
+
{
|
|
230
|
+
parent: parent ? `${parent}.${prop}` : prop,
|
|
231
|
+
store: context.store,
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return this.createInputFromSchema<T>(
|
|
236
|
+
prop as keyof Static<T> & string,
|
|
237
|
+
options,
|
|
238
|
+
schema,
|
|
239
|
+
schema.required?.includes(prop as string) || false,
|
|
240
|
+
context,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
protected createInputFromSchema<T extends TObject>(
|
|
248
|
+
name: keyof Static<T> & string,
|
|
249
|
+
options: FormCtrlOptions<T>,
|
|
250
|
+
schema: TObject,
|
|
251
|
+
required: boolean,
|
|
252
|
+
context: {
|
|
253
|
+
parent: string;
|
|
254
|
+
store: Record<string, any>;
|
|
255
|
+
},
|
|
256
|
+
): InputField {
|
|
257
|
+
const parent = context.parent || "";
|
|
258
|
+
const field = schema.properties?.[name];
|
|
259
|
+
if (!field) {
|
|
260
|
+
return {
|
|
261
|
+
path: "",
|
|
262
|
+
required,
|
|
263
|
+
props: {} as InputHTMLAttributes<unknown>,
|
|
264
|
+
schema: schema,
|
|
265
|
+
set: () => {},
|
|
266
|
+
form: this,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const isRequired = schema.required?.includes(name) ?? false;
|
|
271
|
+
|
|
272
|
+
const key = parent ? `${parent}.${name}` : name;
|
|
273
|
+
const path = `/${key.replaceAll(".", "/")}`;
|
|
274
|
+
|
|
275
|
+
const set = (value: any, sync = true) => {
|
|
276
|
+
// Convert to typed value immediately based on schema
|
|
277
|
+
const typedValue = this.getValueFromInput(value, field);
|
|
278
|
+
|
|
279
|
+
if (context.store[key] === typedValue) {
|
|
280
|
+
// no change, do not update
|
|
281
|
+
// return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
context.store[key] = typedValue;
|
|
285
|
+
|
|
286
|
+
if (options.onChange) {
|
|
287
|
+
options.onChange(key, typedValue, context.store);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this.alepha.events.emit("form:change", {
|
|
291
|
+
id: this.id,
|
|
292
|
+
path: path,
|
|
293
|
+
value: typedValue,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
if (sync) {
|
|
297
|
+
const inputElement = window.document.querySelector(
|
|
298
|
+
`[data-path="${path}"]`,
|
|
299
|
+
);
|
|
300
|
+
if (inputElement instanceof HTMLInputElement) {
|
|
301
|
+
if (t.schema.isBoolean(field)) {
|
|
302
|
+
inputElement.checked = Boolean(value);
|
|
303
|
+
} else {
|
|
304
|
+
inputElement.value = value;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const attr: InputHTMLAttributesLike = {
|
|
311
|
+
name: key,
|
|
312
|
+
autoComplete: "off",
|
|
313
|
+
onChange: (event: ChangeEvent<HTMLInputElement> | string | number) => {
|
|
314
|
+
if (typeof event === "string") {
|
|
315
|
+
// If the event is a string, it means it's a direct value change
|
|
316
|
+
set(event, false);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (typeof event === "number") {
|
|
321
|
+
// Some inputs might return number directly
|
|
322
|
+
set(event, false);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (t.schema.isBoolean(field)) {
|
|
327
|
+
set(event.target.checked, false);
|
|
328
|
+
} else {
|
|
329
|
+
set(event.target.value, false);
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
(attr as any)["data-path"] = path;
|
|
335
|
+
|
|
336
|
+
if (options.id) {
|
|
337
|
+
attr.id = `${options.id}-${key}`;
|
|
338
|
+
(attr as any)["data-testid"] = attr.id;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (t.schema.isString(field)) {
|
|
342
|
+
if (field.maxLength != null) {
|
|
343
|
+
attr.maxLength = Number(field.maxLength);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (field.minLength != null) {
|
|
347
|
+
attr.minLength = Number(field.minLength);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (options.initialValues?.[name] != null) {
|
|
352
|
+
attr.defaultValue = this.valueToInputEntry(options.initialValues[name]);
|
|
353
|
+
} else if ("default" in field && field.default != null) {
|
|
354
|
+
attr.defaultValue = this.valueToInputEntry(field.default);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (isRequired) {
|
|
358
|
+
attr.required = true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if ("description" in field && typeof field.description === "string") {
|
|
362
|
+
attr["aria-label"] = field.description;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (t.schema.isInteger(field) || t.schema.isNumber(field)) {
|
|
366
|
+
attr.type = "number";
|
|
367
|
+
} else if (name === "password") {
|
|
368
|
+
attr.type = "password";
|
|
369
|
+
} else if (name === "email") {
|
|
370
|
+
attr.type = "email";
|
|
371
|
+
} else if (name === "url") {
|
|
372
|
+
attr.type = "url";
|
|
373
|
+
} else if (t.schema.isString(field)) {
|
|
374
|
+
if (field.format === "binary") {
|
|
375
|
+
attr.type = "file";
|
|
376
|
+
} else if (field.format === "date") {
|
|
377
|
+
attr.type = "date";
|
|
378
|
+
} else if (field.format === "time") {
|
|
379
|
+
attr.type = "time";
|
|
380
|
+
} else if (field.format === "date-time") {
|
|
381
|
+
attr.type = "datetime-local";
|
|
382
|
+
} else {
|
|
383
|
+
attr.type = "text";
|
|
384
|
+
}
|
|
385
|
+
} else if (t.schema.isBoolean(field)) {
|
|
386
|
+
attr.type = "checkbox";
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (options.onCreateField) {
|
|
390
|
+
const customAttr = options.onCreateField(name, field);
|
|
391
|
+
Object.assign(attr, customAttr);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
path,
|
|
396
|
+
props: attr,
|
|
397
|
+
schema: field,
|
|
398
|
+
set,
|
|
399
|
+
form: this,
|
|
400
|
+
required,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Convert an input value to the correct type based on the schema.
|
|
406
|
+
* Handles raw DOM values (strings, booleans from checkboxes, Files, etc.)
|
|
407
|
+
*/
|
|
408
|
+
protected getValueFromInput(input: any, schema: TSchema): any {
|
|
409
|
+
if (input instanceof File) {
|
|
410
|
+
// for file inputs, return the File object directly
|
|
411
|
+
if (t.schema.isString(schema) && schema.format === "binary") {
|
|
412
|
+
return input;
|
|
413
|
+
}
|
|
414
|
+
// for now, ignore other formats
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (t.schema.isBoolean(schema)) {
|
|
419
|
+
return !!input;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (t.schema.isNumber(schema)) {
|
|
423
|
+
const num = Number(input);
|
|
424
|
+
return Number.isNaN(num) ? null : num;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (t.schema.isString(schema)) {
|
|
428
|
+
if (schema.format === "date") {
|
|
429
|
+
return new Date(input).toISOString().slice(0, 10); // For date input
|
|
430
|
+
}
|
|
431
|
+
if (schema.format === "time") {
|
|
432
|
+
return new Date(`1970-01-01T${input}`).toISOString().slice(11, 16); // For time input
|
|
433
|
+
}
|
|
434
|
+
if (schema.format === "date-time") {
|
|
435
|
+
return new Date(input).toISOString(); // For datetime-local input
|
|
436
|
+
}
|
|
437
|
+
return String(input);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
return input; // fallback for other types
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
protected valueToInputEntry(value: any): string | number | boolean {
|
|
444
|
+
if (value === null || value === undefined) {
|
|
445
|
+
return "";
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (typeof value === "boolean") {
|
|
449
|
+
return value;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (typeof value === "number") {
|
|
453
|
+
return value;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (typeof value === "string") {
|
|
457
|
+
return value;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (value instanceof Date) {
|
|
461
|
+
return value.toISOString().slice(0, 16); // For datetime-local input
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return value;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
export type SchemaToInput<T extends TObject> = {
|
|
469
|
+
[K in keyof T["properties"]]: T["properties"][K] extends TObject
|
|
470
|
+
? SchemaToInput<T["properties"][K]>
|
|
471
|
+
: InputField;
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
export interface FormEventLike {
|
|
475
|
+
preventDefault?: () => void;
|
|
476
|
+
stopPropagation?: () => void;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export interface InputField {
|
|
480
|
+
path: string;
|
|
481
|
+
required: boolean;
|
|
482
|
+
props: InputHTMLAttributesLike;
|
|
483
|
+
schema: TSchema;
|
|
484
|
+
set: (value: any) => void;
|
|
485
|
+
form: FormModel<any>;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
export type InputHTMLAttributesLike = Pick<
|
|
489
|
+
InputHTMLAttributes<unknown>,
|
|
490
|
+
| "id"
|
|
491
|
+
| "name"
|
|
492
|
+
| "type"
|
|
493
|
+
| "value"
|
|
494
|
+
| "defaultValue"
|
|
495
|
+
| "required"
|
|
496
|
+
| "maxLength"
|
|
497
|
+
| "minLength"
|
|
498
|
+
| "aria-label"
|
|
499
|
+
| "autoComplete"
|
|
500
|
+
> & {
|
|
501
|
+
value?: any;
|
|
502
|
+
defaultValue?: any;
|
|
503
|
+
onChange?: (event: any) => void;
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
export type FormCtrlOptions<T extends TObject> = {
|
|
507
|
+
/**
|
|
508
|
+
* The schema defining the structure and validation rules for the form.
|
|
509
|
+
* This should be a TypeBox schema object.
|
|
510
|
+
*/
|
|
511
|
+
schema: T;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Callback function to handle form submission.
|
|
515
|
+
* This function will receive the parsed and validated form values.
|
|
516
|
+
*/
|
|
517
|
+
handler: (values: Static<T>, args: { form: HTMLFormElement }) => unknown;
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Optional initial values for the form fields.
|
|
521
|
+
* This can be used to pre-populate the form with existing data.
|
|
522
|
+
*/
|
|
523
|
+
initialValues?: Partial<Static<T>>;
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Optional function to create custom field attributes.
|
|
527
|
+
* This can be used to add custom validation, styles, or other attributes.
|
|
528
|
+
*/
|
|
529
|
+
onCreateField?: (
|
|
530
|
+
name: keyof Static<T> & string,
|
|
531
|
+
schema: TSchema,
|
|
532
|
+
) => InputHTMLAttributes<unknown>;
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* If defined, this will generate a unique ID for each field, prefixed with this string.
|
|
536
|
+
*
|
|
537
|
+
* > "username" with id="form-123" will become "form-123-username".
|
|
538
|
+
*
|
|
539
|
+
* If omitted, IDs will not be generated.
|
|
540
|
+
*/
|
|
541
|
+
id?: string;
|
|
542
|
+
|
|
543
|
+
onError?: (error: Error, args: { form: HTMLFormElement }) => void;
|
|
544
|
+
|
|
545
|
+
onChange?: (key: string, value: any, store: Record<string, any>) => void;
|
|
546
|
+
|
|
547
|
+
onReset?: () => void;
|
|
548
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { $inject, createDescriptor, Descriptor, KIND } from "alepha";
|
|
2
|
+
import type { Head } from "../interfaces/Head.ts";
|
|
3
|
+
import { HeadProvider } from "../providers/HeadProvider.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set global `<head>` options for the application.
|
|
7
|
+
*/
|
|
8
|
+
export const $head = (options: HeadDescriptorOptions) => {
|
|
9
|
+
return createDescriptor(HeadDescriptor, options);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
export type HeadDescriptorOptions = Head | (() => Head);
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
export class HeadDescriptor extends Descriptor<HeadDescriptorOptions> {
|
|
19
|
+
protected readonly provider = $inject(HeadProvider);
|
|
20
|
+
protected onInit() {
|
|
21
|
+
this.provider.global = this.options;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
$head[KIND] = HeadDescriptor;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useInject } from "@alepha/react";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
4
|
+
import type { Head } from "../interfaces/Head.ts";
|
|
5
|
+
import { BrowserHeadProvider } from "../providers/BrowserHeadProvider.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const App = () => {
|
|
10
|
+
* const [head, setHead] = useHead({
|
|
11
|
+
* // will set the document title on the first render
|
|
12
|
+
* title: "My App",
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* // This will update the document title when the button is clicked
|
|
17
|
+
* <button onClick={() => setHead({ title: "Change Title" })}>
|
|
18
|
+
* Change Title {head.title}
|
|
19
|
+
* </button>
|
|
20
|
+
* );
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const useHead = (options?: UseHeadOptions): UseHeadReturn => {
|
|
25
|
+
const alepha = useInject(Alepha);
|
|
26
|
+
|
|
27
|
+
const current = useMemo(() => {
|
|
28
|
+
if (!alepha.isBrowser()) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return alepha.inject(BrowserHeadProvider).getHead(window.document);
|
|
33
|
+
}, []);
|
|
34
|
+
|
|
35
|
+
const setHead = useCallback((head?: Head | ((previous?: Head) => Head)) => {
|
|
36
|
+
if (!alepha.isBrowser()) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
alepha
|
|
41
|
+
.inject(BrowserHeadProvider)
|
|
42
|
+
.renderHead(
|
|
43
|
+
window.document,
|
|
44
|
+
typeof head === "function" ? head(current) : head || {},
|
|
45
|
+
);
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (options) {
|
|
50
|
+
setHead(options);
|
|
51
|
+
}
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
return [current, setHead];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type UseHeadOptions = Head | ((previous?: Head) => Head);
|
|
58
|
+
|
|
59
|
+
export type UseHeadReturn = [
|
|
60
|
+
Head,
|
|
61
|
+
(head?: Head | ((previous?: Head) => Head)) => void,
|
|
62
|
+
];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AlephaReact } from "@alepha/react";
|
|
2
|
+
import { $module } from "alepha";
|
|
3
|
+
import { $head } from "./descriptors/$head.ts";
|
|
4
|
+
import { BrowserHeadProvider } from "./providers/BrowserHeadProvider.ts";
|
|
5
|
+
|
|
6
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
export * from "./descriptors/$head.ts";
|
|
9
|
+
export * from "./hooks/useHead.ts";
|
|
10
|
+
export * from "./interfaces/Head.ts";
|
|
11
|
+
export * from "./providers/BrowserHeadProvider.ts";
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Alepha React Head Module
|
|
17
|
+
*
|
|
18
|
+
* @see {@link BrowserHeadProvider}
|
|
19
|
+
* @module alepha.react.head
|
|
20
|
+
*/
|
|
21
|
+
export const AlephaReactHead = $module({
|
|
22
|
+
name: "alepha.react.head",
|
|
23
|
+
descriptors: [$head],
|
|
24
|
+
services: [AlephaReact, BrowserHeadProvider],
|
|
25
|
+
});
|