@metaobjectsdev/react 0.5.0-rc.1
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 +189 -0
- package/README.md +13 -0
- package/dist/currency-input.d.ts +29 -0
- package/dist/currency-input.d.ts.map +1 -0
- package/dist/currency-input.js +66 -0
- package/dist/currency-input.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/use-entity-form.d.ts +49 -0
- package/dist/use-entity-form.d.ts.map +1 -0
- package/dist/use-entity-form.js +70 -0
- package/dist/use-entity-form.js.map +1 -0
- package/package.json +72 -0
- package/src/currency-input.tsx +103 -0
- package/src/index.ts +11 -0
- package/src/use-entity-form.tsx +138 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of recording and
|
|
55
|
+
discussing modifications to the Work, but excluding communication that
|
|
56
|
+
is conspicuously marked or designated in writing by the copyright owner
|
|
57
|
+
as "Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under
|
|
83
|
+
this License for that Work shall terminate as of the date such
|
|
84
|
+
litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
You may add Your own license statement for Your modifications and
|
|
119
|
+
may provide additional grant of rights to use, reproduce, modify,
|
|
120
|
+
prepare derivative works of, distribute, and sublicense such modifications,
|
|
121
|
+
as an additional requirement of this License.
|
|
122
|
+
|
|
123
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
124
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
125
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
126
|
+
this License, without any additional terms or conditions.
|
|
127
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
128
|
+
the terms of any separate license agreement you may have executed
|
|
129
|
+
with Licensor regarding such Contributions.
|
|
130
|
+
|
|
131
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
132
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
133
|
+
except as required for reasonable and customary use in describing the
|
|
134
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
135
|
+
|
|
136
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
137
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
138
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
139
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
140
|
+
implied, including, without limitation, any conditions of TITLE,
|
|
141
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
142
|
+
PURPOSE. You are solely responsible for determining the
|
|
143
|
+
appropriateness of using or reproducing the Work and assume any
|
|
144
|
+
risks associated with Your exercise of permissions under this License.
|
|
145
|
+
|
|
146
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
147
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
148
|
+
unless required by applicable law (such as deliberate and grossly
|
|
149
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
150
|
+
liable to You for damages, including any direct, indirect, special,
|
|
151
|
+
incidental, or exemplary damages of any character arising as a
|
|
152
|
+
result of this License or out of the use or inability to use the
|
|
153
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
154
|
+
work stoppage, computer failure or malfunction, or all other
|
|
155
|
+
commercial damages or losses), even if such Contributor has been
|
|
156
|
+
advised of the possibility of such damages.
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
159
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
160
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
161
|
+
or other liability obligations and/or rights consistent with this
|
|
162
|
+
License. However, in accepting such obligations, You may offer
|
|
163
|
+
only obligations consistent to this License provided that the
|
|
164
|
+
obligations are consistent with this License.
|
|
165
|
+
|
|
166
|
+
END OF TERMS AND CONDITIONS
|
|
167
|
+
|
|
168
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
169
|
+
|
|
170
|
+
To apply the Apache License to your work, attach the following
|
|
171
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
172
|
+
replaced with your own identifying information. (Don't include
|
|
173
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
174
|
+
comment syntax for the file format in question. Also add information
|
|
175
|
+
on how to contact you electronically and/or by mail.
|
|
176
|
+
|
|
177
|
+
Copyright 2026 MetaObjects Contributors
|
|
178
|
+
|
|
179
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
180
|
+
you may not use this file except in compliance with the License.
|
|
181
|
+
You may obtain a copy of the License at
|
|
182
|
+
|
|
183
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
184
|
+
|
|
185
|
+
Unless required by applicable law or agreed to in writing, software
|
|
186
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
187
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
188
|
+
See the License for the specific language governing permissions and
|
|
189
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @metaobjectsdev/react
|
|
2
|
+
|
|
3
|
+
React runtime for metaobjects: `useEntityForm` hook (react-hook-form integration) and `<CurrencyInput>` component. Pairs with `@metaobjectsdev/codegen-ts-react`, which generates `<Entity>.form.tsx` files that import from this package.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @metaobjectsdev/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## License
|
|
12
|
+
|
|
13
|
+
Apache-2.0.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
export interface CurrencyInputProps {
|
|
3
|
+
/** Value in integer minor units (cents for USD). */
|
|
4
|
+
value: number;
|
|
5
|
+
/** Called with new value in integer minor units after blur. */
|
|
6
|
+
onChange: (cents: number) => void;
|
|
7
|
+
/** ISO 4217 currency code. Defaults to "USD". */
|
|
8
|
+
currency?: string;
|
|
9
|
+
/** BCP 47 locale code. Defaults to "en-US". */
|
|
10
|
+
locale?: string;
|
|
11
|
+
className?: string;
|
|
12
|
+
placeholder?: string;
|
|
13
|
+
required?: boolean;
|
|
14
|
+
id?: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Bidirectional currency input. Displays formatted dollars; captures
|
|
19
|
+
* dollars from the user; emits cents to onChange on blur.
|
|
20
|
+
*
|
|
21
|
+
* - Focus strips symbol + grouping for easier editing
|
|
22
|
+
* - Blur re-formats + emits cents
|
|
23
|
+
* - Empty input + !required → emits 0
|
|
24
|
+
* - Empty input + required → reverts to last valid; no onChange
|
|
25
|
+
* - Unparseable input on blur → reverts to last valid format; no onChange
|
|
26
|
+
* - Float drift avoided via Math.round in parseCurrency
|
|
27
|
+
*/
|
|
28
|
+
export declare function CurrencyInput(props: CurrencyInputProps): ReactElement;
|
|
29
|
+
//# sourceMappingURL=currency-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency-input.d.ts","sourceRoot":"","sources":["../src/currency-input.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAA2B,MAAM,OAAO,CAAC;AAGnE,MAAM,WAAW,kBAAkB;IACjC,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAC;IACd,+DAA+D;IAC/D,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,YAAY,CAuErE"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useEffect, useRef } from "react";
|
|
3
|
+
import { formatCurrency, parseCurrency, minorUnitsFor } from "@metaobjectsdev/runtime-web";
|
|
4
|
+
/**
|
|
5
|
+
* Bidirectional currency input. Displays formatted dollars; captures
|
|
6
|
+
* dollars from the user; emits cents to onChange on blur.
|
|
7
|
+
*
|
|
8
|
+
* - Focus strips symbol + grouping for easier editing
|
|
9
|
+
* - Blur re-formats + emits cents
|
|
10
|
+
* - Empty input + !required → emits 0
|
|
11
|
+
* - Empty input + required → reverts to last valid; no onChange
|
|
12
|
+
* - Unparseable input on blur → reverts to last valid format; no onChange
|
|
13
|
+
* - Float drift avoided via Math.round in parseCurrency
|
|
14
|
+
*/
|
|
15
|
+
export function CurrencyInput(props) {
|
|
16
|
+
const currency = props.currency ?? "USD";
|
|
17
|
+
const locale = props.locale ?? "en-US";
|
|
18
|
+
const [text, setText] = useState(() => formatCurrency(props.value, currency, locale));
|
|
19
|
+
const [isInvalid, setIsInvalid] = useState(false);
|
|
20
|
+
const lastValidValue = useRef(props.value);
|
|
21
|
+
// Re-format when external value changes (controlled component sync)
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (props.value !== lastValidValue.current) {
|
|
24
|
+
setText(formatCurrency(props.value, currency, locale));
|
|
25
|
+
lastValidValue.current = props.value;
|
|
26
|
+
}
|
|
27
|
+
}, [props.value, currency, locale]);
|
|
28
|
+
function handleFocus(_e) {
|
|
29
|
+
setIsInvalid(false);
|
|
30
|
+
// Strip symbol + grouping so user can edit the raw number
|
|
31
|
+
const minorUnits = minorUnitsFor(currency, locale);
|
|
32
|
+
const factor = Math.pow(10, minorUnits);
|
|
33
|
+
const dollars = (props.value / factor).toFixed(minorUnits);
|
|
34
|
+
setText(dollars);
|
|
35
|
+
}
|
|
36
|
+
function handleBlur(_e) {
|
|
37
|
+
const trimmed = text.trim();
|
|
38
|
+
if (trimmed === "") {
|
|
39
|
+
if (props.required) {
|
|
40
|
+
setIsInvalid(true);
|
|
41
|
+
setText(formatCurrency(props.value, currency, locale));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
setIsInvalid(false);
|
|
45
|
+
props.onChange(0);
|
|
46
|
+
lastValidValue.current = 0;
|
|
47
|
+
setText(formatCurrency(0, currency, locale));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const parsed = parseCurrency(trimmed, currency, locale);
|
|
51
|
+
if (parsed === null) {
|
|
52
|
+
// Unparseable — revert to last valid
|
|
53
|
+
setText(formatCurrency(lastValidValue.current, currency, locale));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
setIsInvalid(false);
|
|
57
|
+
props.onChange(parsed);
|
|
58
|
+
lastValidValue.current = parsed;
|
|
59
|
+
setText(formatCurrency(parsed, currency, locale));
|
|
60
|
+
}
|
|
61
|
+
function handleChange(e) {
|
|
62
|
+
setText(e.target.value);
|
|
63
|
+
}
|
|
64
|
+
return (_jsx("input", { type: "text", inputMode: "decimal", value: text, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur, className: props.className, placeholder: props.placeholder, required: props.required, id: props.id, name: props.name, "data-invalid": isInvalid ? "true" : undefined }));
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=currency-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency-input.js","sourceRoot":"","sources":["../src/currency-input.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAkB3F;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC;IACzC,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,OAAO,CAAC;IAEvC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,EAA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AAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { useEntityForm, type EntityFieldMeta, type EntityMeta, type BoundInputProps, type InputAccessor, type UseEntityFormOptions, type UseEntityFormReturn, } from "./use-entity-form.js";
|
|
2
|
+
export { CurrencyInput, type CurrencyInputProps } from "./currency-input.js";
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,OAAO,EACL,aAAa,GAOd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,aAAa,EAA2B,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type FieldValues, type DefaultValues, type RegisterOptions, type UseFormReturn } from "react-hook-form";
|
|
2
|
+
import type { ZodTypeAny } from "zod";
|
|
3
|
+
export interface EntityFieldMeta {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly label: string;
|
|
6
|
+
readonly view: string;
|
|
7
|
+
readonly htmlType?: string;
|
|
8
|
+
readonly placeholder?: string;
|
|
9
|
+
readonly helpText?: string;
|
|
10
|
+
readonly rules?: RegisterOptions;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Minimal shape of an entity constants object. The `$`-prefixed keys are
|
|
14
|
+
* entity-level meta (entity name, table, route path). Every other key is a
|
|
15
|
+
* field-level meta object.
|
|
16
|
+
*/
|
|
17
|
+
export interface EntityMeta {
|
|
18
|
+
readonly $entity: string;
|
|
19
|
+
readonly $table: string;
|
|
20
|
+
readonly $path: string;
|
|
21
|
+
readonly [field: string]: string | EntityFieldMeta;
|
|
22
|
+
}
|
|
23
|
+
/** Props returned by `form.input.<fieldName>` — spreadable onto <input>. */
|
|
24
|
+
export type BoundInputProps = Record<string, any>;
|
|
25
|
+
/**
|
|
26
|
+
* Mapped type: for each non-`$`-prefixed key in TEntity, expose its
|
|
27
|
+
* pre-bound input props. Keys whose value isn't a field meta object are
|
|
28
|
+
* stripped out at the type level so consumers only see real fields.
|
|
29
|
+
*/
|
|
30
|
+
export type InputAccessor<TEntity> = {
|
|
31
|
+
readonly [K in keyof TEntity as TEntity[K] extends EntityFieldMeta ? K : never]: BoundInputProps;
|
|
32
|
+
};
|
|
33
|
+
export interface UseEntityFormOptions<T extends FieldValues> {
|
|
34
|
+
defaultValues?: DefaultValues<T>;
|
|
35
|
+
}
|
|
36
|
+
export type UseEntityFormReturn<T extends FieldValues, TEntity> = UseFormReturn<T> & {
|
|
37
|
+
input: InputAccessor<TEntity>;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* React Hook Form set up with the generated Zod resolver, plus an `.input`
|
|
41
|
+
* accessor that binds register() output to each field's metadata-derived
|
|
42
|
+
* attrs (placeholder, type, aria-label, rules). Consumers spread these
|
|
43
|
+
* props onto <input> elements and never hand-pass anything else.
|
|
44
|
+
*
|
|
45
|
+
* For non-`<input>` controls (textarea, select), `type` will be omitted from
|
|
46
|
+
* the props and consumers render the right element themselves.
|
|
47
|
+
*/
|
|
48
|
+
export declare function useEntityForm<TEntity extends EntityMeta, T extends FieldValues = FieldValues>(entity: TEntity, schema: ZodTypeAny, opts?: UseEntityFormOptions<T>): UseEntityFormReturn<T, TEntity>;
|
|
49
|
+
//# sourceMappingURL=use-entity-form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,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;CAClC;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,WAAW,EAAE,OAAO,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG;IACnF,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CAC/B,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAS,UAAU,EAAE,CAAC,SAAS,WAAW,GAAG,WAAW,EAC3F,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,UAAU,EAClB,IAAI,GAAE,oBAAoB,CAAC,CAAC,CAAM,GACjC,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC,CAmCjC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// React form helper — useEntityForm.
|
|
2
|
+
//
|
|
3
|
+
// Single export that returns React Hook Form's `useForm` surface PLUS a
|
|
4
|
+
// pre-bound `.input` accessor: one entry per field on the entity, ready to
|
|
5
|
+
// spread onto an <input> element. Every metadata-derived attribute
|
|
6
|
+
// (placeholder, type, aria-label, RHF rules) rides along automatically.
|
|
7
|
+
//
|
|
8
|
+
// Usage:
|
|
9
|
+
//
|
|
10
|
+
// import { useEntityForm } from '@metaobjectsdev/react';
|
|
11
|
+
// import { Subscriber, SubscriberInsertSchema } from './generated/Subscriber';
|
|
12
|
+
//
|
|
13
|
+
// const form = useEntityForm(Subscriber, SubscriberInsertSchema);
|
|
14
|
+
//
|
|
15
|
+
// <input {...form.input.email} />
|
|
16
|
+
// <input {...form.input.firstName} />
|
|
17
|
+
//
|
|
18
|
+
// React, react-hook-form, @hookform/resolvers, and zod are OPTIONAL peer
|
|
19
|
+
// dependencies. The subpath is only loaded when imported.
|
|
20
|
+
import { useMemo } from "react";
|
|
21
|
+
import { useForm, } from "react-hook-form";
|
|
22
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
23
|
+
/**
|
|
24
|
+
* React Hook Form set up with the generated Zod resolver, plus an `.input`
|
|
25
|
+
* accessor that binds register() output to each field's metadata-derived
|
|
26
|
+
* attrs (placeholder, type, aria-label, rules). Consumers spread these
|
|
27
|
+
* props onto <input> elements and never hand-pass anything else.
|
|
28
|
+
*
|
|
29
|
+
* For non-`<input>` controls (textarea, select), `type` will be omitted from
|
|
30
|
+
* the props and consumers render the right element themselves.
|
|
31
|
+
*/
|
|
32
|
+
export function useEntityForm(entity, schema, opts = {}) {
|
|
33
|
+
// Cast the resolver: @hookform/resolvers' Zod adapter is typed for Zod 3;
|
|
34
|
+
// schemas authored by codegen-ts may be Zod 4. The runtime shape (safeParse)
|
|
35
|
+
// is compatible, so cast at the boundary.
|
|
36
|
+
// biome-ignore lint/suspicious/noExplicitAny: zod 3/4 type-shape gap
|
|
37
|
+
const resolver = zodResolver(schema);
|
|
38
|
+
const form = useForm({
|
|
39
|
+
resolver,
|
|
40
|
+
...(opts.defaultValues !== undefined ? { defaultValues: opts.defaultValues } : {}),
|
|
41
|
+
});
|
|
42
|
+
// Build `.input` once per render. RHF's register() is stable across renders
|
|
43
|
+
// (its identity doesn't change unless the form instance does), so this is
|
|
44
|
+
// safe to memo on the form object.
|
|
45
|
+
const input = useMemo(() => {
|
|
46
|
+
const acc = {};
|
|
47
|
+
for (const [key, value] of Object.entries(entity)) {
|
|
48
|
+
if (key.startsWith("$"))
|
|
49
|
+
continue;
|
|
50
|
+
if (!value || typeof value !== "object" || !("name" in value))
|
|
51
|
+
continue;
|
|
52
|
+
const field = value;
|
|
53
|
+
// Cast rules: RegisterOptions is parameterized by the form's FieldValues
|
|
54
|
+
// and Path; constants emit a structurally-compatible plain RegisterOptions.
|
|
55
|
+
// biome-ignore lint/suspicious/noExplicitAny: structural rules pass through to RHF
|
|
56
|
+
const registered = form.register(field.name, (field.rules ?? {}));
|
|
57
|
+
const props = { ...registered, "aria-label": field.label };
|
|
58
|
+
if (field.htmlType !== undefined)
|
|
59
|
+
props.type = field.htmlType;
|
|
60
|
+
if (field.placeholder !== undefined)
|
|
61
|
+
props.placeholder = field.placeholder;
|
|
62
|
+
acc[key] = props;
|
|
63
|
+
}
|
|
64
|
+
return acc;
|
|
65
|
+
// form is the only relevant dep; entity is a stable codegen-emitted constant.
|
|
66
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
67
|
+
}, [form, entity]);
|
|
68
|
+
return Object.assign(form, { input });
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=use-entity-form.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-entity-form.js","sourceRoot":"","sources":["../src/use-entity-form.tsx"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,mEAAmE;AACnE,wEAAwE;AACxE,EAAE;AACF,SAAS;AACT,EAAE;AACF,2DAA2D;AAC3D,iFAAiF;AACjF,EAAE;AACF,oEAAoE;AACpE,EAAE;AACF,oCAAoC;AACpC,wCAAwC;AACxC,EAAE;AACF,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EACL,OAAO,GAMR,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AA4DtD;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAC3B,MAAe,EACf,MAAkB,EAClB,OAAgC,EAAE;IAElC,0EAA0E;IAC1E,6EAA6E;IAC7E,0CAA0C;IAC1C,qEAAqE;IACrE,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAa,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,OAAO,CAAI;QACtB,QAAQ;QACR,GAAG,CAAC,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACnF,CAAC,CAAC;IAEH,4EAA4E;IAC5E,0EAA0E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",
|
|
3
|
+
"version": "0.5.0-rc.1",
|
|
4
|
+
"description": "React runtime for metaobjects: useEntityForm hook and CurrencyInput component.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"bun": "./src/index.ts",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist", "src", "README.md", "LICENSE"],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc -p tsconfig.json",
|
|
18
|
+
"typecheck": "tsc -p tsconfig.typecheck.json",
|
|
19
|
+
"test": "bun test"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "Doug Mealing <doug@dougmealing.com>",
|
|
23
|
+
"homepage": "https://metaobjects.dev",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/metaobjectsdev/metaobjects/issues"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/metaobjectsdev/metaobjects.git",
|
|
30
|
+
"directory": "client/web/packages/react"
|
|
31
|
+
},
|
|
32
|
+
"keywords": ["metaobjects", "react", "react-hook-form", "forms", "currency-input"],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@metaobjectsdev/runtime-web": "0.5.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@hookform/resolvers": ">=3.0.0",
|
|
41
|
+
"react": ">=18.0.0",
|
|
42
|
+
"react-hook-form": ">=7.0.0",
|
|
43
|
+
"zod": ">=3.23.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"@hookform/resolvers": {
|
|
47
|
+
"optional": true
|
|
48
|
+
},
|
|
49
|
+
"react": {
|
|
50
|
+
"optional": true
|
|
51
|
+
},
|
|
52
|
+
"react-hook-form": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"zod": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@hookform/resolvers": "^3.10.0",
|
|
61
|
+
"@testing-library/react": "^16.0.0",
|
|
62
|
+
"@testing-library/dom": "^10.0.0",
|
|
63
|
+
"@types/react": "^19.0.0",
|
|
64
|
+
"bun-types": "latest",
|
|
65
|
+
"jsdom": "^29.1.1",
|
|
66
|
+
"react": "^19.0.0",
|
|
67
|
+
"react-dom": "^19.0.0",
|
|
68
|
+
"react-hook-form": "^7.54.0",
|
|
69
|
+
"typescript": "^5.6.0",
|
|
70
|
+
"zod": "^4.4.3"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from "react";
|
|
2
|
+
import type { ReactElement, FocusEvent, ChangeEvent } from "react";
|
|
3
|
+
import { formatCurrency, parseCurrency, minorUnitsFor } from "@metaobjectsdev/runtime-web";
|
|
4
|
+
|
|
5
|
+
export interface CurrencyInputProps {
|
|
6
|
+
/** Value in integer minor units (cents for USD). */
|
|
7
|
+
value: number;
|
|
8
|
+
/** Called with new value in integer minor units after blur. */
|
|
9
|
+
onChange: (cents: number) => void;
|
|
10
|
+
/** ISO 4217 currency code. Defaults to "USD". */
|
|
11
|
+
currency?: string;
|
|
12
|
+
/** BCP 47 locale code. Defaults to "en-US". */
|
|
13
|
+
locale?: string;
|
|
14
|
+
className?: string;
|
|
15
|
+
placeholder?: string;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
id?: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Bidirectional currency input. Displays formatted dollars; captures
|
|
23
|
+
* dollars from the user; emits cents to onChange on blur.
|
|
24
|
+
*
|
|
25
|
+
* - Focus strips symbol + grouping for easier editing
|
|
26
|
+
* - Blur re-formats + emits cents
|
|
27
|
+
* - Empty input + !required → emits 0
|
|
28
|
+
* - Empty input + required → reverts to last valid; no onChange
|
|
29
|
+
* - Unparseable input on blur → reverts to last valid format; no onChange
|
|
30
|
+
* - Float drift avoided via Math.round in parseCurrency
|
|
31
|
+
*/
|
|
32
|
+
export function CurrencyInput(props: CurrencyInputProps): ReactElement {
|
|
33
|
+
const currency = props.currency ?? "USD";
|
|
34
|
+
const locale = props.locale ?? "en-US";
|
|
35
|
+
|
|
36
|
+
const [text, setText] = useState(() => formatCurrency(props.value, currency, locale));
|
|
37
|
+
const [isInvalid, setIsInvalid] = useState(false);
|
|
38
|
+
const lastValidValue = useRef(props.value);
|
|
39
|
+
|
|
40
|
+
// Re-format when external value changes (controlled component sync)
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (props.value !== lastValidValue.current) {
|
|
43
|
+
setText(formatCurrency(props.value, currency, locale));
|
|
44
|
+
lastValidValue.current = props.value;
|
|
45
|
+
}
|
|
46
|
+
}, [props.value, currency, locale]);
|
|
47
|
+
|
|
48
|
+
function handleFocus(_e: FocusEvent<HTMLInputElement>) {
|
|
49
|
+
setIsInvalid(false);
|
|
50
|
+
// Strip symbol + grouping so user can edit the raw number
|
|
51
|
+
const minorUnits = minorUnitsFor(currency, locale);
|
|
52
|
+
const factor = Math.pow(10, minorUnits);
|
|
53
|
+
const dollars = (props.value / factor).toFixed(minorUnits);
|
|
54
|
+
setText(dollars);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function handleBlur(_e: FocusEvent<HTMLInputElement>) {
|
|
58
|
+
const trimmed = text.trim();
|
|
59
|
+
if (trimmed === "") {
|
|
60
|
+
if (props.required) {
|
|
61
|
+
setIsInvalid(true);
|
|
62
|
+
setText(formatCurrency(props.value, currency, locale));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
setIsInvalid(false);
|
|
66
|
+
props.onChange(0);
|
|
67
|
+
lastValidValue.current = 0;
|
|
68
|
+
setText(formatCurrency(0, currency, locale));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const parsed = parseCurrency(trimmed, currency, locale);
|
|
72
|
+
if (parsed === null) {
|
|
73
|
+
// Unparseable — revert to last valid
|
|
74
|
+
setText(formatCurrency(lastValidValue.current, currency, locale));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
setIsInvalid(false);
|
|
78
|
+
props.onChange(parsed);
|
|
79
|
+
lastValidValue.current = parsed;
|
|
80
|
+
setText(formatCurrency(parsed, currency, locale));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function handleChange(e: ChangeEvent<HTMLInputElement>) {
|
|
84
|
+
setText(e.target.value);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<input
|
|
89
|
+
type="text"
|
|
90
|
+
inputMode="decimal"
|
|
91
|
+
value={text}
|
|
92
|
+
onChange={handleChange}
|
|
93
|
+
onFocus={handleFocus}
|
|
94
|
+
onBlur={handleBlur}
|
|
95
|
+
className={props.className}
|
|
96
|
+
placeholder={props.placeholder}
|
|
97
|
+
required={props.required}
|
|
98
|
+
id={props.id}
|
|
99
|
+
name={props.name}
|
|
100
|
+
data-invalid={isInvalid ? "true" : undefined}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Public API surface for @metaobjectsdev/react.
|
|
2
|
+
export {
|
|
3
|
+
useEntityForm,
|
|
4
|
+
type EntityFieldMeta,
|
|
5
|
+
type EntityMeta,
|
|
6
|
+
type BoundInputProps,
|
|
7
|
+
type InputAccessor,
|
|
8
|
+
type UseEntityFormOptions,
|
|
9
|
+
type UseEntityFormReturn,
|
|
10
|
+
} from "./use-entity-form.js";
|
|
11
|
+
export { CurrencyInput, type CurrencyInputProps } from "./currency-input.js";
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// React form helper — useEntityForm.
|
|
2
|
+
//
|
|
3
|
+
// Single export that returns React Hook Form's `useForm` surface PLUS a
|
|
4
|
+
// pre-bound `.input` accessor: one entry per field on the entity, ready to
|
|
5
|
+
// spread onto an <input> element. Every metadata-derived attribute
|
|
6
|
+
// (placeholder, type, aria-label, RHF rules) rides along automatically.
|
|
7
|
+
//
|
|
8
|
+
// Usage:
|
|
9
|
+
//
|
|
10
|
+
// import { useEntityForm } from '@metaobjectsdev/react';
|
|
11
|
+
// import { Subscriber, SubscriberInsertSchema } from './generated/Subscriber';
|
|
12
|
+
//
|
|
13
|
+
// const form = useEntityForm(Subscriber, SubscriberInsertSchema);
|
|
14
|
+
//
|
|
15
|
+
// <input {...form.input.email} />
|
|
16
|
+
// <input {...form.input.firstName} />
|
|
17
|
+
//
|
|
18
|
+
// React, react-hook-form, @hookform/resolvers, and zod are OPTIONAL peer
|
|
19
|
+
// dependencies. The subpath is only loaded when imported.
|
|
20
|
+
|
|
21
|
+
import { useMemo } from "react";
|
|
22
|
+
import {
|
|
23
|
+
useForm,
|
|
24
|
+
type FieldValues,
|
|
25
|
+
type DefaultValues,
|
|
26
|
+
type Path,
|
|
27
|
+
type RegisterOptions,
|
|
28
|
+
type UseFormReturn,
|
|
29
|
+
} from "react-hook-form";
|
|
30
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
31
|
+
import type { ZodTypeAny } from "zod";
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Field shape — what each non-`$`-prefixed key on an Entity constants object
|
|
35
|
+
// looks like. The codegen emits this; consumers don't construct it by hand.
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
export interface EntityFieldMeta {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly label: string;
|
|
41
|
+
readonly view: string;
|
|
42
|
+
readonly htmlType?: string;
|
|
43
|
+
readonly placeholder?: string;
|
|
44
|
+
readonly helpText?: string;
|
|
45
|
+
readonly rules?: RegisterOptions;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Minimal shape of an entity constants object. The `$`-prefixed keys are
|
|
50
|
+
* entity-level meta (entity name, table, route path). Every other key is a
|
|
51
|
+
* field-level meta object.
|
|
52
|
+
*/
|
|
53
|
+
export interface EntityMeta {
|
|
54
|
+
readonly $entity: string;
|
|
55
|
+
readonly $table: string;
|
|
56
|
+
readonly $path: string;
|
|
57
|
+
readonly [field: string]: string | EntityFieldMeta;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Pre-bound input props per field. Spread onto a native <input>.
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
/** Props returned by `form.input.<fieldName>` — spreadable onto <input>. */
|
|
65
|
+
// biome-ignore lint/suspicious/noExplicitAny: RHF's register() returns a heavily
|
|
66
|
+
// generic type; we spread the result and add metadata-derived attrs alongside.
|
|
67
|
+
export type BoundInputProps = Record<string, any>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Mapped type: for each non-`$`-prefixed key in TEntity, expose its
|
|
71
|
+
* pre-bound input props. Keys whose value isn't a field meta object are
|
|
72
|
+
* stripped out at the type level so consumers only see real fields.
|
|
73
|
+
*/
|
|
74
|
+
export type InputAccessor<TEntity> = {
|
|
75
|
+
readonly [K in keyof TEntity as TEntity[K] extends EntityFieldMeta ? K : never]: BoundInputProps;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// useEntityForm
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
export interface UseEntityFormOptions<T extends FieldValues> {
|
|
83
|
+
defaultValues?: DefaultValues<T>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type UseEntityFormReturn<T extends FieldValues, TEntity> = UseFormReturn<T> & {
|
|
87
|
+
input: InputAccessor<TEntity>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* React Hook Form set up with the generated Zod resolver, plus an `.input`
|
|
92
|
+
* accessor that binds register() output to each field's metadata-derived
|
|
93
|
+
* attrs (placeholder, type, aria-label, rules). Consumers spread these
|
|
94
|
+
* props onto <input> elements and never hand-pass anything else.
|
|
95
|
+
*
|
|
96
|
+
* For non-`<input>` controls (textarea, select), `type` will be omitted from
|
|
97
|
+
* the props and consumers render the right element themselves.
|
|
98
|
+
*/
|
|
99
|
+
export function useEntityForm<TEntity extends EntityMeta, T extends FieldValues = FieldValues>(
|
|
100
|
+
entity: TEntity,
|
|
101
|
+
schema: ZodTypeAny,
|
|
102
|
+
opts: UseEntityFormOptions<T> = {},
|
|
103
|
+
): UseEntityFormReturn<T, TEntity> {
|
|
104
|
+
// Cast the resolver: @hookform/resolvers' Zod adapter is typed for Zod 3;
|
|
105
|
+
// schemas authored by codegen-ts may be Zod 4. The runtime shape (safeParse)
|
|
106
|
+
// is compatible, so cast at the boundary.
|
|
107
|
+
// biome-ignore lint/suspicious/noExplicitAny: zod 3/4 type-shape gap
|
|
108
|
+
const resolver = zodResolver(schema as any);
|
|
109
|
+
const form = useForm<T>({
|
|
110
|
+
resolver,
|
|
111
|
+
...(opts.defaultValues !== undefined ? { defaultValues: opts.defaultValues } : {}),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// Build `.input` once per render. RHF's register() is stable across renders
|
|
115
|
+
// (its identity doesn't change unless the form instance does), so this is
|
|
116
|
+
// safe to memo on the form object.
|
|
117
|
+
const input = useMemo(() => {
|
|
118
|
+
const acc: Record<string, BoundInputProps> = {};
|
|
119
|
+
for (const [key, value] of Object.entries(entity)) {
|
|
120
|
+
if (key.startsWith("$")) continue;
|
|
121
|
+
if (!value || typeof value !== "object" || !("name" in value)) continue;
|
|
122
|
+
const field = value as EntityFieldMeta;
|
|
123
|
+
// Cast rules: RegisterOptions is parameterized by the form's FieldValues
|
|
124
|
+
// and Path; constants emit a structurally-compatible plain RegisterOptions.
|
|
125
|
+
// biome-ignore lint/suspicious/noExplicitAny: structural rules pass through to RHF
|
|
126
|
+
const registered = form.register(field.name as Path<T>, (field.rules ?? {}) as any);
|
|
127
|
+
const props: BoundInputProps = { ...registered, "aria-label": field.label };
|
|
128
|
+
if (field.htmlType !== undefined) props.type = field.htmlType;
|
|
129
|
+
if (field.placeholder !== undefined) props.placeholder = field.placeholder;
|
|
130
|
+
acc[key] = props;
|
|
131
|
+
}
|
|
132
|
+
return acc as InputAccessor<TEntity>;
|
|
133
|
+
// form is the only relevant dep; entity is a stable codegen-emitted constant.
|
|
134
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
135
|
+
}, [form, entity]);
|
|
136
|
+
|
|
137
|
+
return Object.assign(form, { input });
|
|
138
|
+
}
|