@vaadin/hilla-react-form 24.7.0-alpha8 → 24.7.0-beta1
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/index.d.ts +41 -42
- package/index.js +179 -189
- package/index.js.map +1 -7
- package/package.json +6 -29
- package/index.d.ts.map +0 -1
package/index.d.ts
CHANGED
|
@@ -1,53 +1,52 @@
|
|
|
1
|
-
import { type AbstractModel, type BinderConfiguration, type DetachedModelConstructor, type Validator, type Value, type ValueError, type ArrayModel, type ArrayItemModel } from
|
|
1
|
+
import { type AbstractModel, type BinderConfiguration, type DetachedModelConstructor, type Validator, type Value, type ValueError, type ArrayModel, type ArrayItemModel } from "@vaadin/hilla-lit-form";
|
|
2
2
|
export type FieldDirectiveResult = Readonly<{
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
name: string
|
|
4
|
+
onBlur(): void
|
|
5
|
+
onChange(): void
|
|
6
|
+
onInput(): void
|
|
7
|
+
ref(element: HTMLElement | null): void
|
|
8
8
|
}>;
|
|
9
9
|
export type FieldDirective = (model: AbstractModel) => FieldDirectiveResult;
|
|
10
10
|
export type UseFormPartResult<M extends AbstractModel> = Readonly<{
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
defaultValue?: Value<M>
|
|
12
|
+
dirty: boolean
|
|
13
|
+
errors: readonly ValueError[]
|
|
14
|
+
invalid: boolean
|
|
15
|
+
model: M
|
|
16
|
+
name: string
|
|
17
|
+
field: FieldDirective
|
|
18
|
+
ownErrors: ReadonlyArray<ValueError<Value<M>>>
|
|
19
|
+
required: boolean
|
|
20
|
+
validators: ReadonlyArray<Validator<Value<M>>>
|
|
21
|
+
value?: Value<M>
|
|
22
|
+
visited: boolean
|
|
23
|
+
addValidator(validator: Validator<Value<M>>): void
|
|
24
|
+
setValidators(validators: ReadonlyArray<Validator<Value<M>>>): void
|
|
25
|
+
setValue(value: Value<M> | undefined): void
|
|
26
|
+
setVisited(visited: boolean): void
|
|
27
|
+
validate(): Promise<readonly ValueError[]>
|
|
28
28
|
}>;
|
|
29
|
-
export type UseFormResult<M extends AbstractModel> = Omit<UseFormPartResult<M>,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
export type UseFormResult<M extends AbstractModel> = Omit<UseFormPartResult<M>, "setValue" | "value"> & Readonly<{
|
|
30
|
+
value: Value<M>
|
|
31
|
+
submitting: boolean
|
|
32
|
+
setDefaultValue(value: Value<M>): void
|
|
33
|
+
setValue(value: Value<M>): void
|
|
34
|
+
submit(): Promise<Value<M> | undefined | void>
|
|
35
|
+
reset(): void
|
|
36
|
+
clear(): void
|
|
37
|
+
read(value: Value<M> | null | undefined): void
|
|
38
|
+
update(): void
|
|
39
39
|
}>;
|
|
40
|
-
export type UseFormArrayPartResult<M extends ArrayModel> = Omit<UseFormPartResult<M>,
|
|
41
|
-
|
|
40
|
+
export type UseFormArrayPartResult<M extends ArrayModel> = Omit<UseFormPartResult<M>, "field"> & {
|
|
41
|
+
items: ReadonlyArray<ArrayItemModel<M>>
|
|
42
42
|
};
|
|
43
43
|
export declare function useForm<M extends AbstractModel>(Model: DetachedModelConstructor<M>, config?: BinderConfiguration<Value<M>>): UseFormResult<M>;
|
|
44
44
|
export declare function useFormPart<M extends AbstractModel>(model: M): UseFormPartResult<M>;
|
|
45
45
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
* Hook to access an array model part of a form. It provides the same API as `useFormPart`,
|
|
47
|
+
* but adds an `items` property that allows to iterate over the items in form of an array of models.
|
|
48
|
+
*
|
|
49
|
+
* @param model - The array model to access
|
|
50
|
+
* @returns The array model part of the form
|
|
51
|
+
*/
|
|
52
52
|
export declare function useFormArrayPart<M extends ArrayModel>(model: M): UseFormArrayPartResult<M>;
|
|
53
|
-
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -1,202 +1,192 @@
|
|
|
1
|
-
|
|
2
|
-
vaadinObj.registrations ??= [];
|
|
3
|
-
vaadinObj.registrations.push({
|
|
4
|
-
is: feature ? `${"@vaadin/hilla-react-form"}/${feature}` : "@vaadin/hilla-react-form",
|
|
5
|
-
version: "24.7.0-alpha8"
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
import {
|
|
9
|
-
_fromString,
|
|
10
|
-
_validity,
|
|
11
|
-
BinderRoot,
|
|
12
|
-
CHANGED,
|
|
13
|
-
getBinderNode,
|
|
14
|
-
getDefaultFieldStrategy,
|
|
15
|
-
hasFromString,
|
|
16
|
-
isFieldElement
|
|
17
|
-
} from "@vaadin/hilla-lit-form";
|
|
1
|
+
import { _fromString, _validity, BinderRoot, CHANGED, getBinderNode, getDefaultFieldStrategy, hasFromString, isFieldElement } from "@vaadin/hilla-lit-form";
|
|
18
2
|
import { useEffect, useMemo, useReducer, useRef } from "react";
|
|
19
|
-
|
|
3
|
+
((feature, vaadinObj = window.Vaadin ??= {}) => {
|
|
4
|
+
vaadinObj.registrations ??= [];
|
|
5
|
+
vaadinObj.registrations.push({
|
|
6
|
+
is: feature ? `@vaadin/hilla-react-form/${feature}` : "@vaadin/hilla-react-form",
|
|
7
|
+
version: "24.7.0-beta1"
|
|
8
|
+
});
|
|
9
|
+
})();
|
|
20
10
|
let isRendering = false;
|
|
21
11
|
function useUpdate() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
const [_, count] = useReducer((x) => x + 1, 0);
|
|
13
|
+
return () => {
|
|
14
|
+
if (isRendering) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
count();
|
|
18
|
+
};
|
|
29
19
|
}
|
|
30
20
|
function convertFieldValue(model, fieldValue) {
|
|
31
|
-
|
|
21
|
+
return typeof fieldValue === "string" && hasFromString(model) ? model[_fromString](fieldValue) : fieldValue;
|
|
32
22
|
}
|
|
33
23
|
function getFormPart(node) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
24
|
+
return {
|
|
25
|
+
addValidator: node.addValidator.bind(node),
|
|
26
|
+
get defaultValue() {
|
|
27
|
+
return node.defaultValue;
|
|
28
|
+
},
|
|
29
|
+
dirty: node.dirty,
|
|
30
|
+
errors: node.errors,
|
|
31
|
+
invalid: node.invalid,
|
|
32
|
+
model: node.model,
|
|
33
|
+
name: node.name,
|
|
34
|
+
ownErrors: node.ownErrors,
|
|
35
|
+
required: node.required,
|
|
36
|
+
setValidators(validators) {
|
|
37
|
+
node.validators = validators;
|
|
38
|
+
},
|
|
39
|
+
setValue(value) {
|
|
40
|
+
node.value = value;
|
|
41
|
+
},
|
|
42
|
+
setVisited(visited) {
|
|
43
|
+
node.visited = visited;
|
|
44
|
+
},
|
|
45
|
+
validate: node.validate.bind(node),
|
|
46
|
+
validators: node.validators,
|
|
47
|
+
get value() {
|
|
48
|
+
return node.value;
|
|
49
|
+
},
|
|
50
|
+
visited: node.visited
|
|
51
|
+
};
|
|
62
52
|
}
|
|
63
53
|
function useFields(node) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
};
|
|
145
|
-
}, [node]);
|
|
54
|
+
const update = useUpdate();
|
|
55
|
+
return useMemo(() => {
|
|
56
|
+
const registry = new WeakMap();
|
|
57
|
+
return (model) => {
|
|
58
|
+
isRendering = true;
|
|
59
|
+
const n = getBinderNode(model);
|
|
60
|
+
let fieldState = registry.get(model);
|
|
61
|
+
if (!fieldState) {
|
|
62
|
+
fieldState = {
|
|
63
|
+
changeHandler() {
|
|
64
|
+
fieldState.inputHandler();
|
|
65
|
+
n.validate().catch(() => {});
|
|
66
|
+
},
|
|
67
|
+
element: undefined,
|
|
68
|
+
errorMessage: "",
|
|
69
|
+
inputHandler() {
|
|
70
|
+
if (fieldState.strategy) {
|
|
71
|
+
fieldState.strategy.invalid = false;
|
|
72
|
+
fieldState.strategy.checkValidity();
|
|
73
|
+
n[_validity] = fieldState.strategy.validity;
|
|
74
|
+
n.value = convertFieldValue(model, fieldState.strategy.value);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
invalid: false,
|
|
78
|
+
blurHandler() {
|
|
79
|
+
fieldState.inputHandler();
|
|
80
|
+
n.validate().catch(() => {});
|
|
81
|
+
n.visited = true;
|
|
82
|
+
},
|
|
83
|
+
ref(element) {
|
|
84
|
+
if (!element) {
|
|
85
|
+
fieldState.element?.removeEventListener("blur", fieldState.blurHandler);
|
|
86
|
+
fieldState.strategy?.removeEventListeners();
|
|
87
|
+
fieldState.element = undefined;
|
|
88
|
+
fieldState.strategy = undefined;
|
|
89
|
+
update();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!isFieldElement(element)) {
|
|
93
|
+
throw new TypeError(`Element '${element.localName}' is not a form element`);
|
|
94
|
+
}
|
|
95
|
+
if (fieldState.element !== element) {
|
|
96
|
+
fieldState.element = element;
|
|
97
|
+
fieldState.element.addEventListener("blur", fieldState.blurHandler);
|
|
98
|
+
fieldState.strategy = getDefaultFieldStrategy(element, model);
|
|
99
|
+
fieldState.strategy.onInput = fieldState.inputHandler;
|
|
100
|
+
fieldState.strategy.onChange = fieldState.changeHandler;
|
|
101
|
+
update();
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
required: false,
|
|
105
|
+
strategy: undefined
|
|
106
|
+
};
|
|
107
|
+
registry.set(model, fieldState);
|
|
108
|
+
}
|
|
109
|
+
if (fieldState.strategy) {
|
|
110
|
+
const valueFromField = convertFieldValue(model, fieldState.strategy.value);
|
|
111
|
+
if (valueFromField !== n.value && !(Number.isNaN(n.value) && Number.isNaN(valueFromField))) {
|
|
112
|
+
fieldState.strategy.value = Number.isNaN(n.value) ? "" : n.value;
|
|
113
|
+
}
|
|
114
|
+
if (fieldState.required !== n.required) {
|
|
115
|
+
fieldState.required = n.required;
|
|
116
|
+
fieldState.strategy.required = n.required;
|
|
117
|
+
}
|
|
118
|
+
const firstError = n.ownErrors.at(0);
|
|
119
|
+
const errorMessage = firstError?.message ?? "";
|
|
120
|
+
if (fieldState.errorMessage !== errorMessage) {
|
|
121
|
+
fieldState.errorMessage = errorMessage;
|
|
122
|
+
fieldState.strategy.errorMessage = errorMessage;
|
|
123
|
+
}
|
|
124
|
+
fieldState.invalid = n.invalid;
|
|
125
|
+
fieldState.strategy.invalid = n.invalid;
|
|
126
|
+
}
|
|
127
|
+
isRendering = false;
|
|
128
|
+
return {
|
|
129
|
+
name: n.name,
|
|
130
|
+
ref: fieldState.ref
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
}, [node]);
|
|
146
134
|
}
|
|
147
|
-
function useForm(Model, config) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
135
|
+
export function useForm(Model, config) {
|
|
136
|
+
const configRef = useRef({});
|
|
137
|
+
configRef.current.onSubmit = config?.onSubmit;
|
|
138
|
+
configRef.current.onChange = config?.onChange;
|
|
139
|
+
const update = useUpdate();
|
|
140
|
+
const binder = useMemo(() => new BinderRoot(Model, configRef.current), [Model]);
|
|
141
|
+
const field = useFields(binder);
|
|
142
|
+
const clear = binder.clear.bind(binder);
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
binder.addEventListener(CHANGED.type, update);
|
|
145
|
+
clear();
|
|
146
|
+
return () => binder.removeEventListener(CHANGED.type, update);
|
|
147
|
+
}, [binder]);
|
|
148
|
+
return {
|
|
149
|
+
...getFormPart(binder),
|
|
150
|
+
clear,
|
|
151
|
+
field,
|
|
152
|
+
read: binder.read.bind(binder),
|
|
153
|
+
reset: binder.reset.bind(binder),
|
|
154
|
+
setDefaultValue(defaultValue) {
|
|
155
|
+
binder.defaultValue = defaultValue;
|
|
156
|
+
},
|
|
157
|
+
setValue(value) {
|
|
158
|
+
binder.value = value;
|
|
159
|
+
},
|
|
160
|
+
submit: binder.submit.bind(binder),
|
|
161
|
+
value: binder.value,
|
|
162
|
+
submitting: binder.submitting,
|
|
163
|
+
update
|
|
164
|
+
};
|
|
177
165
|
}
|
|
178
|
-
function useFormPart(model) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
166
|
+
export function useFormPart(model) {
|
|
167
|
+
isRendering = true;
|
|
168
|
+
const binderNode = getBinderNode(model);
|
|
169
|
+
const field = useFields(binderNode);
|
|
170
|
+
isRendering = false;
|
|
171
|
+
return {
|
|
172
|
+
...getFormPart(binderNode),
|
|
173
|
+
field
|
|
174
|
+
};
|
|
187
175
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Hook to access an array model part of a form. It provides the same API as `useFormPart`,
|
|
178
|
+
* but adds an `items` property that allows to iterate over the items in form of an array of models.
|
|
179
|
+
*
|
|
180
|
+
* @param model - The array model to access
|
|
181
|
+
* @returns The array model part of the form
|
|
182
|
+
*/
|
|
183
|
+
export function useFormArrayPart(model) {
|
|
184
|
+
isRendering = true;
|
|
185
|
+
const binderNode = getBinderNode(model);
|
|
186
|
+
isRendering = false;
|
|
187
|
+
return {
|
|
188
|
+
...getFormPart(binderNode),
|
|
189
|
+
items: Array.from(model, (item) => item.model)
|
|
190
|
+
};
|
|
196
191
|
}
|
|
197
|
-
|
|
198
|
-
useForm,
|
|
199
|
-
useFormArrayPart,
|
|
200
|
-
useFormPart
|
|
201
|
-
};
|
|
202
|
-
//# sourceMappingURL=index.js.map
|
|
192
|
+
//# sourceMappingURL=./index.js.map
|
package/index.js.map
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../scripts/register.js", "src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export function __REGISTER__(feature, vaadinObj = (window.Vaadin ??= {})) {\n vaadinObj.registrations ??= [];\n vaadinObj.registrations.push({\n is: feature ? `${__NAME__}/${feature}` : __NAME__,\n version: __VERSION__,\n });\n}\n", "/* eslint-disable @typescript-eslint/unbound-method */\nimport {\n _fromString,\n _validity,\n type AbstractModel,\n type BinderConfiguration,\n type BinderNode,\n BinderRoot,\n CHANGED,\n type DetachedModelConstructor,\n type FieldStrategy,\n getBinderNode,\n getDefaultFieldStrategy,\n hasFromString,\n isFieldElement,\n type Validator,\n type Value,\n type ValueError,\n type ArrayModel,\n type ArrayItemModel,\n} from '@vaadin/hilla-lit-form';\nimport { useEffect, useMemo, useReducer, useRef } from 'react';\nimport type { Writable } from 'type-fest';\n\n// @ts-expect-error: esbuild injection\n// eslint-disable-next-line @typescript-eslint/no-unsafe-call\n__REGISTER__();\n\nlet isRendering = false;\n\nfunction useUpdate() {\n const [_, count] = useReducer((x: number) => x + 1, 0);\n return () => {\n if (isRendering) {\n return;\n }\n count();\n };\n}\n\nexport type FieldDirectiveResult = Readonly<{\n name: string;\n onBlur(): void;\n onChange(): void;\n onInput(): void;\n ref(element: HTMLElement | null): void;\n}>;\n\nexport type FieldDirective = (model: AbstractModel) => FieldDirectiveResult;\n\nexport type UseFormPartResult<M extends AbstractModel> = Readonly<{\n defaultValue?: Value<M>;\n dirty: boolean;\n errors: readonly ValueError[];\n invalid: boolean;\n model: M;\n name: string;\n field: FieldDirective;\n ownErrors: ReadonlyArray<ValueError<Value<M>>>;\n required: boolean;\n validators: ReadonlyArray<Validator<Value<M>>>;\n value?: Value<M>;\n visited: boolean;\n addValidator(validator: Validator<Value<M>>): void;\n setValidators(validators: ReadonlyArray<Validator<Value<M>>>): void;\n setValue(value: Value<M> | undefined): void;\n setVisited(visited: boolean): void;\n validate(): Promise<readonly ValueError[]>;\n}>;\n\nexport type UseFormResult<M extends AbstractModel> = Omit<UseFormPartResult<M>, 'setValue' | 'value'> &\n Readonly<{\n value: Value<M>;\n submitting: boolean;\n setDefaultValue(value: Value<M>): void;\n setValue(value: Value<M>): void;\n submit(): Promise<Value<M> | undefined | void>;\n reset(): void;\n clear(): void;\n read(value: Value<M> | null | undefined): void;\n update(): void;\n }>;\n\nexport type UseFormArrayPartResult<M extends ArrayModel> = Omit<UseFormPartResult<M>, 'field'> & {\n items: ReadonlyArray<ArrayItemModel<M>>;\n};\n\ntype FieldState<T = unknown> = {\n required: boolean;\n invalid: boolean;\n errorMessage: string;\n strategy?: FieldStrategy<T>;\n element?: HTMLElement;\n inputHandler(): void;\n changeHandler(): void;\n blurHandler(): void;\n ref(element: HTMLElement | null): void;\n};\n\nfunction convertFieldValue<T extends AbstractModel>(model: T, fieldValue: unknown) {\n return typeof fieldValue === 'string' && hasFromString(model) ? model[_fromString](fieldValue) : fieldValue;\n}\n\nfunction getFormPart<M extends AbstractModel>(node: BinderNode<M>): Omit<UseFormPartResult<M>, 'field'> {\n return {\n addValidator: node.addValidator.bind(node),\n get defaultValue() {\n return node.defaultValue;\n },\n dirty: node.dirty,\n errors: node.errors,\n invalid: node.invalid,\n model: node.model,\n name: node.name,\n ownErrors: node.ownErrors,\n required: node.required,\n setValidators(validators) {\n node.validators = validators;\n },\n setValue(value) {\n node.value = value;\n },\n setVisited(visited: boolean) {\n node.visited = visited;\n },\n validate: node.validate.bind(node),\n validators: node.validators,\n get value() {\n return node.value;\n },\n visited: node.visited,\n };\n}\n\nfunction useFields<M extends AbstractModel>(node: BinderNode<M>): FieldDirective {\n const update = useUpdate();\n\n return useMemo(() => {\n const registry = new WeakMap<AbstractModel, FieldState>();\n\n return ((model: AbstractModel) => {\n isRendering = true;\n const n = getBinderNode(model);\n\n let fieldState = registry.get(model);\n\n if (!fieldState) {\n fieldState = {\n changeHandler() {\n fieldState!.inputHandler();\n n.validate().catch(() => {});\n },\n element: undefined,\n errorMessage: '',\n inputHandler() {\n if (fieldState!.strategy) {\n // Remove invalid flag, so that .checkValidity() in Vaadin Components\n // does not interfere with errors from Hilla.\n fieldState!.strategy.invalid = false;\n // When bad input is detected, skip reading new value in binder state\n fieldState!.strategy.checkValidity();\n n[_validity] = fieldState!.strategy.validity;\n n.value = convertFieldValue(model, fieldState!.strategy.value);\n }\n },\n invalid: false,\n blurHandler() {\n fieldState!.inputHandler();\n n.validate().catch(() => {});\n n.visited = true;\n },\n ref(element: HTMLElement | null) {\n if (!element) {\n fieldState!.element?.removeEventListener('blur', fieldState!.blurHandler);\n fieldState!.strategy?.removeEventListeners();\n fieldState!.element = undefined;\n fieldState!.strategy = undefined;\n update();\n return;\n }\n\n if (!isFieldElement(element)) {\n throw new TypeError(`Element '${element.localName}' is not a form element`);\n }\n\n if (fieldState!.element !== element) {\n fieldState!.element = element;\n fieldState!.element.addEventListener('blur', fieldState!.blurHandler);\n fieldState!.strategy = getDefaultFieldStrategy(element, model);\n fieldState!.strategy.onInput = fieldState!.inputHandler;\n fieldState!.strategy.onChange = fieldState!.changeHandler;\n update();\n }\n },\n required: false,\n strategy: undefined,\n };\n\n registry.set(model, fieldState);\n }\n\n if (fieldState.strategy) {\n const valueFromField = convertFieldValue(model, fieldState.strategy.value);\n if (valueFromField !== n.value && !(Number.isNaN(n.value) && Number.isNaN(valueFromField))) {\n fieldState.strategy.value = Number.isNaN(n.value) ? '' : n.value;\n }\n\n if (fieldState.required !== n.required) {\n fieldState.required = n.required;\n fieldState.strategy.required = n.required;\n }\n\n const firstError = n.ownErrors.at(0);\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const errorMessage = firstError?.message ?? '';\n if (fieldState.errorMessage !== errorMessage) {\n fieldState.errorMessage = errorMessage;\n fieldState.strategy.errorMessage = errorMessage;\n }\n\n // Make sure invalid state is always in sync\n fieldState.invalid = n.invalid;\n fieldState.strategy.invalid = n.invalid;\n }\n\n isRendering = false;\n return {\n name: n.name,\n ref: fieldState.ref,\n };\n }) as FieldDirective;\n }, [node]);\n}\n\nexport function useForm<M extends AbstractModel>(\n Model: DetachedModelConstructor<M>,\n config?: BinderConfiguration<Value<M>>,\n): UseFormResult<M> {\n const configRef = useRef<Writable<BinderConfiguration<Value<M>>>>({});\n configRef.current.onSubmit = config?.onSubmit;\n configRef.current.onChange = config?.onChange;\n const update = useUpdate();\n const binder = useMemo(() => new BinderRoot(Model, configRef.current), [Model]);\n const field = useFields(binder);\n const clear = binder.clear.bind(binder);\n\n useEffect(() => {\n binder.addEventListener(CHANGED.type, update);\n clear(); // this allows to initialize the validation strategies (issue 2282)\n return () => binder.removeEventListener(CHANGED.type, update);\n }, [binder]);\n\n return {\n ...getFormPart(binder),\n clear,\n field,\n read: binder.read.bind(binder),\n reset: binder.reset.bind(binder),\n setDefaultValue(defaultValue) {\n binder.defaultValue = defaultValue;\n },\n setValue(value) {\n binder.value = value;\n },\n submit: binder.submit.bind(binder),\n value: binder.value,\n submitting: binder.submitting,\n update,\n };\n}\n\nexport function useFormPart<M extends AbstractModel>(model: M): UseFormPartResult<M> {\n isRendering = true;\n const binderNode = getBinderNode(model);\n const field = useFields(binderNode);\n isRendering = false;\n\n return {\n ...getFormPart(binderNode),\n field,\n };\n}\n\n/**\n * Hook to access an array model part of a form. It provides the same API as `useFormPart`,\n * but adds an `items` property that allows to iterate over the items in form of an array of models.\n *\n * @param model - The array model to access\n * @returns The array model part of the form\n */\nexport function useFormArrayPart<M extends ArrayModel>(model: M): UseFormArrayPartResult<M> {\n isRendering = true;\n const binderNode = getBinderNode(model);\n isRendering = false;\n return {\n ...getFormPart(binderNode),\n items: Array.from(model, (item) => item.model as ArrayItemModel<M>),\n };\n}\n"],
|
|
5
|
-
"mappings": "AAAO,SAAS,aAAa,SAAS,YAAa,OAAO,WAAW,CAAC,GAAI;AACxE,YAAU,kBAAkB,CAAC;AAC7B,YAAU,cAAc,KAAK;AAAA,IAC3B,IAAI,UAAU,GAAG,0BAAQ,IAAI,OAAO,KAAK;AAAA,IACzC,SAAS;AAAA,EACX,CAAC;AACH;ACLA;AAAA,EACE;AAAA,EACA;AAAA,EAIA;AAAA,EACA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAMK;AACP,SAAS,WAAW,SAAS,YAAY,cAAc;AAKvD,aAAa;AAEb,IAAI,cAAc;AAElB,SAAS,YAAY;AACnB,QAAM,CAAC,GAAG,KAAK,IAAI,WAAW,CAAC,MAAc,IAAI,GAAG,CAAC;AACrD,SAAO,MAAM;AACX,QAAI,aAAa;AACf;AAAA,IACF;AACA,UAAM;AAAA,EACR;AACF;AA6DA,SAAS,kBAA2C,OAAU,YAAqB;AACjF,SAAO,OAAO,eAAe,YAAY,cAAc,KAAK,IAAI,MAAM,WAAW,EAAE,UAAU,IAAI;AACnG;AAEA,SAAS,YAAqC,MAA0D;AACtG,SAAO;AAAA,IACL,cAAc,KAAK,aAAa,KAAK,IAAI;AAAA,IACzC,IAAI,eAAe;AACjB,aAAO,KAAK;AAAA,IACd;AAAA,IACA,OAAO,KAAK;AAAA,IACZ,QAAQ,KAAK;AAAA,IACb,SAAS,KAAK;AAAA,IACd,OAAO,KAAK;AAAA,IACZ,MAAM,KAAK;AAAA,IACX,WAAW,KAAK;AAAA,IAChB,UAAU,KAAK;AAAA,IACf,cAAc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,cAAc,MAAM;CACvC,MAAM,QAAQ,UAAU,WAAW;AACnC,eAAc;AAEd,QAAO;EACL,GAAG,YAAY,WAAW;EAC1B;CACD;AACF;;;;;;;;AASD,OAAO,SAAS,iBAAuCA,OAAqC;AAC1F,eAAc;CACd,MAAM,aAAa,cAAc,MAAM;AACvC,eAAc;AACd,QAAO;EACL,GAAG,YAAY,WAAW;EAC1B,OAAO,MAAM,KAAK,OAAO,CAAC,SAAS,KAAK,MAA2B;CACpE;AACF","names":["x: number","model: T","fieldValue: unknown","node: BinderNode<M>","visited: boolean","model: AbstractModel","element: HTMLElement | null","Model: DetachedModelConstructor<M>","config?: BinderConfiguration<Value<M>>","model: M"],"sources":["/opt/agent/work/1af72d8adc613024/hilla/packages/ts/react-form/src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/unbound-method */\nimport {\n _fromString,\n _validity,\n type AbstractModel,\n type BinderConfiguration,\n type BinderNode,\n BinderRoot,\n CHANGED,\n type DetachedModelConstructor,\n type FieldStrategy,\n getBinderNode,\n getDefaultFieldStrategy,\n hasFromString,\n isFieldElement,\n type Validator,\n type Value,\n type ValueError,\n type ArrayModel,\n type ArrayItemModel,\n} from '@vaadin/hilla-lit-form';\nimport { useEffect, useMemo, useReducer, useRef } from 'react';\nimport type { Writable } from 'type-fest';\n\n// @ts-expect-error: esbuild injection\n// eslint-disable-next-line @typescript-eslint/no-unsafe-call\n((feature, vaadinObj = (window.Vaadin ??= {})) => {\n vaadinObj.registrations ??= [];\n vaadinObj.registrations.push({\n is: feature ? `@vaadin/hilla-react-form/${feature}` : '@vaadin/hilla-react-form',\n version: '24.7.0-beta1',\n });\n})();\n\nlet isRendering = false;\n\nfunction useUpdate() {\n const [_, count] = useReducer((x: number) => x + 1, 0);\n return () => {\n if (isRendering) {\n return;\n }\n count();\n };\n}\n\nexport type FieldDirectiveResult = Readonly<{\n name: string;\n onBlur(): void;\n onChange(): void;\n onInput(): void;\n ref(element: HTMLElement | null): void;\n}>;\n\nexport type FieldDirective = (model: AbstractModel) => FieldDirectiveResult;\n\nexport type UseFormPartResult<M extends AbstractModel> = Readonly<{\n defaultValue?: Value<M>;\n dirty: boolean;\n errors: readonly ValueError[];\n invalid: boolean;\n model: M;\n name: string;\n field: FieldDirective;\n ownErrors: ReadonlyArray<ValueError<Value<M>>>;\n required: boolean;\n validators: ReadonlyArray<Validator<Value<M>>>;\n value?: Value<M>;\n visited: boolean;\n addValidator(validator: Validator<Value<M>>): void;\n setValidators(validators: ReadonlyArray<Validator<Value<M>>>): void;\n setValue(value: Value<M> | undefined): void;\n setVisited(visited: boolean): void;\n validate(): Promise<readonly ValueError[]>;\n}>;\n\nexport type UseFormResult<M extends AbstractModel> = Omit<UseFormPartResult<M>, 'setValue' | 'value'> &\n Readonly<{\n value: Value<M>;\n submitting: boolean;\n setDefaultValue(value: Value<M>): void;\n setValue(value: Value<M>): void;\n submit(): Promise<Value<M> | undefined | void>;\n reset(): void;\n clear(): void;\n read(value: Value<M> | null | undefined): void;\n update(): void;\n }>;\n\nexport type UseFormArrayPartResult<M extends ArrayModel> = Omit<UseFormPartResult<M>, 'field'> & {\n items: ReadonlyArray<ArrayItemModel<M>>;\n};\n\ntype FieldState<T = unknown> = {\n required: boolean;\n invalid: boolean;\n errorMessage: string;\n strategy?: FieldStrategy<T>;\n element?: HTMLElement;\n inputHandler(): void;\n changeHandler(): void;\n blurHandler(): void;\n ref(element: HTMLElement | null): void;\n};\n\nfunction convertFieldValue<T extends AbstractModel>(model: T, fieldValue: unknown) {\n return typeof fieldValue === 'string' && hasFromString(model) ? model[_fromString](fieldValue) : fieldValue;\n}\n\nfunction getFormPart<M extends AbstractModel>(node: BinderNode<M>): Omit<UseFormPartResult<M>, 'field'> {\n return {\n addValidator: node.addValidator.bind(node),\n get defaultValue() {\n return node.defaultValue;\n },\n dirty: node.dirty,\n errors: node.errors,\n invalid: node.invalid,\n model: node.model,\n name: node.name,\n ownErrors: node.ownErrors,\n required: node.required,\n setValidators(validators) {\n node.validators = validators;\n },\n setValue(value) {\n node.value = value;\n },\n setVisited(visited: boolean) {\n node.visited = visited;\n },\n validate: node.validate.bind(node),\n validators: node.validators,\n get value() {\n return node.value;\n },\n visited: node.visited,\n };\n}\n\nfunction useFields<M extends AbstractModel>(node: BinderNode<M>): FieldDirective {\n const update = useUpdate();\n\n return useMemo(() => {\n const registry = new WeakMap<AbstractModel, FieldState>();\n\n return ((model: AbstractModel) => {\n isRendering = true;\n const n = getBinderNode(model);\n\n let fieldState = registry.get(model);\n\n if (!fieldState) {\n fieldState = {\n changeHandler() {\n fieldState!.inputHandler();\n n.validate().catch(() => {});\n },\n element: undefined,\n errorMessage: '',\n inputHandler() {\n if (fieldState!.strategy) {\n // Remove invalid flag, so that .checkValidity() in Vaadin Components\n // does not interfere with errors from Hilla.\n fieldState!.strategy.invalid = false;\n // When bad input is detected, skip reading new value in binder state\n fieldState!.strategy.checkValidity();\n n[_validity] = fieldState!.strategy.validity;\n n.value = convertFieldValue(model, fieldState!.strategy.value);\n }\n },\n invalid: false,\n blurHandler() {\n fieldState!.inputHandler();\n n.validate().catch(() => {});\n n.visited = true;\n },\n ref(element: HTMLElement | null) {\n if (!element) {\n fieldState!.element?.removeEventListener('blur', fieldState!.blurHandler);\n fieldState!.strategy?.removeEventListeners();\n fieldState!.element = undefined;\n fieldState!.strategy = undefined;\n update();\n return;\n }\n\n if (!isFieldElement(element)) {\n throw new TypeError(`Element '${element.localName}' is not a form element`);\n }\n\n if (fieldState!.element !== element) {\n fieldState!.element = element;\n fieldState!.element.addEventListener('blur', fieldState!.blurHandler);\n fieldState!.strategy = getDefaultFieldStrategy(element, model);\n fieldState!.strategy.onInput = fieldState!.inputHandler;\n fieldState!.strategy.onChange = fieldState!.changeHandler;\n update();\n }\n },\n required: false,\n strategy: undefined,\n };\n\n registry.set(model, fieldState);\n }\n\n if (fieldState.strategy) {\n const valueFromField = convertFieldValue(model, fieldState.strategy.value);\n if (valueFromField !== n.value && !(Number.isNaN(n.value) && Number.isNaN(valueFromField))) {\n fieldState.strategy.value = Number.isNaN(n.value) ? '' : n.value;\n }\n\n if (fieldState.required !== n.required) {\n fieldState.required = n.required;\n fieldState.strategy.required = n.required;\n }\n\n const firstError = n.ownErrors.at(0);\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const errorMessage = firstError?.message ?? '';\n if (fieldState.errorMessage !== errorMessage) {\n fieldState.errorMessage = errorMessage;\n fieldState.strategy.errorMessage = errorMessage;\n }\n\n // Make sure invalid state is always in sync\n fieldState.invalid = n.invalid;\n fieldState.strategy.invalid = n.invalid;\n }\n\n isRendering = false;\n return {\n name: n.name,\n ref: fieldState.ref,\n };\n }) as FieldDirective;\n }, [node]);\n}\n\nexport function useForm<M extends AbstractModel>(\n Model: DetachedModelConstructor<M>,\n config?: BinderConfiguration<Value<M>>,\n): UseFormResult<M> {\n const configRef = useRef<Writable<BinderConfiguration<Value<M>>>>({});\n configRef.current.onSubmit = config?.onSubmit;\n configRef.current.onChange = config?.onChange;\n const update = useUpdate();\n const binder = useMemo(() => new BinderRoot(Model, configRef.current), [Model]);\n const field = useFields(binder);\n const clear = binder.clear.bind(binder);\n\n useEffect(() => {\n binder.addEventListener(CHANGED.type, update);\n clear(); // this allows to initialize the validation strategies (issue 2282)\n return () => binder.removeEventListener(CHANGED.type, update);\n }, [binder]);\n\n return {\n ...getFormPart<M>(binder),\n clear,\n field,\n read: binder.read.bind(binder),\n reset: binder.reset.bind(binder),\n setDefaultValue(defaultValue) {\n binder.defaultValue = defaultValue;\n },\n setValue(value) {\n binder.value = value;\n },\n submit: binder.submit.bind(binder),\n value: binder.value,\n submitting: binder.submitting,\n update,\n };\n}\n\nexport function useFormPart<M extends AbstractModel>(model: M): UseFormPartResult<M> {\n isRendering = true;\n const binderNode = getBinderNode(model);\n const field = useFields(binderNode);\n isRendering = false;\n\n return {\n ...getFormPart(binderNode),\n field,\n };\n}\n\n/**\n * Hook to access an array model part of a form. It provides the same API as `useFormPart`,\n * but adds an `items` property that allows to iterate over the items in form of an array of models.\n *\n * @param model - The array model to access\n * @returns The array model part of the form\n */\nexport function useFormArrayPart<M extends ArrayModel>(model: M): UseFormArrayPartResult<M> {\n isRendering = true;\n const binderNode = getBinderNode(model);\n isRendering = false;\n return {\n ...getFormPart(binderNode),\n items: Array.from(model, (item) => item.model as ArrayItemModel<M>),\n };\n}\n"],"version":3}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/hilla-react-form",
|
|
3
|
-
"version": "24.7.0-
|
|
3
|
+
"version": "24.7.0-beta1",
|
|
4
4
|
"description": "Hilla form utils for React",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -17,15 +17,12 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"clean:build": "git clean -fx . -e .vite -e node_modules",
|
|
20
|
-
"build": "
|
|
21
|
-
"build:esbuild": "tsx ../../../scripts/build.ts",
|
|
22
|
-
"build:dts": "tsc --isolatedModules -p tsconfig.build.json",
|
|
23
|
-
"build:copy": "cd src && copyfiles **/*.d.ts ..",
|
|
20
|
+
"build": "tsx ../../../scripts/fast-build.ts",
|
|
24
21
|
"lint": "eslint src test",
|
|
25
22
|
"lint:fix": "eslint src test --fix",
|
|
26
|
-
"test": "
|
|
27
|
-
"test:coverage": "
|
|
28
|
-
"test:watch": "
|
|
23
|
+
"test": "vitest --run",
|
|
24
|
+
"test:coverage": "vitest --run --coverage",
|
|
25
|
+
"test:watch": "vitest",
|
|
29
26
|
"typecheck": "tsc --noEmit"
|
|
30
27
|
},
|
|
31
28
|
"exports": {
|
|
@@ -46,30 +43,10 @@
|
|
|
46
43
|
"access": "public"
|
|
47
44
|
},
|
|
48
45
|
"dependencies": {
|
|
49
|
-
"@vaadin/hilla-lit-form": "24.7.0-
|
|
46
|
+
"@vaadin/hilla-lit-form": "24.7.0-beta1"
|
|
50
47
|
},
|
|
51
48
|
"peerDependencies": {
|
|
52
49
|
"react": "18 || 19",
|
|
53
50
|
"react-dom": "18 || 19"
|
|
54
|
-
},
|
|
55
|
-
"devDependencies": {
|
|
56
|
-
"@testing-library/dom": "^10.4.0",
|
|
57
|
-
"@testing-library/react": "^16.1.0",
|
|
58
|
-
"@testing-library/user-event": "^14.5.2",
|
|
59
|
-
"@types/chai": "^4.3.20",
|
|
60
|
-
"@types/chai-as-promised": "^7.1.8",
|
|
61
|
-
"@types/chai-dom": "^1.11.3",
|
|
62
|
-
"@types/mocha": "^10.0.10",
|
|
63
|
-
"@types/react": "^18.3.18",
|
|
64
|
-
"@types/react-dom": "^18",
|
|
65
|
-
"@types/sinon": "^10.0.20",
|
|
66
|
-
"@types/sinon-chai": "^3.2.12",
|
|
67
|
-
"@types/validator": "^13.12.2",
|
|
68
|
-
"chai": "^5.1.2",
|
|
69
|
-
"chai-as-promised": "^7.1.2",
|
|
70
|
-
"chai-dom": "^1.12.0",
|
|
71
|
-
"sinon": "^16.1.3",
|
|
72
|
-
"sinon-chai": "^3.7.0",
|
|
73
|
-
"typescript": "5.7.3"
|
|
74
51
|
}
|
|
75
52
|
}
|
package/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,mBAAmB,EAIxB,KAAK,wBAAwB,EAM7B,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,wBAAwB,CAAC;AAoBhC,MAAM,MAAM,oBAAoB,GAAG,QAAQ,CAAC;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,IAAI,IAAI,CAAC;IACf,QAAQ,IAAI,IAAI,CAAC;IACjB,OAAO,IAAI,IAAI,CAAC;IAChB,GAAG,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,GAAG,IAA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