@molecule/app-rule-builder-svelte 0.0.0-stage → 1.1.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 +202 -0
- package/README.md +441 -2
- package/dist/RuleBuilder.svelte +273 -0
- package/dist/RuleBuilder.svelte.d.ts +5 -0
- package/dist/RuleBuilder.svelte.d.ts.map +1 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +7 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/operators.d.ts +60 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +96 -0
- package/dist/style.d.ts +26 -0
- package/dist/style.d.ts.map +1 -0
- package/dist/style.js +52 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +15 -0
- package/dist/utilities.d.ts +67 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +107 -0
- package/package.json +76 -4
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
RuleBuilder: the Svelte counterpart of `@molecule/app-rule-builder-react`'s
|
|
3
|
+
`RuleBuilder` — a controlled recursive AND / OR group editor of
|
|
4
|
+
field-operator-value leaves. Same props (React names), same DOM for the same
|
|
5
|
+
input. Each group and leaf is rendered by a snippet that calls itself for
|
|
6
|
+
nested groups, so the tree nests to any depth.
|
|
7
|
+
-->
|
|
8
|
+
<script lang="ts">
|
|
9
|
+
import { useTranslation } from '@molecule/app-svelte'
|
|
10
|
+
import { getClassMap } from '@molecule/app-ui'
|
|
11
|
+
import { Button, Input, Select } from '@molecule/app-ui-svelte'
|
|
12
|
+
|
|
13
|
+
import { operatorById, operatorsFor } from './operators.js'
|
|
14
|
+
import { reactStyle } from './style.js'
|
|
15
|
+
import type {
|
|
16
|
+
Rule,
|
|
17
|
+
RuleBuilderProps,
|
|
18
|
+
RuleField,
|
|
19
|
+
RuleGroup,
|
|
20
|
+
RuleLeaf,
|
|
21
|
+
RuleLeafValue,
|
|
22
|
+
} from './types.js'
|
|
23
|
+
import {
|
|
24
|
+
appendToGroup,
|
|
25
|
+
emptyGroup,
|
|
26
|
+
emptyLeaf,
|
|
27
|
+
removeById,
|
|
28
|
+
replaceById,
|
|
29
|
+
toggleGroupOp,
|
|
30
|
+
} from './utilities.js'
|
|
31
|
+
|
|
32
|
+
let { schema, rules, onChange, className }: RuleBuilderProps = $props()
|
|
33
|
+
|
|
34
|
+
const cm = $derived(getClassMap())
|
|
35
|
+
const { t } = useTranslation()
|
|
36
|
+
|
|
37
|
+
/** The root group React renders: the tree itself when it is a group. */
|
|
38
|
+
const rootGroup = $derived<RuleGroup>(
|
|
39
|
+
rules.kind === 'group' ? rules : { kind: 'group', id: 'root', op: 'AND', children: [rules] },
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
/** Narrows a rule to a group. */
|
|
43
|
+
const isGroup = (rule: Rule): rule is RuleGroup => rule.kind === 'group'
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reads the string a native input event carries.
|
|
47
|
+
* @param event - The native input event.
|
|
48
|
+
* @returns The input's current value.
|
|
49
|
+
*/
|
|
50
|
+
const inputValue = (event: Event): string => (event.target as HTMLInputElement).value
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<div class={cm.cn(cm.flex({ direction: 'col', gap: 'sm' }), className)} data-mol-id="rule-builder">
|
|
54
|
+
{@render groupMarkup(
|
|
55
|
+
rootGroup,
|
|
56
|
+
true,
|
|
57
|
+
(next) => onChange(next),
|
|
58
|
+
() => onChange(emptyGroup()),
|
|
59
|
+
)}
|
|
60
|
+
</div>
|
|
61
|
+
|
|
62
|
+
{#snippet groupMarkup(
|
|
63
|
+
group: RuleGroup,
|
|
64
|
+
isRoot: boolean,
|
|
65
|
+
onReplace: (next: Rule) => void,
|
|
66
|
+
onRemove: () => void,
|
|
67
|
+
)}
|
|
68
|
+
<div
|
|
69
|
+
class={cm.cn(cm.flex({ direction: 'col', gap: 'xs' }), cm.borderAll, cm.surface)}
|
|
70
|
+
use:reactStyle={cm.sp({ p: 3 })}
|
|
71
|
+
data-mol-id={`rule-builder-group-${group.id}`}
|
|
72
|
+
data-group-op={group.op}
|
|
73
|
+
>
|
|
74
|
+
<div class={cm.flex({ align: 'center', gap: 'xs' })}>
|
|
75
|
+
<Button
|
|
76
|
+
variant="outline"
|
|
77
|
+
size="sm"
|
|
78
|
+
onClick={() => onReplace(toggleGroupOp(group, group.id) as RuleGroup)}
|
|
79
|
+
aria-label={t('ruleBuilder.aria.toggleOp', {}, { defaultValue: 'Toggle AND / OR' })}
|
|
80
|
+
data-mol-id={`rule-builder-group-${group.id}-toggle`}
|
|
81
|
+
>
|
|
82
|
+
{group.op === 'AND'
|
|
83
|
+
? t('ruleBuilder.op.and', {}, { defaultValue: 'AND' })
|
|
84
|
+
: t('ruleBuilder.op.or', {}, { defaultValue: 'OR' })}
|
|
85
|
+
</Button>
|
|
86
|
+
<span class={cm.cn(cm.textSize('xs'), cm.textMuted)}>
|
|
87
|
+
{group.op === 'AND'
|
|
88
|
+
? t('ruleBuilder.help.allMustMatch', {}, { defaultValue: 'All conditions must match' })
|
|
89
|
+
: t('ruleBuilder.help.anyMustMatch', {}, { defaultValue: 'Any condition may match' })}
|
|
90
|
+
</span>
|
|
91
|
+
{#if !isRoot}
|
|
92
|
+
<Button
|
|
93
|
+
variant="ghost"
|
|
94
|
+
size="sm"
|
|
95
|
+
onClick={onRemove}
|
|
96
|
+
aria-label={t('ruleBuilder.aria.removeGroup', {}, { defaultValue: 'Remove group' })}
|
|
97
|
+
data-mol-id={`rule-builder-group-${group.id}-remove`}
|
|
98
|
+
>
|
|
99
|
+
{t('ruleBuilder.action.removeGroup', {}, { defaultValue: 'Remove group' })}
|
|
100
|
+
</Button>
|
|
101
|
+
{/if}
|
|
102
|
+
</div>
|
|
103
|
+
|
|
104
|
+
<div class={cm.flex({ direction: 'col', gap: 'xs' })} use:reactStyle={cm.sp({ p: 2 })}>
|
|
105
|
+
{#each group.children as child (child.id)}
|
|
106
|
+
{#if isGroup(child)}
|
|
107
|
+
{@render groupMarkup(
|
|
108
|
+
child,
|
|
109
|
+
false,
|
|
110
|
+
(next) => onReplace(replaceById(group, child.id, next)),
|
|
111
|
+
() => onReplace(removeById(group, child.id)),
|
|
112
|
+
)}
|
|
113
|
+
{:else}
|
|
114
|
+
{@render leafMarkup(
|
|
115
|
+
child,
|
|
116
|
+
(next) => onReplace(replaceById(group, child.id, next)),
|
|
117
|
+
() => onReplace(removeById(group, child.id)),
|
|
118
|
+
)}
|
|
119
|
+
{/if}
|
|
120
|
+
{/each}
|
|
121
|
+
</div>
|
|
122
|
+
|
|
123
|
+
<div class={cm.flex({ align: 'center', gap: 'xs' })}>
|
|
124
|
+
<Button
|
|
125
|
+
variant="outline"
|
|
126
|
+
size="sm"
|
|
127
|
+
onClick={() => onReplace(appendToGroup(group, group.id, emptyLeaf()) as RuleGroup)}
|
|
128
|
+
data-mol-id={`rule-builder-group-${group.id}-add-condition`}
|
|
129
|
+
>
|
|
130
|
+
{t('ruleBuilder.action.addCondition', {}, { defaultValue: '+ Add condition' })}
|
|
131
|
+
</Button>
|
|
132
|
+
<Button
|
|
133
|
+
variant="ghost"
|
|
134
|
+
size="sm"
|
|
135
|
+
onClick={() => onReplace(appendToGroup(group, group.id, emptyGroup()) as RuleGroup)}
|
|
136
|
+
data-mol-id={`rule-builder-group-${group.id}-add-group`}
|
|
137
|
+
>
|
|
138
|
+
{t('ruleBuilder.action.addGroup', {}, { defaultValue: '+ Add group' })}
|
|
139
|
+
</Button>
|
|
140
|
+
</div>
|
|
141
|
+
</div>
|
|
142
|
+
{/snippet}
|
|
143
|
+
|
|
144
|
+
{#snippet leafMarkup(leaf: RuleLeaf, onReplace: (next: Rule) => void, onRemove: () => void)}
|
|
145
|
+
{@const field = schema.fields.find((f) => f.name === leaf.field)}
|
|
146
|
+
{@const ops = field ? operatorsFor(field) : []}
|
|
147
|
+
{@const op = field ? operatorById(field, leaf.op) : undefined}
|
|
148
|
+
{@const arity = op?.arity ?? 'binary'}
|
|
149
|
+
<div
|
|
150
|
+
class={cm.flex({ align: 'center', gap: 'xs', wrap: 'wrap' })}
|
|
151
|
+
data-mol-id={`rule-builder-leaf-${leaf.id}`}
|
|
152
|
+
>
|
|
153
|
+
<Select
|
|
154
|
+
value={leaf.field}
|
|
155
|
+
onValueChange={(name: string) => {
|
|
156
|
+
const f = schema.fields.find((x) => x.name === name)
|
|
157
|
+
const firstOp = f ? operatorsFor(f)[0] : undefined
|
|
158
|
+
onReplace({ ...leaf, field: name, op: firstOp?.id ?? '', value: undefined })
|
|
159
|
+
}}
|
|
160
|
+
options={[
|
|
161
|
+
{
|
|
162
|
+
value: '',
|
|
163
|
+
label: t('ruleBuilder.field.placeholder', {}, { defaultValue: 'Select field…' }),
|
|
164
|
+
},
|
|
165
|
+
...schema.fields.map((f) => ({ value: f.name, label: f.label })),
|
|
166
|
+
]}
|
|
167
|
+
aria-label={t('ruleBuilder.aria.field', {}, { defaultValue: 'Field' })}
|
|
168
|
+
/>
|
|
169
|
+
{#if field}
|
|
170
|
+
<Select
|
|
171
|
+
value={leaf.op}
|
|
172
|
+
onValueChange={(opId: string) => onReplace({ ...leaf, op: opId, value: undefined })}
|
|
173
|
+
options={ops.map((o) => ({ value: o.id, label: o.label }))}
|
|
174
|
+
aria-label={t('ruleBuilder.aria.operator', {}, { defaultValue: 'Operator' })}
|
|
175
|
+
/>
|
|
176
|
+
{/if}
|
|
177
|
+
{#if field && op && arity !== 'unary'}
|
|
178
|
+
{@render valueMarkup(field, arity, leaf.value, (value) => onReplace({ ...leaf, value }))}
|
|
179
|
+
{/if}
|
|
180
|
+
<Button
|
|
181
|
+
variant="ghost"
|
|
182
|
+
size="sm"
|
|
183
|
+
onClick={onRemove}
|
|
184
|
+
aria-label={t(
|
|
185
|
+
'ruleBuilder.aria.removeCondition',
|
|
186
|
+
{},
|
|
187
|
+
{
|
|
188
|
+
defaultValue: 'Remove condition',
|
|
189
|
+
},
|
|
190
|
+
)}
|
|
191
|
+
data-mol-id={`rule-builder-leaf-${leaf.id}-remove`}
|
|
192
|
+
>
|
|
193
|
+
{t('ruleBuilder.action.removeCondition', {}, { defaultValue: 'Remove' })}
|
|
194
|
+
</Button>
|
|
195
|
+
</div>
|
|
196
|
+
{/snippet}
|
|
197
|
+
|
|
198
|
+
{#snippet valueMarkup(
|
|
199
|
+
field: RuleField,
|
|
200
|
+
arity: 'binary' | 'between',
|
|
201
|
+
value: RuleLeafValue | undefined,
|
|
202
|
+
onChange: (next: RuleLeafValue | undefined) => void,
|
|
203
|
+
)}
|
|
204
|
+
{#if arity === 'between'}
|
|
205
|
+
{@const tuple =
|
|
206
|
+
Array.isArray(value) && value.length === 2
|
|
207
|
+
? value
|
|
208
|
+
: field.type === 'number'
|
|
209
|
+
? ([0, 0] as [number, number])
|
|
210
|
+
: (['', ''] as [string, string])}
|
|
211
|
+
{@const inputType =
|
|
212
|
+
field.type === 'date' ? 'date' : field.type === 'number' ? 'number' : 'text'}
|
|
213
|
+
<span class={cm.flex({ align: 'center', gap: 'xs' })}>
|
|
214
|
+
<Input
|
|
215
|
+
type={inputType}
|
|
216
|
+
value={String(tuple[0] ?? '')}
|
|
217
|
+
onChange={(event: Event) => {
|
|
218
|
+
const v = inputValue(event)
|
|
219
|
+
onChange(
|
|
220
|
+
(field.type === 'number'
|
|
221
|
+
? [Number(v), (tuple as [number, number])[1]]
|
|
222
|
+
: [v, (tuple as [string, string])[1]]) as RuleLeafValue,
|
|
223
|
+
)
|
|
224
|
+
}}
|
|
225
|
+
aria-label={t('ruleBuilder.aria.valueFrom', {}, { defaultValue: 'From' })}
|
|
226
|
+
/>
|
|
227
|
+
<span>{t('ruleBuilder.between.separator', {}, { defaultValue: '–' })}</span>
|
|
228
|
+
<Input
|
|
229
|
+
type={inputType}
|
|
230
|
+
value={String(tuple[1] ?? '')}
|
|
231
|
+
onChange={(event: Event) => {
|
|
232
|
+
const v = inputValue(event)
|
|
233
|
+
onChange(
|
|
234
|
+
(field.type === 'number'
|
|
235
|
+
? [(tuple as [number, number])[0], Number(v)]
|
|
236
|
+
: [(tuple as [string, string])[0], v]) as RuleLeafValue,
|
|
237
|
+
)
|
|
238
|
+
}}
|
|
239
|
+
aria-label={t('ruleBuilder.aria.valueTo', {}, { defaultValue: 'To' })}
|
|
240
|
+
/>
|
|
241
|
+
</span>
|
|
242
|
+
{:else if field.type === 'select'}
|
|
243
|
+
<Select
|
|
244
|
+
value={typeof value === 'string' ? value : ''}
|
|
245
|
+
onValueChange={(v: string) => onChange(v)}
|
|
246
|
+
options={field.options ?? []}
|
|
247
|
+
aria-label={t('ruleBuilder.aria.value', {}, { defaultValue: 'Value' })}
|
|
248
|
+
/>
|
|
249
|
+
{:else if field.type === 'boolean'}
|
|
250
|
+
<Select
|
|
251
|
+
value={value === true ? 'true' : value === false ? 'false' : ''}
|
|
252
|
+
onValueChange={(v: string) => onChange(v === 'true')}
|
|
253
|
+
options={[
|
|
254
|
+
{ value: 'true', label: t('ruleBuilder.boolean.true', {}, { defaultValue: 'true' }) },
|
|
255
|
+
{ value: 'false', label: t('ruleBuilder.boolean.false', {}, { defaultValue: 'false' }) },
|
|
256
|
+
]}
|
|
257
|
+
aria-label={t('ruleBuilder.aria.value', {}, { defaultValue: 'Value' })}
|
|
258
|
+
/>
|
|
259
|
+
{:else}
|
|
260
|
+
{@const inputType =
|
|
261
|
+
field.type === 'date' ? 'date' : field.type === 'number' ? 'number' : 'text'}
|
|
262
|
+
<Input
|
|
263
|
+
type={inputType}
|
|
264
|
+
value={value === undefined ? '' : String(value)}
|
|
265
|
+
onChange={(event: Event) => {
|
|
266
|
+
const raw = inputValue(event)
|
|
267
|
+
onChange(field.type === 'number' ? Number(raw) : raw)
|
|
268
|
+
}}
|
|
269
|
+
placeholder={field.placeholder}
|
|
270
|
+
aria-label={t('ruleBuilder.aria.value', {}, { defaultValue: 'Value' })}
|
|
271
|
+
/>
|
|
272
|
+
{/if}
|
|
273
|
+
{/snippet}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuleBuilder.svelte.d.ts","sourceRoot":"","sources":["../src/RuleBuilder.svelte"],"names":[],"mappings":"AASA,OAAO,KAAK,EAER,gBAAgB,EAKjB,MAAM,YAAY,CAAA;AA8LrB,QAAA,MAAM,WAAW,sDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's components: one Svelte 5 component per React component in
|
|
3
|
+
* `@molecule/app-rule-builder-react`, under the same React name.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export { default as RuleBuilder } from './RuleBuilder.svelte';
|
|
8
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,sBAAsB,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual condition / rule builder for Svelte.
|
|
3
|
+
*
|
|
4
|
+
* Exports:
|
|
5
|
+
* - `<RuleBuilder>` — controlled tree editor for AND / OR groups of
|
|
6
|
+
* field-operator-value leaves.
|
|
7
|
+
* - `RuleSchema`, `RuleField`, `RuleOperator`, `Rule`, `RuleGroup`,
|
|
8
|
+
* `RuleLeaf`, `RuleLeafValue`, `RuleGroupOp`, `RuleFieldType` types.
|
|
9
|
+
* - `defaultOperators`, `operatorsFor`, `operatorById`, `OP` operator
|
|
10
|
+
* catalog helpers.
|
|
11
|
+
* - `emptyGroup`, `emptyLeaf`, `replaceById`, `removeById`,
|
|
12
|
+
* `appendToGroup`, `toggleGroupOp`, `makeId` pure tree utilities.
|
|
13
|
+
*
|
|
14
|
+
* Used by feature-flag-manager (audience targeting),
|
|
15
|
+
* ai-workflow-automator (trigger conditions), email-marketing
|
|
16
|
+
* (segments), iot-device-manager + smart-home-dashboard (scenes /
|
|
17
|
+
* automations).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```svelte
|
|
21
|
+
* <script lang="ts">
|
|
22
|
+
* import { RuleBuilder, emptyGroup, type Rule, type RuleSchema } from '@molecule/app-rule-builder-svelte'
|
|
23
|
+
*
|
|
24
|
+
* const schema: RuleSchema = {
|
|
25
|
+
* fields: [
|
|
26
|
+
* { name: 'country', label: 'Country', type: 'select', options: [{ value: 'US', label: 'United States' }] },
|
|
27
|
+
* { name: 'plan', label: 'Plan', type: 'text' },
|
|
28
|
+
* { name: 'spend', label: 'Spend', type: 'number' },
|
|
29
|
+
* ],
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* let rules = $state<Rule>(emptyGroup('AND'))
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <RuleBuilder {schema} {rules} onChange={(next) => (rules = next)} />
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - `<RuleBuilder>` throws unless the app provides an i18n provider (bonded
|
|
40
|
+
* `@molecule/app-i18n`, or `setMoleculeContext({ i18n })`) with a bonded
|
|
41
|
+
* ClassMap (`setClassMap()`).
|
|
42
|
+
* - Output is a plain JSON-serializable `Rule` tree — this package only
|
|
43
|
+
* renders and edits it. Evaluation (matching rows/users against the tree)
|
|
44
|
+
* is the app's job, typically server-side.
|
|
45
|
+
* - Fully controlled: keep the tree in `$state` and pass it back via `rules`;
|
|
46
|
+
* start with `emptyGroup('AND')`.
|
|
47
|
+
* - Translations: registered companion bond `@molecule/app-locales-rule-builder`.
|
|
48
|
+
*
|
|
49
|
+
* @module
|
|
50
|
+
*/
|
|
51
|
+
export * from './components.js';
|
|
52
|
+
export * from './operators.js';
|
|
53
|
+
export * from './types.js';
|
|
54
|
+
export * from './utilities.js';
|
|
55
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual condition / rule builder for Svelte.
|
|
3
|
+
*
|
|
4
|
+
* Exports:
|
|
5
|
+
* - `<RuleBuilder>` — controlled tree editor for AND / OR groups of
|
|
6
|
+
* field-operator-value leaves.
|
|
7
|
+
* - `RuleSchema`, `RuleField`, `RuleOperator`, `Rule`, `RuleGroup`,
|
|
8
|
+
* `RuleLeaf`, `RuleLeafValue`, `RuleGroupOp`, `RuleFieldType` types.
|
|
9
|
+
* - `defaultOperators`, `operatorsFor`, `operatorById`, `OP` operator
|
|
10
|
+
* catalog helpers.
|
|
11
|
+
* - `emptyGroup`, `emptyLeaf`, `replaceById`, `removeById`,
|
|
12
|
+
* `appendToGroup`, `toggleGroupOp`, `makeId` pure tree utilities.
|
|
13
|
+
*
|
|
14
|
+
* Used by feature-flag-manager (audience targeting),
|
|
15
|
+
* ai-workflow-automator (trigger conditions), email-marketing
|
|
16
|
+
* (segments), iot-device-manager + smart-home-dashboard (scenes /
|
|
17
|
+
* automations).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```svelte
|
|
21
|
+
* <script lang="ts">
|
|
22
|
+
* import { RuleBuilder, emptyGroup, type Rule, type RuleSchema } from '@molecule/app-rule-builder-svelte'
|
|
23
|
+
*
|
|
24
|
+
* const schema: RuleSchema = {
|
|
25
|
+
* fields: [
|
|
26
|
+
* { name: 'country', label: 'Country', type: 'select', options: [{ value: 'US', label: 'United States' }] },
|
|
27
|
+
* { name: 'plan', label: 'Plan', type: 'text' },
|
|
28
|
+
* { name: 'spend', label: 'Spend', type: 'number' },
|
|
29
|
+
* ],
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* let rules = $state<Rule>(emptyGroup('AND'))
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <RuleBuilder {schema} {rules} onChange={(next) => (rules = next)} />
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - `<RuleBuilder>` throws unless the app provides an i18n provider (bonded
|
|
40
|
+
* `@molecule/app-i18n`, or `setMoleculeContext({ i18n })`) with a bonded
|
|
41
|
+
* ClassMap (`setClassMap()`).
|
|
42
|
+
* - Output is a plain JSON-serializable `Rule` tree — this package only
|
|
43
|
+
* renders and edits it. Evaluation (matching rows/users against the tree)
|
|
44
|
+
* is the app's job, typically server-side.
|
|
45
|
+
* - Fully controlled: keep the tree in `$state` and pass it back via `rules`;
|
|
46
|
+
* start with `emptyGroup('AND')`.
|
|
47
|
+
* - Translations: registered companion bond `@molecule/app-locales-rule-builder`.
|
|
48
|
+
*
|
|
49
|
+
* @module
|
|
50
|
+
*/
|
|
51
|
+
export * from './components.js';
|
|
52
|
+
export * from './operators.js';
|
|
53
|
+
export * from './types.js';
|
|
54
|
+
export * from './utilities.js';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default operator catalogs per field type, plus a small helper that
|
|
3
|
+
* picks the catalog for a given field (preferring the field's own
|
|
4
|
+
* `operators` override when provided).
|
|
5
|
+
*
|
|
6
|
+
* All operator labels here use English fallbacks via `t(key, ..., {
|
|
7
|
+
* defaultValue })` at the call site — this module is consumed by
|
|
8
|
+
* `RuleBuilder.svelte`, which translates labels through the
|
|
9
|
+
* `@molecule/app-svelte` i18n hooks before passing them to the dropdown.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import type { RuleField, RuleFieldType, RuleOperator } from './types.js';
|
|
14
|
+
/** Operator id constants — exported so consumers can inspect leaves. */
|
|
15
|
+
export declare const OP: {
|
|
16
|
+
readonly Equals: "eq";
|
|
17
|
+
readonly NotEquals: "neq";
|
|
18
|
+
readonly Contains: "contains";
|
|
19
|
+
readonly NotContains: "notContains";
|
|
20
|
+
readonly StartsWith: "startsWith";
|
|
21
|
+
readonly EndsWith: "endsWith";
|
|
22
|
+
readonly GreaterThan: "gt";
|
|
23
|
+
readonly GreaterThanOrEqual: "gte";
|
|
24
|
+
readonly LessThan: "lt";
|
|
25
|
+
readonly LessThanOrEqual: "lte";
|
|
26
|
+
readonly Between: "between";
|
|
27
|
+
readonly Before: "before";
|
|
28
|
+
readonly After: "after";
|
|
29
|
+
readonly IsTrue: "isTrue";
|
|
30
|
+
readonly IsFalse: "isFalse";
|
|
31
|
+
readonly IsEmpty: "isEmpty";
|
|
32
|
+
readonly IsNotEmpty: "isNotEmpty";
|
|
33
|
+
readonly In: "in";
|
|
34
|
+
readonly NotIn: "notIn";
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Untranslated operator descriptors per field type.
|
|
38
|
+
*
|
|
39
|
+
* `RuleBuilder` translates each `label` at render time; the strings
|
|
40
|
+
* here are English fallbacks only, never shown directly.
|
|
41
|
+
*/
|
|
42
|
+
export declare const defaultOperators: Record<RuleFieldType, RuleOperator[]>;
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the operator catalog for `field`. Returns the field's own
|
|
45
|
+
* `operators` override when provided, otherwise the built-in catalog
|
|
46
|
+
* for the field's `type`.
|
|
47
|
+
*
|
|
48
|
+
* @param field - Field descriptor from the schema.
|
|
49
|
+
* @returns Operator catalog applicable to this field.
|
|
50
|
+
*/
|
|
51
|
+
export declare function operatorsFor(field: RuleField): RuleOperator[];
|
|
52
|
+
/**
|
|
53
|
+
* Look up a single operator descriptor by id within `field`'s catalog.
|
|
54
|
+
*
|
|
55
|
+
* @param field - Field descriptor.
|
|
56
|
+
* @param opId - Operator id to look up.
|
|
57
|
+
* @returns The operator descriptor, or `undefined` if `opId` isn't in the catalog.
|
|
58
|
+
*/
|
|
59
|
+
export declare function operatorById(field: RuleField, opId: string): RuleOperator | undefined;
|
|
60
|
+
//# sourceMappingURL=operators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operators.d.ts","sourceRoot":"","sources":["../src/operators.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAExE,wEAAwE;AACxE,eAAO,MAAM,EAAE;;;;;;;;;;;;;;;;;;;;CAoBL,CAAA;AAEV;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,aAAa,EAAE,YAAY,EAAE,CAkClE,CAAA;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,YAAY,EAAE,CAE7D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAErF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default operator catalogs per field type, plus a small helper that
|
|
3
|
+
* picks the catalog for a given field (preferring the field's own
|
|
4
|
+
* `operators` override when provided).
|
|
5
|
+
*
|
|
6
|
+
* All operator labels here use English fallbacks via `t(key, ..., {
|
|
7
|
+
* defaultValue })` at the call site — this module is consumed by
|
|
8
|
+
* `RuleBuilder.svelte`, which translates labels through the
|
|
9
|
+
* `@molecule/app-svelte` i18n hooks before passing them to the dropdown.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
/** Operator id constants — exported so consumers can inspect leaves. */
|
|
14
|
+
export const OP = {
|
|
15
|
+
Equals: 'eq',
|
|
16
|
+
NotEquals: 'neq',
|
|
17
|
+
Contains: 'contains',
|
|
18
|
+
NotContains: 'notContains',
|
|
19
|
+
StartsWith: 'startsWith',
|
|
20
|
+
EndsWith: 'endsWith',
|
|
21
|
+
GreaterThan: 'gt',
|
|
22
|
+
GreaterThanOrEqual: 'gte',
|
|
23
|
+
LessThan: 'lt',
|
|
24
|
+
LessThanOrEqual: 'lte',
|
|
25
|
+
Between: 'between',
|
|
26
|
+
Before: 'before',
|
|
27
|
+
After: 'after',
|
|
28
|
+
IsTrue: 'isTrue',
|
|
29
|
+
IsFalse: 'isFalse',
|
|
30
|
+
IsEmpty: 'isEmpty',
|
|
31
|
+
IsNotEmpty: 'isNotEmpty',
|
|
32
|
+
In: 'in',
|
|
33
|
+
NotIn: 'notIn',
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Untranslated operator descriptors per field type.
|
|
37
|
+
*
|
|
38
|
+
* `RuleBuilder` translates each `label` at render time; the strings
|
|
39
|
+
* here are English fallbacks only, never shown directly.
|
|
40
|
+
*/
|
|
41
|
+
export const defaultOperators = {
|
|
42
|
+
text: [
|
|
43
|
+
{ id: OP.Equals, label: 'equals' },
|
|
44
|
+
{ id: OP.NotEquals, label: 'does not equal' },
|
|
45
|
+
{ id: OP.Contains, label: 'contains' },
|
|
46
|
+
{ id: OP.NotContains, label: 'does not contain' },
|
|
47
|
+
{ id: OP.StartsWith, label: 'starts with' },
|
|
48
|
+
{ id: OP.EndsWith, label: 'ends with' },
|
|
49
|
+
{ id: OP.IsEmpty, label: 'is empty', arity: 'unary' },
|
|
50
|
+
{ id: OP.IsNotEmpty, label: 'is not empty', arity: 'unary' },
|
|
51
|
+
],
|
|
52
|
+
number: [
|
|
53
|
+
{ id: OP.Equals, label: '=' },
|
|
54
|
+
{ id: OP.NotEquals, label: '≠' },
|
|
55
|
+
{ id: OP.GreaterThan, label: '>' },
|
|
56
|
+
{ id: OP.GreaterThanOrEqual, label: '≥' },
|
|
57
|
+
{ id: OP.LessThan, label: '<' },
|
|
58
|
+
{ id: OP.LessThanOrEqual, label: '≤' },
|
|
59
|
+
{ id: OP.Between, label: 'between', arity: 'between' },
|
|
60
|
+
],
|
|
61
|
+
date: [
|
|
62
|
+
{ id: OP.Equals, label: 'on' },
|
|
63
|
+
{ id: OP.Before, label: 'before' },
|
|
64
|
+
{ id: OP.After, label: 'after' },
|
|
65
|
+
{ id: OP.Between, label: 'between', arity: 'between' },
|
|
66
|
+
],
|
|
67
|
+
select: [
|
|
68
|
+
{ id: OP.Equals, label: 'is' },
|
|
69
|
+
{ id: OP.NotEquals, label: 'is not' },
|
|
70
|
+
],
|
|
71
|
+
boolean: [
|
|
72
|
+
{ id: OP.IsTrue, label: 'is true', arity: 'unary' },
|
|
73
|
+
{ id: OP.IsFalse, label: 'is false', arity: 'unary' },
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Resolve the operator catalog for `field`. Returns the field's own
|
|
78
|
+
* `operators` override when provided, otherwise the built-in catalog
|
|
79
|
+
* for the field's `type`.
|
|
80
|
+
*
|
|
81
|
+
* @param field - Field descriptor from the schema.
|
|
82
|
+
* @returns Operator catalog applicable to this field.
|
|
83
|
+
*/
|
|
84
|
+
export function operatorsFor(field) {
|
|
85
|
+
return field.operators ?? defaultOperators[field.type] ?? [];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Look up a single operator descriptor by id within `field`'s catalog.
|
|
89
|
+
*
|
|
90
|
+
* @param field - Field descriptor.
|
|
91
|
+
* @param opId - Operator id to look up.
|
|
92
|
+
* @returns The operator descriptor, or `undefined` if `opId` isn't in the catalog.
|
|
93
|
+
*/
|
|
94
|
+
export function operatorById(field, opId) {
|
|
95
|
+
return operatorsFor(field).find((o) => o.id === opId);
|
|
96
|
+
}
|
package/dist/style.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline style application shared by this package's components: a Svelte
|
|
3
|
+
* action that applies a React-shaped style object the way react-dom does, so
|
|
4
|
+
* the declarations land on the element through the CSSOM exactly as React's
|
|
5
|
+
* runtime leaves them (jsdom and browsers serialize the attribute the same
|
|
6
|
+
* way for every framework that assigns properties instead of writing the
|
|
7
|
+
* attribute string).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/** A React-shaped inline style object (camelCase keys, string values). */
|
|
12
|
+
export type CSSStyle = Record<string, string | number | undefined | null | false>;
|
|
13
|
+
/**
|
|
14
|
+
* Svelte action applying a React-shaped style object to an element the way
|
|
15
|
+
* React does: every entry is assigned through the CSSOM property setter (in
|
|
16
|
+
* object order, values serialized as strings), and keys removed by a
|
|
17
|
+
* re-render are cleared too.
|
|
18
|
+
*
|
|
19
|
+
* @param node - The element to style.
|
|
20
|
+
* @param style - The style object (may be undefined).
|
|
21
|
+
* @returns The action with an update that re-applies on prop changes.
|
|
22
|
+
*/
|
|
23
|
+
export declare function reactStyle(node: HTMLElement, style: CSSStyle | undefined): {
|
|
24
|
+
update: (next: CSSStyle | undefined) => void;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=style.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../src/style.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,0EAA0E;AAC1E,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,KAAK,CAAC,CAAA;AAUjF;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,WAAW,EACjB,KAAK,EAAE,QAAQ,GAAG,SAAS,GAC1B;IAAE,MAAM,EAAE,CAAC,IAAI,EAAE,QAAQ,GAAG,SAAS,KAAK,IAAI,CAAA;CAAE,CAuBlD"}
|
package/dist/style.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline style application shared by this package's components: a Svelte
|
|
3
|
+
* action that applies a React-shaped style object the way react-dom does, so
|
|
4
|
+
* the declarations land on the element through the CSSOM exactly as React's
|
|
5
|
+
* runtime leaves them (jsdom and browsers serialize the attribute the same
|
|
6
|
+
* way for every framework that assigns properties instead of writing the
|
|
7
|
+
* attribute string).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Turns a camelCase CSS property name into its kebab-case DOM spelling.
|
|
13
|
+
*
|
|
14
|
+
* @param name - The camelCase property (`borderRadius`).
|
|
15
|
+
* @returns The kebab-case property (`border-radius`).
|
|
16
|
+
*/
|
|
17
|
+
const kebab = (name) => name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
18
|
+
/**
|
|
19
|
+
* Svelte action applying a React-shaped style object to an element the way
|
|
20
|
+
* React does: every entry is assigned through the CSSOM property setter (in
|
|
21
|
+
* object order, values serialized as strings), and keys removed by a
|
|
22
|
+
* re-render are cleared too.
|
|
23
|
+
*
|
|
24
|
+
* @param node - The element to style.
|
|
25
|
+
* @param style - The style object (may be undefined).
|
|
26
|
+
* @returns The action with an update that re-applies on prop changes.
|
|
27
|
+
*/
|
|
28
|
+
export function reactStyle(node, style) {
|
|
29
|
+
const applied = new Set();
|
|
30
|
+
const apply = (next) => {
|
|
31
|
+
const removed = new Set(applied);
|
|
32
|
+
if (next) {
|
|
33
|
+
for (const [key, value] of Object.entries(next)) {
|
|
34
|
+
removed.delete(key);
|
|
35
|
+
const text = value === undefined || value === null || value === false || value === ''
|
|
36
|
+
? ''
|
|
37
|
+
: String(value).trim();
|
|
38
|
+
node.style.setProperty(kebab(key), text);
|
|
39
|
+
if (text)
|
|
40
|
+
applied.add(key);
|
|
41
|
+
else
|
|
42
|
+
applied.delete(key);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const key of removed) {
|
|
46
|
+
node.style.setProperty(kebab(key), '');
|
|
47
|
+
applied.delete(key);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
apply(style);
|
|
51
|
+
return { update: apply };
|
|
52
|
+
}
|