@marianmeres/stuic 3.153.0 → 3.154.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/dist/components/FieldsBuilder/FieldsBuilder.svelte +1214 -0
- package/dist/components/FieldsBuilder/FieldsBuilder.svelte.d.ts +102 -0
- package/dist/components/FieldsBuilder/README.md +248 -0
- package/dist/components/FieldsBuilder/_internal/LocalizedTextInput.svelte +229 -0
- package/dist/components/FieldsBuilder/_internal/LocalizedTextInput.svelte.d.ts +30 -0
- package/dist/components/FieldsBuilder/_internal/OptionsEditor.svelte +296 -0
- package/dist/components/FieldsBuilder/_internal/OptionsEditor.svelte.d.ts +18 -0
- package/dist/components/FieldsBuilder/i18n-sk.d.ts +22 -0
- package/dist/components/FieldsBuilder/i18n-sk.js +81 -0
- package/dist/components/FieldsBuilder/i18n.d.ts +84 -0
- package/dist/components/FieldsBuilder/i18n.js +90 -0
- package/dist/components/FieldsBuilder/index.css +187 -0
- package/dist/components/FieldsBuilder/index.d.ts +5 -0
- package/dist/components/FieldsBuilder/index.js +4 -0
- package/dist/components/FieldsBuilder/types.d.ts +76 -0
- package/dist/components/FieldsBuilder/types.js +1 -0
- package/dist/components/FieldsBuilder/utils.d.ts +66 -0
- package/dist/components/FieldsBuilder/utils.js +153 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/docs/domains/components.md +61 -1
- package/package.json +1 -1
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* ============================================================================
|
|
2
|
+
FIELDS BUILDER COMPONENT TOKENS
|
|
3
|
+
Override globally: :root { --stuic-fields-builder-row-border: red; }
|
|
4
|
+
Override locally: <FieldsBuilder style="--stuic-fields-builder-chip-bg: ..." />
|
|
5
|
+
============================================================================ */
|
|
6
|
+
|
|
7
|
+
/* prettier-ignore */
|
|
8
|
+
:root {
|
|
9
|
+
--stuic-fields-builder-row-border: var(--stuic-color-border);
|
|
10
|
+
--stuic-fields-builder-row-toggle-bg-hover: var(--stuic-color-muted);
|
|
11
|
+
--stuic-fields-builder-key-text: var(--stuic-color-muted-foreground);
|
|
12
|
+
--stuic-fields-builder-chip-bg: var(--stuic-color-muted);
|
|
13
|
+
--stuic-fields-builder-chip-text: var(--stuic-color-muted-foreground);
|
|
14
|
+
--stuic-fields-builder-muted-text: var(--stuic-color-muted-foreground);
|
|
15
|
+
--stuic-fields-builder-warning-text: var(--stuic-color-surface-warning-foreground);
|
|
16
|
+
--stuic-fields-builder-error-text: var(--stuic-color-destructive);
|
|
17
|
+
--stuic-fields-builder-drop-indicator-color: var(--stuic-color-primary);
|
|
18
|
+
--stuic-fields-builder-drop-indicator-height: 2px;
|
|
19
|
+
--stuic-fields-builder-row-opacity-dragging: 0.4;
|
|
20
|
+
--stuic-fields-builder-row-opacity-deleted: 0.55;
|
|
21
|
+
--stuic-fields-builder-preview-border: var(--stuic-color-border);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@layer components {
|
|
25
|
+
/* =============================================================================
|
|
26
|
+
ROWS
|
|
27
|
+
============================================================================= */
|
|
28
|
+
|
|
29
|
+
.stuic-fields-builder .fb-row-divider {
|
|
30
|
+
border-top: 1px solid var(--stuic-fields-builder-row-border);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.stuic-fields-builder .fb-row[data-dragging] {
|
|
34
|
+
opacity: var(--stuic-fields-builder-row-opacity-dragging);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.stuic-fields-builder .fb-row[data-drop-position="before"] {
|
|
38
|
+
box-shadow: inset 0 var(--stuic-fields-builder-drop-indicator-height) 0
|
|
39
|
+
var(--stuic-fields-builder-drop-indicator-color);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.stuic-fields-builder .fb-row[data-drop-position="after"] {
|
|
43
|
+
box-shadow: inset 0 calc(-1 * var(--stuic-fields-builder-drop-indicator-height)) 0
|
|
44
|
+
var(--stuic-fields-builder-drop-indicator-color);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.stuic-fields-builder .fb-row-deleted .fb-row-header {
|
|
48
|
+
opacity: var(--stuic-fields-builder-row-opacity-deleted);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.stuic-fields-builder .fb-row-deleted .fb-row-label {
|
|
52
|
+
text-decoration: line-through;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* =============================================================================
|
|
56
|
+
ROW HEADER
|
|
57
|
+
============================================================================= */
|
|
58
|
+
|
|
59
|
+
.stuic-fields-builder .fb-row-header {
|
|
60
|
+
padding-block: calc(var(--spacing) * 1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.stuic-fields-builder .fb-row-toggle {
|
|
64
|
+
padding: calc(var(--spacing) * 1.5) calc(var(--spacing) * 1);
|
|
65
|
+
transition: background var(--stuic-transition);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.stuic-fields-builder .fb-row-toggle:hover:not(:disabled) {
|
|
69
|
+
background: var(--stuic-fields-builder-row-toggle-bg-hover);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.stuic-fields-builder .fb-row-toggle:disabled {
|
|
73
|
+
cursor: default;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.stuic-fields-builder .fb-row-label {
|
|
77
|
+
font-weight: 500;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.stuic-fields-builder .fb-row-required {
|
|
81
|
+
opacity: 0.5;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.stuic-fields-builder .fb-key {
|
|
85
|
+
font-family: var(--font-mono);
|
|
86
|
+
font-size: var(--text-xs);
|
|
87
|
+
color: var(--stuic-fields-builder-key-text);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.stuic-fields-builder .fb-chip {
|
|
91
|
+
background: var(--stuic-fields-builder-chip-bg);
|
|
92
|
+
color: var(--stuic-fields-builder-chip-text);
|
|
93
|
+
font-size: var(--text-xs);
|
|
94
|
+
padding: calc(var(--spacing) * 0.5) calc(var(--spacing) * 1.5);
|
|
95
|
+
border-radius: var(--stuic-fields-builder-chip-radius, var(--stuic-radius));
|
|
96
|
+
white-space: nowrap;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.stuic-fields-builder .fb-chip-warning {
|
|
100
|
+
color: var(--stuic-fields-builder-warning-text);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.stuic-fields-builder .fb-chevron {
|
|
104
|
+
opacity: 0.5;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.stuic-fields-builder .fb-handle {
|
|
108
|
+
cursor: grab;
|
|
109
|
+
opacity: 0.4;
|
|
110
|
+
touch-action: none;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.stuic-fields-builder .fb-handle:hover {
|
|
114
|
+
opacity: 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.stuic-fields-builder .fb-handle-spacer {
|
|
118
|
+
width: 16px;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* =============================================================================
|
|
122
|
+
ROW BODY (expanded editor)
|
|
123
|
+
============================================================================= */
|
|
124
|
+
|
|
125
|
+
.stuic-fields-builder .fb-row-body {
|
|
126
|
+
display: flex;
|
|
127
|
+
flex-direction: column;
|
|
128
|
+
gap: calc(var(--spacing) * 3);
|
|
129
|
+
padding: calc(var(--spacing) * 1) calc(var(--spacing) * 2) calc(var(--spacing) * 3)
|
|
130
|
+
calc(var(--spacing) * 2);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.stuic-fields-builder .fb-sub-label {
|
|
134
|
+
display: block;
|
|
135
|
+
font-size: var(--text-xs);
|
|
136
|
+
font-weight: 500;
|
|
137
|
+
color: var(--stuic-fields-builder-muted-text);
|
|
138
|
+
margin-bottom: calc(var(--spacing) * 0.5);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.stuic-fields-builder .fb-hint {
|
|
142
|
+
color: var(--stuic-fields-builder-muted-text);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.stuic-fields-builder .fb-error-text {
|
|
146
|
+
color: var(--stuic-fields-builder-error-text);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.stuic-fields-builder .fb-warning-text {
|
|
150
|
+
color: var(--stuic-fields-builder-warning-text);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.stuic-fields-builder .fb-advanced-toggle {
|
|
154
|
+
color: var(--stuic-fields-builder-muted-text);
|
|
155
|
+
transition: color var(--stuic-transition);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.stuic-fields-builder .fb-advanced-toggle:hover {
|
|
159
|
+
color: var(--stuic-color-foreground);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* =============================================================================
|
|
163
|
+
OPTIONS EDITOR
|
|
164
|
+
============================================================================= */
|
|
165
|
+
|
|
166
|
+
.stuic-fields-builder .fb-option-divider {
|
|
167
|
+
border-top: 1px dashed var(--stuic-fields-builder-row-border);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* =============================================================================
|
|
171
|
+
PREVIEW PANE
|
|
172
|
+
============================================================================= */
|
|
173
|
+
|
|
174
|
+
.stuic-fields-builder .fb-preview-side {
|
|
175
|
+
border-left: 1px solid var(--stuic-fields-builder-preview-border);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.stuic-fields-builder .fb-preview-below {
|
|
179
|
+
border-top: 1px solid var(--stuic-fields-builder-preview-border);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.stuic-fields-builder .fb-preview-title {
|
|
183
|
+
color: var(--stuic-fields-builder-muted-text);
|
|
184
|
+
text-transform: uppercase;
|
|
185
|
+
letter-spacing: 0.05em;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as FieldsBuilder, type Props as FieldsBuilderProps, } from "./FieldsBuilder.svelte";
|
|
2
|
+
export type { FieldDef, FieldLock, FieldOptionDef, FieldTypeDef, FieldTypeExtraDef, LocalizedText, } from "./types.js";
|
|
3
|
+
export { DEFAULT_FIELD_TYPES as FIELDS_BUILDER_DEFAULT_TYPES, DEFAULT_KEY_PATTERN as FIELDS_BUILDER_DEFAULT_KEY_PATTERN, getLocalizedText, slugifyKey, uniqueKey, validateFieldDefs, type FieldDefRowErrors, type FieldDefsValidationResult, type ValidateFieldDefsOptions, } from "./utils.js";
|
|
4
|
+
export { createFieldsBuilderT, FIELDS_BUILDER_MESSAGES_EN, type FieldsBuilderMessageKey, type FieldsBuilderMessages, } from "./i18n.js";
|
|
5
|
+
export { FIELDS_BUILDER_MESSAGES_SK, FIELDS_BUILDER_DEFAULT_TYPES_SK, } from "./i18n-sk.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default as FieldsBuilder, } from "./FieldsBuilder.svelte";
|
|
2
|
+
export { DEFAULT_FIELD_TYPES as FIELDS_BUILDER_DEFAULT_TYPES, DEFAULT_KEY_PATTERN as FIELDS_BUILDER_DEFAULT_KEY_PATTERN, getLocalizedText, slugifyKey, uniqueKey, validateFieldDefs, } from "./utils.js";
|
|
3
|
+
export { createFieldsBuilderT, FIELDS_BUILDER_MESSAGES_EN, } from "./i18n.js";
|
|
4
|
+
export { FIELDS_BUILDER_MESSAGES_SK, FIELDS_BUILDER_DEFAULT_TYPES_SK, } from "./i18n-sk.js";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Snippet } from "svelte";
|
|
2
|
+
/** A localized string: either one plain string, or a per-language map. */
|
|
3
|
+
export type LocalizedText = string | Record<string, string>;
|
|
4
|
+
/** A single option of a choice-like field type (one declaring `supportsOptions`). */
|
|
5
|
+
export interface FieldOptionDef {
|
|
6
|
+
/** Machine value, unique within the field's options. */
|
|
7
|
+
value: string;
|
|
8
|
+
label: LocalizedText;
|
|
9
|
+
}
|
|
10
|
+
/** What the user may NOT change on a field. Absent flag = editable. */
|
|
11
|
+
export interface FieldLock {
|
|
12
|
+
key?: boolean;
|
|
13
|
+
type?: boolean;
|
|
14
|
+
required?: boolean;
|
|
15
|
+
options?: boolean;
|
|
16
|
+
/** Cannot be removed from the list. */
|
|
17
|
+
delete?: boolean;
|
|
18
|
+
/** Cannot be dragged, and other fields cannot be moved past it. */
|
|
19
|
+
reorder?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* One field definition — the unit of the `FieldsBuilder` value. The component
|
|
23
|
+
* emits an ordered `FieldDef[]`; what the list is compiled into (a form, a
|
|
24
|
+
* schema, a template...) is entirely the consumer's business.
|
|
25
|
+
*/
|
|
26
|
+
export interface FieldDef {
|
|
27
|
+
/** Machine key. Unique within the list. */
|
|
28
|
+
key: string;
|
|
29
|
+
/** One of the `types` palette entries' `type`. */
|
|
30
|
+
type: string;
|
|
31
|
+
label: LocalizedText;
|
|
32
|
+
description?: LocalizedText;
|
|
33
|
+
required?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Edited and validated only for palette entries declaring
|
|
36
|
+
* `supportsOptions`. NOTE: when a field's type is changed away from a
|
|
37
|
+
* choice type, existing `options` are deliberately RETAINED on the def
|
|
38
|
+
* (never silently drop data; switching back restores them) — consumers
|
|
39
|
+
* compiling the list should ignore `options` on non-choice types.
|
|
40
|
+
*/
|
|
41
|
+
options?: FieldOptionDef[];
|
|
42
|
+
/**
|
|
43
|
+
* Per-type extra flags, driven by the palette entry's `extras`. An open bag
|
|
44
|
+
* on purpose — the component never interprets these, it only renders a
|
|
45
|
+
* control per declared extra and round-trips the value.
|
|
46
|
+
*/
|
|
47
|
+
extras?: Record<string, unknown>;
|
|
48
|
+
/** What the user may NOT change. Absent = fully editable. */
|
|
49
|
+
lock?: FieldLock;
|
|
50
|
+
}
|
|
51
|
+
/** An extra per-field control declared by a palette entry (v1: booleans only). */
|
|
52
|
+
export interface FieldTypeExtraDef {
|
|
53
|
+
/** Stored under `FieldDef.extras[key]`. */
|
|
54
|
+
key: string;
|
|
55
|
+
label: LocalizedText;
|
|
56
|
+
description?: LocalizedText;
|
|
57
|
+
/** v1: booleans only (rendered as a checkbox). */
|
|
58
|
+
type: "boolean";
|
|
59
|
+
default?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/** One entry of the type palette (the `types` prop). */
|
|
62
|
+
export interface FieldTypeDef {
|
|
63
|
+
/** Stored in `FieldDef.type`. */
|
|
64
|
+
type: string;
|
|
65
|
+
/** Shown in the type picker. */
|
|
66
|
+
label: LocalizedText;
|
|
67
|
+
description?: LocalizedText;
|
|
68
|
+
/** Icon html string (e.g. from `@marianmeres/icons-fns`) or a snippet. */
|
|
69
|
+
icon?: string | Snippet;
|
|
70
|
+
/** Renders the option editor and allows `FieldDef.options`. */
|
|
71
|
+
supportsOptions?: boolean;
|
|
72
|
+
/** Extra per-field controls, rendered into `FieldDef.extras[key]`. */
|
|
73
|
+
extras?: FieldTypeExtraDef[];
|
|
74
|
+
/** Optional live preview of a single field of this type. */
|
|
75
|
+
preview?: Snippet<[FieldDef]>;
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { FieldDef, FieldTypeDef, LocalizedText } from "./types.js";
|
|
2
|
+
/** Default machine-key policy: lowercase snake_case, starts with a letter, max 63 chars. */
|
|
3
|
+
export declare const DEFAULT_KEY_PATTERN: RegExp;
|
|
4
|
+
export declare const DEFAULT_KEY_MAX_LENGTH = 63;
|
|
5
|
+
/** Minimal translate signature the pure helpers below need. */
|
|
6
|
+
export type FieldsBuilderTranslate = (key: string, values?: Record<string, string | number>) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Read the display text of a `LocalizedText`: the string itself, the preferred
|
|
9
|
+
* language's entry, or the first non-empty entry as a fallback.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getLocalizedText(text: LocalizedText | null | undefined, preferredLanguage?: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Derive a machine key from a human label: transliterates diacritics
|
|
14
|
+
* (`Ročník` → `rocnik`), lowercases, collapses everything else to `_`. A slug
|
|
15
|
+
* not starting with a letter is prefixed with `f_` (`2024` → `f_2024`) so the
|
|
16
|
+
* result always satisfies `DEFAULT_KEY_PATTERN`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function slugifyKey(input: string, maxLength?: number): string;
|
|
19
|
+
/**
|
|
20
|
+
* Make `base` unique against `isTaken` by suffixing `_2`, `_3`, ... The base is
|
|
21
|
+
* truncated when a suffixed candidate would exceed `maxLength`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function uniqueKey(base: string, isTaken: (key: string) => boolean, maxLength?: number): string;
|
|
24
|
+
export declare function isKeyReserved(key: string, reservedKeys?: string[] | ((key: string) => boolean)): boolean;
|
|
25
|
+
export interface FieldDefRowErrors {
|
|
26
|
+
label?: string;
|
|
27
|
+
key?: string;
|
|
28
|
+
options?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface FieldDefsValidationResult {
|
|
31
|
+
valid: boolean;
|
|
32
|
+
/** First error found — the component-level summary message. Empty when valid. */
|
|
33
|
+
message: string;
|
|
34
|
+
/** Index-aligned with the input defs. `null` = row has no errors. */
|
|
35
|
+
rowErrors: (FieldDefRowErrors | null)[];
|
|
36
|
+
}
|
|
37
|
+
export interface ValidateFieldDefsOptions {
|
|
38
|
+
/**
|
|
39
|
+
* The type palette. When provided, defs with a `type` not present here are
|
|
40
|
+
* treated as unknown: they are NOT validated (they round-trip untouched and
|
|
41
|
+
* must never block the rest of the list), but their keys still count toward
|
|
42
|
+
* uniqueness.
|
|
43
|
+
*/
|
|
44
|
+
types?: Pick<FieldTypeDef, "type" | "supportsOptions">[];
|
|
45
|
+
keyPattern?: RegExp;
|
|
46
|
+
keyMaxLength?: number;
|
|
47
|
+
reservedKeys?: string[] | ((key: string) => boolean);
|
|
48
|
+
maxFields?: number;
|
|
49
|
+
defaultLanguage?: string;
|
|
50
|
+
/** Translator for the error messages; defaults to returning the message key. */
|
|
51
|
+
t?: FieldsBuilderTranslate;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validate a list of field defs: label non-empty, key present/pattern/length/
|
|
55
|
+
* unique/not-reserved, choice types have at least one option with non-empty
|
|
56
|
+
* unique values, `maxFields` not exceeded.
|
|
57
|
+
*
|
|
58
|
+
* This is client-side convenience only — a consumer persisting the list MUST
|
|
59
|
+
* re-validate server-side; this function is not a security boundary.
|
|
60
|
+
*/
|
|
61
|
+
export declare function validateFieldDefs(defs: FieldDef[], opts?: ValidateFieldDefsOptions): FieldDefsValidationResult;
|
|
62
|
+
/**
|
|
63
|
+
* A small general-purpose palette — handy for demos and for consumers with no
|
|
64
|
+
* opinion. `types` is a required prop, so nobody gets this by accident.
|
|
65
|
+
*/
|
|
66
|
+
export declare const DEFAULT_FIELD_TYPES: FieldTypeDef[];
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** Default machine-key policy: lowercase snake_case, starts with a letter, max 63 chars. */
|
|
2
|
+
export const DEFAULT_KEY_PATTERN = /^[a-z][a-z0-9_]{0,62}$/;
|
|
3
|
+
export const DEFAULT_KEY_MAX_LENGTH = 63;
|
|
4
|
+
/**
|
|
5
|
+
* Read the display text of a `LocalizedText`: the string itself, the preferred
|
|
6
|
+
* language's entry, or the first non-empty entry as a fallback.
|
|
7
|
+
*/
|
|
8
|
+
export function getLocalizedText(text, preferredLanguage) {
|
|
9
|
+
if (text == null)
|
|
10
|
+
return "";
|
|
11
|
+
if (typeof text === "string")
|
|
12
|
+
return text;
|
|
13
|
+
if (preferredLanguage && text[preferredLanguage])
|
|
14
|
+
return text[preferredLanguage];
|
|
15
|
+
for (const v of Object.values(text))
|
|
16
|
+
if (v)
|
|
17
|
+
return v;
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Derive a machine key from a human label: transliterates diacritics
|
|
22
|
+
* (`Ročník` → `rocnik`), lowercases, collapses everything else to `_`. A slug
|
|
23
|
+
* not starting with a letter is prefixed with `f_` (`2024` → `f_2024`) so the
|
|
24
|
+
* result always satisfies `DEFAULT_KEY_PATTERN`.
|
|
25
|
+
*/
|
|
26
|
+
export function slugifyKey(input, maxLength = DEFAULT_KEY_MAX_LENGTH) {
|
|
27
|
+
let s = (input ?? "")
|
|
28
|
+
.normalize("NFKD")
|
|
29
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
32
|
+
.replace(/^_+|_+$/g, "");
|
|
33
|
+
if (s && !/^[a-z]/.test(s))
|
|
34
|
+
s = `f_${s}`;
|
|
35
|
+
if (maxLength > 0 && s.length > maxLength) {
|
|
36
|
+
s = s.slice(0, maxLength).replace(/_+$/, "");
|
|
37
|
+
}
|
|
38
|
+
return s;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Make `base` unique against `isTaken` by suffixing `_2`, `_3`, ... The base is
|
|
42
|
+
* truncated when a suffixed candidate would exceed `maxLength`.
|
|
43
|
+
*/
|
|
44
|
+
export function uniqueKey(base, isTaken, maxLength = DEFAULT_KEY_MAX_LENGTH) {
|
|
45
|
+
if (!base || !isTaken(base))
|
|
46
|
+
return base;
|
|
47
|
+
for (let i = 2; i < 1_000; i++) {
|
|
48
|
+
const suffix = `_${i}`;
|
|
49
|
+
let candidate = base + suffix;
|
|
50
|
+
if (maxLength > 0 && candidate.length > maxLength) {
|
|
51
|
+
candidate = base.slice(0, maxLength - suffix.length).replace(/_+$/, "") + suffix;
|
|
52
|
+
}
|
|
53
|
+
if (!isTaken(candidate))
|
|
54
|
+
return candidate;
|
|
55
|
+
}
|
|
56
|
+
return base;
|
|
57
|
+
}
|
|
58
|
+
export function isKeyReserved(key, reservedKeys) {
|
|
59
|
+
if (!reservedKeys)
|
|
60
|
+
return false;
|
|
61
|
+
return typeof reservedKeys === "function"
|
|
62
|
+
? !!reservedKeys(key)
|
|
63
|
+
: reservedKeys.includes(key);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Validate a list of field defs: label non-empty, key present/pattern/length/
|
|
67
|
+
* unique/not-reserved, choice types have at least one option with non-empty
|
|
68
|
+
* unique values, `maxFields` not exceeded.
|
|
69
|
+
*
|
|
70
|
+
* This is client-side convenience only — a consumer persisting the list MUST
|
|
71
|
+
* re-validate server-side; this function is not a security boundary.
|
|
72
|
+
*/
|
|
73
|
+
export function validateFieldDefs(defs, opts = {}) {
|
|
74
|
+
const t = opts.t ?? ((k) => k);
|
|
75
|
+
const keyPattern = opts.keyPattern ?? DEFAULT_KEY_PATTERN;
|
|
76
|
+
const keyMaxLength = opts.keyMaxLength ?? DEFAULT_KEY_MAX_LENGTH;
|
|
77
|
+
const typeMap = opts.types ? new Map(opts.types.map((td) => [td.type, td])) : null;
|
|
78
|
+
const rowErrors = defs.map(() => null);
|
|
79
|
+
const put = (i, field, msg) => {
|
|
80
|
+
rowErrors[i] ??= {};
|
|
81
|
+
rowErrors[i][field] ??= msg;
|
|
82
|
+
};
|
|
83
|
+
// keys of ALL defs (incl. unknown types) occupy the key space
|
|
84
|
+
const keyCounts = new Map();
|
|
85
|
+
for (const d of defs) {
|
|
86
|
+
const k = (d.key ?? "").trim();
|
|
87
|
+
if (k)
|
|
88
|
+
keyCounts.set(k, (keyCounts.get(k) ?? 0) + 1);
|
|
89
|
+
}
|
|
90
|
+
defs.forEach((d, i) => {
|
|
91
|
+
// unknown type: keep as-is, do not block (see the doc comment above)
|
|
92
|
+
if (typeMap && !typeMap.has(d.type))
|
|
93
|
+
return;
|
|
94
|
+
if (!getLocalizedText(d.label, opts.defaultLanguage).trim()) {
|
|
95
|
+
put(i, "label", t("err_label_required"));
|
|
96
|
+
}
|
|
97
|
+
// presence is checked trimmed, but pattern/length run on the RAW key —
|
|
98
|
+
// a whitespace-padded key must fail here, not at the consumer's gate
|
|
99
|
+
const k = d.key ?? "";
|
|
100
|
+
if (!k.trim()) {
|
|
101
|
+
put(i, "key", t("err_key_required"));
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (k.length > keyMaxLength) {
|
|
105
|
+
put(i, "key", t("err_key_maxlength", { max: keyMaxLength }));
|
|
106
|
+
}
|
|
107
|
+
else if (!keyPattern.test(k)) {
|
|
108
|
+
put(i, "key", t("err_key_pattern"));
|
|
109
|
+
}
|
|
110
|
+
if ((keyCounts.get(k.trim()) ?? 0) > 1)
|
|
111
|
+
put(i, "key", t("err_key_duplicate"));
|
|
112
|
+
if (isKeyReserved(k, opts.reservedKeys))
|
|
113
|
+
put(i, "key", t("err_key_reserved"));
|
|
114
|
+
}
|
|
115
|
+
if (typeMap?.get(d.type)?.supportsOptions) {
|
|
116
|
+
const options = d.options ?? [];
|
|
117
|
+
const values = options.map((o) => (o.value ?? "").trim());
|
|
118
|
+
if (!options.length) {
|
|
119
|
+
put(i, "options", t("err_options_required"));
|
|
120
|
+
}
|
|
121
|
+
else if (values.some((v) => !v)) {
|
|
122
|
+
put(i, "options", t("err_option_value_required"));
|
|
123
|
+
}
|
|
124
|
+
else if (new Set(values).size !== values.length) {
|
|
125
|
+
put(i, "options", t("err_option_value_duplicate"));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
let message = rowErrors.find(Boolean)
|
|
130
|
+
? Object.values(rowErrors.find(Boolean))[0]
|
|
131
|
+
: "";
|
|
132
|
+
if (!message && opts.maxFields && defs.length > opts.maxFields) {
|
|
133
|
+
message = t("err_max_fields", { max: opts.maxFields });
|
|
134
|
+
}
|
|
135
|
+
return { valid: !message, message, rowErrors };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* A small general-purpose palette — handy for demos and for consumers with no
|
|
139
|
+
* opinion. `types` is a required prop, so nobody gets this by accident.
|
|
140
|
+
*/
|
|
141
|
+
export const DEFAULT_FIELD_TYPES = [
|
|
142
|
+
{ type: "text", label: "Text", description: "A single line of text" },
|
|
143
|
+
{ type: "longtext", label: "Long text", description: "Multiple lines of text" },
|
|
144
|
+
{ type: "number", label: "Number", description: "A numeric value" },
|
|
145
|
+
{ type: "checkbox", label: "Yes / no", description: "A single on/off checkbox" },
|
|
146
|
+
{
|
|
147
|
+
type: "select",
|
|
148
|
+
label: "Choice",
|
|
149
|
+
description: "Pick one from a list of choices",
|
|
150
|
+
supportsOptions: true,
|
|
151
|
+
},
|
|
152
|
+
{ type: "date", label: "Date", description: "A calendar date" },
|
|
153
|
+
];
|
package/dist/index.css
CHANGED
|
@@ -80,6 +80,7 @@ In practice:
|
|
|
80
80
|
@import "./components/DismissibleMessage/index.css";
|
|
81
81
|
@import "./components/DropdownMenu/index.css";
|
|
82
82
|
@import "./components/EmailVerifyForm/index.css";
|
|
83
|
+
@import "./components/FieldsBuilder/index.css";
|
|
83
84
|
@import "./components/Float/index.css";
|
|
84
85
|
@import "./components/H/index.css";
|
|
85
86
|
@import "./components/Header/index.css";
|
package/dist/index.d.ts
CHANGED
|
@@ -45,6 +45,7 @@ export * from "./components/DismissibleMessage/index.js";
|
|
|
45
45
|
export * from "./components/Drawer/index.js";
|
|
46
46
|
export * from "./components/DropdownMenu/index.js";
|
|
47
47
|
export * from "./components/EmailVerifyForm/index.js";
|
|
48
|
+
export * from "./components/FieldsBuilder/index.js";
|
|
48
49
|
export * from "./components/Float/index.js";
|
|
49
50
|
export * from "./components/H/index.js";
|
|
50
51
|
export * from "./components/Header/index.js";
|
package/dist/index.js
CHANGED
|
@@ -51,6 +51,7 @@ export * from "./components/DismissibleMessage/index.js";
|
|
|
51
51
|
export * from "./components/Drawer/index.js";
|
|
52
52
|
export * from "./components/DropdownMenu/index.js";
|
|
53
53
|
export * from "./components/EmailVerifyForm/index.js";
|
|
54
|
+
export * from "./components/FieldsBuilder/index.js";
|
|
54
55
|
export * from "./components/Float/index.js";
|
|
55
56
|
export * from "./components/H/index.js";
|
|
56
57
|
export * from "./components/Header/index.js";
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
64 Svelte 5 component directories with consistent API patterns. All use runes-based reactivity.
|
|
6
6
|
|
|
7
7
|
## Component Categories
|
|
8
8
|
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
| CronInput | Cron expression editor with presets and validation |
|
|
63
63
|
| Fieldset | Field grouping with legend |
|
|
64
64
|
| FieldKeyValues | Key-value pair editor |
|
|
65
|
+
| FieldsBuilder | Field-definition list editor ("what properties does a thing have?") |
|
|
65
66
|
| FieldAssets | File/asset management |
|
|
66
67
|
| LoginForm, LoginFormModal | Standalone login form with optional modal variant |
|
|
67
68
|
| RegisterForm | Standalone registration form with declarative extra fields |
|
|
@@ -509,6 +510,65 @@ Exported from Input: `FieldObject`, `FieldObjectProps`.
|
|
|
509
510
|
|
|
510
511
|
---
|
|
511
512
|
|
|
513
|
+
## FieldsBuilder
|
|
514
|
+
|
|
515
|
+
Composite form control for authoring an ordered list of field definitions — the end
|
|
516
|
+
user (typically non-technical) answers "what properties does a thing have?": add a
|
|
517
|
+
field, name it, pick its type, mark it required, reorder, delete. The bindable `value`
|
|
518
|
+
is a plain `FieldDef[]`; the type palette is a required prop (`types`), never a
|
|
519
|
+
built-in union. Emits a field list, NOT JSON Schema — compiling (and re-validating)
|
|
520
|
+
the list is the consumer's job, server-side. Own directory: `components/FieldsBuilder/`
|
|
521
|
+
(composes `InputWrap`, exports the validate quintet).
|
|
522
|
+
|
|
523
|
+
### Exports
|
|
524
|
+
|
|
525
|
+
| Export | Kind | Description |
|
|
526
|
+
| ------------------------------------------------------------------ | --------- | ----------------------------------------------------------- |
|
|
527
|
+
| `FieldsBuilder` | component | Main component |
|
|
528
|
+
| `FieldsBuilderProps` | type | Props type |
|
|
529
|
+
| `FieldDef` | type | One field definition (the value unit) |
|
|
530
|
+
| `FieldLock` | type | Per-field edit locks |
|
|
531
|
+
| `FieldOptionDef` | type | Choice-type option `{ value, label }` |
|
|
532
|
+
| `FieldTypeDef` | type | Palette entry |
|
|
533
|
+
| `FieldTypeExtraDef` | type | Palette-declared boolean extra |
|
|
534
|
+
| `LocalizedText` | type | `string \| Record<language, string>` |
|
|
535
|
+
| `FIELDS_BUILDER_DEFAULT_TYPES` | constant | Starter palette (text/longtext/number/checkbox/select/date) |
|
|
536
|
+
| `FIELDS_BUILDER_DEFAULT_KEY_PATTERN` | constant | `/^[a-z][a-z0-9_]{0,62}$/` |
|
|
537
|
+
| `getLocalizedText`, `slugifyKey`, `uniqueKey`, `validateFieldDefs` | functions | Pure helpers (node-testable) |
|
|
538
|
+
|
|
539
|
+
### Key Props
|
|
540
|
+
|
|
541
|
+
| Prop | Type | Default | Description |
|
|
542
|
+
| -------------------- | ---------------------------------------------- | -------- | ------------------------------------------------ |
|
|
543
|
+
| `value` | `FieldDef[]` | required | Bindable ordered field list |
|
|
544
|
+
| `name` | `string` | required | Hidden-input name |
|
|
545
|
+
| `types` | `FieldTypeDef[]` | required | The type palette |
|
|
546
|
+
| `languages` | `string[]` | — | Multi-language labels/descriptions/options |
|
|
547
|
+
| `keysImmutable` | `boolean` | `true` | Freeze keys loaded from `value` |
|
|
548
|
+
| `deriveKeyFromLabel` | `boolean \| (label) => string` | `true` | Live slug derivation (diacritics transliterated) |
|
|
549
|
+
| `reservedKeys` | `string[] \| (key) => boolean` | — | Forbidden keys (derivation auto-avoids them) |
|
|
550
|
+
| `deleteMode` | `"mark" \| "immediate"` | `"mark"` | Mark+undo vs outright removal |
|
|
551
|
+
| `onBeforeDelete` | `(field) => void \| false \| Promise` | — | Delete veto (Tree `onMove`-style) |
|
|
552
|
+
| `onBeforeTypeChange` | `(field, newType) => void \| false \| Promise` | — | Type-change veto for pre-existing fields |
|
|
553
|
+
| `preview` | `Snippet<[{ fields }]>` | — | Live preview pane (side-by-side when wide) |
|
|
554
|
+
| `maxFields` | `number` | — | Add-limit |
|
|
555
|
+
|
|
556
|
+
Features: collapsed rows (label + key + type chip) expanding to the full editor;
|
|
557
|
+
drag reorder + Move up/down buttons with aria-live announcements; `lock.*` per-field
|
|
558
|
+
locks (label/description always editable); unknown-type rows render degraded
|
|
559
|
+
read-only and round-trip untouched; per-row inline validation with
|
|
560
|
+
focus-first-offender `validate()`.
|
|
561
|
+
|
|
562
|
+
### CSS Tokens
|
|
563
|
+
|
|
564
|
+
Prefix: `--stuic-fields-builder-*`
|
|
565
|
+
|
|
566
|
+
`row-border`, `row-toggle-bg-hover`, `key-text`, `chip-bg`, `chip-text`, `chip-radius`,
|
|
567
|
+
`muted-text`, `warning-text`, `error-text`, `drop-indicator-color`,
|
|
568
|
+
`drop-indicator-height`, `row-opacity-dragging`, `row-opacity-deleted`, `preview-border`
|
|
569
|
+
|
|
570
|
+
---
|
|
571
|
+
|
|
512
572
|
## Checkout Components
|
|
513
573
|
|
|
514
574
|
14 exported sub-components organized as atomic building blocks + composite step pages.
|