@raketa-builder/next-form 1.0.8 → 1.1.0

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.
Files changed (2) hide show
  1. package/dist/index.js +331 -0
  2. package/package.json +2 -2
package/dist/index.js ADDED
@@ -0,0 +1,331 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
51
+ mod
52
+ ));
53
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
54
+
55
+ // src/index.js
56
+ var src_exports = {};
57
+ __export(src_exports, {
58
+ default: () => src_default
59
+ });
60
+ module.exports = __toCommonJS(src_exports);
61
+
62
+ // src/Form.js
63
+ var import_react2 = __toESM(require("react"));
64
+
65
+ // src/Context.js
66
+ var import_react = __toESM(require("react"));
67
+ var Context_default = import_react.default.createContext({});
68
+
69
+ // src/Form.js
70
+ var import_jsx_runtime = require("react/jsx-runtime");
71
+ var Form = (_a) => {
72
+ var _b = _a, { children, defaultValues = {}, onSubmit } = _b, rest = __objRest(_b, ["children", "defaultValues", "onSubmit"]);
73
+ const [state, setState] = import_react2.default.useState({ submitting: false, errors: {} });
74
+ const setErrors = (errors) => {
75
+ let newErrors = {};
76
+ errors.map((error) => newErrors[error.key] = error.message);
77
+ if (state.submitting) {
78
+ setState({
79
+ submitting: false,
80
+ errors: newErrors
81
+ });
82
+ } else {
83
+ setState({
84
+ submitting: state.submitting,
85
+ errors: newErrors
86
+ });
87
+ }
88
+ };
89
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
90
+ "form",
91
+ __spreadProps(__spreadValues({
92
+ onSubmit: (e) => {
93
+ if (!onSubmit) {
94
+ return;
95
+ }
96
+ e.preventDefault();
97
+ const formData = new FormData(e.target);
98
+ setState(__spreadProps(__spreadValues({}, state), { submitting: true }));
99
+ onSubmit(formData, setErrors, e.target);
100
+ }
101
+ }, rest), {
102
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
103
+ Context_default.Provider,
104
+ {
105
+ value: {
106
+ defaultValues,
107
+ errors: state.errors,
108
+ submitting: state.submitting
109
+ },
110
+ children
111
+ }
112
+ )
113
+ })
114
+ );
115
+ };
116
+ var Form_default = Form;
117
+
118
+ // src/Fields.js
119
+ var import_react3 = __toESM(require("react"));
120
+ var import_ui = require("@raketa-builder/ui");
121
+ var import_jsx_runtime2 = require("react/jsx-runtime");
122
+ var HiddenInput = ({ value, name }) => {
123
+ if (typeof value === "string") {
124
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Input, { type: "hidden", defaultValue: value, name });
125
+ } else {
126
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Input, { type: "hidden", defaultValue: JSON.stringify(value), name });
127
+ }
128
+ };
129
+ var CustomField = ({ component, name }) => {
130
+ const { defaultValues, errors } = import_react3.default.useContext(Context_default);
131
+ const [value, setValue] = import_react3.default.useState(defaultValues[name]);
132
+ const Component = component;
133
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react3.default.Fragment, { children: [
134
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { onChange: (newValue) => setValue(newValue), value }),
135
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(HiddenInput, { name, value }),
136
+ errors[name] && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { type: "error", children: errors[name] })
137
+ ] });
138
+ };
139
+ var CheckboxGroup = ({ name, label, hint = "", entities }) => {
140
+ const { defaultValues, errors } = import_react3.default.useContext(Context_default);
141
+ let defaultChecked = [];
142
+ if (defaultValues[name]) {
143
+ defaultChecked = Array.isArray(defaultValues[name]) ? defaultValues[name] : [defaultValues[name]];
144
+ }
145
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_ui.FormGroup, { children: [
146
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Label, { htmlFor: name, children: label }),
147
+ entities.map((entity, idx) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
148
+ "label",
149
+ {
150
+ className: (0, import_ui.cn)("r-reset", "r-next-form-checkbox-label"),
151
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
152
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
153
+ "input",
154
+ {
155
+ type: "checkbox",
156
+ name,
157
+ value: `${entity.value}`,
158
+ defaultChecked: !!defaultChecked.find((dv) => dv === entity.value)
159
+ }
160
+ ),
161
+ "\xA0",
162
+ entity.label
163
+ ] })
164
+ },
165
+ `${entity.value}-${idx}`
166
+ )),
167
+ hint && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { children: hint }),
168
+ errors[name] && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { type: "error", children: errors[name] })
169
+ ] });
170
+ };
171
+ var Checkbox = (_a) => {
172
+ var _b = _a, { name, label, hint = "" } = _b, rest = __objRest(_b, ["name", "label", "hint"]);
173
+ const { defaultValues, errors } = import_react3.default.useContext(Context_default);
174
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_ui.FormGroup, { children: [
175
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Label, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_ui.Stack, { className: "r-stack-align-center", children: [
176
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
177
+ "input",
178
+ __spreadValues({
179
+ type: "checkbox",
180
+ name,
181
+ defaultChecked: defaultValues[name]
182
+ }, rest)
183
+ ),
184
+ "\xA0",
185
+ label
186
+ ] }) }),
187
+ hint && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { children: hint }),
188
+ errors[name] && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { type: "error", children: errors[name] })
189
+ ] });
190
+ };
191
+ var Field = (_a) => {
192
+ var _b = _a, {
193
+ as = "input",
194
+ hint = "",
195
+ name,
196
+ label,
197
+ children
198
+ } = _b, props = __objRest(_b, [
199
+ "as",
200
+ "hint",
201
+ "name",
202
+ "label",
203
+ "children"
204
+ ]);
205
+ const { defaultValues, errors } = import_react3.default.useContext(Context_default);
206
+ const types = {
207
+ input: import_ui.Input,
208
+ select: import_ui.Select,
209
+ textarea: import_ui.Textarea
210
+ };
211
+ const Component = types[as] || import_ui.Input;
212
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_ui.FormGroup, { children: [
213
+ label && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Label, { htmlFor: name, children: label }),
214
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
215
+ Component,
216
+ __spreadProps(__spreadValues({
217
+ id: name,
218
+ name,
219
+ defaultValue: defaultValues[name]
220
+ }, props), {
221
+ children
222
+ })
223
+ ),
224
+ hint && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { children: hint }),
225
+ errors[name] && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui.Hint, { type: "error", children: errors[name] })
226
+ ] });
227
+ };
228
+
229
+ // src/Submit.js
230
+ var import_react4 = __toESM(require("react"));
231
+ var import_ui2 = require("@raketa-builder/ui");
232
+ var import_jsx_runtime3 = require("react/jsx-runtime");
233
+ var Submit = ({ children }) => {
234
+ const { submitting } = import_react4.default.useContext(Context_default);
235
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_ui2.Button, { disabled: submitting, type: "submit", variant: "primary", children });
236
+ };
237
+ var Submit_default = Submit;
238
+
239
+ // src/Fieldset.js
240
+ var import_react5 = __toESM(require("react"));
241
+ var import_ui3 = require("@raketa-builder/ui");
242
+
243
+ // src/utils/slugify.js
244
+ var slugify = (string) => string.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");
245
+ var slugify_default = slugify;
246
+
247
+ // src/Fieldset.js
248
+ var import_jsx_runtime4 = require("react/jsx-runtime");
249
+ var Wrapper = (_a) => {
250
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
251
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
252
+ "div",
253
+ __spreadValues({
254
+ className: (0, import_ui3.cn)("r-reset", "r-next-form-fieldset-wrapper", className)
255
+ }, rest)
256
+ );
257
+ };
258
+ var LegendWrapper = (_a) => {
259
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
260
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
261
+ "div",
262
+ __spreadValues({
263
+ className: (0, import_ui3.cn)("r-reset", "r-next-form-legend-wrapper", className)
264
+ }, rest)
265
+ );
266
+ };
267
+ var Legend = (_a) => {
268
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
269
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", __spreadValues({ className: (0, import_ui3.cn)("r-reset", "r-next-form-legend", className) }, rest));
270
+ };
271
+ var Description = (_a) => {
272
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
273
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
274
+ "div",
275
+ __spreadValues({
276
+ className: (0, import_ui3.cn)("r-reset", "r-next-form-description", className)
277
+ }, rest)
278
+ );
279
+ };
280
+ var FieldWrapper = (_a) => {
281
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
282
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
283
+ "div",
284
+ __spreadValues({
285
+ className: (0, import_ui3.cn)("r-reset", "r-next-form-field-wrapper", className)
286
+ }, rest)
287
+ );
288
+ };
289
+ var Fieldset = ({ label, description, children }) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(Wrapper, { id: slugify_default(label), children: [
290
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(LegendWrapper, { children: [
291
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Legend, { children: label }),
292
+ description && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Description, { dangerouslySetInnerHTML: { __html: description } })
293
+ ] }),
294
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(FieldWrapper, { children })
295
+ ] });
296
+ var Fieldset_default = Fieldset;
297
+
298
+ // src/FormNavigation.js
299
+ var import_react6 = __toESM(require("react"));
300
+ var import_ui4 = require("@raketa-builder/ui");
301
+ var import_jsx_runtime5 = require("react/jsx-runtime");
302
+ var Wrapper2 = (_a) => {
303
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
304
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
305
+ "div",
306
+ __spreadValues({
307
+ className: (0, import_ui4.cn)("r-reset", "r-next-form-nav-wrapper", className)
308
+ }, rest)
309
+ );
310
+ };
311
+ var Link = (_a) => {
312
+ var _b = _a, { className } = _b, rest = __objRest(_b, ["className"]);
313
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("a", __spreadValues({ className: (0, import_ui4.cn)("r-reset", "r-next-form-nav-link", className) }, rest));
314
+ };
315
+ var FormNavigation = ({ sections, children }) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(Wrapper2, { children: [
316
+ sections.map((section, idx) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Link, { href: `#${slugify_default(section)}`, children: section }, `${section}-${idx}`)),
317
+ children
318
+ ] });
319
+ var FormNavigation_default = FormNavigation;
320
+
321
+ // src/index.js
322
+ Form_default.Field = Field;
323
+ Form_default.CustomField = CustomField;
324
+ Form_default.CheckboxGroup = CheckboxGroup;
325
+ Form_default.Checkbox = Checkbox;
326
+ Form_default.Submit = Submit_default;
327
+ Form_default.Fieldset = Fieldset_default;
328
+ Form_default.FormNavigation = FormNavigation_default;
329
+ var src_default = Form_default;
330
+ // Annotate the CommonJS export names for ESM import in node:
331
+ 0 && (module.exports = {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raketa-builder/next-form",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "license": "MIT",
@@ -15,7 +15,7 @@
15
15
  "react-dom": "18.2.0"
16
16
  },
17
17
  "dependencies": {
18
- "@raketa-builder/ui": "1.0.8"
18
+ "@raketa-builder/ui": "1.1.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@biomejs/biome": "1.5.2",