formo-react 0.0.0-stage → 0.2.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/LICENSE +201 -0
- package/README.md +21 -2
- package/dist/components/FormoBlueprint.d.ts +5 -0
- package/dist/components/FormoBlueprint.js +30 -0
- package/dist/components/FormoButton.d.ts +6 -0
- package/dist/components/FormoButton.js +23 -0
- package/dist/components/FormoCheckbox.d.ts +7 -0
- package/dist/components/FormoCheckbox.js +10 -0
- package/dist/components/FormoChecklist.d.ts +5 -0
- package/dist/components/FormoChecklist.js +15 -0
- package/dist/components/FormoChoices.d.ts +8 -0
- package/dist/components/FormoChoices.js +33 -0
- package/dist/components/FormoContext.d.ts +27 -0
- package/dist/components/FormoContext.js +69 -0
- package/dist/components/FormoErrors.d.ts +5 -0
- package/dist/components/FormoErrors.js +14 -0
- package/dist/components/FormoField.d.ts +17 -0
- package/dist/components/FormoField.js +148 -0
- package/dist/components/FormoForm.d.ts +15 -0
- package/dist/components/FormoForm.js +76 -0
- package/dist/components/FormoGroup.d.ts +5 -0
- package/dist/components/FormoGroup.js +15 -0
- package/dist/components/FormoHelp.d.ts +6 -0
- package/dist/components/FormoHelp.js +9 -0
- package/dist/components/FormoInput.d.ts +6 -0
- package/dist/components/FormoInput.js +24 -0
- package/dist/components/FormoLabel.d.ts +6 -0
- package/dist/components/FormoLabel.js +9 -0
- package/dist/components/FormoProvider.d.ts +11 -0
- package/dist/components/FormoProvider.js +13 -0
- package/dist/components/FormoRest.d.ts +6 -0
- package/dist/components/FormoRest.js +23 -0
- package/dist/components/FormoSelect.d.ts +6 -0
- package/dist/components/FormoSelect.js +36 -0
- package/dist/components/FormoUneditable.d.ts +6 -0
- package/dist/components/FormoUneditable.js +12 -0
- package/dist/core/builder.d.ts +42 -0
- package/dist/core/builder.js +166 -0
- package/dist/core/controls.d.ts +27 -0
- package/dist/core/controls.js +24 -0
- package/dist/core/fields.d.ts +63 -0
- package/dist/core/fields.js +177 -0
- package/dist/core/messages.d.ts +35 -0
- package/dist/core/messages.js +69 -0
- package/dist/core/normalize.d.ts +35 -0
- package/dist/core/normalize.js +234 -0
- package/dist/core/options.d.ts +44 -0
- package/dist/core/options.js +146 -0
- package/dist/core/rules.d.ts +41 -0
- package/dist/core/rules.js +230 -0
- package/dist/core/store.d.ts +155 -0
- package/dist/core/store.js +830 -0
- package/dist/core/theme.d.ts +20 -0
- package/dist/core/theme.js +21 -0
- package/dist/core/tree.d.ts +74 -0
- package/dist/core/tree.js +119 -0
- package/dist/core/utils.d.ts +35 -0
- package/dist/core/utils.js +206 -0
- package/dist/hooks/subscriptions.d.ts +38 -0
- package/dist/hooks/subscriptions.js +65 -0
- package/dist/hooks/useFormo.d.ts +18 -0
- package/dist/hooks/useFormo.js +25 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +27 -0
- package/dist/schema/form.d.ts +92 -0
- package/dist/schema/form.js +304 -0
- package/dist/schema/index.d.ts +22 -0
- package/dist/schema/index.js +21 -0
- package/dist/schema/ir.d.ts +189 -0
- package/dist/schema/ir.js +130 -0
- package/dist/schema/json-schema.d.ts +25 -0
- package/dist/schema/json-schema.js +351 -0
- package/dist/schema/laravel.d.ts +57 -0
- package/dist/schema/laravel.js +555 -0
- package/dist/schema/openapi.d.ts +24 -0
- package/dist/schema/openapi.js +67 -0
- package/dist/schema/policy.d.ts +60 -0
- package/dist/schema/policy.js +315 -0
- package/dist/schema/spec.d.ts +75 -0
- package/dist/schema/spec.js +1 -0
- package/dist/schema/values.d.ts +35 -0
- package/dist/schema/values.js +151 -0
- package/dist/schema/zod.d.ts +79 -0
- package/dist/schema/zod.js +236 -0
- package/dist/types.d.ts +400 -0
- package/dist/types.js +1 -0
- package/package.json +70 -4
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { blankLabel, firstEnabledOption, isMultiple, normalizeOptions, settleValue } from './options';
|
|
2
|
+
import { deepClone, deepMerge, getPath, humanize, isPlainObject, joinPath, setPath } from './utils';
|
|
3
|
+
const INPUT_TYPES = new Set(['text', 'email', 'password', 'number', 'tel', 'url', 'search']);
|
|
4
|
+
/** A builder (anything with `toSchema()`), as a definition */
|
|
5
|
+
function schemaOfBuilder(input) {
|
|
6
|
+
return input && typeof input.toSchema === 'function' ? input.toSchema() : undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The entries of a `fields` object, formo-vue style: keys are aliases, strings
|
|
10
|
+
* are drivers, builders are subforms / fields, falsy entries are skipped
|
|
11
|
+
* (`admin: isAdmin && { driver: 'checkbox' }`)
|
|
12
|
+
*/
|
|
13
|
+
function fieldsObjectEntries(fields) {
|
|
14
|
+
const out = [];
|
|
15
|
+
for (const [alias, child] of Object.entries(fields)) {
|
|
16
|
+
if (!child)
|
|
17
|
+
continue;
|
|
18
|
+
if (typeof child === 'string')
|
|
19
|
+
out.push({ alias, driver: child });
|
|
20
|
+
else
|
|
21
|
+
out.push({ ...(schemaOfBuilder(child) ?? child), alias });
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
function toFieldDef(input) {
|
|
26
|
+
const built = schemaOfBuilder(input);
|
|
27
|
+
if (built)
|
|
28
|
+
return built;
|
|
29
|
+
if (typeof input === 'string')
|
|
30
|
+
return { alias: input };
|
|
31
|
+
if (Array.isArray(input)) {
|
|
32
|
+
const [alias, driver, val, opts] = input;
|
|
33
|
+
return { ...(opts ?? {}), alias, ...(driver ? { driver } : {}), ...(input.length >= 3 ? { val } : {}) };
|
|
34
|
+
}
|
|
35
|
+
return input;
|
|
36
|
+
}
|
|
37
|
+
function normalizeRules(rules, required) {
|
|
38
|
+
const list = [];
|
|
39
|
+
if (required)
|
|
40
|
+
list.push('required');
|
|
41
|
+
if (!rules)
|
|
42
|
+
return list;
|
|
43
|
+
if (typeof rules === 'string') {
|
|
44
|
+
for (const part of rules.split('|').filter(Boolean))
|
|
45
|
+
list.push(part);
|
|
46
|
+
}
|
|
47
|
+
else if (Array.isArray(rules)) {
|
|
48
|
+
if (typeof rules[0] === 'string' && (rules.length === 1 || Array.isArray(rules[1]))) {
|
|
49
|
+
list.push(rules);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
for (const item of rules) {
|
|
53
|
+
if (item !== undefined)
|
|
54
|
+
list.push(item);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
list.push(rules);
|
|
60
|
+
}
|
|
61
|
+
return list;
|
|
62
|
+
}
|
|
63
|
+
function normalizeNode(def, fallbackDriver = 'input', nested = true) {
|
|
64
|
+
let driver = typeof def.driver === 'string' && def.driver ? def.driver : '';
|
|
65
|
+
let type = def.type;
|
|
66
|
+
if (driver.includes('|')) {
|
|
67
|
+
;
|
|
68
|
+
[driver, type] = driver.split('|');
|
|
69
|
+
}
|
|
70
|
+
if (driver === 'bool')
|
|
71
|
+
driver = 'checkbox';
|
|
72
|
+
if (driver && INPUT_TYPES.has(driver)) {
|
|
73
|
+
type = driver;
|
|
74
|
+
driver = 'input';
|
|
75
|
+
}
|
|
76
|
+
const hasFields = Array.isArray(def.fields)
|
|
77
|
+
? def.fields.some(Boolean)
|
|
78
|
+
: isPlainObject(def.fields) && Object.values(def.fields).some(Boolean);
|
|
79
|
+
if (!driver) {
|
|
80
|
+
driver = hasFields || def.blueprint === true ? 'group' : fallbackDriver;
|
|
81
|
+
}
|
|
82
|
+
// "form" is used once: a form added to a form is a subform (formo-vue)
|
|
83
|
+
if (nested && driver === 'form')
|
|
84
|
+
driver = 'group';
|
|
85
|
+
const children = [];
|
|
86
|
+
if (Array.isArray(def.fields)) {
|
|
87
|
+
for (const child of def.fields)
|
|
88
|
+
if (child)
|
|
89
|
+
children.push(normalizeNode(toFieldDef(child)));
|
|
90
|
+
}
|
|
91
|
+
else if (isPlainObject(def.fields)) {
|
|
92
|
+
for (const childDef of fieldsObjectEntries(def.fields))
|
|
93
|
+
children.push(normalizeNode(childDef));
|
|
94
|
+
}
|
|
95
|
+
const alias = def.alias ?? 'formo';
|
|
96
|
+
return {
|
|
97
|
+
alias,
|
|
98
|
+
driver,
|
|
99
|
+
type,
|
|
100
|
+
val: def.val ?? def.value,
|
|
101
|
+
// `false` / `null` hide the label (formo-vue): resolved to ''
|
|
102
|
+
label: def.label === false || def.label === null ? '' : (def.label ?? humanize(alias)),
|
|
103
|
+
placeholder: def.placeholder ?? def.attr?.placeholder,
|
|
104
|
+
message: def.message,
|
|
105
|
+
required: Boolean(def.required),
|
|
106
|
+
editable: typeof def.editable === 'function' ? def.editable : def.editable !== false,
|
|
107
|
+
disabled: Boolean(def.disabled),
|
|
108
|
+
render: def.render !== false,
|
|
109
|
+
exclude: def.exclude,
|
|
110
|
+
rules: normalizeRules(def.rules, def.required),
|
|
111
|
+
errorMessages: { ...(def.error_messages ?? {}), ...(def.errorMessages ?? {}) },
|
|
112
|
+
errorDisplay: def.checklist === true ? 'checklist' : def.errorDisplay,
|
|
113
|
+
requirements: { ...(def.requirements ?? {}) },
|
|
114
|
+
options: normalizeOptions(def.opts ?? def.options),
|
|
115
|
+
blank: def.blank,
|
|
116
|
+
config: def.config,
|
|
117
|
+
fields: children,
|
|
118
|
+
blueprint: def.blueprint === true,
|
|
119
|
+
min: typeof def.min === 'number' ? def.min : undefined,
|
|
120
|
+
max: typeof def.max === 'number' ? def.max : undefined,
|
|
121
|
+
addLabel: def.addLabel,
|
|
122
|
+
removeLabel: def.removeLabel,
|
|
123
|
+
rowLabel: def.rowLabel,
|
|
124
|
+
component: def.component,
|
|
125
|
+
props: def.props,
|
|
126
|
+
model: def.model,
|
|
127
|
+
view: def.view,
|
|
128
|
+
attr: { ...(def.attr ?? {}) },
|
|
129
|
+
raw: def,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export function normalize(input) {
|
|
133
|
+
const def = typeof input?.toSchema === 'function' ? input.toSchema() : input;
|
|
134
|
+
return normalizeNode(def, 'form', false);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A name for a field even when its label is hidden (`label: false`): for
|
|
138
|
+
* accessibility labels, button texts, messages
|
|
139
|
+
*/
|
|
140
|
+
export function accessibleLabel(node) {
|
|
141
|
+
return node.label || humanize(node.alias);
|
|
142
|
+
}
|
|
143
|
+
/** Subforms and blueprints: nodes whose children are fields */
|
|
144
|
+
export function isGroupNode(node) {
|
|
145
|
+
return node.driver === 'group' || node.driver === 'form' || node.blueprint || node.fields.length > 0;
|
|
146
|
+
}
|
|
147
|
+
/** Buttons: rendered, but no value */
|
|
148
|
+
export function isValueless(node) {
|
|
149
|
+
return node.driver === 'button' || node.driver === 'submit';
|
|
150
|
+
}
|
|
151
|
+
/** `hidden` fields (`driver: 'hidden'`, `input|hidden`): a value, nothing rendered */
|
|
152
|
+
export function isHidden(node) {
|
|
153
|
+
return node.driver === 'hidden' || (node.driver === 'input' && node.type === 'hidden');
|
|
154
|
+
}
|
|
155
|
+
/** Whether a group nests its fields' values under its alias (`namespaces` config, cascading) */
|
|
156
|
+
export function groupNamespaces(node, inherited) {
|
|
157
|
+
return node.blueprint || (node.config?.namespaces ?? inherited.namespaces ?? true) !== false;
|
|
158
|
+
}
|
|
159
|
+
/** `config` of a node, layered over what it inherits */
|
|
160
|
+
export function nodeConfig(node, inherited) {
|
|
161
|
+
return node.config ? { ...inherited, ...node.config } : inherited;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The value a field starts with when its definition has no `val` (formo-vue's driver defaults):
|
|
165
|
+
* `checkbox` / `switch` → `false`, `checkboxes` and multiple selects → `[]`, `radios` → `null`,
|
|
166
|
+
* a select with a blank option → `null`, a select without one → its first enabled option
|
|
167
|
+
* (what a browser `<select>` selects), everything else → `''`.
|
|
168
|
+
*/
|
|
169
|
+
export function defaultValueFor(node) {
|
|
170
|
+
switch (node.driver) {
|
|
171
|
+
case 'checkbox':
|
|
172
|
+
case 'switch':
|
|
173
|
+
return false;
|
|
174
|
+
case 'checkboxes':
|
|
175
|
+
return [];
|
|
176
|
+
case 'radios':
|
|
177
|
+
return null;
|
|
178
|
+
case 'select':
|
|
179
|
+
if (isMultiple(node))
|
|
180
|
+
return [];
|
|
181
|
+
if (blankLabel(node) !== null)
|
|
182
|
+
return null;
|
|
183
|
+
return firstEnabledOption(node.options)?.value ?? null;
|
|
184
|
+
default:
|
|
185
|
+
return '';
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** The values of one new row of a blueprint: its fields' defaults */
|
|
189
|
+
export function rowDefaults(blueprint, config = {}) {
|
|
190
|
+
return buildInitialValues({ ...blueprint, blueprint: false, val: undefined }, config);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The initial values of a form (formo-vue's `buildInitialValues`): each field's
|
|
194
|
+
* `val`, or its driver's default. Groups nest (unless `namespaces: false`),
|
|
195
|
+
* blueprints are arrays of rows — their `val` rows over the row defaults, else
|
|
196
|
+
* `min` empty rows. A group's (or the form's) `val` object is merged in.
|
|
197
|
+
* Buttons have no value.
|
|
198
|
+
*/
|
|
199
|
+
export function buildInitialValues(node, config = {}, out = {}, prefix = '') {
|
|
200
|
+
const cfg = nodeConfig(node, config);
|
|
201
|
+
for (const child of node.fields) {
|
|
202
|
+
const path = joinPath(prefix, child.alias);
|
|
203
|
+
if (isGroupNode(child)) {
|
|
204
|
+
const childCfg = nodeConfig(child, cfg);
|
|
205
|
+
if (child.blueprint) {
|
|
206
|
+
const defaults = rowDefaults(child, childCfg);
|
|
207
|
+
const rows = Array.isArray(child.val)
|
|
208
|
+
? child.val.map((row) => deepMerge(deepClone(defaults), isPlainObject(row) ? row : {}))
|
|
209
|
+
: Array.from({ length: child.min ?? 0 }, () => deepClone(defaults));
|
|
210
|
+
setPath(out, path, rows);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const nextPrefix = groupNamespaces(child, cfg) ? path : prefix;
|
|
214
|
+
buildInitialValues(child, cfg, out, nextPrefix);
|
|
215
|
+
if (isPlainObject(child.val)) {
|
|
216
|
+
// A group's `val` is a values object for its fields
|
|
217
|
+
const target = nextPrefix ? (getPath(out, nextPrefix) ?? {}) : out;
|
|
218
|
+
const merged = deepMerge(target, child.val);
|
|
219
|
+
if (nextPrefix)
|
|
220
|
+
setPath(out, nextPrefix, merged);
|
|
221
|
+
else
|
|
222
|
+
Object.assign(out, merged);
|
|
223
|
+
}
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (isValueless(child))
|
|
227
|
+
continue;
|
|
228
|
+
setPath(out, path, child.val !== undefined ? settleValue(child, deepClone(child.val)) : defaultValueFor(child));
|
|
229
|
+
}
|
|
230
|
+
// The form's own `val` (ex: `formFromModel(model, { values })`)
|
|
231
|
+
if (!prefix && isPlainObject(node.val))
|
|
232
|
+
return deepMerge(out, node.val);
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { FieldNode, FormoOption, FormoOptionGroup, FormoOptionItem, FormoOptionsInput } from '../types';
|
|
2
|
+
export declare function isOptionGroup(item: FormoOptionItem): item is FormoOptionGroup;
|
|
3
|
+
/**
|
|
4
|
+
* Normalize any supported `opts` shape into `[{ value, label }]` / option groups.
|
|
5
|
+
* Object keys are used as values (Formo: `$key => $label`), nested objects become groups,
|
|
6
|
+
* bare values (`['a', 'b']`) are their own label.
|
|
7
|
+
*/
|
|
8
|
+
export declare function normalizeOptions(input: FormoOptionsInput | undefined | null): FormoOptionItem[];
|
|
9
|
+
/** All options, groups flattened. Options of a disabled group are disabled */
|
|
10
|
+
export declare function flattenOptions(items: FormoOptionItem[]): FormoOption[];
|
|
11
|
+
/** Loose equality for option values (`1` matches `'1'`) */
|
|
12
|
+
export declare function sameValue(a: unknown, b: unknown): boolean;
|
|
13
|
+
export declare function optionLabel(items: FormoOptionItem[], value: unknown): string | undefined;
|
|
14
|
+
/** A select with `attr.multiple`: an array value, no blank option */
|
|
15
|
+
export declare function isMultiple(node: FieldNode): boolean;
|
|
16
|
+
/** The first option a browser would select by default: the first one that isn't disabled */
|
|
17
|
+
export declare function firstEnabledOption(items: FormoOptionItem[]): FormoOption | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The blank option's label for single selects (`null` when the select has none):
|
|
20
|
+
* `blank: 'Pick one'` → `'Pick one'`, `blank: true` → `'Select item'`
|
|
21
|
+
*/
|
|
22
|
+
export declare function blankLabel(node: FieldNode): string | null;
|
|
23
|
+
/** What a multiple select shows with nothing selected: the `blank` text, else `'Select items'` */
|
|
24
|
+
export declare function multiplePlaceholder(node: FieldNode): string;
|
|
25
|
+
/**
|
|
26
|
+
* The value a select settles on, like a browser `<select>`: a single select
|
|
27
|
+
* without a blank option always has an option selected — when the value matches
|
|
28
|
+
* none, the first enabled option. Other fields keep their value.
|
|
29
|
+
*/
|
|
30
|
+
export declare function settleValue(node: FieldNode, value: unknown): unknown;
|
|
31
|
+
/** The selected values of a choice field (radios: one, checkboxes / multiple selects: an array) */
|
|
32
|
+
export declare function selectedValues(node: FieldNode, value: unknown): unknown[];
|
|
33
|
+
export declare function isSelected(node: FieldNode, current: unknown, value: unknown): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Check / uncheck one value of a checkbox list (or multiple select). The result
|
|
36
|
+
* keeps the options' order, whatever the order values were picked in.
|
|
37
|
+
*/
|
|
38
|
+
export declare function toggleValue(node: FieldNode, current: unknown, value: unknown, checked: boolean): unknown[];
|
|
39
|
+
/**
|
|
40
|
+
* A value as text — what the read-only view shows (formo-vue's `display`):
|
|
41
|
+
* booleans as Yes / No, choices as their option labels, lists joined with `, `.
|
|
42
|
+
* An empty string for empty values.
|
|
43
|
+
*/
|
|
44
|
+
export declare function displayValue(node: FieldNode, value: unknown): string;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { uiStrings } from './messages';
|
|
2
|
+
import { isPlainObject, toArray } from './utils';
|
|
3
|
+
/* -------------------------------------------------------------------------- */
|
|
4
|
+
/* Options — ported from formo-vue's core/options.ts */
|
|
5
|
+
/* -------------------------------------------------------------------------- */
|
|
6
|
+
export function isOptionGroup(item) {
|
|
7
|
+
return Array.isArray(item.options);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Normalize any supported `opts` shape into `[{ value, label }]` / option groups.
|
|
11
|
+
* Object keys are used as values (Formo: `$key => $label`), nested objects become groups,
|
|
12
|
+
* bare values (`['a', 'b']`) are their own label.
|
|
13
|
+
*/
|
|
14
|
+
export function normalizeOptions(input) {
|
|
15
|
+
if (!input)
|
|
16
|
+
return [];
|
|
17
|
+
if (Array.isArray(input)) {
|
|
18
|
+
return input.map((item) => {
|
|
19
|
+
if (Array.isArray(item)) {
|
|
20
|
+
return { value: item[0], label: String(item[1] ?? item[0]) };
|
|
21
|
+
}
|
|
22
|
+
if (isPlainObject(item)) {
|
|
23
|
+
if (Array.isArray(item.options)) {
|
|
24
|
+
return {
|
|
25
|
+
...item,
|
|
26
|
+
label: String(item.label ?? ''),
|
|
27
|
+
options: normalizeOptions(item.options),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if ('value' in item) {
|
|
31
|
+
return { ...item, label: String(item.label ?? item.value) };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { value: item, label: String(item) };
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (isPlainObject(input)) {
|
|
38
|
+
return Object.keys(input).map((key) => {
|
|
39
|
+
const label = input[key];
|
|
40
|
+
if (isPlainObject(label) || Array.isArray(label)) {
|
|
41
|
+
return { label: key, options: normalizeOptions(label) };
|
|
42
|
+
}
|
|
43
|
+
return { value: key, label: String(label) };
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
/** All options, groups flattened. Options of a disabled group are disabled */
|
|
49
|
+
export function flattenOptions(items) {
|
|
50
|
+
return items.flatMap((item) => isOptionGroup(item) ? (item.disabled ? item.options.map((o) => ({ ...o, disabled: true })) : item.options) : [item]);
|
|
51
|
+
}
|
|
52
|
+
/** Loose equality for option values (`1` matches `'1'`) */
|
|
53
|
+
export function sameValue(a, b) {
|
|
54
|
+
if (a === b)
|
|
55
|
+
return true;
|
|
56
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
57
|
+
return false;
|
|
58
|
+
if (typeof a === 'object' || typeof b === 'object')
|
|
59
|
+
return false;
|
|
60
|
+
return String(a) === String(b);
|
|
61
|
+
}
|
|
62
|
+
export function optionLabel(items, value) {
|
|
63
|
+
return flattenOptions(items).find((o) => sameValue(o.value, value))?.label;
|
|
64
|
+
}
|
|
65
|
+
/* -------------------------------------------------------------------------- */
|
|
66
|
+
/* Choice drivers: select, radios, checkboxes */
|
|
67
|
+
/* -------------------------------------------------------------------------- */
|
|
68
|
+
/** A select with `attr.multiple`: an array value, no blank option */
|
|
69
|
+
export function isMultiple(node) {
|
|
70
|
+
return node.driver === 'select' && !!node.attr.multiple;
|
|
71
|
+
}
|
|
72
|
+
/** The first option a browser would select by default: the first one that isn't disabled */
|
|
73
|
+
export function firstEnabledOption(items) {
|
|
74
|
+
return flattenOptions(items).find((o) => !o.disabled);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The blank option's label for single selects (`null` when the select has none):
|
|
78
|
+
* `blank: 'Pick one'` → `'Pick one'`, `blank: true` → `'Select item'`
|
|
79
|
+
*/
|
|
80
|
+
export function blankLabel(node) {
|
|
81
|
+
if (isMultiple(node))
|
|
82
|
+
return null;
|
|
83
|
+
const b = node.blank;
|
|
84
|
+
if (b === undefined || b === false || b === null)
|
|
85
|
+
return null;
|
|
86
|
+
return typeof b === 'string' && b !== '' ? b : uiStrings.selectItem;
|
|
87
|
+
}
|
|
88
|
+
/** What a multiple select shows with nothing selected: the `blank` text, else `'Select items'` */
|
|
89
|
+
export function multiplePlaceholder(node) {
|
|
90
|
+
return typeof node.blank === 'string' && node.blank !== '' ? node.blank : uiStrings.selectItems;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The value a select settles on, like a browser `<select>`: a single select
|
|
94
|
+
* without a blank option always has an option selected — when the value matches
|
|
95
|
+
* none, the first enabled option. Other fields keep their value.
|
|
96
|
+
*/
|
|
97
|
+
export function settleValue(node, value) {
|
|
98
|
+
if (node.driver !== 'select' || isMultiple(node) || blankLabel(node) !== null)
|
|
99
|
+
return value;
|
|
100
|
+
const flat = flattenOptions(node.options);
|
|
101
|
+
if (flat.some((o) => sameValue(o.value, value)))
|
|
102
|
+
return value;
|
|
103
|
+
const first = firstEnabledOption(node.options);
|
|
104
|
+
return first ? first.value : value;
|
|
105
|
+
}
|
|
106
|
+
/** The selected values of a choice field (radios: one, checkboxes / multiple selects: an array) */
|
|
107
|
+
export function selectedValues(node, value) {
|
|
108
|
+
if (node.driver === 'checkboxes' || isMultiple(node))
|
|
109
|
+
return Array.isArray(value) ? value : [];
|
|
110
|
+
return value === null || value === undefined || value === '' ? [] : [value];
|
|
111
|
+
}
|
|
112
|
+
export function isSelected(node, current, value) {
|
|
113
|
+
return selectedValues(node, current).some((v) => sameValue(v, value));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Check / uncheck one value of a checkbox list (or multiple select). The result
|
|
117
|
+
* keeps the options' order, whatever the order values were picked in.
|
|
118
|
+
*/
|
|
119
|
+
export function toggleValue(node, current, value, checked) {
|
|
120
|
+
const selected = selectedValues(node, current);
|
|
121
|
+
return flattenOptions(node.options)
|
|
122
|
+
.map((o) => o.value)
|
|
123
|
+
.filter((v) => (sameValue(v, value) ? checked : selected.some((s) => sameValue(s, v))));
|
|
124
|
+
}
|
|
125
|
+
/* -------------------------------------------------------------------------- */
|
|
126
|
+
/* Read-only display */
|
|
127
|
+
/* -------------------------------------------------------------------------- */
|
|
128
|
+
const OPTION_DRIVERS = new Set(['select', 'radios', 'checkboxes']);
|
|
129
|
+
/**
|
|
130
|
+
* A value as text — what the read-only view shows (formo-vue's `display`):
|
|
131
|
+
* booleans as Yes / No, choices as their option labels, lists joined with `, `.
|
|
132
|
+
* An empty string for empty values.
|
|
133
|
+
*/
|
|
134
|
+
export function displayValue(node, value) {
|
|
135
|
+
if (typeof value === 'boolean')
|
|
136
|
+
return value ? uiStrings.yes : uiStrings.no;
|
|
137
|
+
if (OPTION_DRIVERS.has(node.driver)) {
|
|
138
|
+
return toArray(value)
|
|
139
|
+
.filter((v) => v !== '')
|
|
140
|
+
.map((v) => optionLabel(node.options, v) ?? String(v))
|
|
141
|
+
.join(', ');
|
|
142
|
+
}
|
|
143
|
+
if (value === null || value === undefined)
|
|
144
|
+
return '';
|
|
145
|
+
return Array.isArray(value) ? value.join(', ') : String(value);
|
|
146
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ChecklistItem, FieldNode, RuleContext, RuleFn, RuleSpec } from '../types';
|
|
2
|
+
/** A rule after shape-normalization */
|
|
3
|
+
export interface ParsedRule {
|
|
4
|
+
name: string;
|
|
5
|
+
params: unknown[];
|
|
6
|
+
/** Inline implementation (function rules, schemas) */
|
|
7
|
+
fn?: RuleFn;
|
|
8
|
+
/** Message used when it fails (wins over the built-in one) */
|
|
9
|
+
message?: string;
|
|
10
|
+
/** Requirement text for checklists */
|
|
11
|
+
label?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Any notation formo-vue accepts → parsed rules */
|
|
14
|
+
export declare function parseRule(spec: RuleSpec): ParsedRule[];
|
|
15
|
+
export declare function parseRules(specs: RuleSpec[]): ParsedRule[];
|
|
16
|
+
/**
|
|
17
|
+
* Built-in rules. Names follow formo-vue (VeeValidate + Laravel).
|
|
18
|
+
* Every rule except `required` and `accepted` passes on empty values.
|
|
19
|
+
*/
|
|
20
|
+
export declare const builtinRules: Record<string, (value: unknown, params: unknown[], ctx: RuleContext) => boolean>;
|
|
21
|
+
export interface ValidateOptions {
|
|
22
|
+
/** Values of the field's group (`@ref` params resolve here first). Default: from `path` */
|
|
23
|
+
parentValues?: Record<string, any>;
|
|
24
|
+
/** Label of another field, for `:other` in messages (`confirmed:@password`). Default: humanized path */
|
|
25
|
+
labelFor?: (path: string) => string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Every failing message of a field. `bails` (default `true`) stops at the first
|
|
29
|
+
* one — fields showing `all` errors or a checklist run every rule.
|
|
30
|
+
*/
|
|
31
|
+
export declare function validateFieldAll(node: FieldNode, value: unknown, values: Record<string, any>, path: string, opts?: ValidateOptions & {
|
|
32
|
+
bails?: boolean;
|
|
33
|
+
}): Promise<string[]>;
|
|
34
|
+
/** The first failing message of a field (`undefined` when it's valid) */
|
|
35
|
+
export declare function validateField(node: FieldNode, value: unknown, values: Record<string, any>, path: string, opts?: ValidateOptions): Promise<string | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Evaluate every rule independently for a checklist. Empty values meet no
|
|
38
|
+
* requirement (rules normally pass on empty values). `required` is left out
|
|
39
|
+
* unless it has a label — it's implied by the required marker.
|
|
40
|
+
*/
|
|
41
|
+
export declare function evaluateChecklist(node: FieldNode, value: unknown, values: Record<string, any>, path: string, opts?: ValidateOptions): Promise<ChecklistItem[]>;
|