@bacano/sdk 1.1.15 → 1.1.18
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/README.md +14 -0
- package/STOREFRONT_CONTENT_LOCATOR.md +111 -0
- package/STOREFRONT_FORMS.md +211 -0
- package/client.d.ts +8 -1
- package/content-entry-locator.d.ts +16 -0
- package/index.cjs +195 -2
- package/index.d.ts +3 -1
- package/index.js +195 -2
- package/modules/forms.d.ts +9 -0
- package/package.json +6 -1
- package/react/form-field-control.d.ts +11 -0
- package/react/forms.cjs +460 -0
- package/react/forms.d.ts +49 -0
- package/react/forms.js +446 -0
- package/react.cjs +172 -2
- package/react.js +172 -2
- package/skills/bacano-figma-web/SKILL.md +8 -0
- package/skills/bacano-figma-web/references/bacano-mapping.md +5 -3
- package/skills/bacano-figma-web/references/content-workflow.md +11 -1
- package/skills/bacano-figma-web/references/implementation-quality.md +12 -0
- package/skills/bacano-web/SKILL.md +7 -1
- package/skills/bacano-web/references/common.md +7 -0
- package/skills/bacano-web/references/forms.md +67 -0
- package/skills/bacano-web/references/informational.md +3 -0
- package/types.d.ts +87 -0
package/react/forms.cjs
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
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
|
+
|
|
20
|
+
// libs/sdk/src/react/forms.tsx
|
|
21
|
+
var forms_exports = {};
|
|
22
|
+
__export(forms_exports, {
|
|
23
|
+
BacanoForm: () => BacanoForm,
|
|
24
|
+
useBacanoForm: () => useBacanoForm
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(forms_exports);
|
|
27
|
+
var import_react3 = require("react");
|
|
28
|
+
|
|
29
|
+
// libs/sdk/src/react/form-field-control.tsx
|
|
30
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
+
function BacanoFormFieldControl(props) {
|
|
32
|
+
const { field, className, value, onChange } = props;
|
|
33
|
+
const controlId = props.id ?? field.key;
|
|
34
|
+
const common = {
|
|
35
|
+
id: controlId,
|
|
36
|
+
name: field.key,
|
|
37
|
+
required: field.required,
|
|
38
|
+
className
|
|
39
|
+
};
|
|
40
|
+
if (field.type === "textarea") {
|
|
41
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
42
|
+
"textarea",
|
|
43
|
+
{
|
|
44
|
+
...common,
|
|
45
|
+
value: typeof value === "string" ? value : "",
|
|
46
|
+
placeholder: field.placeholder ?? void 0,
|
|
47
|
+
minLength: field.validation?.minLength ?? void 0,
|
|
48
|
+
maxLength: field.validation?.maxLength ?? void 0,
|
|
49
|
+
onChange: (event) => onChange(event.target.value)
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (field.type === "select" || field.type === "multi_select") {
|
|
54
|
+
const isMultiple = field.type === "multi_select";
|
|
55
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
56
|
+
"select",
|
|
57
|
+
{
|
|
58
|
+
...common,
|
|
59
|
+
multiple: isMultiple,
|
|
60
|
+
value: isMultiple && Array.isArray(value) ? value : String(value ?? ""),
|
|
61
|
+
onChange: (event) => onChange(
|
|
62
|
+
isMultiple ? Array.from(
|
|
63
|
+
event.target.selectedOptions,
|
|
64
|
+
(option) => option.value
|
|
65
|
+
) : event.target.value
|
|
66
|
+
),
|
|
67
|
+
children: [
|
|
68
|
+
!isMultiple ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("option", { value: "", children: field.placeholder ?? "Selecciona una opci\xF3n" }) : null,
|
|
69
|
+
(field.options ?? []).map((option) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("option", { value: option.value, children: option.label }, option.value))
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (field.type === "radio") {
|
|
75
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { role: "radiogroup", "aria-labelledby": `${controlId}-label`, children: (field.options ?? []).map((option) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("label", { children: [
|
|
76
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
77
|
+
"input",
|
|
78
|
+
{
|
|
79
|
+
type: "radio",
|
|
80
|
+
name: field.key,
|
|
81
|
+
value: option.value,
|
|
82
|
+
checked: value === option.value,
|
|
83
|
+
required: field.required,
|
|
84
|
+
onChange: (event) => onChange(event.target.value)
|
|
85
|
+
}
|
|
86
|
+
),
|
|
87
|
+
option.label
|
|
88
|
+
] }, option.value)) });
|
|
89
|
+
}
|
|
90
|
+
if (field.type === "checkbox") {
|
|
91
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
92
|
+
"input",
|
|
93
|
+
{
|
|
94
|
+
...common,
|
|
95
|
+
type: "checkbox",
|
|
96
|
+
checked: value === true,
|
|
97
|
+
onChange: (event) => onChange(event.target.checked)
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
102
|
+
"input",
|
|
103
|
+
{
|
|
104
|
+
...common,
|
|
105
|
+
type: field.type === "phone" ? "tel" : field.type,
|
|
106
|
+
value: typeof value === "string" ? value : "",
|
|
107
|
+
placeholder: field.placeholder ?? void 0,
|
|
108
|
+
min: field.validation?.min ?? void 0,
|
|
109
|
+
max: field.validation?.max ?? void 0,
|
|
110
|
+
minLength: field.validation?.minLength ?? void 0,
|
|
111
|
+
maxLength: field.validation?.maxLength ?? void 0,
|
|
112
|
+
onChange: (event) => onChange(event.target.value)
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// libs/sdk/src/react/hooks.ts
|
|
118
|
+
var import_react2 = require("react");
|
|
119
|
+
|
|
120
|
+
// libs/sdk/src/react/context-registry.ts
|
|
121
|
+
var import_react = require("react");
|
|
122
|
+
var REACT_CONTEXT_REGISTRY = /* @__PURE__ */ Symbol.for(
|
|
123
|
+
"@bacano/sdk/react-context-registry/v1"
|
|
124
|
+
);
|
|
125
|
+
function getReactContextRegistry() {
|
|
126
|
+
const globalRegistry = globalThis;
|
|
127
|
+
const existing = globalRegistry[REACT_CONTEXT_REGISTRY];
|
|
128
|
+
if (existing instanceof Map) {
|
|
129
|
+
return existing;
|
|
130
|
+
}
|
|
131
|
+
const registry = /* @__PURE__ */ new Map();
|
|
132
|
+
globalRegistry[REACT_CONTEXT_REGISTRY] = registry;
|
|
133
|
+
return registry;
|
|
134
|
+
}
|
|
135
|
+
function getOrCreateReactContext(key, defaultValue) {
|
|
136
|
+
const registry = getReactContextRegistry();
|
|
137
|
+
const existing = registry.get(key);
|
|
138
|
+
if (existing) {
|
|
139
|
+
return existing;
|
|
140
|
+
}
|
|
141
|
+
const context = (0, import_react.createContext)(defaultValue);
|
|
142
|
+
registry.set(key, context);
|
|
143
|
+
return context;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// libs/sdk/src/react/context.ts
|
|
147
|
+
var BacanoReactContext = getOrCreateReactContext("bacano-client-v1", {
|
|
148
|
+
client: null,
|
|
149
|
+
loading: true,
|
|
150
|
+
error: null
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// libs/sdk/src/react/hooks.ts
|
|
154
|
+
function useBacanoState() {
|
|
155
|
+
return (0, import_react2.useContext)(BacanoReactContext);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// libs/sdk/src/react/forms.tsx
|
|
159
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
160
|
+
var EMPTY_FORM_FIELDS = [];
|
|
161
|
+
var EMPTY_FORM_SECTIONS = [];
|
|
162
|
+
var EMPTY_FORM_STEPS = [];
|
|
163
|
+
function useBacanoForm(formKey, options) {
|
|
164
|
+
const {
|
|
165
|
+
client,
|
|
166
|
+
loading: clientLoading,
|
|
167
|
+
error: clientError
|
|
168
|
+
} = useBacanoState();
|
|
169
|
+
const enabled = options?.enabled ?? true;
|
|
170
|
+
const initialForm = options?.initialForm?.key === formKey ? options.initialForm : null;
|
|
171
|
+
const [form, setForm] = (0, import_react3.useState)(initialForm);
|
|
172
|
+
const formRef = (0, import_react3.useRef)(initialForm);
|
|
173
|
+
const [loading, setLoading] = (0, import_react3.useState)(enabled && !initialForm);
|
|
174
|
+
const [error, setError] = (0, import_react3.useState)(null);
|
|
175
|
+
const load = (0, import_react3.useCallback)(
|
|
176
|
+
async (force = false) => {
|
|
177
|
+
if (!enabled || !client) return;
|
|
178
|
+
setLoading(formRef.current === null);
|
|
179
|
+
setError(null);
|
|
180
|
+
try {
|
|
181
|
+
const nextForm = await client.forms.getDefinition(formKey, { force });
|
|
182
|
+
formRef.current = nextForm;
|
|
183
|
+
setForm(nextForm);
|
|
184
|
+
} catch (loadError) {
|
|
185
|
+
if (!formRef.current) setForm(null);
|
|
186
|
+
setError(
|
|
187
|
+
loadError instanceof Error ? loadError : new Error(String(loadError))
|
|
188
|
+
);
|
|
189
|
+
} finally {
|
|
190
|
+
setLoading(false);
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
[client, enabled, formKey]
|
|
194
|
+
);
|
|
195
|
+
(0, import_react3.useEffect)(() => {
|
|
196
|
+
if (!enabled) {
|
|
197
|
+
formRef.current = null;
|
|
198
|
+
setForm(null);
|
|
199
|
+
setLoading(false);
|
|
200
|
+
setError(null);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (client) void load();
|
|
204
|
+
}, [client, enabled, load]);
|
|
205
|
+
return {
|
|
206
|
+
form,
|
|
207
|
+
loading: enabled && form === null && (clientLoading || loading),
|
|
208
|
+
error: clientError ?? error,
|
|
209
|
+
refresh: async () => load(true)
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function FormFields(props) {
|
|
213
|
+
return props.fields.map((field) => {
|
|
214
|
+
const controlId = `${props.idPrefix}-${field.key}`;
|
|
215
|
+
const value = props.values[field.key] ?? (field.type === "checkbox" ? false : field.type === "multi_select" ? [] : "");
|
|
216
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: props.classNames?.field, children: [
|
|
217
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
218
|
+
"label",
|
|
219
|
+
{
|
|
220
|
+
id: `${controlId}-label`,
|
|
221
|
+
htmlFor: field.type === "radio" ? void 0 : controlId,
|
|
222
|
+
className: props.classNames?.label,
|
|
223
|
+
children: field.label
|
|
224
|
+
}
|
|
225
|
+
),
|
|
226
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
227
|
+
BacanoFormFieldControl,
|
|
228
|
+
{
|
|
229
|
+
field,
|
|
230
|
+
id: controlId,
|
|
231
|
+
value,
|
|
232
|
+
className: props.classNames?.input,
|
|
233
|
+
onChange: (nextValue) => props.onChange(field.key, nextValue)
|
|
234
|
+
}
|
|
235
|
+
),
|
|
236
|
+
field.helpText ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: props.classNames?.helpText, children: field.helpText }) : null
|
|
237
|
+
] }, field.key);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
function FormSections(props) {
|
|
241
|
+
return props.sections.map((section) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("section", { className: props.classNames?.section, children: [
|
|
242
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { className: props.classNames?.sectionTitle, children: section.title }),
|
|
243
|
+
section.description ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: props.classNames?.sectionDescription, children: section.description }) : null,
|
|
244
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
245
|
+
FormFields,
|
|
246
|
+
{
|
|
247
|
+
fields: section.fields,
|
|
248
|
+
idPrefix: props.idPrefix,
|
|
249
|
+
values: props.values,
|
|
250
|
+
classNames: props.classNames,
|
|
251
|
+
onChange: props.onChange
|
|
252
|
+
}
|
|
253
|
+
)
|
|
254
|
+
] }, section.key));
|
|
255
|
+
}
|
|
256
|
+
function BacanoForm(props) {
|
|
257
|
+
const reactId = (0, import_react3.useId)();
|
|
258
|
+
const idPrefix = `${props.formKey}-${reactId.replace(/:/g, "")}`;
|
|
259
|
+
const { client } = useBacanoState();
|
|
260
|
+
const { form, loading, error } = useBacanoForm(props.formKey, {
|
|
261
|
+
initialForm: props.initialForm
|
|
262
|
+
});
|
|
263
|
+
const [values, setValues] = (0, import_react3.useState)({});
|
|
264
|
+
const [step, setStep] = (0, import_react3.useState)(0);
|
|
265
|
+
const [submitting, setSubmitting] = (0, import_react3.useState)(false);
|
|
266
|
+
const [submitted, setSubmitted] = (0, import_react3.useState)(false);
|
|
267
|
+
const [submissionError, setSubmissionError] = (0, import_react3.useState)(null);
|
|
268
|
+
const [honeypot, setHoneypot] = (0, import_react3.useState)("");
|
|
269
|
+
const idempotencyKey = (0, import_react3.useRef)(crypto.randomUUID());
|
|
270
|
+
const fields = form?.definition.fields ?? EMPTY_FORM_FIELDS;
|
|
271
|
+
const sections = form?.definition.sections ?? EMPTY_FORM_SECTIONS;
|
|
272
|
+
const steps = form?.definition.steps ?? EMPTY_FORM_STEPS;
|
|
273
|
+
const fieldKeys = (0, import_react3.useMemo)(
|
|
274
|
+
() => new Set(
|
|
275
|
+
[
|
|
276
|
+
...fields,
|
|
277
|
+
...sections.flatMap((section) => section.fields),
|
|
278
|
+
...steps.flatMap((formStep) => [
|
|
279
|
+
...formStep.fields,
|
|
280
|
+
...formStep.sections.flatMap((section) => section.fields)
|
|
281
|
+
])
|
|
282
|
+
].map((field) => field.key)
|
|
283
|
+
),
|
|
284
|
+
[fields, sections, steps]
|
|
285
|
+
);
|
|
286
|
+
(0, import_react3.useEffect)(() => {
|
|
287
|
+
setStep(0);
|
|
288
|
+
setValues(
|
|
289
|
+
(current) => Object.fromEntries(
|
|
290
|
+
Object.entries(current).filter(([key]) => fieldKeys.has(key))
|
|
291
|
+
)
|
|
292
|
+
);
|
|
293
|
+
setSubmitted(false);
|
|
294
|
+
setSubmissionError(null);
|
|
295
|
+
setHoneypot("");
|
|
296
|
+
idempotencyKey.current = crypto.randomUUID();
|
|
297
|
+
}, [fieldKeys, form?.version]);
|
|
298
|
+
if (loading) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: props.loadingFallback ?? null });
|
|
299
|
+
if (error && !form)
|
|
300
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: props.errorFallback?.(error) ?? props.unavailableFallback ?? null });
|
|
301
|
+
if (!form) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: props.unavailableFallback ?? null });
|
|
302
|
+
if (form.status === "PAUSED") {
|
|
303
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: props.classNames?.message, "data-bacano-form-unavailable": true, children: props.unavailableFallback ?? form.definition.unavailableMessage });
|
|
304
|
+
}
|
|
305
|
+
if (submitted) {
|
|
306
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: props.classNames?.message, "data-bacano-form-success": true, children: form.definition.successMessage });
|
|
307
|
+
}
|
|
308
|
+
const isMultiStep = steps.length > 0;
|
|
309
|
+
const activeStep = isMultiStep ? steps[step] : null;
|
|
310
|
+
const isLastStep = !isMultiStep || step === steps.length - 1;
|
|
311
|
+
const setFieldValue = (key, value) => setValues((current) => ({ ...current, [key]: value }));
|
|
312
|
+
const submit = async (event) => {
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
if (!isLastStep) {
|
|
315
|
+
setStep((current) => current + 1);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
setSubmitting(true);
|
|
319
|
+
setSubmissionError(null);
|
|
320
|
+
try {
|
|
321
|
+
let responseId = null;
|
|
322
|
+
if (props.onSubmit) {
|
|
323
|
+
await props.onSubmit(values, form);
|
|
324
|
+
} else {
|
|
325
|
+
if (!client) throw new Error("Bacano client is not ready");
|
|
326
|
+
const result = await client.forms.submitResponse({
|
|
327
|
+
formKey: form.key,
|
|
328
|
+
formVersion: form.version,
|
|
329
|
+
definitionHash: form.definitionHash,
|
|
330
|
+
idempotencyKey: idempotencyKey.current,
|
|
331
|
+
values,
|
|
332
|
+
honeypot
|
|
333
|
+
});
|
|
334
|
+
if (!result.accepted) throw new Error(result.message);
|
|
335
|
+
responseId = result.responseId;
|
|
336
|
+
}
|
|
337
|
+
setSubmitted(true);
|
|
338
|
+
props.onSubmitted?.(responseId);
|
|
339
|
+
} catch (submitError) {
|
|
340
|
+
setSubmissionError(
|
|
341
|
+
submitError instanceof Error ? submitError : new Error(String(submitError))
|
|
342
|
+
);
|
|
343
|
+
} finally {
|
|
344
|
+
setSubmitting(false);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
348
|
+
"form",
|
|
349
|
+
{
|
|
350
|
+
className: props.classNames?.root,
|
|
351
|
+
"data-bacano-form": form.key,
|
|
352
|
+
"data-bacano-form-version": form.version,
|
|
353
|
+
onSubmit: (event) => void submit(event),
|
|
354
|
+
children: [
|
|
355
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
356
|
+
"div",
|
|
357
|
+
{
|
|
358
|
+
"aria-hidden": "true",
|
|
359
|
+
style: {
|
|
360
|
+
position: "absolute",
|
|
361
|
+
width: 1,
|
|
362
|
+
height: 1,
|
|
363
|
+
overflow: "hidden",
|
|
364
|
+
clip: "rect(0 0 0 0)"
|
|
365
|
+
},
|
|
366
|
+
children: [
|
|
367
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("label", { htmlFor: `${form.key}-company-website`, children: "Empresa" }),
|
|
368
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
369
|
+
"input",
|
|
370
|
+
{
|
|
371
|
+
id: `${form.key}-company-website`,
|
|
372
|
+
name: "company_website",
|
|
373
|
+
tabIndex: -1,
|
|
374
|
+
autoComplete: "off",
|
|
375
|
+
value: honeypot,
|
|
376
|
+
onChange: (event) => setHoneypot(event.target.value)
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
]
|
|
380
|
+
}
|
|
381
|
+
),
|
|
382
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { className: props.classNames?.title, children: form.name }),
|
|
383
|
+
form.description ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: props.classNames?.description, children: form.description }) : null,
|
|
384
|
+
activeStep ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("section", { className: props.classNames?.step, "data-bacano-form-step": true, children: [
|
|
385
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { className: props.classNames?.stepTitle, children: activeStep.title }),
|
|
386
|
+
activeStep.description ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: props.classNames?.stepDescription, children: activeStep.description }) : null,
|
|
387
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
388
|
+
FormFields,
|
|
389
|
+
{
|
|
390
|
+
fields: activeStep.fields,
|
|
391
|
+
idPrefix,
|
|
392
|
+
values,
|
|
393
|
+
classNames: props.classNames,
|
|
394
|
+
onChange: setFieldValue
|
|
395
|
+
}
|
|
396
|
+
),
|
|
397
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
398
|
+
FormSections,
|
|
399
|
+
{
|
|
400
|
+
sections: activeStep.sections,
|
|
401
|
+
idPrefix,
|
|
402
|
+
values,
|
|
403
|
+
classNames: props.classNames,
|
|
404
|
+
onChange: setFieldValue
|
|
405
|
+
}
|
|
406
|
+
)
|
|
407
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
408
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
409
|
+
FormFields,
|
|
410
|
+
{
|
|
411
|
+
fields,
|
|
412
|
+
idPrefix,
|
|
413
|
+
values,
|
|
414
|
+
classNames: props.classNames,
|
|
415
|
+
onChange: setFieldValue
|
|
416
|
+
}
|
|
417
|
+
),
|
|
418
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
419
|
+
FormSections,
|
|
420
|
+
{
|
|
421
|
+
sections,
|
|
422
|
+
idPrefix,
|
|
423
|
+
values,
|
|
424
|
+
classNames: props.classNames,
|
|
425
|
+
onChange: setFieldValue
|
|
426
|
+
}
|
|
427
|
+
)
|
|
428
|
+
] }),
|
|
429
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: props.classNames?.actions, children: [
|
|
430
|
+
isMultiStep && step > 0 ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
431
|
+
"button",
|
|
432
|
+
{
|
|
433
|
+
type: "button",
|
|
434
|
+
onClick: () => setStep((current) => current - 1),
|
|
435
|
+
children: "Anterior"
|
|
436
|
+
}
|
|
437
|
+
) : null,
|
|
438
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
439
|
+
"button",
|
|
440
|
+
{
|
|
441
|
+
type: "submit",
|
|
442
|
+
className: props.classNames?.button,
|
|
443
|
+
disabled: submitting,
|
|
444
|
+
children: isLastStep ? form.definition.submitLabel : "Continuar"
|
|
445
|
+
}
|
|
446
|
+
)
|
|
447
|
+
] }),
|
|
448
|
+
submissionError ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
449
|
+
"div",
|
|
450
|
+
{
|
|
451
|
+
role: "alert",
|
|
452
|
+
className: props.classNames?.message,
|
|
453
|
+
"data-bacano-form-error": true,
|
|
454
|
+
children: submissionError.message
|
|
455
|
+
}
|
|
456
|
+
) : null
|
|
457
|
+
]
|
|
458
|
+
}
|
|
459
|
+
);
|
|
460
|
+
}
|
package/react/forms.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { WebsitePublishedForm } from "../types";
|
|
3
|
+
export type BacanoFormValues = Record<string, string | string[] | boolean>;
|
|
4
|
+
export interface UseBacanoFormOptions {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
/** Definition embedded by a server/build to preserve Static First rendering. */
|
|
7
|
+
initialForm?: WebsitePublishedForm | null;
|
|
8
|
+
}
|
|
9
|
+
export interface UseBacanoFormResult {
|
|
10
|
+
form: WebsitePublishedForm | null;
|
|
11
|
+
loading: boolean;
|
|
12
|
+
error: Error | null;
|
|
13
|
+
refresh(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare function useBacanoForm(formKey: string, options?: UseBacanoFormOptions): UseBacanoFormResult;
|
|
16
|
+
export interface BacanoFormClassNames {
|
|
17
|
+
root?: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
section?: string;
|
|
21
|
+
sectionTitle?: string;
|
|
22
|
+
sectionDescription?: string;
|
|
23
|
+
step?: string;
|
|
24
|
+
stepTitle?: string;
|
|
25
|
+
stepDescription?: string;
|
|
26
|
+
field?: string;
|
|
27
|
+
label?: string;
|
|
28
|
+
input?: string;
|
|
29
|
+
helpText?: string;
|
|
30
|
+
actions?: string;
|
|
31
|
+
button?: string;
|
|
32
|
+
message?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface BacanoFormProps {
|
|
35
|
+
formKey: string;
|
|
36
|
+
/** Definition embedded by a server/build; it is revalidated in runtime. */
|
|
37
|
+
initialForm?: WebsitePublishedForm | null;
|
|
38
|
+
/**
|
|
39
|
+
* Optional custom submission. When omitted, the official Bacano response
|
|
40
|
+
* endpoint is used. Existing storefronts may keep a custom handler.
|
|
41
|
+
*/
|
|
42
|
+
onSubmit?(values: BacanoFormValues, form: WebsitePublishedForm): void | Promise<void>;
|
|
43
|
+
onSubmitted?(responseId: string | null): void;
|
|
44
|
+
classNames?: BacanoFormClassNames;
|
|
45
|
+
loadingFallback?: ReactNode;
|
|
46
|
+
unavailableFallback?: ReactNode;
|
|
47
|
+
errorFallback?: (error: Error) => ReactNode;
|
|
48
|
+
}
|
|
49
|
+
export declare function BacanoForm(props: BacanoFormProps): import("react").JSX.Element;
|