@coherent.js/forms 1.0.0-beta.2

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/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@coherent.js/forms",
3
+ "version": "1.0.0-beta.2",
4
+ "description": "SSR + Hydration form system for Coherent.js applications",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "exports": {
8
+ ".": "./dist/index.js",
9
+ "./form-builder": "./dist/form-builder.js",
10
+ "./hydration": "./dist/form-hydration.js",
11
+ "./validation": "./dist/validation.js",
12
+ "./validators": "./dist/validators.js",
13
+ "./forms": "./dist/forms.js",
14
+ "./advanced-validation": "./dist/advanced-validation.js"
15
+ },
16
+ "keywords": [
17
+ "coherent",
18
+ "forms",
19
+ "validation",
20
+ "form-builder"
21
+ ],
22
+ "author": "Coherent.js Team",
23
+ "license": "MIT",
24
+ "peerDependencies": {
25
+ "@coherent.js/core": "1.0.0-beta.2",
26
+ "@coherent.js/state": "1.0.0-beta.2"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/Tomdrouv1/coherent.js.git"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "types": "./types/index.d.ts",
36
+ "files": [
37
+ "LICENSE",
38
+ "README.md",
39
+ "types/"
40
+ ],
41
+ "scripts": {
42
+ "build": "node build.mjs",
43
+ "clean": "rm -rf dist"
44
+ }
45
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Coherent.js Forms TypeScript Definitions
3
+ * @module @coherent.js/forms
4
+ */
5
+
6
+ // ===== Form Builder Types =====
7
+
8
+ export interface FormField {
9
+ type: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url' | 'date' | 'time' | 'datetime-local' | 'checkbox' | 'radio' | 'select' | 'textarea';
10
+ name: string;
11
+ label?: string;
12
+ placeholder?: string;
13
+ required?: boolean;
14
+ value?: any;
15
+ options?: Array<{ value: string; label: string }>;
16
+ validators?: Validator[];
17
+ attributes?: Record<string, any>;
18
+ }
19
+
20
+ export interface FormConfig {
21
+ fields: FormField[];
22
+ action?: string;
23
+ method?: 'get' | 'post';
24
+ className?: string;
25
+ submitText?: string;
26
+ onSubmit?: (data: FormData) => void | Promise<void>;
27
+ }
28
+
29
+ export class FormBuilder {
30
+ constructor(config: FormConfig);
31
+ addField(field: FormField): this;
32
+ removeField(name: string): this;
33
+ build(): object;
34
+ render(): object;
35
+ }
36
+
37
+ export function createFormBuilder(config: FormConfig): FormBuilder;
38
+ export function buildForm(config: FormConfig): object;
39
+
40
+ // ===== Form Hydration Types =====
41
+
42
+ export interface HydrationOptions {
43
+ validation?: boolean;
44
+ realTimeValidation?: boolean;
45
+ onSubmit?: (event: Event, data: FormData) => void | Promise<void>;
46
+ onValidate?: (errors: ValidationErrors) => void;
47
+ }
48
+
49
+ export interface HydratedForm {
50
+ element: HTMLFormElement;
51
+ validate(): ValidationResult;
52
+ reset(): void;
53
+ getData(): FormData;
54
+ setData(data: Record<string, any>): void;
55
+ destroy(): void;
56
+ }
57
+
58
+ export function hydrateForm(formElement: HTMLFormElement | string, options?: HydrationOptions): HydratedForm;
59
+
60
+ // ===== Validation Types =====
61
+
62
+ export interface ValidationResult {
63
+ valid: boolean;
64
+ errors: ValidationErrors;
65
+ }
66
+
67
+ export interface ValidationErrors {
68
+ [fieldName: string]: string[];
69
+ }
70
+
71
+ export interface Validator {
72
+ (value: any): boolean | string | Promise<boolean | string>;
73
+ }
74
+
75
+ export class FormValidator {
76
+ constructor(rules: Record<string, Validator[]>);
77
+ validate(data: Record<string, any>): ValidationResult;
78
+ validateAsync(data: Record<string, any>): Promise<ValidationResult>;
79
+ addRule(field: string, validator: Validator): void;
80
+ removeRule(field: string, validator: Validator): void;
81
+ }
82
+
83
+ export function createValidator(rules: Record<string, Validator[]>): FormValidator;
84
+ export function validate(data: Record<string, any>, rules: Record<string, Validator[]>): ValidationResult;
85
+
86
+ // ===== Built-in Validators =====
87
+
88
+ export const validators: {
89
+ required(message?: string): Validator;
90
+ email(message?: string): Validator;
91
+ minLength(length: number, message?: string): Validator;
92
+ maxLength(length: number, message?: string): Validator;
93
+ min(value: number, message?: string): Validator;
94
+ max(value: number, message?: string): Validator;
95
+ pattern(regex: RegExp, message?: string): Validator;
96
+ matches(field: string, message?: string): Validator;
97
+ url(message?: string): Validator;
98
+ number(message?: string): Validator;
99
+ integer(message?: string): Validator;
100
+ positive(message?: string): Validator;
101
+ negative(message?: string): Validator;
102
+ date(message?: string): Validator;
103
+ custom(fn: (value: any) => boolean | string, message?: string): Validator;
104
+ async(fn: (value: any) => Promise<boolean | string>): Validator;
105
+ };
106
+
107
+ export const formValidators: typeof validators;
108
+
109
+ // ===== Advanced Validation =====
110
+
111
+ export interface ValidationRule {
112
+ validator: Validator;
113
+ message?: string;
114
+ async?: boolean;
115
+ }
116
+
117
+ export interface FieldValidation {
118
+ rules: ValidationRule[];
119
+ validateOnChange?: boolean;
120
+ validateOnBlur?: boolean;
121
+ debounce?: number;
122
+ }
123
+
124
+ export function createAsyncValidator(fn: (value: any) => Promise<boolean | string>): Validator;
125
+ export function combineValidators(...validators: Validator[]): Validator;
126
+ export function conditionalValidator(condition: (data: Record<string, any>) => boolean, validator: Validator): Validator;
127
+
128
+ // ===== Deprecated SPA-only Functions (for backward compatibility) =====
129
+
130
+ /** @deprecated Use createFormBuilder() on server + hydrateForm() on client */
131
+ export function createForm(config: FormConfig): object;
132
+
133
+ /** @deprecated Use validators with hydrateForm() instead */
134
+ export function enhancedForm(config: FormConfig & { validation?: Record<string, Validator[]> }): object;