@10x-media/form-builder 0.1.0-beta.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.
- package/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +1434 -0
- package/dist/constants-B-BUfetP.d.ts +288 -0
- package/dist/exports/client.d.ts +21 -0
- package/dist/exports/client.js +320 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/i18n.d.ts +138 -0
- package/dist/exports/i18n.js +3 -0
- package/dist/exports/react.d.ts +550 -0
- package/dist/exports/react.js +1564 -0
- package/dist/exports/react.js.map +1 -0
- package/dist/exports/rsc.d.ts +16 -0
- package/dist/exports/rsc.js +55 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/exports/types.d.ts +5 -0
- package/dist/exports/types.js +1 -0
- package/dist/fieldTypes-B8jkNRob.d.ts +188 -0
- package/dist/fieldTypes-CzhhJXjg.js +88 -0
- package/dist/fieldTypes-CzhhJXjg.js.map +1 -0
- package/dist/index-DfFYGbVF.d.ts +481 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1921 -0
- package/dist/index.js.map +1 -0
- package/dist/keys-N5xGiUsh.js +132 -0
- package/dist/keys-N5xGiUsh.js.map +1 -0
- package/dist/registry-CoCyhtvB.js +282 -0
- package/dist/registry-CoCyhtvB.js.map +1 -0
- package/dist/resolver-OeQyVH2N.js +665 -0
- package/dist/resolver-OeQyVH2N.js.map +1 -0
- package/dist/translations-edMqq_2e.js +152 -0
- package/dist/translations-edMqq_2e.js.map +1 -0
- package/dist/types-DsJ_6kGJ.d.ts +23 -0
- package/package.json +125 -0
- package/styles/form-builder.css +108 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { n as AnyFormFieldDefinition } from "./fieldTypes-B8jkNRob.js";
|
|
2
|
+
import { Field, Payload, PayloadRequest, Where } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/submissions/types.d.ts
|
|
5
|
+
/** A single answered field: the field's machine name and its typed value. */
|
|
6
|
+
type SubmissionValue = {
|
|
7
|
+
field: string;
|
|
8
|
+
value: unknown;
|
|
9
|
+
};
|
|
10
|
+
/** A localized, self-describing snapshot of an answered field, taken at submit time. */
|
|
11
|
+
type SubmissionDescriptor = {
|
|
12
|
+
field: string;
|
|
13
|
+
label: string;
|
|
14
|
+
fieldType: string;
|
|
15
|
+
optionLabels?: Record<string, string>;
|
|
16
|
+
};
|
|
17
|
+
/** A per-field validation failure (`path` is the field name, for renderer error mapping). */
|
|
18
|
+
type SubmissionFieldError = {
|
|
19
|
+
path: string;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
/** A field instance as stored in a form's `fields` blocks array (shared config plus type-specific keys). */
|
|
23
|
+
type FormFieldInstance = {
|
|
24
|
+
blockType: string;
|
|
25
|
+
name: string;
|
|
26
|
+
label?: string;
|
|
27
|
+
required?: boolean;
|
|
28
|
+
visibleWhen?: Where;
|
|
29
|
+
validateWhen?: Where;
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/aggregation/types.d.ts
|
|
34
|
+
/** Which submission statuses to count. `complete` (default) excludes partials; `all` counts everything. */
|
|
35
|
+
type SubmissionStatusFilter = 'complete' | 'partial' | 'all';
|
|
36
|
+
/** One answer option (or distinct value) with its tally. */
|
|
37
|
+
type AggregationBucket = {
|
|
38
|
+
/** The answer value, stringified (the grouping key). */value: string; /** Display label: current option label, else the submitted snapshot label, else the raw value. */
|
|
39
|
+
label: string;
|
|
40
|
+
count: number; /** Share of respondents, 0-100, rounded to one decimal. Can sum past 100 for multi-value fields. */
|
|
41
|
+
percentage: number;
|
|
42
|
+
};
|
|
43
|
+
/** Aggregated responses for one field across a form's submissions. */
|
|
44
|
+
type FieldAggregation = {
|
|
45
|
+
field: string;
|
|
46
|
+
label?: string;
|
|
47
|
+
fieldType?: string; /** Respondents: submissions with a non-empty answer for this field. The percentage denominator. */
|
|
48
|
+
total: number;
|
|
49
|
+
buckets: AggregationBucket[]; /** True when the `maxSubmissions` cap was hit, so the tallies are a (large) sample, not the full set. */
|
|
50
|
+
truncated: boolean;
|
|
51
|
+
};
|
|
52
|
+
/** The slice of a submission the reducer reads. */
|
|
53
|
+
type AggregationRow = {
|
|
54
|
+
values?: SubmissionValue[];
|
|
55
|
+
descriptors?: SubmissionDescriptor[];
|
|
56
|
+
};
|
|
57
|
+
/** Field metadata the reducer needs to label and order buckets, derived from the live form definition. */
|
|
58
|
+
type FieldMeta = {
|
|
59
|
+
field: string;
|
|
60
|
+
label?: string;
|
|
61
|
+
fieldType?: string; /** Ordered known options with current labels; drives bucket order and label resolution. */
|
|
62
|
+
options?: {
|
|
63
|
+
value: string;
|
|
64
|
+
label: string;
|
|
65
|
+
}[];
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/calc/types.d.ts
|
|
69
|
+
/** A serializable, safe-by-construction calculation expression. No strings are parsed; the evaluator tree-walks this closed node set (never `eval`). */
|
|
70
|
+
type CalcExpression = {
|
|
71
|
+
type: 'lit';
|
|
72
|
+
value: number;
|
|
73
|
+
} | {
|
|
74
|
+
type: 'ref';
|
|
75
|
+
field: string;
|
|
76
|
+
} | {
|
|
77
|
+
type: 'op';
|
|
78
|
+
op: '+' | '-' | '*' | '/' | '%';
|
|
79
|
+
left: CalcExpression;
|
|
80
|
+
right: CalcExpression;
|
|
81
|
+
} | {
|
|
82
|
+
type: 'neg';
|
|
83
|
+
operand: CalcExpression;
|
|
84
|
+
} | {
|
|
85
|
+
type: 'fn';
|
|
86
|
+
fn: 'min' | 'max' | 'round' | 'abs' | 'ceil' | 'floor';
|
|
87
|
+
args: CalcExpression[];
|
|
88
|
+
} | {
|
|
89
|
+
type: 'weight';
|
|
90
|
+
field: string;
|
|
91
|
+
weights: Record<string, number>;
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/calc/computeCalcFields.d.ts
|
|
95
|
+
/** Returns the calc expression if the field carries one (non-null object), otherwise undefined. */
|
|
96
|
+
declare const calcExpressionOf: (field: FormFieldInstance) => CalcExpression | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Returns answers with every calc field's value derived from its expression,
|
|
99
|
+
* folded in declaration order so a calc may reference an earlier calc's result.
|
|
100
|
+
* Identity when no field carries an expression.
|
|
101
|
+
*/
|
|
102
|
+
declare const computeCalcFields: (fields: FormFieldInstance[], answers: Record<string, unknown>) => Record<string, unknown>;
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/calc/evaluate.d.ts
|
|
105
|
+
/** Evaluate a calc expression against form answers. Total + safe: no `eval`, always finite, div/mod by zero -> 0, missing ref -> 0, depth-guarded. Isomorphic (client + server). */
|
|
106
|
+
declare const evaluateCalc: (expr: CalcExpression | null | undefined, answers: Record<string, unknown>) => number;
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/conditions/evaluate.d.ts
|
|
109
|
+
/**
|
|
110
|
+
* Evaluate a serializable `Where`-shaped condition against a flat map of (already coerced) form answers.
|
|
111
|
+
* An absent or empty condition matches (returns true). Operator semantics mirror Payload's query
|
|
112
|
+
* adapters: coerce then compare; `not_equals`/`not_in` are null-inclusive; `exists` treats `''`/absent
|
|
113
|
+
* as not-existing; `like` is case-insensitive and space-splits (every word must be a substring), and
|
|
114
|
+
* `not_like` is its logical negation (at least one word absent); `contains` is a single case-insensitive
|
|
115
|
+
* substring; comma-delimited `in`/`not_in` string values are split and trimmed. Geo (`near`/`within`/
|
|
116
|
+
* `intersects`) and `all` are out of scope and evaluate to false. Isomorphic: no `req`/DB access, so the
|
|
117
|
+
* renderer reuses it client-side.
|
|
118
|
+
*/
|
|
119
|
+
declare const evaluateCondition: (where: Where | null | undefined, answers: Record<string, unknown>) => boolean;
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/fields/registry.d.ts
|
|
122
|
+
type FieldTypeRegistry = Map<string, AnyFormFieldDefinition>;
|
|
123
|
+
/** Per-type opt-in: `false` removes a built-in, `true` keeps it, an object adds a new type or replaces one. */
|
|
124
|
+
type FieldTypeOption = boolean | AnyFormFieldDefinition;
|
|
125
|
+
type FieldTypesConfig = Record<string, FieldTypeOption>;
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region src/prefill/valuesFromSearchParams.d.ts
|
|
128
|
+
type PrefillOptions = {
|
|
129
|
+
/** Map a query-param name to a field name. Unmapped params use their own name. */map?: Record<string, string>; /** If set, only these field names may be prefilled. */
|
|
130
|
+
allow?: string[]; /** These field names may never be prefilled. */
|
|
131
|
+
deny?: string[];
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Map URL/query params to typed initial values for KNOWN fields only, coerced by each field's value
|
|
135
|
+
* kind. Unknown, denied, or un-allowed params are ignored; invalid numbers are dropped. Prefilled
|
|
136
|
+
* values are never trusted -- they still validate on submit. Pass the result to `<Form initialValues>`.
|
|
137
|
+
*/
|
|
138
|
+
declare const valuesFromSearchParams: (params: URLSearchParams | Record<string, string>, fields: FormFieldInstance[], registry: FieldTypeRegistry, options?: PrefillOptions) => Record<string, unknown>;
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/events/types.d.ts
|
|
141
|
+
type FormEventBase = {
|
|
142
|
+
formId: string;
|
|
143
|
+
at: string;
|
|
144
|
+
};
|
|
145
|
+
type FormEvent = (FormEventBase & {
|
|
146
|
+
type: 'form.viewed';
|
|
147
|
+
}) | (FormEventBase & {
|
|
148
|
+
type: 'form.started';
|
|
149
|
+
}) | (FormEventBase & {
|
|
150
|
+
type: 'step.viewed';
|
|
151
|
+
stepId: string;
|
|
152
|
+
}) | (FormEventBase & {
|
|
153
|
+
type: 'step.completed';
|
|
154
|
+
stepId: string;
|
|
155
|
+
}) | (FormEventBase & {
|
|
156
|
+
type: 'field.errored';
|
|
157
|
+
field: string;
|
|
158
|
+
message: string;
|
|
159
|
+
}) | (FormEventBase & {
|
|
160
|
+
type: 'form.abandoned';
|
|
161
|
+
}) | (FormEventBase & {
|
|
162
|
+
type: 'submission.created';
|
|
163
|
+
submissionId?: string;
|
|
164
|
+
});
|
|
165
|
+
type FormEventSink = {
|
|
166
|
+
emit: (event: FormEvent) => Promise<void> | void;
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/validation/types.d.ts
|
|
170
|
+
type ValidationSeverity = 'error' | 'warning';
|
|
171
|
+
/** A rule outcome: pass, a plain error string, or an explicit message + severity. */
|
|
172
|
+
type ValidationRuleResult = true | string | {
|
|
173
|
+
message: string;
|
|
174
|
+
severity?: ValidationSeverity;
|
|
175
|
+
};
|
|
176
|
+
/** Resolves this rule instance's message (custom override or localized default) with `{var}` interpolation. */
|
|
177
|
+
type MessageFn = (vars?: Record<string, unknown>) => string;
|
|
178
|
+
/** Resolved per-instance rule params (loose at the DB boundary, narrowed by the author's generic). */
|
|
179
|
+
type ValidationParams = Record<string, unknown>;
|
|
180
|
+
type ValidationRuleValidateArgs<TParams extends ValidationParams, TValue, TData extends Record<string, unknown> = Record<string, unknown>> = {
|
|
181
|
+
value: TValue | null | undefined;
|
|
182
|
+
params: TParams;
|
|
183
|
+
siblingData: TData;
|
|
184
|
+
data: TData;
|
|
185
|
+
field: FormFieldInstance;
|
|
186
|
+
fieldType: string;
|
|
187
|
+
operation: 'create' | 'update';
|
|
188
|
+
event: 'onChange' | 'submit';
|
|
189
|
+
locale: string;
|
|
190
|
+
message: MessageFn; /** Server only. Absent in the client (req-less) context, so a server-only rule cannot run there. */
|
|
191
|
+
req?: PayloadRequest;
|
|
192
|
+
payload?: Payload; /** Server only. The form being submitted, for form-scoped lookups (e.g. notAlreadySubmitted). */
|
|
193
|
+
formId?: number | string;
|
|
194
|
+
};
|
|
195
|
+
type ValidationRuleValidate<TParams extends ValidationParams, TValue, TData extends Record<string, unknown> = Record<string, unknown>> = (args: ValidationRuleValidateArgs<TParams, TValue, TData>) => Promise<ValidationRuleResult> | ValidationRuleResult;
|
|
196
|
+
/**
|
|
197
|
+
* A validation rule type, authored once. `params` is a Payload `Field[]` rendered in the per-field
|
|
198
|
+
* constraint list; `validate` returns `true | string | { message, severity }`. `client` defaults from
|
|
199
|
+
* whether the rule is pure (a rule that is async or uses `req`/`payload` must set `client: false`).
|
|
200
|
+
*/
|
|
201
|
+
type ValidationRuleDefinition<TParams extends ValidationParams = ValidationParams, TValue = unknown, TData extends Record<string, unknown> = Record<string, unknown>> = {
|
|
202
|
+
type: string;
|
|
203
|
+
label: string;
|
|
204
|
+
description?: string; /** Field type slugs this rule may be added to. Omit for all field types. */
|
|
205
|
+
appliesTo?: string[];
|
|
206
|
+
params?: Field[];
|
|
207
|
+
defaultMessage: string; /** Runs client-side too. Defaults to `true`; set `false` for async or `req`/`payload`-using rules. */
|
|
208
|
+
client?: boolean; /** Default severity when `validate` returns a bare string. Defaults to `error`. */
|
|
209
|
+
severity?: ValidationSeverity;
|
|
210
|
+
validate: ValidationRuleValidate<TParams, TValue, TData>;
|
|
211
|
+
};
|
|
212
|
+
/** Erased shape stored in the heterogeneous registry; params re-narrow per matched rule at execution. */
|
|
213
|
+
type AnyValidationRuleValidate = (args: {
|
|
214
|
+
value: unknown;
|
|
215
|
+
params: ValidationParams;
|
|
216
|
+
siblingData: Record<string, unknown>;
|
|
217
|
+
data: Record<string, unknown>;
|
|
218
|
+
field: FormFieldInstance;
|
|
219
|
+
fieldType: string;
|
|
220
|
+
operation: 'create' | 'update';
|
|
221
|
+
event: 'onChange' | 'submit';
|
|
222
|
+
locale: string;
|
|
223
|
+
message: MessageFn;
|
|
224
|
+
req?: PayloadRequest;
|
|
225
|
+
payload?: Payload;
|
|
226
|
+
formId?: number | string;
|
|
227
|
+
}) => Promise<ValidationRuleResult> | ValidationRuleResult;
|
|
228
|
+
type AnyValidationRuleDefinition = {
|
|
229
|
+
type: string;
|
|
230
|
+
label: string;
|
|
231
|
+
description?: string;
|
|
232
|
+
appliesTo?: string[];
|
|
233
|
+
params?: Field[];
|
|
234
|
+
defaultMessage: string;
|
|
235
|
+
client?: boolean;
|
|
236
|
+
severity?: ValidationSeverity;
|
|
237
|
+
validate: AnyValidationRuleValidate;
|
|
238
|
+
};
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/presentations/types.d.ts
|
|
241
|
+
/** How a presentation surfaces the form. Drives wrapper selection and CSS. */
|
|
242
|
+
type PresentationSurface = 'inline' | 'page' | 'overlay';
|
|
243
|
+
type PresentationDensity = 'comfortable' | 'compact';
|
|
244
|
+
/**
|
|
245
|
+
* Serializable, framework-agnostic description of a presentation. The admin select and the
|
|
246
|
+
* stored `defaultPresentation` use only this; the React `Wrapper` is added in `./react`. No React here.
|
|
247
|
+
*/
|
|
248
|
+
type PresentationDescriptor = {
|
|
249
|
+
name: string; /** i18n key or literal shown in the admin select. */
|
|
250
|
+
label: string;
|
|
251
|
+
surface: PresentationSurface;
|
|
252
|
+
density: PresentationDensity; /** Overlay surfaces request dismissal after a successful submit. */
|
|
253
|
+
dismissOnSuccess?: boolean;
|
|
254
|
+
};
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/recall/interpolate.d.ts
|
|
257
|
+
/**
|
|
258
|
+
* Replace `{{ name }}` and `{{ name|fallback }}` tokens. `resolve` returns the recalled value (''
|
|
259
|
+
* when absent); the fallback (or '') is substituted when `resolve` yields ''. Token-free input is
|
|
260
|
+
* returned unchanged, so this is a no-op for ordinary text.
|
|
261
|
+
*/
|
|
262
|
+
declare const interpolate: (template: string, resolve: (name: string) => string) => string;
|
|
263
|
+
//#endregion
|
|
264
|
+
//#region src/recall/resolver.d.ts
|
|
265
|
+
type RecallResolver = (name: string) => string;
|
|
266
|
+
/** Derive a value->label map from a select-like field instance's `options`. */
|
|
267
|
+
declare const optionLabelsFor: (field: FormFieldInstance) => Record<string, string> | undefined;
|
|
268
|
+
/**
|
|
269
|
+
* Build a recall resolver: `{{name}}` -> the field's current value, formatted via its type's `format`
|
|
270
|
+
* (so a select shows its option label, a checkbox Yes/No, a date localized). Missing field or empty
|
|
271
|
+
* value -> ''. Pure and isomorphic; the renderer and server templates share it.
|
|
272
|
+
*/
|
|
273
|
+
declare const buildRecallResolver: (args: {
|
|
274
|
+
fields: FormFieldInstance[];
|
|
275
|
+
values: Record<string, unknown>;
|
|
276
|
+
registry: Map<string, AnyFormFieldDefinition>;
|
|
277
|
+
locale: string;
|
|
278
|
+
t: (key: string) => string;
|
|
279
|
+
}) => RecallResolver;
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/spam/constants.d.ts
|
|
282
|
+
/** Default honeypot decoy field name. Plausible to bots, unlikely to collide with a real field name. */
|
|
283
|
+
declare const DEFAULT_HONEYPOT_FIELD = "confirm_email";
|
|
284
|
+
/** Reserved `values` entry carrying a captcha token from the client. */
|
|
285
|
+
declare const CAPTCHA_TOKEN_KEY = "__fb_captcha";
|
|
286
|
+
//#endregion
|
|
287
|
+
export { FieldMeta as A, evaluateCalc as C, AggregationBucket as D, CalcExpression as E, SubmissionValue as F, FormFieldInstance as M, SubmissionDescriptor as N, AggregationRow as O, SubmissionFieldError as P, evaluateCondition as S, computeCalcFields as T, PrefillOptions as _, optionLabelsFor as a, FieldTypeRegistry as b, PresentationDescriptor as c, ValidationParams as d, ValidationRuleDefinition as f, FormEventSink as g, FormEvent as h, buildRecallResolver as i, SubmissionStatusFilter as j, FieldAggregation as k, PresentationSurface as l, ValidationSeverity as m, DEFAULT_HONEYPOT_FIELD as n, interpolate as o, ValidationRuleResult as p, RecallResolver as r, PresentationDensity as s, CAPTCHA_TOKEN_KEY as t, AnyValidationRuleDefinition as u, valuesFromSearchParams as v, calcExpressionOf as w, FieldTypesConfig as x, FieldTypeOption as y };
|
|
288
|
+
//# sourceMappingURL=constants-B-BUfetP.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { t as ConditionFieldType } from "../fieldTypes-B8jkNRob.js";
|
|
2
|
+
|
|
3
|
+
//#region src/client/FormConditionField.d.ts
|
|
4
|
+
/** Props: standard JSON field client props plus the `conditionTypes` map we pass via clientProps. */
|
|
5
|
+
type FormConditionFieldProps = {
|
|
6
|
+
path?: string;
|
|
7
|
+
field?: {
|
|
8
|
+
label?: unknown;
|
|
9
|
+
name?: string;
|
|
10
|
+
};
|
|
11
|
+
label?: unknown;
|
|
12
|
+
conditionTypes: Record<string, ConditionFieldType>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The `Field` mounted on a form field's `visibleWhen`/`validateWhen` json column. Binds the field's own
|
|
16
|
+
* path with `useField<Where>()` (the QueryPresetsWhereField precedent) and renders the native builder.
|
|
17
|
+
*/
|
|
18
|
+
declare const FormConditionField: (props: FormConditionFieldProps) => import("react/jsx-runtime").JSX.Element;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { FormConditionField };
|
|
21
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { t as keys } from "../keys-N5xGiUsh.js";
|
|
3
|
+
import { a as operatorValueShape, i as operatorLabelKey, r as firstOperatorFor, t as conditionOperators } from "../fieldTypes-CzhhJXjg.js";
|
|
4
|
+
import { reduceFieldsToValues, transformWhereQuery } from "payload/shared";
|
|
5
|
+
import { DateCondition, FieldLabel, NumberCondition, ReactSelect, SelectCondition, TextCondition, useField, useFormFields, useTranslation } from "@payloadcms/ui";
|
|
6
|
+
import { useMemo } from "react";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
//#region src/translations/useTranslation.ts
|
|
9
|
+
/**
|
|
10
|
+
* `useTranslation` bound to this plugin's keys, so `t(keys.X)` typechecks without
|
|
11
|
+
* a per-call `@ts-expect-error`. Returns Payload's `{ t, i18n }` unchanged.
|
|
12
|
+
*/
|
|
13
|
+
const useTranslation$1 = () => useTranslation();
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/client/synthesizeClientField.ts
|
|
16
|
+
/** Project a stored field row into a condition operand, or `null` when the row has no usable name. */
|
|
17
|
+
const operandFromRow = (row, conditionTypes) => {
|
|
18
|
+
const name = typeof row.name === "string" ? row.name.trim() : "";
|
|
19
|
+
if (name.length === 0) return null;
|
|
20
|
+
const conditionType = conditionTypes[row.blockType] ?? "text";
|
|
21
|
+
return {
|
|
22
|
+
name,
|
|
23
|
+
label: typeof row.label === "string" && row.label.length > 0 ? row.label : name,
|
|
24
|
+
conditionType,
|
|
25
|
+
options: Array.isArray(row.options) ? row.options : void 0
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Build the minimal `*FieldClient` a Payload leaf condition input requires for a given operand. Casts
|
|
30
|
+
* are localized to these four synths and nowhere else: `FieldBaseClient` requires only `name`, and each
|
|
31
|
+
* leaf is `FieldBaseClient & Pick<…, 'type' | …>` where every picked member but `type` (and `select`'s
|
|
32
|
+
* required `options`) is optional, so the literals are structurally complete. `dateClientField` omits
|
|
33
|
+
* `admin.date`, so `DateCondition` falls back to its default date format. Each synth returns a single
|
|
34
|
+
* concrete client type so leaf `field` props need no narrowing.
|
|
35
|
+
*/
|
|
36
|
+
const textClientField = (operand) => ({
|
|
37
|
+
name: operand.name,
|
|
38
|
+
label: operand.label,
|
|
39
|
+
type: "text"
|
|
40
|
+
});
|
|
41
|
+
const numberClientField = (operand) => ({
|
|
42
|
+
name: operand.name,
|
|
43
|
+
label: operand.label,
|
|
44
|
+
type: "number"
|
|
45
|
+
});
|
|
46
|
+
const dateClientField = (operand) => ({
|
|
47
|
+
name: operand.name,
|
|
48
|
+
label: operand.label,
|
|
49
|
+
type: "date"
|
|
50
|
+
});
|
|
51
|
+
const selectClientField = (operand) => ({
|
|
52
|
+
name: operand.name,
|
|
53
|
+
label: operand.label,
|
|
54
|
+
type: "select",
|
|
55
|
+
options: operand.options ?? []
|
|
56
|
+
});
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/client/ConditionRow.tsx
|
|
59
|
+
/** ReactSelect's `onChange` hands back `Option | Option[]`; narrow it to the single chosen option. */
|
|
60
|
+
const singleOption = (selected) => Array.isArray(selected) ? selected[0] : selected;
|
|
61
|
+
const ConditionRow = ({ condition, operands, onChange, onRemove }) => {
|
|
62
|
+
const { t, i18n } = useTranslation$1();
|
|
63
|
+
const operand = operands.find((entry) => entry.name === condition.field);
|
|
64
|
+
const conditionType = operand?.conditionType ?? "text";
|
|
65
|
+
const operators = conditionOperators[conditionType];
|
|
66
|
+
const fieldOptions = operands.map((entry) => ({
|
|
67
|
+
label: entry.label,
|
|
68
|
+
value: entry.name
|
|
69
|
+
}));
|
|
70
|
+
const operatorOptions = operators.map((operator) => ({
|
|
71
|
+
label: i18n.t(operatorLabelKey(operator)),
|
|
72
|
+
value: operator
|
|
73
|
+
}));
|
|
74
|
+
const handleFieldChange = (selected) => {
|
|
75
|
+
const chosen = singleOption(selected);
|
|
76
|
+
const next = operands.find((entry) => entry.name === chosen?.value);
|
|
77
|
+
if (!next) return;
|
|
78
|
+
onChange({
|
|
79
|
+
field: next.name,
|
|
80
|
+
operator: firstOperatorFor(next.conditionType),
|
|
81
|
+
value: void 0
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
const handleOperatorChange = (selected) => {
|
|
85
|
+
const chosen = singleOption(selected);
|
|
86
|
+
if (!chosen) return;
|
|
87
|
+
const operator = chosen.value;
|
|
88
|
+
const keepValue = operatorValueShape(condition.operator) === operatorValueShape(operator);
|
|
89
|
+
onChange({
|
|
90
|
+
...condition,
|
|
91
|
+
operator,
|
|
92
|
+
value: keepValue ? condition.value : void 0
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
96
|
+
className: "form-builder-condition-row",
|
|
97
|
+
style: {
|
|
98
|
+
display: "flex",
|
|
99
|
+
gap: 8,
|
|
100
|
+
alignItems: "flex-start"
|
|
101
|
+
},
|
|
102
|
+
children: [
|
|
103
|
+
/* @__PURE__ */ jsx("div", {
|
|
104
|
+
style: { minWidth: 160 },
|
|
105
|
+
children: /* @__PURE__ */ jsx(ReactSelect, {
|
|
106
|
+
options: fieldOptions,
|
|
107
|
+
value: fieldOptions.find((option) => option.value === condition.field),
|
|
108
|
+
placeholder: t(keys.conditionSelectField),
|
|
109
|
+
isClearable: false,
|
|
110
|
+
onChange: handleFieldChange
|
|
111
|
+
})
|
|
112
|
+
}),
|
|
113
|
+
/* @__PURE__ */ jsx("div", {
|
|
114
|
+
style: { minWidth: 140 },
|
|
115
|
+
children: /* @__PURE__ */ jsx(ReactSelect, {
|
|
116
|
+
options: operatorOptions,
|
|
117
|
+
value: operatorOptions.find((option) => option.value === condition.operator),
|
|
118
|
+
disabled: !operand,
|
|
119
|
+
isClearable: false,
|
|
120
|
+
onChange: handleOperatorChange
|
|
121
|
+
})
|
|
122
|
+
}),
|
|
123
|
+
/* @__PURE__ */ jsx("div", {
|
|
124
|
+
style: { flex: 1 },
|
|
125
|
+
children: /* @__PURE__ */ jsx(ConditionValue, {
|
|
126
|
+
conditionType,
|
|
127
|
+
operand,
|
|
128
|
+
operator: condition.operator,
|
|
129
|
+
value: condition.value,
|
|
130
|
+
onChange: (value) => onChange({
|
|
131
|
+
...condition,
|
|
132
|
+
value
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
}),
|
|
136
|
+
/* @__PURE__ */ jsx("button", {
|
|
137
|
+
type: "button",
|
|
138
|
+
onClick: onRemove,
|
|
139
|
+
"aria-label": t(keys.conditionRemove),
|
|
140
|
+
children: t(keys.conditionRemove)
|
|
141
|
+
})
|
|
142
|
+
]
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
const ConditionValue = ({ conditionType, operand, operator, value, onChange }) => {
|
|
146
|
+
const { t } = useTranslation$1();
|
|
147
|
+
if (conditionType === "checkbox" || operatorValueShape(operator) === "boolean") {
|
|
148
|
+
const options = [{
|
|
149
|
+
label: t(keys.conditionTrue),
|
|
150
|
+
value: "true"
|
|
151
|
+
}, {
|
|
152
|
+
label: t(keys.conditionFalse),
|
|
153
|
+
value: "false"
|
|
154
|
+
}];
|
|
155
|
+
const handleBooleanChange = (selected) => {
|
|
156
|
+
onChange(singleOption(selected)?.value === "true");
|
|
157
|
+
};
|
|
158
|
+
return /* @__PURE__ */ jsx(ReactSelect, {
|
|
159
|
+
options,
|
|
160
|
+
value: options.find((option) => String(option.value) === String(value)) ?? options[0],
|
|
161
|
+
isClearable: false,
|
|
162
|
+
onChange: handleBooleanChange
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
if (!operand) return null;
|
|
166
|
+
switch (conditionType) {
|
|
167
|
+
case "number": return /* @__PURE__ */ jsx(NumberCondition, {
|
|
168
|
+
disabled: false,
|
|
169
|
+
field: numberClientField(operand),
|
|
170
|
+
operator,
|
|
171
|
+
value,
|
|
172
|
+
onChange: (next) => onChange(next)
|
|
173
|
+
});
|
|
174
|
+
case "date": return /* @__PURE__ */ jsx(DateCondition, {
|
|
175
|
+
disabled: false,
|
|
176
|
+
field: dateClientField(operand),
|
|
177
|
+
operator,
|
|
178
|
+
value,
|
|
179
|
+
onChange: (next) => onChange(next)
|
|
180
|
+
});
|
|
181
|
+
case "select": return /* @__PURE__ */ jsx(SelectCondition, {
|
|
182
|
+
disabled: false,
|
|
183
|
+
field: selectClientField(operand),
|
|
184
|
+
operator,
|
|
185
|
+
options: operand.options ?? [],
|
|
186
|
+
value,
|
|
187
|
+
onChange: (next) => onChange(next)
|
|
188
|
+
});
|
|
189
|
+
default: return /* @__PURE__ */ jsx(TextCondition, {
|
|
190
|
+
disabled: false,
|
|
191
|
+
field: textClientField(operand),
|
|
192
|
+
operator,
|
|
193
|
+
value,
|
|
194
|
+
onChange: (next) => onChange(next)
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/client/ConditionBuilder.tsx
|
|
200
|
+
const toRow = (constraint) => {
|
|
201
|
+
const field = Object.keys(constraint)[0];
|
|
202
|
+
if (!field) return null;
|
|
203
|
+
const ops = constraint[field];
|
|
204
|
+
const operator = Object.keys(ops)[0];
|
|
205
|
+
if (!operator) return null;
|
|
206
|
+
return {
|
|
207
|
+
field,
|
|
208
|
+
operator,
|
|
209
|
+
value: ops[operator]
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
const fromRow = (row) => ({ [row.field]: { [row.operator]: row.value } });
|
|
213
|
+
const readGroups = (value) => {
|
|
214
|
+
if (!value || Object.keys(value).length === 0) return [];
|
|
215
|
+
const canonical = transformWhereQuery(value);
|
|
216
|
+
return (Array.isArray(canonical.or) ? canonical.or : []).map((group) => {
|
|
217
|
+
return (Array.isArray(group.and) ? group.and : []).map((constraint) => toRow(constraint)).filter((row) => row !== null);
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
const writeGroups = (groups) => ({ or: groups.filter((group) => group.length > 0).map((group) => ({ and: group.map(fromRow) })) });
|
|
221
|
+
const operandsFromData = (data, conditionTypes, selfName) => {
|
|
222
|
+
return (Array.isArray(data.fields) ? data.fields : []).map((row) => operandFromRow(row, conditionTypes)).filter((operand) => operand !== null && operand.name !== selfName);
|
|
223
|
+
};
|
|
224
|
+
const ConditionBuilder = ({ value, onChange, conditionTypes, selfName }) => {
|
|
225
|
+
const { t } = useTranslation$1();
|
|
226
|
+
const operandsJson = useFormFields(([fields]) => JSON.stringify(operandsFromData(reduceFieldsToValues(fields, true), conditionTypes, selfName)));
|
|
227
|
+
const operands = useMemo(() => JSON.parse(operandsJson), [operandsJson]);
|
|
228
|
+
const groups = readGroups(value);
|
|
229
|
+
const emit = (next) => onChange(writeGroups(next));
|
|
230
|
+
const addCondition = (groupIndex) => {
|
|
231
|
+
const first = operands[0];
|
|
232
|
+
if (!first) return;
|
|
233
|
+
const row = {
|
|
234
|
+
field: first.name,
|
|
235
|
+
operator: firstOperatorFor(first.conditionType),
|
|
236
|
+
value: void 0
|
|
237
|
+
};
|
|
238
|
+
emit(groups.length === 0 ? [[row]] : groups.map((group, index) => index === groupIndex ? [...group, row] : group));
|
|
239
|
+
};
|
|
240
|
+
const addOrGroup = () => {
|
|
241
|
+
const first = operands[0];
|
|
242
|
+
if (!first) return;
|
|
243
|
+
emit([...groups, [{
|
|
244
|
+
field: first.name,
|
|
245
|
+
operator: firstOperatorFor(first.conditionType),
|
|
246
|
+
value: void 0
|
|
247
|
+
}]]);
|
|
248
|
+
};
|
|
249
|
+
const updateRow = (groupIndex, rowIndex, row) => emit(groups.map((group, index) => index === groupIndex ? group.map((existing, position) => position === rowIndex ? row : existing) : group));
|
|
250
|
+
const removeRow = (groupIndex, rowIndex) => emit(groups.map((group, index) => index === groupIndex ? group.filter((_, position) => position !== rowIndex) : group).filter((group) => group.length > 0));
|
|
251
|
+
if (operands.length === 0) return /* @__PURE__ */ jsx("p", {
|
|
252
|
+
className: "form-builder-condition__hint",
|
|
253
|
+
children: t(keys.conditionNoFields)
|
|
254
|
+
});
|
|
255
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
256
|
+
className: "form-builder-condition",
|
|
257
|
+
children: [groups.length === 0 ? /* @__PURE__ */ jsx("p", {
|
|
258
|
+
className: "form-builder-condition__hint",
|
|
259
|
+
children: t(keys.conditionEmpty)
|
|
260
|
+
}) : groups.map((group, groupIndex) => /* @__PURE__ */ jsxs("div", {
|
|
261
|
+
className: "form-builder-condition__group",
|
|
262
|
+
children: [
|
|
263
|
+
groupIndex > 0 ? /* @__PURE__ */ jsx("div", {
|
|
264
|
+
className: "form-builder-condition__or",
|
|
265
|
+
children: t(keys.conditionOr)
|
|
266
|
+
}) : null,
|
|
267
|
+
group.map((row, rowIndex) => /* @__PURE__ */ jsxs("div", {
|
|
268
|
+
className: "form-builder-condition__and",
|
|
269
|
+
children: [rowIndex > 0 ? /* @__PURE__ */ jsx("span", { children: t(keys.conditionAnd) }) : null, /* @__PURE__ */ jsx(ConditionRow, {
|
|
270
|
+
condition: row,
|
|
271
|
+
operands,
|
|
272
|
+
onChange: (next) => updateRow(groupIndex, rowIndex, next),
|
|
273
|
+
onRemove: () => removeRow(groupIndex, rowIndex)
|
|
274
|
+
})]
|
|
275
|
+
}, rowIndex)),
|
|
276
|
+
/* @__PURE__ */ jsx("button", {
|
|
277
|
+
type: "button",
|
|
278
|
+
onClick: () => addCondition(groupIndex),
|
|
279
|
+
children: t(keys.conditionAddCondition)
|
|
280
|
+
})
|
|
281
|
+
]
|
|
282
|
+
}, groupIndex)), /* @__PURE__ */ jsx("div", {
|
|
283
|
+
className: "form-builder-condition__actions",
|
|
284
|
+
children: /* @__PURE__ */ jsx("button", {
|
|
285
|
+
type: "button",
|
|
286
|
+
onClick: () => groups.length === 0 ? addCondition(0) : addOrGroup(),
|
|
287
|
+
children: groups.length === 0 ? t(keys.conditionAddCondition) : t(keys.conditionAddOr)
|
|
288
|
+
})
|
|
289
|
+
})]
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/client/FormConditionField.tsx
|
|
294
|
+
const toStaticLabel = (label) => {
|
|
295
|
+
if (typeof label === "string") return label;
|
|
296
|
+
if (label && typeof label === "object") return label;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* The `Field` mounted on a form field's `visibleWhen`/`validateWhen` json column. Binds the field's own
|
|
300
|
+
* path with `useField<Where>()` (the QueryPresetsWhereField precedent) and renders the native builder.
|
|
301
|
+
*/
|
|
302
|
+
const FormConditionField = (props) => {
|
|
303
|
+
const { path, setValue, value } = useField();
|
|
304
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
305
|
+
className: "field-type form-builder-condition-field",
|
|
306
|
+
children: [/* @__PURE__ */ jsx(FieldLabel, {
|
|
307
|
+
label: toStaticLabel(props.field?.label ?? props.label),
|
|
308
|
+
path
|
|
309
|
+
}), /* @__PURE__ */ jsx(ConditionBuilder, {
|
|
310
|
+
value: value ?? void 0,
|
|
311
|
+
onChange: setValue,
|
|
312
|
+
conditionTypes: props.conditionTypes,
|
|
313
|
+
selfName: props.field?.name
|
|
314
|
+
})]
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
//#endregion
|
|
318
|
+
export { FormConditionField };
|
|
319
|
+
|
|
320
|
+
//# sourceMappingURL=client.js.map
|