@page-speed/forms 0.1.4 → 0.1.6
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 +1 -1
- package/dist/core.cjs +376 -21
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +356 -1
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +376 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +356 -1
- package/dist/index.js.map +1 -1
- package/dist/inputs.cjs +253 -0
- package/dist/inputs.cjs.map +1 -1
- package/dist/inputs.d.cts +77 -1
- package/dist/inputs.d.ts +77 -1
- package/dist/inputs.js +253 -1
- package/dist/inputs.js.map +1 -1
- package/dist/integration.cjs +243 -0
- package/dist/integration.cjs.map +1 -0
- package/dist/integration.d.cts +381 -0
- package/dist/integration.d.ts +381 -0
- package/dist/integration.js +217 -0
- package/dist/integration.js.map +1 -0
- package/dist/upload.cjs +348 -0
- package/dist/upload.cjs.map +1 -0
- package/dist/upload.d.cts +174 -0
- package/dist/upload.d.ts +174 -0
- package/dist/upload.js +326 -0
- package/dist/upload.js.map +1 -0
- package/dist/validation-rules.cjs +231 -75
- package/dist/validation-rules.cjs.map +1 -1
- package/dist/validation-rules.js +215 -1
- package/dist/validation-rules.js.map +1 -1
- package/dist/validation-utils.cjs +133 -43
- package/dist/validation-utils.cjs.map +1 -1
- package/dist/validation-utils.js +125 -1
- package/dist/validation-utils.js.map +1 -1
- package/dist/validation.cjs +364 -115
- package/dist/validation.cjs.map +1 -1
- package/dist/validation.js +339 -2
- package/dist/validation.js.map +1 -1
- package/package.json +14 -4
- package/dist/chunk-2FXAQT7S.cjs +0 -236
- package/dist/chunk-2FXAQT7S.cjs.map +0 -1
- package/dist/chunk-A3UV7BIN.js +0 -357
- package/dist/chunk-A3UV7BIN.js.map +0 -1
- package/dist/chunk-P37YLBFA.cjs +0 -138
- package/dist/chunk-P37YLBFA.cjs.map +0 -1
- package/dist/chunk-WHQMBQNI.js +0 -127
- package/dist/chunk-WHQMBQNI.js.map +0 -1
- package/dist/chunk-YTTOWHBZ.js +0 -217
- package/dist/chunk-YTTOWHBZ.js.map +0 -1
- package/dist/chunk-ZQCPEOB6.cjs +0 -382
- package/dist/chunk-ZQCPEOB6.cjs.map +0 -1
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopNamespace(e) {
|
|
6
|
+
if (e && e.__esModule) return e;
|
|
7
|
+
var n = Object.create(null);
|
|
8
|
+
if (e) {
|
|
9
|
+
Object.keys(e).forEach(function (k) {
|
|
10
|
+
if (k !== 'default') {
|
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return e[k]; }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
n.default = e;
|
|
20
|
+
return Object.freeze(n);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
24
|
+
|
|
25
|
+
// src/integration/ContactFormSerializer.ts
|
|
26
|
+
var STANDARD_FIELDS = [
|
|
27
|
+
"content",
|
|
28
|
+
"email",
|
|
29
|
+
"firstName",
|
|
30
|
+
"lastName",
|
|
31
|
+
"locationId",
|
|
32
|
+
"phone",
|
|
33
|
+
"subject",
|
|
34
|
+
"redemptionStatus",
|
|
35
|
+
"birthday",
|
|
36
|
+
"city",
|
|
37
|
+
"state",
|
|
38
|
+
"websiteFormAssignmentId",
|
|
39
|
+
"websiteId",
|
|
40
|
+
"acceptsSmsMarketing",
|
|
41
|
+
"acceptsEmailMarketing",
|
|
42
|
+
"visitorIpAddress"
|
|
43
|
+
];
|
|
44
|
+
function camelToSnake(str) {
|
|
45
|
+
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
46
|
+
}
|
|
47
|
+
function isStandardField(fieldName) {
|
|
48
|
+
return STANDARD_FIELDS.includes(fieldName);
|
|
49
|
+
}
|
|
50
|
+
function extractUploadTokens(values) {
|
|
51
|
+
const tokens = [];
|
|
52
|
+
for (const value of Object.values(values)) {
|
|
53
|
+
if (typeof value === "string" && value.startsWith("upload_")) {
|
|
54
|
+
tokens.push(value);
|
|
55
|
+
} else if (Array.isArray(value)) {
|
|
56
|
+
for (const item of value) {
|
|
57
|
+
if (typeof item === "string" && item.startsWith("upload_")) {
|
|
58
|
+
tokens.push(item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return tokens;
|
|
64
|
+
}
|
|
65
|
+
function formatDateForRails(value) {
|
|
66
|
+
if (value instanceof Date) {
|
|
67
|
+
return value.toISOString();
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === "string") {
|
|
70
|
+
const date = new Date(value);
|
|
71
|
+
if (!isNaN(date.getTime())) {
|
|
72
|
+
return date.toISOString();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
function serializeForRails(values, config) {
|
|
78
|
+
const standardFields = {};
|
|
79
|
+
const customFields = {};
|
|
80
|
+
const uploadTokens = extractUploadTokens(values);
|
|
81
|
+
for (const [key, value] of Object.entries(values)) {
|
|
82
|
+
if (typeof value === "string" && value.startsWith("upload_")) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(value) && value.every(
|
|
86
|
+
(item) => typeof item === "string" && item.startsWith("upload_")
|
|
87
|
+
)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const snakeKey = camelToSnake(key);
|
|
91
|
+
if (isStandardField(key)) {
|
|
92
|
+
if (key === "birthday") {
|
|
93
|
+
const formatted = formatDateForRails(value);
|
|
94
|
+
if (formatted) {
|
|
95
|
+
standardFields[snakeKey] = formatted;
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
standardFields[snakeKey] = value;
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
customFields[snakeKey] = value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (config.websiteId !== void 0) {
|
|
105
|
+
standardFields.website_id = config.websiteId;
|
|
106
|
+
}
|
|
107
|
+
if (config.websiteFormAssignmentId !== void 0) {
|
|
108
|
+
standardFields.website_form_assignment_id = config.websiteFormAssignmentId;
|
|
109
|
+
}
|
|
110
|
+
if (config.visitorIpAddress !== void 0) {
|
|
111
|
+
standardFields.visitor_ip_address = config.visitorIpAddress;
|
|
112
|
+
}
|
|
113
|
+
const contact = {
|
|
114
|
+
...standardFields
|
|
115
|
+
};
|
|
116
|
+
if (Object.keys(customFields).length > 0) {
|
|
117
|
+
contact.custom_fields = customFields;
|
|
118
|
+
}
|
|
119
|
+
if (uploadTokens.length > 0) {
|
|
120
|
+
contact.contact_form_upload_tokens = uploadTokens;
|
|
121
|
+
}
|
|
122
|
+
const serialized = {
|
|
123
|
+
api_key: config.apiKey,
|
|
124
|
+
contact
|
|
125
|
+
};
|
|
126
|
+
if (config.contactCategoryToken !== void 0) {
|
|
127
|
+
serialized.contact_category_token = config.contactCategoryToken;
|
|
128
|
+
}
|
|
129
|
+
if (config.locationId !== void 0) {
|
|
130
|
+
serialized.location_id = config.locationId;
|
|
131
|
+
}
|
|
132
|
+
return serialized;
|
|
133
|
+
}
|
|
134
|
+
function snakeToCamel(str) {
|
|
135
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
136
|
+
}
|
|
137
|
+
function deserializeErrors(railsErrors) {
|
|
138
|
+
const formErrors = {};
|
|
139
|
+
for (const [field, messages] of Object.entries(railsErrors.errors)) {
|
|
140
|
+
if (field === "base") {
|
|
141
|
+
formErrors._form = Array.isArray(messages) ? messages[0] : messages;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (field === "custom_fields" && typeof messages === "object") {
|
|
145
|
+
for (const [customField, customMessages] of Object.entries(messages)) {
|
|
146
|
+
const camelField2 = snakeToCamel(customField);
|
|
147
|
+
const errorMessage2 = Array.isArray(customMessages) ? customMessages[0] : customMessages;
|
|
148
|
+
formErrors[camelField2] = errorMessage2;
|
|
149
|
+
}
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const camelField = snakeToCamel(field);
|
|
153
|
+
const errorMessage = Array.isArray(messages) ? messages[0] : messages;
|
|
154
|
+
formErrors[camelField] = errorMessage;
|
|
155
|
+
}
|
|
156
|
+
return formErrors;
|
|
157
|
+
}
|
|
158
|
+
var ChaiBlockErrorBoundary = class extends React__namespace.Component {
|
|
159
|
+
constructor(props) {
|
|
160
|
+
super(props);
|
|
161
|
+
this.state = { error: null };
|
|
162
|
+
}
|
|
163
|
+
static getDerivedStateFromError(error) {
|
|
164
|
+
return { error };
|
|
165
|
+
}
|
|
166
|
+
componentDidCatch(error, errorInfo) {
|
|
167
|
+
console.error(`ChaiBlock render error (${this.props.block._id}):`, error, errorInfo);
|
|
168
|
+
}
|
|
169
|
+
render() {
|
|
170
|
+
if (this.state.error) {
|
|
171
|
+
if (this.props.fallback) {
|
|
172
|
+
return this.props.fallback(this.state.error, this.props.block);
|
|
173
|
+
}
|
|
174
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
175
|
+
"div",
|
|
176
|
+
{
|
|
177
|
+
className: "chai-block-error border border-red-300 bg-red-50 p-4 rounded text-red-700",
|
|
178
|
+
"data-block-id": this.props.block._id,
|
|
179
|
+
"data-block-type": this.props.block._type
|
|
180
|
+
},
|
|
181
|
+
/* @__PURE__ */ React__namespace.createElement("p", { className: "font-semibold" }, "Block Render Error"),
|
|
182
|
+
/* @__PURE__ */ React__namespace.createElement("p", { className: "text-sm" }, "Block: ", this.props.block._name || this.props.block._id, " (", this.props.block._type, ")"),
|
|
183
|
+
/* @__PURE__ */ React__namespace.createElement("p", { className: "text-sm mt-1" }, this.state.error.message)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return this.props.children;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
function createChaiBlockAdapter(Component2, options = {}) {
|
|
190
|
+
const {
|
|
191
|
+
defaultProps = {},
|
|
192
|
+
transformProps,
|
|
193
|
+
withErrorBoundary = true,
|
|
194
|
+
errorFallback
|
|
195
|
+
} = options;
|
|
196
|
+
const AdaptedComponent = ({ block, children, renderChildren }) => {
|
|
197
|
+
const blockProps = block.blockProps || {};
|
|
198
|
+
const mergedProps = { ...defaultProps, ...blockProps };
|
|
199
|
+
const finalProps = transformProps ? transformProps(mergedProps, block) : mergedProps;
|
|
200
|
+
const dataAttrs = {
|
|
201
|
+
"data-block-id": block._id,
|
|
202
|
+
"data-block-type": block._type,
|
|
203
|
+
...block._name && { "data-block-name": block._name }
|
|
204
|
+
};
|
|
205
|
+
const componentProps = {
|
|
206
|
+
...finalProps,
|
|
207
|
+
...dataAttrs
|
|
208
|
+
};
|
|
209
|
+
const renderedChildren = renderChildren ? renderChildren(block._id) : children;
|
|
210
|
+
const element = /* @__PURE__ */ React__namespace.createElement(Component2, { ...componentProps }, renderedChildren);
|
|
211
|
+
if (withErrorBoundary) {
|
|
212
|
+
return /* @__PURE__ */ React__namespace.createElement(ChaiBlockErrorBoundary, { block, fallback: errorFallback }, element);
|
|
213
|
+
}
|
|
214
|
+
return element;
|
|
215
|
+
};
|
|
216
|
+
const componentName = Component2.displayName || Component2.name || "Component";
|
|
217
|
+
AdaptedComponent.displayName = `ChaiBlockAdapter(${componentName})`;
|
|
218
|
+
return AdaptedComponent;
|
|
219
|
+
}
|
|
220
|
+
function standardInputTransformer(blockProps, block) {
|
|
221
|
+
return {
|
|
222
|
+
...blockProps,
|
|
223
|
+
// Use content as label if not already provided
|
|
224
|
+
...block.content && !blockProps.label && { label: block.content },
|
|
225
|
+
// Apply ChaiBlock styles as className
|
|
226
|
+
...block.styles && { className: block.styles }
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function createChaiBlockAdapters(components, options = {}) {
|
|
230
|
+
const adapted = {};
|
|
231
|
+
for (const [name, component] of Object.entries(components)) {
|
|
232
|
+
adapted[name] = createChaiBlockAdapter(component, options);
|
|
233
|
+
}
|
|
234
|
+
return adapted;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
exports.createChaiBlockAdapter = createChaiBlockAdapter;
|
|
238
|
+
exports.createChaiBlockAdapters = createChaiBlockAdapters;
|
|
239
|
+
exports.deserializeErrors = deserializeErrors;
|
|
240
|
+
exports.serializeForRails = serializeForRails;
|
|
241
|
+
exports.standardInputTransformer = standardInputTransformer;
|
|
242
|
+
//# sourceMappingURL=integration.cjs.map
|
|
243
|
+
//# sourceMappingURL=integration.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/integration/ContactFormSerializer.ts","../src/integration/ChaiBlockAdapter.tsx"],"names":["camelField","errorMessage","React","Component"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA,IAAM,eAAA,GAAkB;AAAA,EACtB,SAAA;AAAA,EACA,OAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,SAAA;AAAA,EACA,kBAAA;AAAA,EACA,UAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,yBAAA;AAAA,EACA,WAAA;AAAA,EACA,qBAAA;AAAA,EACA,uBAAA;AAAA,EACA;AACF,CAAA;AAmGA,SAAS,aAAa,GAAA,EAAqB;AACzC,EAAA,OAAO,GAAA,CAAI,QAAQ,QAAA,EAAU,CAAC,WAAW,CAAA,CAAA,EAAI,MAAA,CAAO,WAAA,EAAa,CAAA,CAAE,CAAA;AACrE;AAKA,SAAS,gBAAgB,SAAA,EAA4B;AACnD,EAAA,OAAO,eAAA,CAAgB,SAAS,SAA6C,CAAA;AAC/E;AAMA,SAAS,oBAAoB,MAAA,EA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AC1D,IAAA,OAAA,CAAQ,IAAI,CAAA,GAAI,sBAAA,CAAuB,SAAA,EAAW,OAAO,CAAA;AAAA,EAC3D;AAEA,EAAA,OAAO,OAAA;AACT","file":"integration.cjs","sourcesContent":["/**\n * @page-speed/forms - Rails API Serializer\n *\n * Serializes form data for the DashTrack ContactsController API.\n * Handles field name conversion (camelCase → snake_case), custom fields separation,\n * and upload token arrays.\n *\n * @see https://github.com/opensite-ai/page-speed-forms\n */\n\n/**\n * Standard fields recognized by the Rails ContactsController API.\n * These are serialized to snake_case and sent in the contact object.\n */\nconst STANDARD_FIELDS = [\n \"content\",\n \"email\",\n \"firstName\",\n \"lastName\",\n \"locationId\",\n \"phone\",\n \"subject\",\n \"redemptionStatus\",\n \"birthday\",\n \"city\",\n \"state\",\n \"websiteFormAssignmentId\",\n \"websiteId\",\n \"acceptsSmsMarketing\",\n \"acceptsEmailMarketing\",\n \"visitorIpAddress\",\n] as const;\n\n/**\n * Configuration parameters for Rails API submission.\n */\nexport interface RailsApiConfig {\n /**\n * API key for authentication.\n * Sent as top-level parameter: api_key\n */\n apiKey: string;\n\n /**\n * Contact category token for categorization.\n * Sent as top-level parameter: contact_category_token\n */\n contactCategoryToken?: string;\n\n /**\n * Location ID for multi-location organizations.\n * Sent as top-level parameter: location_id\n */\n locationId?: string;\n\n /**\n * Website ID for tracking form submissions.\n * Sent within contact object: website_id\n */\n websiteId?: string;\n\n /**\n * Website form assignment ID for form tracking.\n * Sent within contact object: website_form_assignment_id\n */\n websiteFormAssignmentId?: string;\n\n /**\n * Visitor IP address. If not provided, will be auto-detected on server.\n * Sent within contact object: visitor_ip_address\n */\n visitorIpAddress?: string;\n}\n\n/**\n * Serialized form data ready for Rails API submission.\n */\nexport interface SerializedFormData {\n /**\n * Top-level API key parameter.\n */\n api_key: string;\n\n /**\n * Top-level contact category token (optional).\n */\n contact_category_token?: string;\n\n /**\n * Top-level location ID (optional).\n */\n location_id?: string;\n\n /**\n * Contact object with standard fields and metadata.\n */\n contact: {\n /**\n * Standard contact fields in snake_case.\n */\n [key: string]: unknown;\n\n /**\n * Custom fields that don't match standard schema.\n * Stored as separate hash in Rails.\n */\n custom_fields?: Record<string, unknown>;\n\n /**\n * Array of upload tokens from file uploads.\n * These reference ContactFormUpload records in Rails.\n */\n contact_form_upload_tokens?: string[];\n };\n}\n\n/**\n * Form values from the form library (camelCase keys).\n */\nexport type FormValues = Record<string, unknown>;\n\n/**\n * Convert camelCase to snake_case.\n *\n * @example\n * ```ts\n * camelToSnake(\"firstName\") // \"first_name\"\n * camelToSnake(\"acceptsSmsMarketing\") // \"accepts_sms_marketing\"\n * ```\n */\nfunction camelToSnake(str: string): string {\n return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);\n}\n\n/**\n * Check if a field name is a standard Rails contact field.\n */\nfunction isStandardField(fieldName: string): boolean {\n return STANDARD_FIELDS.includes(fieldName as (typeof STANDARD_FIELDS)[number]);\n}\n\n/**\n * Extract upload tokens from form values.\n * Handles both string tokens and arrays of tokens.\n */\nfunction extractUploadTokens(values: FormValues): string[] {\n const tokens: string[] = [];\n\n for (const value of Object.values(values)) {\n if (typeof value === \"string\" && value.startsWith(\"upload_\")) {\n tokens.push(value);\n } else if (Array.isArray(value)) {\n for (const item of value) {\n if (typeof item === \"string\" && item.startsWith(\"upload_\")) {\n tokens.push(item);\n }\n }\n }\n }\n\n return tokens;\n}\n\n/**\n * Format date/time values for Rails API.\n * Rails expects ISO 8601 format.\n */\nfunction formatDateForRails(value: unknown): string | undefined {\n if (value instanceof Date) {\n return value.toISOString();\n }\n if (typeof value === \"string\") {\n // Attempt to parse and reformat to ensure valid ISO 8601\n const date = new Date(value);\n if (!isNaN(date.getTime())) {\n return date.toISOString();\n }\n }\n return undefined;\n}\n\n/**\n * Serialize form values for Rails ContactsController API.\n *\n * This function:\n * 1. Converts camelCase field names to snake_case\n * 2. Separates standard fields from custom fields\n * 3. Extracts upload tokens into contact_form_upload_tokens array\n * 4. Formats dates to ISO 8601\n * 5. Includes API configuration parameters\n *\n * @param values - Form values from useForm hook (camelCase keys)\n * @param config - Rails API configuration (apiKey, locationId, etc.)\n * @returns Serialized data ready for POST to /contacts\n *\n * @example\n * ```ts\n * const serialized = serializeForRails(\n * {\n * firstName: \"John\",\n * lastName: \"Doe\",\n * email: \"john@example.com\",\n * phone: \"555-1234\",\n * companySize: \"50-100\", // Custom field\n * resumeToken: \"upload_abc123\",\n * },\n * {\n * apiKey: \"key_123\",\n * contactCategoryToken: \"cat_xyz\",\n * locationId: \"loc_456\",\n * }\n * );\n *\n * // Result:\n * // {\n * // api_key: \"key_123\",\n * // contact_category_token: \"cat_xyz\",\n * // location_id: \"loc_456\",\n * // contact: {\n * // first_name: \"John\",\n * // last_name: \"Doe\",\n * // email: \"john@example.com\",\n * // phone: \"555-1234\",\n * // custom_fields: {\n * // company_size: \"50-100\"\n * // },\n * // contact_form_upload_tokens: [\"upload_abc123\"]\n * // }\n * // }\n * ```\n */\nexport function serializeForRails(\n values: FormValues,\n config: RailsApiConfig\n): SerializedFormData {\n const standardFields: Record<string, unknown> = {};\n const customFields: Record<string, unknown> = {};\n\n // Extract upload tokens\n const uploadTokens = extractUploadTokens(values);\n\n // Separate standard and custom fields\n for (const [key, value] of Object.entries(values)) {\n // Skip upload token fields - they're handled separately\n if (typeof value === \"string\" && value.startsWith(\"upload_\")) {\n continue;\n }\n if (Array.isArray(value) && value.every(\n (item) => typeof item === \"string\" && item.startsWith(\"upload_\")\n )) {\n continue;\n }\n\n const snakeKey = camelToSnake(key);\n\n if (isStandardField(key)) {\n // Format dates for birthday field\n if (key === \"birthday\") {\n const formatted = formatDateForRails(value);\n if (formatted) {\n standardFields[snakeKey] = formatted;\n }\n } else {\n standardFields[snakeKey] = value;\n }\n } else {\n // Custom fields\n customFields[snakeKey] = value;\n }\n }\n\n // Add config fields to standard fields if provided\n if (config.websiteId !== undefined) {\n standardFields.website_id = config.websiteId;\n }\n if (config.websiteFormAssignmentId !== undefined) {\n standardFields.website_form_assignment_id = config.websiteFormAssignmentId;\n }\n if (config.visitorIpAddress !== undefined) {\n standardFields.visitor_ip_address = config.visitorIpAddress;\n }\n\n // Build contact object\n const contact: SerializedFormData[\"contact\"] = {\n ...standardFields,\n };\n\n // Add custom fields if any\n if (Object.keys(customFields).length > 0) {\n contact.custom_fields = customFields;\n }\n\n // Add upload tokens if any\n if (uploadTokens.length > 0) {\n contact.contact_form_upload_tokens = uploadTokens;\n }\n\n // Build final serialized data\n const serialized: SerializedFormData = {\n api_key: config.apiKey,\n contact,\n };\n\n // Add optional top-level parameters\n if (config.contactCategoryToken !== undefined) {\n serialized.contact_category_token = config.contactCategoryToken;\n }\n if (config.locationId !== undefined) {\n serialized.location_id = config.locationId;\n }\n\n return serialized;\n}\n\n/**\n * Rails API error response format.\n */\nexport interface RailsErrorResponse {\n errors: {\n [field: string]: string[];\n };\n status: number;\n}\n\n/**\n * Form error format used by @page-speed/forms.\n */\nexport type FormErrors = Record<string, string | undefined>;\n\n/**\n * Convert snake_case to camelCase.\n *\n * @example\n * ```ts\n * snakeToCamel(\"first_name\") // \"firstName\"\n * snakeToCamel(\"accepts_sms_marketing\") // \"acceptsSmsMarketing\"\n * ```\n */\nfunction snakeToCamel(str: string): string {\n return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());\n}\n\n/**\n * Deserialize Rails API errors to form error format.\n *\n * Converts Rails error format to the format expected by @page-speed/forms:\n * - Maps snake_case field names to camelCase\n * - Flattens error arrays to single error string (first error)\n * - Handles custom_fields errors by mapping back to original field names\n * - Extracts base errors to form-level errors\n *\n * @param railsErrors - Error response from Rails API\n * @returns Form errors object (camelCase keys)\n *\n * @example\n * ```ts\n * const formErrors = deserializeErrors({\n * errors: {\n * first_name: [\"can't be blank\", \"is too short\"],\n * email: [\"is invalid\"],\n * custom_fields: {\n * company_size: [\"is required\"]\n * },\n * base: [\"Something went wrong\"]\n * },\n * status: 422\n * });\n *\n * // Result:\n * // {\n * // firstName: \"can't be blank\",\n * // email: \"is invalid\",\n * // companySize: \"is required\",\n * // _form: \"Something went wrong\"\n * // }\n * ```\n */\nexport function deserializeErrors(railsErrors: RailsErrorResponse): FormErrors {\n const formErrors: FormErrors = {};\n\n for (const [field, messages] of Object.entries(railsErrors.errors)) {\n // Handle base errors (form-level errors)\n if (field === \"base\") {\n formErrors._form = Array.isArray(messages) ? messages[0] : messages;\n continue;\n }\n\n // Handle custom_fields errors\n if (field === \"custom_fields\" && typeof messages === \"object\") {\n for (const [customField, customMessages] of Object.entries(messages)) {\n const camelField = snakeToCamel(customField);\n const errorMessage = Array.isArray(customMessages)\n ? customMessages[0]\n : customMessages;\n formErrors[camelField] = errorMessage;\n }\n continue;\n }\n\n // Handle standard field errors\n const camelField = snakeToCamel(field);\n const errorMessage = Array.isArray(messages) ? messages[0] : messages;\n formErrors[camelField] = errorMessage;\n }\n\n return formErrors;\n}\n","/**\n * @page-speed/forms - ChaiBlock Adapter\n *\n * Adapts form components for use in opensite-blocks ChaiBlock rendering system.\n * Wraps form components to accept ChaiBlock prop structure and transforms them\n * to component-native props.\n *\n * @see https://github.com/opensite-ai/page-speed-forms\n */\n\n\"use client\";\n\nimport * as React from \"react\";\n\n/**\n * ChaiBlock structure from @opensite/blocks.\n * Minimal type definition for adapter compatibility.\n */\nexport interface ChaiBlock {\n _id: string;\n _type: string;\n _name?: string;\n _parent?: string | null;\n tag?: string;\n styles?: string;\n content?: string;\n blockProps?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\n/**\n * Options for ChaiBlock adapter.\n */\nexport interface ChaiBlockAdapterOptions {\n /**\n * Default props to merge with block props.\n */\n defaultProps?: Record<string, unknown>;\n\n /**\n * Transform function to convert ChaiBlock props to component props.\n * Useful for custom prop mapping logic.\n */\n transformProps?: (blockProps: Record<string, unknown>, block: ChaiBlock) => Record<string, unknown>;\n\n /**\n * Extract display name from block for debugging.\n * Defaults to using _name or _type from block.\n */\n getDisplayName?: (block: ChaiBlock) => string;\n\n /**\n * Enable React error boundary wrapping.\n * Defaults to true.\n */\n withErrorBoundary?: boolean;\n\n /**\n * Custom error fallback component.\n * If not provided, renders basic error message.\n */\n errorFallback?: (error: Error, block: ChaiBlock) => React.ReactNode;\n}\n\n/**\n * Props passed to adapted component.\n */\nexport interface AdaptedComponentProps {\n /**\n * ChaiBlock data from design payload.\n */\n block: ChaiBlock;\n\n /**\n * Child blocks for rendering nested content.\n * Used by container components like Form.\n */\n children?: React.ReactNode;\n\n /**\n * Callback to render child blocks.\n * Provided by opensite-blocks BlocksRenderer.\n */\n renderChildren?: (blockId: string) => React.ReactNode;\n}\n\n/**\n * Error boundary component for catching render errors.\n */\nclass ChaiBlockErrorBoundary extends React.Component<\n {\n block: ChaiBlock;\n fallback?: (error: Error, block: ChaiBlock) => React.ReactNode;\n children: React.ReactNode;\n },\n { error: Error | null }\n> {\n constructor(props: ChaiBlockErrorBoundary[\"props\"]) {\n super(props);\n this.state = { error: null };\n }\n\n static getDerivedStateFromError(error: Error) {\n return { error };\n }\n\n componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {\n console.error(`ChaiBlock render error (${this.props.block._id}):`, error, errorInfo);\n }\n\n render() {\n if (this.state.error) {\n if (this.props.fallback) {\n return this.props.fallback(this.state.error, this.props.block);\n }\n\n return (\n <div\n className=\"chai-block-error border border-red-300 bg-red-50 p-4 rounded text-red-700\"\n data-block-id={this.props.block._id}\n data-block-type={this.props.block._type}\n >\n <p className=\"font-semibold\">Block Render Error</p>\n <p className=\"text-sm\">\n Block: {this.props.block._name || this.props.block._id} ({this.props.block._type})\n </p>\n <p className=\"text-sm mt-1\">{this.state.error.message}</p>\n </div>\n );\n }\n\n return this.props.children;\n }\n}\n\n/**\n * Create a ChaiBlock-compatible wrapper for a form component.\n *\n * This adapter transforms ChaiBlock props (from design payload) into\n * component-native props. It handles:\n * - Extracting props from `blockProps` field\n * - Merging with default props\n * - Custom prop transformation\n * - Error boundary wrapping\n * - Children rendering support\n *\n * @param Component - React component to adapt\n * @param options - Adapter configuration options\n * @returns ChaiBlock-compatible component\n *\n * @example\n * ```tsx\n * import { TextInput } from \"@page-speed/forms/inputs\";\n * import { createChaiBlockAdapter } from \"@page-speed/forms/integration\";\n *\n * // Create ChaiBlock-compatible TextInput\n * const ChaiTextInput = createChaiBlockAdapter(TextInput, {\n * defaultProps: {\n * placeholder: \"Enter text...\",\n * },\n * transformProps: (blockProps, block) => ({\n * ...blockProps,\n * // Map ChaiBlock content to component props\n * label: block.content || blockProps.label,\n * // Apply ChaiBlock styles as className\n * className: block.styles,\n * }),\n * });\n *\n * // Register with opensite-blocks\n * registerBlockType(\"TextInput\", ChaiTextInput);\n * ```\n *\n * @example\n * ```tsx\n * // ChaiBlock from design payload:\n * {\n * _id: \"field-email\",\n * _type: \"TextInput\",\n * _name: \"Email Field\",\n * content: \"Email Address\",\n * styles: \"w-full mb-4\",\n * blockProps: {\n * name: \"email\",\n * type: \"email\",\n * placeholder: \"you@example.com\",\n * required: true,\n * }\n * }\n *\n * // Transformed to TextInput props:\n * <TextInput\n * name=\"email\"\n * type=\"email\"\n * placeholder=\"you@example.com\"\n * required={true}\n * label=\"Email Address\"\n * className=\"w-full mb-4\"\n * />\n * ```\n */\nexport function createChaiBlockAdapter<TProps extends Record<string, unknown>>(\n Component: React.ComponentType<TProps>,\n options: ChaiBlockAdapterOptions = {}\n): React.ComponentType<AdaptedComponentProps> {\n const {\n defaultProps = {},\n transformProps,\n withErrorBoundary = true,\n errorFallback,\n } = options;\n\n const AdaptedComponent: React.FC<AdaptedComponentProps> = ({ block, children, renderChildren }) => {\n // Extract component props from blockProps\n const blockProps = (block.blockProps || {}) as Record<string, unknown>;\n\n // Merge with default props\n const mergedProps = { ...defaultProps, ...blockProps };\n\n // Apply custom transformation if provided\n const finalProps = transformProps\n ? transformProps(mergedProps, block)\n : mergedProps;\n\n // Add data attributes for debugging\n const dataAttrs = {\n \"data-block-id\": block._id,\n \"data-block-type\": block._type,\n ...(block._name && { \"data-block-name\": block._name }),\n };\n\n // Merge data attributes with final props\n const componentProps = {\n ...finalProps,\n ...dataAttrs,\n } as unknown as TProps;\n\n // Render children if renderChildren callback provided\n const renderedChildren = renderChildren ? renderChildren(block._id) : children;\n\n const element = (\n <Component {...componentProps}>\n {renderedChildren}\n </Component>\n );\n\n // Wrap with error boundary if enabled\n if (withErrorBoundary) {\n return (\n <ChaiBlockErrorBoundary block={block} fallback={errorFallback}>\n {element}\n </ChaiBlockErrorBoundary>\n );\n }\n\n return element;\n };\n\n // Set display name for debugging\n const componentName = Component.displayName || Component.name || \"Component\";\n AdaptedComponent.displayName = `ChaiBlockAdapter(${componentName})`;\n\n return AdaptedComponent;\n}\n\n/**\n * Standard prop transformer for form input components.\n *\n * Applies common transformations:\n * - Maps ChaiBlock `content` to `label`\n * - Maps ChaiBlock `styles` to `className`\n * - Preserves all blockProps\n *\n * @param blockProps - Props from ChaiBlock.blockProps\n * @param block - Full ChaiBlock object\n * @returns Transformed props for component\n *\n * @example\n * ```tsx\n * const ChaiTextInput = createChaiBlockAdapter(TextInput, {\n * transformProps: standardInputTransformer,\n * });\n * ```\n */\nexport function standardInputTransformer(\n blockProps: Record<string, unknown>,\n block: ChaiBlock\n): Record<string, unknown> {\n return {\n ...blockProps,\n // Use content as label if not already provided\n ...(block.content && !blockProps.label && { label: block.content }),\n // Apply ChaiBlock styles as className\n ...(block.styles && { className: block.styles }),\n };\n}\n\n/**\n * Create multiple ChaiBlock adapters with shared options.\n *\n * Convenience function for adapting multiple components at once\n * with the same configuration.\n *\n * @param components - Record of component name to component\n * @param options - Shared adapter options\n * @returns Record of adapted components\n *\n * @example\n * ```tsx\n * import * as Inputs from \"@page-speed/forms/inputs\";\n * import { createChaiBlockAdapters, standardInputTransformer } from \"@page-speed/forms/integration\";\n *\n * const ChaiInputs = createChaiBlockAdapters(\n * {\n * TextInput: Inputs.TextInput,\n * TextArea: Inputs.TextArea,\n * Select: Inputs.Select,\n * },\n * {\n * transformProps: standardInputTransformer,\n * withErrorBoundary: true,\n * }\n * );\n *\n * // ChaiInputs.TextInput, ChaiInputs.TextArea, ChaiInputs.Select\n * // are now ChaiBlock-compatible\n * ```\n */\nexport function createChaiBlockAdapters<\n TComponents extends Record<string, React.ComponentType<any>>\n>(\n components: TComponents,\n options: ChaiBlockAdapterOptions = {}\n): Record<keyof TComponents, React.ComponentType<AdaptedComponentProps>> {\n const adapted: Record<string, React.ComponentType<AdaptedComponentProps>> = {};\n\n for (const [name, component] of Object.entries(components)) {\n adapted[name] = createChaiBlockAdapter(component, options);\n }\n\n return adapted as Record<keyof TComponents, React.ComponentType<AdaptedComponentProps>>;\n}\n"]}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @page-speed/forms - Rails API Serializer
|
|
5
|
+
*
|
|
6
|
+
* Serializes form data for the DashTrack ContactsController API.
|
|
7
|
+
* Handles field name conversion (camelCase → snake_case), custom fields separation,
|
|
8
|
+
* and upload token arrays.
|
|
9
|
+
*
|
|
10
|
+
* @see https://github.com/opensite-ai/page-speed-forms
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Configuration parameters for Rails API submission.
|
|
14
|
+
*/
|
|
15
|
+
interface RailsApiConfig {
|
|
16
|
+
/**
|
|
17
|
+
* API key for authentication.
|
|
18
|
+
* Sent as top-level parameter: api_key
|
|
19
|
+
*/
|
|
20
|
+
apiKey: string;
|
|
21
|
+
/**
|
|
22
|
+
* Contact category token for categorization.
|
|
23
|
+
* Sent as top-level parameter: contact_category_token
|
|
24
|
+
*/
|
|
25
|
+
contactCategoryToken?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Location ID for multi-location organizations.
|
|
28
|
+
* Sent as top-level parameter: location_id
|
|
29
|
+
*/
|
|
30
|
+
locationId?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Website ID for tracking form submissions.
|
|
33
|
+
* Sent within contact object: website_id
|
|
34
|
+
*/
|
|
35
|
+
websiteId?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Website form assignment ID for form tracking.
|
|
38
|
+
* Sent within contact object: website_form_assignment_id
|
|
39
|
+
*/
|
|
40
|
+
websiteFormAssignmentId?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Visitor IP address. If not provided, will be auto-detected on server.
|
|
43
|
+
* Sent within contact object: visitor_ip_address
|
|
44
|
+
*/
|
|
45
|
+
visitorIpAddress?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Serialized form data ready for Rails API submission.
|
|
49
|
+
*/
|
|
50
|
+
interface SerializedFormData {
|
|
51
|
+
/**
|
|
52
|
+
* Top-level API key parameter.
|
|
53
|
+
*/
|
|
54
|
+
api_key: string;
|
|
55
|
+
/**
|
|
56
|
+
* Top-level contact category token (optional).
|
|
57
|
+
*/
|
|
58
|
+
contact_category_token?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Top-level location ID (optional).
|
|
61
|
+
*/
|
|
62
|
+
location_id?: string;
|
|
63
|
+
/**
|
|
64
|
+
* Contact object with standard fields and metadata.
|
|
65
|
+
*/
|
|
66
|
+
contact: {
|
|
67
|
+
/**
|
|
68
|
+
* Standard contact fields in snake_case.
|
|
69
|
+
*/
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
/**
|
|
72
|
+
* Custom fields that don't match standard schema.
|
|
73
|
+
* Stored as separate hash in Rails.
|
|
74
|
+
*/
|
|
75
|
+
custom_fields?: Record<string, unknown>;
|
|
76
|
+
/**
|
|
77
|
+
* Array of upload tokens from file uploads.
|
|
78
|
+
* These reference ContactFormUpload records in Rails.
|
|
79
|
+
*/
|
|
80
|
+
contact_form_upload_tokens?: string[];
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Form values from the form library (camelCase keys).
|
|
85
|
+
*/
|
|
86
|
+
type FormValues = Record<string, unknown>;
|
|
87
|
+
/**
|
|
88
|
+
* Serialize form values for Rails ContactsController API.
|
|
89
|
+
*
|
|
90
|
+
* This function:
|
|
91
|
+
* 1. Converts camelCase field names to snake_case
|
|
92
|
+
* 2. Separates standard fields from custom fields
|
|
93
|
+
* 3. Extracts upload tokens into contact_form_upload_tokens array
|
|
94
|
+
* 4. Formats dates to ISO 8601
|
|
95
|
+
* 5. Includes API configuration parameters
|
|
96
|
+
*
|
|
97
|
+
* @param values - Form values from useForm hook (camelCase keys)
|
|
98
|
+
* @param config - Rails API configuration (apiKey, locationId, etc.)
|
|
99
|
+
* @returns Serialized data ready for POST to /contacts
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const serialized = serializeForRails(
|
|
104
|
+
* {
|
|
105
|
+
* firstName: "John",
|
|
106
|
+
* lastName: "Doe",
|
|
107
|
+
* email: "john@example.com",
|
|
108
|
+
* phone: "555-1234",
|
|
109
|
+
* companySize: "50-100", // Custom field
|
|
110
|
+
* resumeToken: "upload_abc123",
|
|
111
|
+
* },
|
|
112
|
+
* {
|
|
113
|
+
* apiKey: "key_123",
|
|
114
|
+
* contactCategoryToken: "cat_xyz",
|
|
115
|
+
* locationId: "loc_456",
|
|
116
|
+
* }
|
|
117
|
+
* );
|
|
118
|
+
*
|
|
119
|
+
* // Result:
|
|
120
|
+
* // {
|
|
121
|
+
* // api_key: "key_123",
|
|
122
|
+
* // contact_category_token: "cat_xyz",
|
|
123
|
+
* // location_id: "loc_456",
|
|
124
|
+
* // contact: {
|
|
125
|
+
* // first_name: "John",
|
|
126
|
+
* // last_name: "Doe",
|
|
127
|
+
* // email: "john@example.com",
|
|
128
|
+
* // phone: "555-1234",
|
|
129
|
+
* // custom_fields: {
|
|
130
|
+
* // company_size: "50-100"
|
|
131
|
+
* // },
|
|
132
|
+
* // contact_form_upload_tokens: ["upload_abc123"]
|
|
133
|
+
* // }
|
|
134
|
+
* // }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
declare function serializeForRails(values: FormValues, config: RailsApiConfig): SerializedFormData;
|
|
138
|
+
/**
|
|
139
|
+
* Rails API error response format.
|
|
140
|
+
*/
|
|
141
|
+
interface RailsErrorResponse {
|
|
142
|
+
errors: {
|
|
143
|
+
[field: string]: string[];
|
|
144
|
+
};
|
|
145
|
+
status: number;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Form error format used by @page-speed/forms.
|
|
149
|
+
*/
|
|
150
|
+
type FormErrors = Record<string, string | undefined>;
|
|
151
|
+
/**
|
|
152
|
+
* Deserialize Rails API errors to form error format.
|
|
153
|
+
*
|
|
154
|
+
* Converts Rails error format to the format expected by @page-speed/forms:
|
|
155
|
+
* - Maps snake_case field names to camelCase
|
|
156
|
+
* - Flattens error arrays to single error string (first error)
|
|
157
|
+
* - Handles custom_fields errors by mapping back to original field names
|
|
158
|
+
* - Extracts base errors to form-level errors
|
|
159
|
+
*
|
|
160
|
+
* @param railsErrors - Error response from Rails API
|
|
161
|
+
* @returns Form errors object (camelCase keys)
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const formErrors = deserializeErrors({
|
|
166
|
+
* errors: {
|
|
167
|
+
* first_name: ["can't be blank", "is too short"],
|
|
168
|
+
* email: ["is invalid"],
|
|
169
|
+
* custom_fields: {
|
|
170
|
+
* company_size: ["is required"]
|
|
171
|
+
* },
|
|
172
|
+
* base: ["Something went wrong"]
|
|
173
|
+
* },
|
|
174
|
+
* status: 422
|
|
175
|
+
* });
|
|
176
|
+
*
|
|
177
|
+
* // Result:
|
|
178
|
+
* // {
|
|
179
|
+
* // firstName: "can't be blank",
|
|
180
|
+
* // email: "is invalid",
|
|
181
|
+
* // companySize: "is required",
|
|
182
|
+
* // _form: "Something went wrong"
|
|
183
|
+
* // }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
declare function deserializeErrors(railsErrors: RailsErrorResponse): FormErrors;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @page-speed/forms - ChaiBlock Adapter
|
|
190
|
+
*
|
|
191
|
+
* Adapts form components for use in opensite-blocks ChaiBlock rendering system.
|
|
192
|
+
* Wraps form components to accept ChaiBlock prop structure and transforms them
|
|
193
|
+
* to component-native props.
|
|
194
|
+
*
|
|
195
|
+
* @see https://github.com/opensite-ai/page-speed-forms
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* ChaiBlock structure from @opensite/blocks.
|
|
200
|
+
* Minimal type definition for adapter compatibility.
|
|
201
|
+
*/
|
|
202
|
+
interface ChaiBlock {
|
|
203
|
+
_id: string;
|
|
204
|
+
_type: string;
|
|
205
|
+
_name?: string;
|
|
206
|
+
_parent?: string | null;
|
|
207
|
+
tag?: string;
|
|
208
|
+
styles?: string;
|
|
209
|
+
content?: string;
|
|
210
|
+
blockProps?: Record<string, unknown>;
|
|
211
|
+
[key: string]: unknown;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Options for ChaiBlock adapter.
|
|
215
|
+
*/
|
|
216
|
+
interface ChaiBlockAdapterOptions {
|
|
217
|
+
/**
|
|
218
|
+
* Default props to merge with block props.
|
|
219
|
+
*/
|
|
220
|
+
defaultProps?: Record<string, unknown>;
|
|
221
|
+
/**
|
|
222
|
+
* Transform function to convert ChaiBlock props to component props.
|
|
223
|
+
* Useful for custom prop mapping logic.
|
|
224
|
+
*/
|
|
225
|
+
transformProps?: (blockProps: Record<string, unknown>, block: ChaiBlock) => Record<string, unknown>;
|
|
226
|
+
/**
|
|
227
|
+
* Extract display name from block for debugging.
|
|
228
|
+
* Defaults to using _name or _type from block.
|
|
229
|
+
*/
|
|
230
|
+
getDisplayName?: (block: ChaiBlock) => string;
|
|
231
|
+
/**
|
|
232
|
+
* Enable React error boundary wrapping.
|
|
233
|
+
* Defaults to true.
|
|
234
|
+
*/
|
|
235
|
+
withErrorBoundary?: boolean;
|
|
236
|
+
/**
|
|
237
|
+
* Custom error fallback component.
|
|
238
|
+
* If not provided, renders basic error message.
|
|
239
|
+
*/
|
|
240
|
+
errorFallback?: (error: Error, block: ChaiBlock) => React.ReactNode;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Props passed to adapted component.
|
|
244
|
+
*/
|
|
245
|
+
interface AdaptedComponentProps {
|
|
246
|
+
/**
|
|
247
|
+
* ChaiBlock data from design payload.
|
|
248
|
+
*/
|
|
249
|
+
block: ChaiBlock;
|
|
250
|
+
/**
|
|
251
|
+
* Child blocks for rendering nested content.
|
|
252
|
+
* Used by container components like Form.
|
|
253
|
+
*/
|
|
254
|
+
children?: React.ReactNode;
|
|
255
|
+
/**
|
|
256
|
+
* Callback to render child blocks.
|
|
257
|
+
* Provided by opensite-blocks BlocksRenderer.
|
|
258
|
+
*/
|
|
259
|
+
renderChildren?: (blockId: string) => React.ReactNode;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Create a ChaiBlock-compatible wrapper for a form component.
|
|
263
|
+
*
|
|
264
|
+
* This adapter transforms ChaiBlock props (from design payload) into
|
|
265
|
+
* component-native props. It handles:
|
|
266
|
+
* - Extracting props from `blockProps` field
|
|
267
|
+
* - Merging with default props
|
|
268
|
+
* - Custom prop transformation
|
|
269
|
+
* - Error boundary wrapping
|
|
270
|
+
* - Children rendering support
|
|
271
|
+
*
|
|
272
|
+
* @param Component - React component to adapt
|
|
273
|
+
* @param options - Adapter configuration options
|
|
274
|
+
* @returns ChaiBlock-compatible component
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```tsx
|
|
278
|
+
* import { TextInput } from "@page-speed/forms/inputs";
|
|
279
|
+
* import { createChaiBlockAdapter } from "@page-speed/forms/integration";
|
|
280
|
+
*
|
|
281
|
+
* // Create ChaiBlock-compatible TextInput
|
|
282
|
+
* const ChaiTextInput = createChaiBlockAdapter(TextInput, {
|
|
283
|
+
* defaultProps: {
|
|
284
|
+
* placeholder: "Enter text...",
|
|
285
|
+
* },
|
|
286
|
+
* transformProps: (blockProps, block) => ({
|
|
287
|
+
* ...blockProps,
|
|
288
|
+
* // Map ChaiBlock content to component props
|
|
289
|
+
* label: block.content || blockProps.label,
|
|
290
|
+
* // Apply ChaiBlock styles as className
|
|
291
|
+
* className: block.styles,
|
|
292
|
+
* }),
|
|
293
|
+
* });
|
|
294
|
+
*
|
|
295
|
+
* // Register with opensite-blocks
|
|
296
|
+
* registerBlockType("TextInput", ChaiTextInput);
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```tsx
|
|
301
|
+
* // ChaiBlock from design payload:
|
|
302
|
+
* {
|
|
303
|
+
* _id: "field-email",
|
|
304
|
+
* _type: "TextInput",
|
|
305
|
+
* _name: "Email Field",
|
|
306
|
+
* content: "Email Address",
|
|
307
|
+
* styles: "w-full mb-4",
|
|
308
|
+
* blockProps: {
|
|
309
|
+
* name: "email",
|
|
310
|
+
* type: "email",
|
|
311
|
+
* placeholder: "you@example.com",
|
|
312
|
+
* required: true,
|
|
313
|
+
* }
|
|
314
|
+
* }
|
|
315
|
+
*
|
|
316
|
+
* // Transformed to TextInput props:
|
|
317
|
+
* <TextInput
|
|
318
|
+
* name="email"
|
|
319
|
+
* type="email"
|
|
320
|
+
* placeholder="you@example.com"
|
|
321
|
+
* required={true}
|
|
322
|
+
* label="Email Address"
|
|
323
|
+
* className="w-full mb-4"
|
|
324
|
+
* />
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
declare function createChaiBlockAdapter<TProps extends Record<string, unknown>>(Component: React.ComponentType<TProps>, options?: ChaiBlockAdapterOptions): React.ComponentType<AdaptedComponentProps>;
|
|
328
|
+
/**
|
|
329
|
+
* Standard prop transformer for form input components.
|
|
330
|
+
*
|
|
331
|
+
* Applies common transformations:
|
|
332
|
+
* - Maps ChaiBlock `content` to `label`
|
|
333
|
+
* - Maps ChaiBlock `styles` to `className`
|
|
334
|
+
* - Preserves all blockProps
|
|
335
|
+
*
|
|
336
|
+
* @param blockProps - Props from ChaiBlock.blockProps
|
|
337
|
+
* @param block - Full ChaiBlock object
|
|
338
|
+
* @returns Transformed props for component
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```tsx
|
|
342
|
+
* const ChaiTextInput = createChaiBlockAdapter(TextInput, {
|
|
343
|
+
* transformProps: standardInputTransformer,
|
|
344
|
+
* });
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
declare function standardInputTransformer(blockProps: Record<string, unknown>, block: ChaiBlock): Record<string, unknown>;
|
|
348
|
+
/**
|
|
349
|
+
* Create multiple ChaiBlock adapters with shared options.
|
|
350
|
+
*
|
|
351
|
+
* Convenience function for adapting multiple components at once
|
|
352
|
+
* with the same configuration.
|
|
353
|
+
*
|
|
354
|
+
* @param components - Record of component name to component
|
|
355
|
+
* @param options - Shared adapter options
|
|
356
|
+
* @returns Record of adapted components
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```tsx
|
|
360
|
+
* import * as Inputs from "@page-speed/forms/inputs";
|
|
361
|
+
* import { createChaiBlockAdapters, standardInputTransformer } from "@page-speed/forms/integration";
|
|
362
|
+
*
|
|
363
|
+
* const ChaiInputs = createChaiBlockAdapters(
|
|
364
|
+
* {
|
|
365
|
+
* TextInput: Inputs.TextInput,
|
|
366
|
+
* TextArea: Inputs.TextArea,
|
|
367
|
+
* Select: Inputs.Select,
|
|
368
|
+
* },
|
|
369
|
+
* {
|
|
370
|
+
* transformProps: standardInputTransformer,
|
|
371
|
+
* withErrorBoundary: true,
|
|
372
|
+
* }
|
|
373
|
+
* );
|
|
374
|
+
*
|
|
375
|
+
* // ChaiInputs.TextInput, ChaiInputs.TextArea, ChaiInputs.Select
|
|
376
|
+
* // are now ChaiBlock-compatible
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
declare function createChaiBlockAdapters<TComponents extends Record<string, React.ComponentType<any>>>(components: TComponents, options?: ChaiBlockAdapterOptions): Record<keyof TComponents, React.ComponentType<AdaptedComponentProps>>;
|
|
380
|
+
|
|
381
|
+
export { type AdaptedComponentProps, type ChaiBlock, type ChaiBlockAdapterOptions, type FormErrors, type RailsApiConfig, type RailsErrorResponse, type SerializedFormData, createChaiBlockAdapter, createChaiBlockAdapters, deserializeErrors, serializeForRails, standardInputTransformer };
|