@cedarjs/forms 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/CheckboxField.d.ts +7 -0
- package/dist/CheckboxField.d.ts.map +1 -0
- package/dist/CheckboxField.js +85 -0
- package/dist/FieldError.d.ts +43 -0
- package/dist/FieldError.d.ts.map +1 -0
- package/dist/FieldError.js +56 -0
- package/dist/FieldProps.d.ts +26 -0
- package/dist/FieldProps.d.ts.map +1 -0
- package/dist/FieldProps.js +16 -0
- package/dist/Form.d.ts +41 -0
- package/dist/Form.d.ts.map +1 -0
- package/dist/Form.js +69 -0
- package/dist/FormError.d.ts +36 -0
- package/dist/FormError.d.ts.map +1 -0
- package/dist/FormError.js +90 -0
- package/dist/InputComponents.d.ts +30 -0
- package/dist/InputComponents.d.ts.map +1 -0
- package/dist/InputComponents.js +180 -0
- package/dist/Label.d.ts +10 -0
- package/dist/Label.d.ts.map +1 -0
- package/dist/Label.js +58 -0
- package/dist/SelectField.d.ts +7 -0
- package/dist/SelectField.d.ts.map +1 -0
- package/dist/SelectField.js +76 -0
- package/dist/ServerErrorsContext.d.ts +8 -0
- package/dist/ServerErrorsContext.d.ts.map +1 -0
- package/dist/ServerErrorsContext.js +41 -0
- package/dist/Submit.d.ts +16 -0
- package/dist/Submit.d.ts.map +1 -0
- package/dist/Submit.js +39 -0
- package/dist/TextAreaField.d.ts +9 -0
- package/dist/TextAreaField.d.ts.map +1 -0
- package/dist/TextAreaField.js +76 -0
- package/dist/coercion.d.ts +59 -0
- package/dist/coercion.d.ts.map +1 -0
- package/dist/coercion.js +178 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +118 -0
- package/dist/useErrorStyles.d.ts +18 -0
- package/dist/useErrorStyles.d.ts.map +1 -0
- package/dist/useErrorStyles.js +68 -0
- package/dist/useRegister.d.ts +32 -0
- package/dist/useRegister.d.ts.map +1 -0
- package/dist/useRegister.js +69 -0
- package/package.json +65 -0
package/dist/coercion.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var coercion_exports = {};
|
|
20
|
+
__export(coercion_exports, {
|
|
21
|
+
setCoercion: () => setCoercion
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(coercion_exports);
|
|
24
|
+
const isValueEmpty = (val) => val === "";
|
|
25
|
+
const SET_VALUE_AS_FUNCTIONS = {
|
|
26
|
+
// valueAsBoolean is commented out as r-h-f does not currently support
|
|
27
|
+
// setValueAs functionality for checkboxes. May investigate future
|
|
28
|
+
// integration
|
|
29
|
+
/* valueAsBoolean: {
|
|
30
|
+
// r-h-f returns a boolean if a checkBox type, but also handle string case in case valueAsBoolean is used
|
|
31
|
+
base: (val: boolean | string): boolean => !!val,
|
|
32
|
+
emptyAsNull: (val: boolean | string): boolean | null => (val ? true : null),
|
|
33
|
+
emptyAsUndefined: (val: boolean | string): boolean | undefined =>
|
|
34
|
+
val ? true : undefined,
|
|
35
|
+
},*/
|
|
36
|
+
valueAsDate: {
|
|
37
|
+
emptyAsNull: (val) => isValueEmpty(val) ? null : new Date(val),
|
|
38
|
+
emptyAsUndefined: (val) => isValueEmpty(val) ? void 0 : new Date(val),
|
|
39
|
+
emptyAsString: (val) => isValueEmpty(val) ? "" : new Date(val),
|
|
40
|
+
emptyAsZero: (val) => isValueEmpty(val) ? 0 : new Date(val)
|
|
41
|
+
},
|
|
42
|
+
valueAsJSON: {
|
|
43
|
+
emptyAsNull: (val) => {
|
|
44
|
+
if (isValueEmpty(val)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(val);
|
|
49
|
+
} catch {
|
|
50
|
+
return NaN;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
emptyAsString: (val) => {
|
|
54
|
+
if (isValueEmpty(val)) {
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(val);
|
|
59
|
+
} catch {
|
|
60
|
+
return NaN;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
emptyAsUndefined: (val) => {
|
|
64
|
+
if (isValueEmpty(val)) {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
return JSON.parse(val);
|
|
69
|
+
} catch {
|
|
70
|
+
return NaN;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
emptyAsZero: (val) => {
|
|
74
|
+
if (isValueEmpty(val)) {
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(val);
|
|
79
|
+
} catch {
|
|
80
|
+
return NaN;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
valueAsNumber: {
|
|
85
|
+
emptyAsNull: (val) => isValueEmpty(val) ? null : +val,
|
|
86
|
+
emptyAsUndefined: (val) => isValueEmpty(val) ? void 0 : +val,
|
|
87
|
+
emptyAsNaN: (val) => isValueEmpty(val) ? NaN : +val,
|
|
88
|
+
emptyAsString: (val) => isValueEmpty(val) ? "" : +val,
|
|
89
|
+
emptyAsZero: (val) => isValueEmpty(val) ? 0 : +val
|
|
90
|
+
},
|
|
91
|
+
valueAsString: {
|
|
92
|
+
emptyAsNull: (val) => isValueEmpty(val) ? null : val,
|
|
93
|
+
emptyAsUndefined: (val) => isValueEmpty(val) ? void 0 : val,
|
|
94
|
+
emptyAsString: (val) => isValueEmpty(val) ? "" : val,
|
|
95
|
+
emptyAsZero: (val) => isValueEmpty(val) ? 0 : val
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const getSetValueAsFn = (type, emptyAs, required, isId) => {
|
|
99
|
+
const typeObj = SET_VALUE_AS_FUNCTIONS[type];
|
|
100
|
+
if (typeObj === void 0) {
|
|
101
|
+
throw Error(`Type ${type} is unsupported.`);
|
|
102
|
+
}
|
|
103
|
+
let fn;
|
|
104
|
+
switch (emptyAs) {
|
|
105
|
+
case null:
|
|
106
|
+
fn = typeObj["emptyAsNull"];
|
|
107
|
+
break;
|
|
108
|
+
case "undefined":
|
|
109
|
+
fn = typeObj["emptyAsUndefined"];
|
|
110
|
+
break;
|
|
111
|
+
case 0:
|
|
112
|
+
fn = typeObj["emptyAsZero"];
|
|
113
|
+
break;
|
|
114
|
+
case "":
|
|
115
|
+
fn = typeObj["emptyAsString"];
|
|
116
|
+
break;
|
|
117
|
+
case void 0:
|
|
118
|
+
default:
|
|
119
|
+
if (required || isId) {
|
|
120
|
+
fn = typeObj.emptyAsNull;
|
|
121
|
+
} else {
|
|
122
|
+
switch (type) {
|
|
123
|
+
case "valueAsNumber":
|
|
124
|
+
fn = typeObj.emptyAsNaN;
|
|
125
|
+
break;
|
|
126
|
+
case "valueAsDate":
|
|
127
|
+
case "valueAsJSON":
|
|
128
|
+
fn = typeObj.emptyAsNull;
|
|
129
|
+
break;
|
|
130
|
+
case "valueAsString":
|
|
131
|
+
fn = typeObj.emptyAsString;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
if (fn === void 0) {
|
|
138
|
+
console.error(`emptyAs prop of ${emptyAs} is unsupported for this type.`);
|
|
139
|
+
}
|
|
140
|
+
return fn;
|
|
141
|
+
};
|
|
142
|
+
const JSONValidation = (val) => typeof val === "number" ? !isNaN(val) : true;
|
|
143
|
+
const setCoercion = (validation, { type, name, emptyAs }) => {
|
|
144
|
+
if (validation.setValueAs) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
let valueAs;
|
|
148
|
+
if (validation.valueAsBoolean || type === "checkbox") {
|
|
149
|
+
return;
|
|
150
|
+
} else if (validation.valueAsJSON) {
|
|
151
|
+
validation.validate = JSONValidation;
|
|
152
|
+
delete validation.valueAsJSON;
|
|
153
|
+
valueAs = "valueAsJSON";
|
|
154
|
+
} else if (type === "date" || type === "datetime-local" || validation.valueAsDate) {
|
|
155
|
+
valueAs = "valueAsDate";
|
|
156
|
+
} else if (type === "number" || validation.valueAsNumber) {
|
|
157
|
+
valueAs = "valueAsNumber";
|
|
158
|
+
if (validation.valueAsNumber && emptyAs !== void 0) {
|
|
159
|
+
delete validation.valueAsNumber;
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
valueAs = "valueAsString";
|
|
163
|
+
}
|
|
164
|
+
validation.setValueAs = getSetValueAsFn(
|
|
165
|
+
valueAs,
|
|
166
|
+
// type
|
|
167
|
+
emptyAs,
|
|
168
|
+
// emptyAs
|
|
169
|
+
validation.required !== void 0 && validation.required !== false,
|
|
170
|
+
// required
|
|
171
|
+
(name || "").endsWith("Id")
|
|
172
|
+
// isId
|
|
173
|
+
);
|
|
174
|
+
};
|
|
175
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
176
|
+
0 && (module.exports = {
|
|
177
|
+
setCoercion
|
|
178
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @cedarjs/forms
|
|
3
|
+
*
|
|
4
|
+
* Redwood's form library.
|
|
5
|
+
* Mostly simple wrappers around `react-hook-form` that make it even easier to use.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
*
|
|
9
|
+
* @cedarjs/forms slightly extends `react-hook-form`'s `valueAs` props because it's important for us to coerce values
|
|
10
|
+
* to the correct type for GraphQL.
|
|
11
|
+
* The properties that are exclusive to Redwood are:
|
|
12
|
+
* - `valueAsBoolean`
|
|
13
|
+
* - `valueAsJSON`
|
|
14
|
+
* - `emptyAs`
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://react-hook-form.com/}
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
*
|
|
20
|
+
* We make all of `react-hook-form`'s exports available as well.
|
|
21
|
+
*
|
|
22
|
+
* @privateRemarks
|
|
23
|
+
*
|
|
24
|
+
* The two main hooks in this library are:
|
|
25
|
+
* - `useErrorStyles`
|
|
26
|
+
* - `useRegister`
|
|
27
|
+
*
|
|
28
|
+
* `useErrorStyles` implements the error-specific styling via `useEffect`.
|
|
29
|
+
*
|
|
30
|
+
* `useRegister` hooks fields up to `react-hook-form` while providing some sensible defaults
|
|
31
|
+
* based on the field's type.
|
|
32
|
+
|
|
33
|
+
* @privateRemarks
|
|
34
|
+
*
|
|
35
|
+
* We use `React.ComponentPropsWithRef` and `React.ComponentPropsWithoutRef` instead of `React.FC`
|
|
36
|
+
* because the community seems to be shifting away from `React.FC`.
|
|
37
|
+
*
|
|
38
|
+
* @see {@link https://fettblog.eu/typescript-react-why-i-dont-use-react-fc/}
|
|
39
|
+
* @see {@link https://github.com/facebook/create-react-app/pull/8177}
|
|
40
|
+
* @see {@link https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components/}
|
|
41
|
+
*
|
|
42
|
+
* @privateRemarks
|
|
43
|
+
*
|
|
44
|
+
* As for interfaces vs types, we're going with TypesScript's recommendation to use interfaces until types are needed.
|
|
45
|
+
*
|
|
46
|
+
* @see {@link https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces}
|
|
47
|
+
*/
|
|
48
|
+
export * from 'react-hook-form';
|
|
49
|
+
export { CheckboxField } from './CheckboxField';
|
|
50
|
+
export type { EmptyAsValue, RedwoodRegisterOptions } from './coercion';
|
|
51
|
+
export { FieldError } from './FieldError';
|
|
52
|
+
export { Form, FormProps } from './Form';
|
|
53
|
+
export { default as FormError } from './FormError';
|
|
54
|
+
export type { ServerError, RWGqlError, ServerParseError } from './FormError';
|
|
55
|
+
export { InputField, ButtonField, ColorField, DateField, DatetimeLocalField, EmailField, FileField, HiddenField, ImageField, MonthField, NumberField, PasswordField, RadioField, RangeField, ResetField, SearchField, SubmitField, TelField, TextField, TimeField, UrlField, WeekField, } from './InputComponents';
|
|
56
|
+
export type { InputFieldProps } from './InputComponents';
|
|
57
|
+
export { Label } from './Label';
|
|
58
|
+
export { SelectField } from './SelectField';
|
|
59
|
+
export { ServerErrorsContext } from './ServerErrorsContext';
|
|
60
|
+
export { Submit } from './Submit';
|
|
61
|
+
export { TextAreaField } from './TextAreaField';
|
|
62
|
+
export { useErrorStyles } from './useErrorStyles';
|
|
63
|
+
export { useRegister } from './useRegister';
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,cAAc,iBAAiB,CAAA;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,YAAY,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACxC,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAA;AAClD,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAC5E,OAAO,EACL,UAAU,EACV,WAAW,EACX,UAAU,EACV,SAAS,EACT,kBAAkB,EAClB,UAAU,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,UAAU,EACV,WAAW,EACX,aAAa,EACb,UAAU,EACV,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,SAAS,EACT,SAAS,EACT,QAAQ,EACR,SAAS,GACV,MAAM,mBAAmB,CAAA;AAC1B,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
ButtonField: () => import_InputComponents.ButtonField,
|
|
33
|
+
CheckboxField: () => import_CheckboxField.CheckboxField,
|
|
34
|
+
ColorField: () => import_InputComponents.ColorField,
|
|
35
|
+
DateField: () => import_InputComponents.DateField,
|
|
36
|
+
DatetimeLocalField: () => import_InputComponents.DatetimeLocalField,
|
|
37
|
+
EmailField: () => import_InputComponents.EmailField,
|
|
38
|
+
FieldError: () => import_FieldError.FieldError,
|
|
39
|
+
FileField: () => import_InputComponents.FileField,
|
|
40
|
+
Form: () => import_Form.Form,
|
|
41
|
+
FormError: () => import_FormError.default,
|
|
42
|
+
FormProps: () => import_Form.FormProps,
|
|
43
|
+
HiddenField: () => import_InputComponents.HiddenField,
|
|
44
|
+
ImageField: () => import_InputComponents.ImageField,
|
|
45
|
+
InputField: () => import_InputComponents.InputField,
|
|
46
|
+
Label: () => import_Label.Label,
|
|
47
|
+
MonthField: () => import_InputComponents.MonthField,
|
|
48
|
+
NumberField: () => import_InputComponents.NumberField,
|
|
49
|
+
PasswordField: () => import_InputComponents.PasswordField,
|
|
50
|
+
RadioField: () => import_InputComponents.RadioField,
|
|
51
|
+
RangeField: () => import_InputComponents.RangeField,
|
|
52
|
+
ResetField: () => import_InputComponents.ResetField,
|
|
53
|
+
SearchField: () => import_InputComponents.SearchField,
|
|
54
|
+
SelectField: () => import_SelectField.SelectField,
|
|
55
|
+
ServerErrorsContext: () => import_ServerErrorsContext.ServerErrorsContext,
|
|
56
|
+
Submit: () => import_Submit.Submit,
|
|
57
|
+
SubmitField: () => import_InputComponents.SubmitField,
|
|
58
|
+
TelField: () => import_InputComponents.TelField,
|
|
59
|
+
TextAreaField: () => import_TextAreaField.TextAreaField,
|
|
60
|
+
TextField: () => import_InputComponents.TextField,
|
|
61
|
+
TimeField: () => import_InputComponents.TimeField,
|
|
62
|
+
UrlField: () => import_InputComponents.UrlField,
|
|
63
|
+
WeekField: () => import_InputComponents.WeekField,
|
|
64
|
+
useErrorStyles: () => import_useErrorStyles.useErrorStyles,
|
|
65
|
+
useRegister: () => import_useRegister.useRegister
|
|
66
|
+
});
|
|
67
|
+
module.exports = __toCommonJS(index_exports);
|
|
68
|
+
__reExport(index_exports, require("react-hook-form"), module.exports);
|
|
69
|
+
var import_CheckboxField = require("./CheckboxField");
|
|
70
|
+
var import_FieldError = require("./FieldError");
|
|
71
|
+
var import_Form = require("./Form");
|
|
72
|
+
var import_FormError = __toESM(require("./FormError"));
|
|
73
|
+
var import_InputComponents = require("./InputComponents");
|
|
74
|
+
var import_Label = require("./Label");
|
|
75
|
+
var import_SelectField = require("./SelectField");
|
|
76
|
+
var import_ServerErrorsContext = require("./ServerErrorsContext");
|
|
77
|
+
var import_Submit = require("./Submit");
|
|
78
|
+
var import_TextAreaField = require("./TextAreaField");
|
|
79
|
+
var import_useErrorStyles = require("./useErrorStyles");
|
|
80
|
+
var import_useRegister = require("./useRegister");
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
ButtonField,
|
|
84
|
+
CheckboxField,
|
|
85
|
+
ColorField,
|
|
86
|
+
DateField,
|
|
87
|
+
DatetimeLocalField,
|
|
88
|
+
EmailField,
|
|
89
|
+
FieldError,
|
|
90
|
+
FileField,
|
|
91
|
+
Form,
|
|
92
|
+
FormError,
|
|
93
|
+
FormProps,
|
|
94
|
+
HiddenField,
|
|
95
|
+
ImageField,
|
|
96
|
+
InputField,
|
|
97
|
+
Label,
|
|
98
|
+
MonthField,
|
|
99
|
+
NumberField,
|
|
100
|
+
PasswordField,
|
|
101
|
+
RadioField,
|
|
102
|
+
RangeField,
|
|
103
|
+
ResetField,
|
|
104
|
+
SearchField,
|
|
105
|
+
SelectField,
|
|
106
|
+
ServerErrorsContext,
|
|
107
|
+
Submit,
|
|
108
|
+
SubmitField,
|
|
109
|
+
TelField,
|
|
110
|
+
TextAreaField,
|
|
111
|
+
TextField,
|
|
112
|
+
TimeField,
|
|
113
|
+
UrlField,
|
|
114
|
+
WeekField,
|
|
115
|
+
useErrorStyles,
|
|
116
|
+
useRegister,
|
|
117
|
+
...require("react-hook-form")
|
|
118
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { FieldProps } from './FieldProps';
|
|
3
|
+
export type UseErrorStylesProps = Pick<FieldProps, 'name' | 'errorClassName' | 'errorStyle' | 'className' | 'style'>;
|
|
4
|
+
/**
|
|
5
|
+
* Adds styling to a field when an error is present.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
*
|
|
9
|
+
* Mostly just a `useEffect` hook.
|
|
10
|
+
*
|
|
11
|
+
* `className` and `style` get swapped with `errorClassName` and `errorStyle` respectively
|
|
12
|
+
* when an error's present (on the server or otherwise).
|
|
13
|
+
*/
|
|
14
|
+
export declare const useErrorStyles: ({ name, errorClassName, errorStyle, className, style, }: UseErrorStylesProps) => {
|
|
15
|
+
className: string | undefined;
|
|
16
|
+
style: React.CSSProperties | undefined;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=useErrorStyles.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useErrorStyles.d.ts","sourceRoot":"","sources":["../src/useErrorStyles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAqB,MAAM,OAAO,CAAA;AAIzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAG9C,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,UAAU,EACV,MAAM,GAAG,gBAAgB,GAAG,YAAY,GAAG,WAAW,GAAG,OAAO,CACjE,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,4DAMxB,mBAAmB;;;CA2BrB,CAAA"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var useErrorStyles_exports = {};
|
|
30
|
+
__export(useErrorStyles_exports, {
|
|
31
|
+
useErrorStyles: () => useErrorStyles
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(useErrorStyles_exports);
|
|
34
|
+
var import_react = __toESM(require("react"));
|
|
35
|
+
var import_react_hook_form = require("react-hook-form");
|
|
36
|
+
var import_ServerErrorsContext = require("./ServerErrorsContext");
|
|
37
|
+
const useErrorStyles = ({
|
|
38
|
+
name,
|
|
39
|
+
errorClassName,
|
|
40
|
+
errorStyle,
|
|
41
|
+
className,
|
|
42
|
+
style
|
|
43
|
+
}) => {
|
|
44
|
+
const {
|
|
45
|
+
formState: { errors },
|
|
46
|
+
setError
|
|
47
|
+
} = (0, import_react_hook_form.useFormContext)();
|
|
48
|
+
const serverError = (0, import_react.useContext)(import_ServerErrorsContext.ServerErrorsContext)[name];
|
|
49
|
+
import_react.default.useEffect(() => {
|
|
50
|
+
if (serverError) {
|
|
51
|
+
setError(name, { type: "server", message: serverError });
|
|
52
|
+
}
|
|
53
|
+
}, [serverError, name, setError]);
|
|
54
|
+
const validationError = name ? (0, import_react_hook_form.get)(errors, name) : void 0;
|
|
55
|
+
if (validationError) {
|
|
56
|
+
if (errorClassName) {
|
|
57
|
+
className = errorClassName;
|
|
58
|
+
}
|
|
59
|
+
if (errorStyle) {
|
|
60
|
+
style = errorStyle;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { className, style };
|
|
64
|
+
};
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
useErrorStyles
|
|
68
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { EmptyAsValue } from './coercion';
|
|
3
|
+
import type { FieldProps } from './FieldProps';
|
|
4
|
+
export type UseRegisterProps<Element extends HTMLTextAreaElement | HTMLSelectElement | HTMLInputElement = HTMLInputElement> = Pick<FieldProps<Element>, 'name' | 'validation' | 'type' | 'onBlur' | 'onChange'>;
|
|
5
|
+
/**
|
|
6
|
+
* useRegister
|
|
7
|
+
*
|
|
8
|
+
* Register the field into `react-hook-form` with defaults.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
*
|
|
12
|
+
* A field's `validation` prop is `react-hook-form`'s `RegisterOptions`
|
|
13
|
+
* (with Redwood's extended `valueAs` props).
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://react-hook-form.com/api/useform/register}
|
|
16
|
+
*/
|
|
17
|
+
export declare const useRegister: <T, Element extends HTMLTextAreaElement | HTMLSelectElement | HTMLInputElement = HTMLInputElement>(props: UseRegisterProps<Element> & {
|
|
18
|
+
element?: string;
|
|
19
|
+
}, ref?: React.ForwardedRef<T>, emptyAs?: EmptyAsValue) => {
|
|
20
|
+
ref: (element: T) => void;
|
|
21
|
+
onBlur: React.FocusEventHandler<Element>;
|
|
22
|
+
onChange: React.ChangeEventHandler<Element>;
|
|
23
|
+
name: string;
|
|
24
|
+
min?: string | number;
|
|
25
|
+
max?: string | number;
|
|
26
|
+
maxLength?: number;
|
|
27
|
+
minLength?: number;
|
|
28
|
+
pattern?: string;
|
|
29
|
+
required?: boolean;
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=useRegister.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRegister.d.ts","sourceRoot":"","sources":["../src/useRegister.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAI9B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAE9C,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SACH,mBAAmB,GACnB,iBAAiB,GACjB,gBAAgB,GAAG,gBAAgB,IACrC,IAAI,CACN,UAAU,CAAC,OAAO,CAAC,EACnB,MAAM,GAAG,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,CACvD,CAAA;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,GACtB,CAAC,EACD,OAAO,SACH,mBAAmB,GACnB,iBAAiB,GACjB,gBAAgB,4BAEb,gBAAgB,CAAC,OAAO,CAAC,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,QACjD,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,YACjB,YAAY;mBAmCL,CAAC;;;;;;;;;;;CAYnB,CAAA"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var useRegister_exports = {};
|
|
20
|
+
__export(useRegister_exports, {
|
|
21
|
+
useRegister: () => useRegister
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(useRegister_exports);
|
|
24
|
+
var import_react_hook_form = require("react-hook-form");
|
|
25
|
+
var import_coercion = require("./coercion");
|
|
26
|
+
const useRegister = (props, ref, emptyAs) => {
|
|
27
|
+
const { register } = (0, import_react_hook_form.useFormContext)();
|
|
28
|
+
const { name } = props;
|
|
29
|
+
if (!name) {
|
|
30
|
+
throw Error("`name` prop must be provided");
|
|
31
|
+
}
|
|
32
|
+
const validation = props.validation || { required: false };
|
|
33
|
+
(0, import_coercion.setCoercion)(validation, {
|
|
34
|
+
type: props.type,
|
|
35
|
+
name,
|
|
36
|
+
emptyAs
|
|
37
|
+
});
|
|
38
|
+
const {
|
|
39
|
+
ref: _ref,
|
|
40
|
+
onBlur: handleBlur,
|
|
41
|
+
onChange: handleChange,
|
|
42
|
+
...rest
|
|
43
|
+
} = register(name, validation);
|
|
44
|
+
const onBlur = (event) => {
|
|
45
|
+
handleBlur(event);
|
|
46
|
+
props.onBlur?.(event);
|
|
47
|
+
};
|
|
48
|
+
const onChange = (event) => {
|
|
49
|
+
handleChange(event);
|
|
50
|
+
props.onChange?.(event);
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
...rest,
|
|
54
|
+
ref: (element) => {
|
|
55
|
+
_ref(element);
|
|
56
|
+
if (typeof ref === "function") {
|
|
57
|
+
ref(element);
|
|
58
|
+
} else if (ref) {
|
|
59
|
+
ref.current = element;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
onBlur,
|
|
63
|
+
onChange
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {
|
|
68
|
+
useRegister
|
|
69
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedarjs/forms",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
7
|
+
"directory": "packages/forms"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"default": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"main": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsx ./build.mts && yarn build:types",
|
|
26
|
+
"build:pack": "yarn pack -o cedar-forms.tgz",
|
|
27
|
+
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
|
28
|
+
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
|
|
29
|
+
"check:attw": "yarn rw-fwtools-attw",
|
|
30
|
+
"check:package": "concurrently npm:check:attw yarn:publint",
|
|
31
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"test:watch": "vitest watch"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"graphql": "16.9.0",
|
|
37
|
+
"pascalcase": "1.0.0",
|
|
38
|
+
"react-hook-form": "7.54.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@cedarjs/framework-tools": "0.0.4",
|
|
42
|
+
"@testing-library/dom": "9.3.4",
|
|
43
|
+
"@testing-library/jest-dom": "6.5.0",
|
|
44
|
+
"@testing-library/react": "14.3.1",
|
|
45
|
+
"@testing-library/user-event": "14.5.2",
|
|
46
|
+
"@types/pascalcase": "1.0.3",
|
|
47
|
+
"@types/react": "^18.2.55",
|
|
48
|
+
"@types/react-dom": "^18.2.19",
|
|
49
|
+
"concurrently": "8.2.2",
|
|
50
|
+
"nodemon": "3.1.9",
|
|
51
|
+
"publint": "0.3.11",
|
|
52
|
+
"react": "18.3.1",
|
|
53
|
+
"react-dom": "18.3.1",
|
|
54
|
+
"tsx": "4.19.3",
|
|
55
|
+
"typescript": "5.6.2",
|
|
56
|
+
"vitest": "2.1.9"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react": "18.3.1"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
64
|
+
"gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
|
|
65
|
+
}
|