@rangka/client 0.1.1 → 0.1.3
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/App.d.ts.map +1 -1
- package/dist/App.js +7 -1
- package/dist/App.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +20 -0
- package/dist/main.js.map +1 -1
- package/dist/shell/assets/index-63v1sBS3.css +1 -0
- package/dist/shell/assets/index-Dh7K40cQ.js +8634 -0
- package/dist/shell/assets/vendor-query-B2cydN5j.js +1 -0
- package/dist/shell/assets/vendor-radix-BJxYPxPb.js +69 -0
- package/dist/shell/assets/vendor-router-ET_myMt5.js +17 -0
- package/dist/shell/index.html +5 -2
- package/dist/theme.css +82 -0
- package/dist/widgets/components/lazy-manifest.d.ts +11 -0
- package/dist/widgets/components/lazy-manifest.d.ts.map +1 -0
- package/dist/widgets/components/lazy-manifest.js +32 -0
- package/dist/widgets/components/lazy-manifest.js.map +1 -0
- package/dist/widgets/components/register.js +12 -12
- package/dist/widgets/components/register.js.map +1 -1
- package/dist/widgets/loader.d.ts +3 -0
- package/dist/widgets/loader.d.ts.map +1 -0
- package/dist/widgets/loader.js +73 -0
- package/dist/widgets/loader.js.map +1 -0
- package/dist/widgets/renderer/LazyWidget.d.ts +8 -0
- package/dist/widgets/renderer/LazyWidget.d.ts.map +1 -0
- package/dist/widgets/renderer/LazyWidget.js +31 -0
- package/dist/widgets/renderer/LazyWidget.js.map +1 -0
- package/dist/widgets/renderer/WidgetErrorBoundary.d.ts +17 -0
- package/dist/widgets/renderer/WidgetErrorBoundary.d.ts.map +1 -0
- package/dist/widgets/renderer/WidgetErrorBoundary.js +18 -0
- package/dist/widgets/renderer/WidgetErrorBoundary.js.map +1 -0
- package/dist/widgets/renderer/WidgetRenderer.d.ts.map +1 -1
- package/dist/widgets/renderer/WidgetRenderer.js +8 -6
- package/dist/widgets/renderer/WidgetRenderer.js.map +1 -1
- package/package.json +7 -4
- package/.claude/skills/add-widget/SKILL.md +0 -101
- package/.turbo/turbo-build.log +0 -29
- package/CHANGELOG.md +0 -25
- package/CLAUDE.md +0 -236
- package/components.json +0 -25
- package/dist/components/ui/chart.d.ts +0 -45
- package/dist/components/ui/chart.d.ts.map +0 -1
- package/dist/components/ui/chart.js +0 -119
- package/dist/components/ui/chart.js.map +0 -1
- package/dist/shell/assets/index--35CAvcP.js +0 -8715
- package/dist/shell/assets/index-COLmoPYo.css +0 -1
- package/index.html +0 -12
- package/src/App.tsx +0 -44
- package/src/__tests__/setup.ts +0 -1
- package/src/api/auth.ts +0 -41
- package/src/api/boot.ts +0 -10
- package/src/api/client.ts +0 -26
- package/src/api/paths.ts +0 -3
- package/src/api/token.ts +0 -13
- package/src/auth/LoginForm.tsx +0 -67
- package/src/auth/SessionExpired.tsx +0 -24
- package/src/auth/SetupForm.tsx +0 -76
- package/src/boot/BootGate.tsx +0 -35
- package/src/boot/BootProvider.tsx +0 -28
- package/src/boot/types.ts +0 -9
- package/src/boot/useBoot.ts +0 -111
- package/src/components/Icon.tsx +0 -17
- package/src/components/ui/accordion.tsx +0 -82
- package/src/components/ui/alert-dialog.tsx +0 -180
- package/src/components/ui/alert.tsx +0 -76
- package/src/components/ui/aspect-ratio.tsx +0 -9
- package/src/components/ui/avatar.tsx +0 -94
- package/src/components/ui/badge.tsx +0 -45
- package/src/components/ui/breadcrumb.tsx +0 -104
- package/src/components/ui/button-group.tsx +0 -78
- package/src/components/ui/button.tsx +0 -65
- package/src/components/ui/calendar.tsx +0 -187
- package/src/components/ui/card.tsx +0 -85
- package/src/components/ui/carousel.tsx +0 -229
- package/src/components/ui/chart.tsx +0 -339
- package/src/components/ui/checkbox.tsx +0 -27
- package/src/components/ui/collapsible.tsx +0 -21
- package/src/components/ui/combobox.tsx +0 -275
- package/src/components/ui/command.tsx +0 -178
- package/src/components/ui/context-menu.tsx +0 -242
- package/src/components/ui/dialog.tsx +0 -146
- package/src/components/ui/direction.tsx +0 -20
- package/src/components/ui/drawer.tsx +0 -118
- package/src/components/ui/dropdown-menu.tsx +0 -247
- package/src/components/ui/empty.tsx +0 -94
- package/src/components/ui/field.tsx +0 -224
- package/src/components/ui/hover-card.tsx +0 -36
- package/src/components/ui/input-group.tsx +0 -142
- package/src/components/ui/input-otp.tsx +0 -86
- package/src/components/ui/input.tsx +0 -19
- package/src/components/ui/item.tsx +0 -182
- package/src/components/ui/kbd.tsx +0 -26
- package/src/components/ui/label.tsx +0 -19
- package/src/components/ui/menubar.tsx +0 -260
- package/src/components/ui/native-select.tsx +0 -55
- package/src/components/ui/navigation-menu.tsx +0 -160
- package/src/components/ui/pagination.tsx +0 -112
- package/src/components/ui/popover.tsx +0 -74
- package/src/components/ui/progress.tsx +0 -31
- package/src/components/ui/radio-group.tsx +0 -42
- package/src/components/ui/resizable.tsx +0 -42
- package/src/components/ui/scroll-area.tsx +0 -53
- package/src/components/ui/select.tsx +0 -185
- package/src/components/ui/separator.tsx +0 -26
- package/src/components/ui/sheet.tsx +0 -128
- package/src/components/ui/sidebar.tsx +0 -669
- package/src/components/ui/skeleton.tsx +0 -13
- package/src/components/ui/slider.tsx +0 -54
- package/src/components/ui/sonner.tsx +0 -43
- package/src/components/ui/spinner.tsx +0 -16
- package/src/components/ui/switch.tsx +0 -33
- package/src/components/ui/table.tsx +0 -87
- package/src/components/ui/tabs.tsx +0 -80
- package/src/components/ui/textarea.tsx +0 -18
- package/src/components/ui/toggle-group.tsx +0 -86
- package/src/components/ui/toggle.tsx +0 -44
- package/src/components/ui/tooltip.tsx +0 -53
- package/src/context/MetaContext.tsx +0 -22
- package/src/context/ModuleContext.tsx +0 -62
- package/src/context/PermissionsContext.tsx +0 -39
- package/src/context/ShellProviders.tsx +0 -33
- package/src/context/UserContext.tsx +0 -16
- package/src/data/QueryProvider.tsx +0 -7
- package/src/data/queryClient.ts +0 -18
- package/src/data/useModelMeta.ts +0 -17
- package/src/data/useMutation.ts +0 -60
- package/src/data/useRecord.ts +0 -29
- package/src/data/useSource.ts +0 -112
- package/src/hooks/use-mobile.ts +0 -19
- package/src/index.css +0 -260
- package/src/index.ts +0 -16
- package/src/lib/utils.ts +0 -6
- package/src/main.tsx +0 -17
- package/src/router/NotFound.tsx +0 -8
- package/src/router/RouterProvider.tsx +0 -7
- package/src/router/buildRouteTree.tsx +0 -63
- package/src/router/createShellRouter.ts +0 -9
- package/src/router/hooks.ts +0 -43
- package/src/shell/CommandPalette.tsx +0 -76
- package/src/shell/ConfirmDialog.tsx +0 -34
- package/src/shell/ConfirmProvider.tsx +0 -56
- package/src/shell/DrawerContext.tsx +0 -44
- package/src/shell/HeaderActions.tsx +0 -31
- package/src/shell/ModuleSelectorPage.tsx +0 -149
- package/src/shell/PageOutlet.tsx +0 -21
- package/src/shell/ShellContext.tsx +0 -45
- package/src/shell/ShellDevTools.tsx +0 -153
- package/src/shell/ShellLayout.tsx +0 -231
- package/src/shell/Toast.tsx +0 -58
- package/src/shell/ToastProvider.tsx +0 -60
- package/src/shell/app-sidebar/AppSidebar.tsx +0 -44
- package/src/shell/app-sidebar/ModuleSwitcher.tsx +0 -87
- package/src/shell/app-sidebar/NavMain.tsx +0 -64
- package/src/shell/app-sidebar/NavUser.tsx +0 -97
- package/src/shell/app-sidebar/SearchMenu.tsx +0 -22
- package/src/shell/app-sidebar/index.ts +0 -8
- package/src/shell/app-sidebar/types.ts +0 -38
- package/src/shell/types.ts +0 -6
- package/src/shell/useBreadcrumbs.ts +0 -42
- package/src/studio/bridge.ts +0 -125
- package/src/studio/index.ts +0 -3
- package/src/studio/overlay.ts +0 -47
- package/src/studio/types.ts +0 -32
- package/src/studio/walker.ts +0 -48
- package/src/vite-env.d.ts +0 -1
- package/src/widgets/__tests__/action-edge-cases.test.ts +0 -281
- package/src/widgets/__tests__/action.test.ts +0 -236
- package/src/widgets/__tests__/attachment-widget.test.tsx +0 -85
- package/src/widgets/__tests__/attachments-widget.test.tsx +0 -109
- package/src/widgets/__tests__/binding.test.ts +0 -76
- package/src/widgets/__tests__/button-widget.test.tsx +0 -145
- package/src/widgets/__tests__/checkbox-widget.test.tsx +0 -158
- package/src/widgets/__tests__/code-widget.test.tsx +0 -64
- package/src/widgets/__tests__/computed-widget.test.tsx +0 -62
- package/src/widgets/__tests__/condition-edge-cases.test.ts +0 -120
- package/src/widgets/__tests__/condition.test.ts +0 -221
- package/src/widgets/__tests__/context.test.ts +0 -99
- package/src/widgets/__tests__/data-widget.test.tsx +0 -204
- package/src/widgets/__tests__/datepicker-widget.test.tsx +0 -66
- package/src/widgets/__tests__/datetime-widget.test.tsx +0 -67
- package/src/widgets/__tests__/drawer-widget.test.tsx +0 -149
- package/src/widgets/__tests__/dynamic-link-widget.test.tsx +0 -52
- package/src/widgets/__tests__/edge-cases.test.ts +0 -232
- package/src/widgets/__tests__/evaluator.test.ts +0 -107
- package/src/widgets/__tests__/functions.test.ts +0 -147
- package/src/widgets/__tests__/grid-widget.test.tsx +0 -137
- package/src/widgets/__tests__/hooks.test.tsx +0 -249
- package/src/widgets/__tests__/icon-widget.test.tsx +0 -129
- package/src/widgets/__tests__/input-widget.test.tsx +0 -264
- package/src/widgets/__tests__/integration.test.ts +0 -116
- package/src/widgets/__tests__/json-widget.test.tsx +0 -70
- package/src/widgets/__tests__/link-widget.test.tsx +0 -92
- package/src/widgets/__tests__/many-to-many-widget.test.tsx +0 -93
- package/src/widgets/__tests__/modal-widget.test.tsx +0 -148
- package/src/widgets/__tests__/money-widget.test.tsx +0 -97
- package/src/widgets/__tests__/parser.test.ts +0 -171
- package/src/widgets/__tests__/reactive-variables.test.ts +0 -383
- package/src/widgets/__tests__/renderer.test.tsx +0 -300
- package/src/widgets/__tests__/repeat-widget.test.tsx +0 -229
- package/src/widgets/__tests__/select-widget.test.tsx +0 -231
- package/src/widgets/__tests__/sequence-widget.test.tsx +0 -58
- package/src/widgets/__tests__/shell-integration.test.tsx +0 -1343
- package/src/widgets/__tests__/split-widget.test.tsx +0 -133
- package/src/widgets/__tests__/state-edge-cases.test.ts +0 -118
- package/src/widgets/__tests__/state.test.ts +0 -106
- package/src/widgets/__tests__/table-data-binding.test.tsx +0 -482
- package/src/widgets/__tests__/table-filter-popover.test.tsx +0 -486
- package/src/widgets/__tests__/table-search.test.tsx +0 -305
- package/src/widgets/__tests__/table-widget.test.tsx +0 -509
- package/src/widgets/__tests__/textarea-widget.test.tsx +0 -105
- package/src/widgets/__tests__/tracker-validator-edge-cases.test.ts +0 -242
- package/src/widgets/__tests__/tracker.test.ts +0 -133
- package/src/widgets/__tests__/tree-widget.test.tsx +0 -97
- package/src/widgets/__tests__/use-model-source.test.ts +0 -67
- package/src/widgets/__tests__/validator.test.ts +0 -208
- package/src/widgets/action/dispatcher.ts +0 -334
- package/src/widgets/action/index.ts +0 -2
- package/src/widgets/binding/index.ts +0 -2
- package/src/widgets/binding/resolver.ts +0 -61
- package/src/widgets/components/AttachmentWidget.tsx +0 -111
- package/src/widgets/components/AttachmentsWidget.tsx +0 -121
- package/src/widgets/components/BadgeWidget.tsx +0 -35
- package/src/widgets/components/ButtonWidget.tsx +0 -43
- package/src/widgets/components/CardWidget.tsx +0 -68
- package/src/widgets/components/CheckboxWidget.tsx +0 -39
- package/src/widgets/components/CodeWidget.tsx +0 -44
- package/src/widgets/components/ColumnWidget.tsx +0 -22
- package/src/widgets/components/ComputedWidget.tsx +0 -49
- package/src/widgets/components/DataWidget.tsx +0 -189
- package/src/widgets/components/DatePickerWidget.tsx +0 -73
- package/src/widgets/components/DatetimeWidget.tsx +0 -160
- package/src/widgets/components/DividerWidget.tsx +0 -37
- package/src/widgets/components/DrawerWidget.tsx +0 -52
- package/src/widgets/components/DynamicLinkWidget.tsx +0 -130
- package/src/widgets/components/GridWidget.tsx +0 -134
- package/src/widgets/components/GroupWidget.tsx +0 -111
- package/src/widgets/components/IconWidget.tsx +0 -29
- package/src/widgets/components/ImageWidget.tsx +0 -28
- package/src/widgets/components/InputWidget.tsx +0 -70
- package/src/widgets/components/JsonWidget.tsx +0 -78
- package/src/widgets/components/LinkWidget.tsx +0 -99
- package/src/widgets/components/ManyToManyWidget.tsx +0 -125
- package/src/widgets/components/ModalWidget.tsx +0 -52
- package/src/widgets/components/MoneyWidget.tsx +0 -80
- package/src/widgets/components/RepeatWidget.tsx +0 -66
- package/src/widgets/components/ScrollAreaWidget.tsx +0 -40
- package/src/widgets/components/SectionWidget.tsx +0 -78
- package/src/widgets/components/SelectWidget.tsx +0 -63
- package/src/widgets/components/SequenceWidget.tsx +0 -32
- package/src/widgets/components/SpacerWidget.tsx +0 -29
- package/src/widgets/components/SplitWidget.tsx +0 -60
- package/src/widgets/components/StackWidget.tsx +0 -44
- package/src/widgets/components/TableWidget.tsx +0 -366
- package/src/widgets/components/TextWidget.tsx +0 -44
- package/src/widgets/components/TextareaWidget.tsx +0 -49
- package/src/widgets/components/TreeWidget.tsx +0 -109
- package/src/widgets/components/index.ts +0 -30
- package/src/widgets/components/register.ts +0 -93
- package/src/widgets/components/table/CellRenderers.tsx +0 -83
- package/src/widgets/components/table/TablePagination.tsx +0 -45
- package/src/widgets/components/table/TableToolbar.tsx +0 -285
- package/src/widgets/components/table/filter-operators.ts +0 -134
- package/src/widgets/components/table/index.ts +0 -11
- package/src/widgets/condition/evaluator.ts +0 -57
- package/src/widgets/condition/index.ts +0 -1
- package/src/widgets/context/builder.ts +0 -99
- package/src/widgets/context/index.ts +0 -8
- package/src/widgets/context/types.ts +0 -37
- package/src/widgets/data/index.ts +0 -5
- package/src/widgets/data/useModelQuery.ts +0 -116
- package/src/widgets/data/useModelRecord.ts +0 -37
- package/src/widgets/expression/evaluator.ts +0 -100
- package/src/widgets/expression/functions.ts +0 -131
- package/src/widgets/expression/index.ts +0 -13
- package/src/widgets/expression/parser.ts +0 -229
- package/src/widgets/expression/types.ts +0 -45
- package/src/widgets/form/FormContext.ts +0 -29
- package/src/widgets/form/FormProvider.tsx +0 -84
- package/src/widgets/form/FormWidget.tsx +0 -42
- package/src/widgets/form/index.ts +0 -4
- package/src/widgets/form/useFormState.ts +0 -127
- package/src/widgets/form/useFormSubmit.ts +0 -90
- package/src/widgets/form/useFormValidation.ts +0 -62
- package/src/widgets/hooks/index.ts +0 -8
- package/src/widgets/hooks/useAction.ts +0 -83
- package/src/widgets/hooks/useBind.ts +0 -34
- package/src/widgets/hooks/useCondition.ts +0 -21
- package/src/widgets/hooks/useDataQuery.ts +0 -48
- package/src/widgets/hooks/useExpression.ts +0 -14
- package/src/widgets/hooks/usePageState.ts +0 -21
- package/src/widgets/hooks/useSurfaceContext.ts +0 -11
- package/src/widgets/hooks/useWidgetContext.ts +0 -14
- package/src/widgets/index.ts +0 -80
- package/src/widgets/lib/layout-props.ts +0 -135
- package/src/widgets/reactivity/index.ts +0 -11
- package/src/widgets/reactivity/tracker.ts +0 -139
- package/src/widgets/reactivity/variables.ts +0 -213
- package/src/widgets/registry.ts +0 -41
- package/src/widgets/renderer/SlotRenderer.tsx +0 -47
- package/src/widgets/renderer/WidgetRenderer.tsx +0 -191
- package/src/widgets/renderer/index.ts +0 -4
- package/src/widgets/shell/WidgetSlotRenderer.tsx +0 -73
- package/src/widgets/shell/index.ts +0 -4
- package/src/widgets/shell/useActionHandlers.ts +0 -170
- package/src/widgets/state/index.ts +0 -2
- package/src/widgets/state/store.ts +0 -96
- package/src/widgets/types.ts +0 -28
- package/src/widgets/validation/index.ts +0 -2
- package/src/widgets/validation/validator.ts +0 -140
- package/tsconfig.json +0 -27
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -21
- package/vitest.config.ts +0 -16
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from 'vitest';
|
|
2
|
-
import { validateWidgetTree } from '../validation/validator.js';
|
|
3
|
-
import type { WidgetDefinitionMeta, WidgetNode } from '@rangka/shared';
|
|
4
|
-
|
|
5
|
-
function makeRegistry(defs: WidgetDefinitionMeta[]): Map<string, WidgetDefinitionMeta> {
|
|
6
|
-
return new Map(defs.map((d) => [d.name, d]));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const buttonDef: WidgetDefinitionMeta = {
|
|
10
|
-
name: 'button',
|
|
11
|
-
label: 'Button',
|
|
12
|
-
category: 'action',
|
|
13
|
-
schema: {
|
|
14
|
-
label: { type: 'string', required: true },
|
|
15
|
-
variant: {
|
|
16
|
-
type: 'enum',
|
|
17
|
-
options: ['primary', 'secondary', 'ghost', 'danger'],
|
|
18
|
-
default: 'secondary',
|
|
19
|
-
},
|
|
20
|
-
size: { type: 'enum', options: ['sm', 'md', 'lg'], default: 'md' },
|
|
21
|
-
},
|
|
22
|
-
binding: 'none',
|
|
23
|
-
triggers: ['click'],
|
|
24
|
-
container: false,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const inputDef: WidgetDefinitionMeta = {
|
|
28
|
-
name: 'input',
|
|
29
|
-
label: 'Input',
|
|
30
|
-
category: 'input',
|
|
31
|
-
schema: {
|
|
32
|
-
label: { type: 'string' },
|
|
33
|
-
placeholder: { type: 'string' },
|
|
34
|
-
readOnly: { type: 'boolean' },
|
|
35
|
-
},
|
|
36
|
-
binding: 'field',
|
|
37
|
-
triggers: ['change', 'focus', 'blur'],
|
|
38
|
-
container: false,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const groupDef: WidgetDefinitionMeta = {
|
|
42
|
-
name: 'group',
|
|
43
|
-
label: 'Group',
|
|
44
|
-
category: 'layout',
|
|
45
|
-
schema: {
|
|
46
|
-
direction: { type: 'enum', options: ['row', 'column'], default: 'column' },
|
|
47
|
-
gap: { type: 'enum', options: ['sm', 'md', 'lg'], default: 'md' },
|
|
48
|
-
},
|
|
49
|
-
binding: 'none',
|
|
50
|
-
triggers: [],
|
|
51
|
-
container: true,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const tableDef: WidgetDefinitionMeta = {
|
|
55
|
-
name: 'table',
|
|
56
|
-
label: 'Table',
|
|
57
|
-
category: 'layout',
|
|
58
|
-
schema: {
|
|
59
|
-
selectable: { type: 'boolean' },
|
|
60
|
-
pageSize: { type: 'number' },
|
|
61
|
-
},
|
|
62
|
-
binding: 'model',
|
|
63
|
-
triggers: ['rowClick', 'select', 'pageChange'],
|
|
64
|
-
container: true,
|
|
65
|
-
accepts: ['column'],
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const columnDef: WidgetDefinitionMeta = {
|
|
69
|
-
name: 'column',
|
|
70
|
-
label: 'Column',
|
|
71
|
-
category: 'layout',
|
|
72
|
-
schema: {
|
|
73
|
-
label: { type: 'string', required: true },
|
|
74
|
-
width: { type: 'string' },
|
|
75
|
-
},
|
|
76
|
-
binding: 'none',
|
|
77
|
-
triggers: [],
|
|
78
|
-
container: true,
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const registry = makeRegistry([buttonDef, inputDef, groupDef, tableDef, columnDef]);
|
|
82
|
-
|
|
83
|
-
describe('Widget Tree Validator', () => {
|
|
84
|
-
test('valid tree produces no errors', () => {
|
|
85
|
-
const nodes: WidgetNode[] = [
|
|
86
|
-
{ type: 'button', props: { label: 'Click me' } },
|
|
87
|
-
{ type: 'input', bind: { field: 'name' } },
|
|
88
|
-
];
|
|
89
|
-
expect(validateWidgetTree(nodes, registry)).toEqual([]);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('rejects unknown widget type', () => {
|
|
93
|
-
const nodes: WidgetNode[] = [{ type: 'unknown_widget' }];
|
|
94
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
95
|
-
expect(errors).toHaveLength(1);
|
|
96
|
-
expect(errors[0].message).toContain("Unknown widget type: 'unknown_widget'");
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('rejects children on non-container', () => {
|
|
100
|
-
const nodes: WidgetNode[] = [
|
|
101
|
-
{ type: 'button', props: { label: 'test' }, children: [{ type: 'input' }] },
|
|
102
|
-
];
|
|
103
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
104
|
-
expect(errors.some((e) => e.message.includes('not a container'))).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('rejects disallowed child types (accepts list)', () => {
|
|
108
|
-
const nodes: WidgetNode[] = [
|
|
109
|
-
{
|
|
110
|
-
type: 'table',
|
|
111
|
-
bind: { model: { name: 'test' } },
|
|
112
|
-
children: [{ type: 'button', props: { label: 'bad' } }],
|
|
113
|
-
},
|
|
114
|
-
];
|
|
115
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
116
|
-
expect(errors.some((e) => e.message.includes('does not accept child type'))).toBe(true);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('allows valid child types', () => {
|
|
120
|
-
const nodes: WidgetNode[] = [
|
|
121
|
-
{
|
|
122
|
-
type: 'table',
|
|
123
|
-
bind: { model: { name: 'test' } },
|
|
124
|
-
children: [{ type: 'column', props: { label: 'Name' } }],
|
|
125
|
-
},
|
|
126
|
-
];
|
|
127
|
-
expect(validateWidgetTree(nodes, registry)).toEqual([]);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test('rejects unknown prop', () => {
|
|
131
|
-
const nodes: WidgetNode[] = [{ type: 'button', props: { label: 'test', unknownProp: true } }];
|
|
132
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
133
|
-
expect(errors.some((e) => e.message.includes("Unknown prop 'unknownProp'"))).toBe(true);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test('rejects invalid prop type', () => {
|
|
137
|
-
const nodes: WidgetNode[] = [{ type: 'button', props: { label: 123 } }];
|
|
138
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
139
|
-
expect(errors.some((e) => e.message.includes("Invalid value for prop 'label'"))).toBe(true);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
test('rejects invalid enum value', () => {
|
|
143
|
-
const nodes: WidgetNode[] = [{ type: 'button', props: { label: 'test', variant: 'invalid' } }];
|
|
144
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
145
|
-
expect(errors.some((e) => e.message.includes("Invalid value for prop 'variant'"))).toBe(true);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test('allows expression strings in props', () => {
|
|
149
|
-
const nodes: WidgetNode[] = [
|
|
150
|
-
{ type: 'button', props: { label: '{{$state.label}}', variant: 'primary' } },
|
|
151
|
-
];
|
|
152
|
-
expect(validateWidgetTree(nodes, registry)).toEqual([]);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('reports missing required prop', () => {
|
|
156
|
-
const nodes: WidgetNode[] = [{ type: 'button', props: { variant: 'primary' } }];
|
|
157
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
158
|
-
expect(errors.some((e) => e.message.includes("Missing required prop 'label'"))).toBe(true);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
test('reports missing required prop when no props at all', () => {
|
|
162
|
-
const nodes: WidgetNode[] = [{ type: 'button' }];
|
|
163
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
164
|
-
expect(errors.some((e) => e.message.includes("Missing required prop 'label'"))).toBe(true);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
test('rejects unknown trigger in on', () => {
|
|
168
|
-
const nodes: WidgetNode[] = [
|
|
169
|
-
{ type: 'button', props: { label: 'test' }, on: { hover: { type: 'navigate', path: '/' } } },
|
|
170
|
-
];
|
|
171
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
172
|
-
expect(errors.some((e) => e.message.includes("Unknown trigger 'hover'"))).toBe(true);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('allows valid trigger', () => {
|
|
176
|
-
const nodes: WidgetNode[] = [
|
|
177
|
-
{ type: 'button', props: { label: 'test' }, on: { click: { type: 'navigate', path: '/' } } },
|
|
178
|
-
];
|
|
179
|
-
expect(validateWidgetTree(nodes, registry)).toEqual([]);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
test('rejects binding on widget that does not support it', () => {
|
|
183
|
-
const nodes: WidgetNode[] = [
|
|
184
|
-
{ type: 'button', props: { label: 'test' }, bind: { field: 'name' } },
|
|
185
|
-
];
|
|
186
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
187
|
-
expect(errors.some((e) => e.message.includes('does not support data binding'))).toBe(true);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
test('rejects wrong binding mode', () => {
|
|
191
|
-
const nodes: WidgetNode[] = [{ type: 'input', bind: { model: { name: 'sales.order' } } }];
|
|
192
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
193
|
-
expect(
|
|
194
|
-
errors.some((e) => e.message.includes("expects binding mode 'field' but got 'model'")),
|
|
195
|
-
).toBe(true);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
test('reports error path correctly for nested widgets', () => {
|
|
199
|
-
const nodes: WidgetNode[] = [
|
|
200
|
-
{
|
|
201
|
-
type: 'group',
|
|
202
|
-
children: [{ type: 'button', props: { label: 'ok' } }, { type: 'unknown_type' }],
|
|
203
|
-
},
|
|
204
|
-
];
|
|
205
|
-
const errors = validateWidgetTree(nodes, registry);
|
|
206
|
-
expect(errors[0].path).toBe('[0].children[1]');
|
|
207
|
-
});
|
|
208
|
-
});
|
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
import type { WidgetAction } from '@rangka/shared';
|
|
2
|
-
import type { WidgetContext } from '../context/types.js';
|
|
3
|
-
import type { StateStore } from '../state/store.js';
|
|
4
|
-
import { parse, evaluate } from '../expression/index.js';
|
|
5
|
-
import { flattenContext } from '../context/types.js';
|
|
6
|
-
|
|
7
|
-
export interface ActionContext {
|
|
8
|
-
widgetContext: WidgetContext;
|
|
9
|
-
state: StateStore;
|
|
10
|
-
response?: Record<string, unknown>;
|
|
11
|
-
selected?: Record<string, unknown>;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ActionHandlers {
|
|
15
|
-
navigate?: (path: string) => void;
|
|
16
|
-
service?: (name: string, params: Record<string, unknown>) => Promise<Record<string, unknown>>;
|
|
17
|
-
refreshSource?: () => void;
|
|
18
|
-
focus?: (field: string) => void;
|
|
19
|
-
fetchOptions?: (field: string, depends: string[]) => void;
|
|
20
|
-
validate?: (fields?: string[]) => Promise<boolean>;
|
|
21
|
-
modelCreate?: (model: string, data: Record<string, unknown>) => Promise<Record<string, unknown>>;
|
|
22
|
-
modelUpdate?: (
|
|
23
|
-
model: string,
|
|
24
|
-
id: string,
|
|
25
|
-
data: Record<string, unknown>,
|
|
26
|
-
) => Promise<Record<string, unknown>>;
|
|
27
|
-
modelDelete?: (model: string, id: string) => Promise<void>;
|
|
28
|
-
modelFetch?: (model: string, id: string) => Promise<Record<string, unknown>>;
|
|
29
|
-
modelList?: (
|
|
30
|
-
model: string,
|
|
31
|
-
filters?: Record<string, unknown>,
|
|
32
|
-
) => Promise<Record<string, unknown>[]>;
|
|
33
|
-
setRecordValue?: (field: string, value: unknown) => void;
|
|
34
|
-
addRow?: (field: string) => void;
|
|
35
|
-
removeRow?: (field: string) => void;
|
|
36
|
-
duplicateRow?: (field: string) => void;
|
|
37
|
-
formSubmit?: () => Promise<void>;
|
|
38
|
-
formReset?: () => void;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function dispatch(
|
|
42
|
-
action: WidgetAction,
|
|
43
|
-
actionCtx: ActionContext,
|
|
44
|
-
handlers: ActionHandlers,
|
|
45
|
-
): Promise<void> {
|
|
46
|
-
switch (action.type) {
|
|
47
|
-
case 'setValue':
|
|
48
|
-
return handleSetValue(action.field, action.value, actionCtx, handlers);
|
|
49
|
-
case 'clearValue':
|
|
50
|
-
return handleSetValue(action.field, null, actionCtx, handlers);
|
|
51
|
-
case 'setValues':
|
|
52
|
-
return handleSetValues(action.values, actionCtx, handlers);
|
|
53
|
-
case 'navigate':
|
|
54
|
-
return handleNavigate(action.path, actionCtx, handlers);
|
|
55
|
-
case 'service':
|
|
56
|
-
return handleService(action, actionCtx, handlers);
|
|
57
|
-
case 'refreshSource':
|
|
58
|
-
handlers.refreshSource?.();
|
|
59
|
-
return;
|
|
60
|
-
case 'focus':
|
|
61
|
-
handlers.focus?.(action.field);
|
|
62
|
-
return;
|
|
63
|
-
case 'fetchOptions':
|
|
64
|
-
handlers.fetchOptions?.(action.field, action.depends);
|
|
65
|
-
return;
|
|
66
|
-
case 'validate':
|
|
67
|
-
await handlers.validate?.(action.fields);
|
|
68
|
-
return;
|
|
69
|
-
case 'model.create':
|
|
70
|
-
return handleModelCreate(action, actionCtx, handlers);
|
|
71
|
-
case 'model.update':
|
|
72
|
-
return handleModelUpdate(action, actionCtx, handlers);
|
|
73
|
-
case 'model.delete':
|
|
74
|
-
return handleModelDelete(action, actionCtx, handlers);
|
|
75
|
-
case 'model.fetch':
|
|
76
|
-
return handleModelFetch(action, actionCtx, handlers);
|
|
77
|
-
case 'model.list':
|
|
78
|
-
return handleModelList(action, actionCtx, handlers);
|
|
79
|
-
case 'addRow':
|
|
80
|
-
handlers.addRow?.(action.field);
|
|
81
|
-
return;
|
|
82
|
-
case 'removeRow':
|
|
83
|
-
handlers.removeRow?.(action.field);
|
|
84
|
-
return;
|
|
85
|
-
case 'duplicateRow':
|
|
86
|
-
handlers.duplicateRow?.(action.field);
|
|
87
|
-
return;
|
|
88
|
-
case 'form.submit':
|
|
89
|
-
await handlers.formSubmit?.();
|
|
90
|
-
return;
|
|
91
|
-
case 'form.reset':
|
|
92
|
-
handlers.formReset?.();
|
|
93
|
-
return;
|
|
94
|
-
case 'sequence':
|
|
95
|
-
return handleSequence(action.actions, actionCtx, handlers);
|
|
96
|
-
case 'conditional':
|
|
97
|
-
return handleConditional(action, actionCtx, handlers);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function handleSetValue(
|
|
102
|
-
field: string,
|
|
103
|
-
value: unknown,
|
|
104
|
-
actionCtx: ActionContext,
|
|
105
|
-
handlers: ActionHandlers,
|
|
106
|
-
): void {
|
|
107
|
-
const resolved = resolveExpressionValue(value, actionCtx);
|
|
108
|
-
|
|
109
|
-
if (field.startsWith('$state.')) {
|
|
110
|
-
const key = field.slice(7);
|
|
111
|
-
actionCtx.state.set(key, resolved);
|
|
112
|
-
} else if (
|
|
113
|
-
field.startsWith('$filter.') ||
|
|
114
|
-
field.startsWith('$sort.') ||
|
|
115
|
-
field.startsWith('$page.')
|
|
116
|
-
) {
|
|
117
|
-
// Reactive query variables are stored with their full key (including prefix)
|
|
118
|
-
actionCtx.state.set(field, resolved);
|
|
119
|
-
} else if (field.startsWith('$parent.')) {
|
|
120
|
-
const realField = field.slice(8);
|
|
121
|
-
handlers.setRecordValue?.(realField, resolved);
|
|
122
|
-
} else {
|
|
123
|
-
actionCtx.widgetContext.record[field] = resolved;
|
|
124
|
-
handlers.setRecordValue?.(field, resolved);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function handleSetValues(
|
|
129
|
-
values: Record<string, unknown>,
|
|
130
|
-
actionCtx: ActionContext,
|
|
131
|
-
handlers: ActionHandlers,
|
|
132
|
-
): void {
|
|
133
|
-
const stateUpdates: Record<string, unknown> = {};
|
|
134
|
-
|
|
135
|
-
for (const [field, value] of Object.entries(values)) {
|
|
136
|
-
const resolved = resolveExpressionValue(value, actionCtx);
|
|
137
|
-
if (field.startsWith('$state.')) {
|
|
138
|
-
stateUpdates[field.slice(7)] = resolved;
|
|
139
|
-
} else if (
|
|
140
|
-
field.startsWith('$filter.') ||
|
|
141
|
-
field.startsWith('$sort.') ||
|
|
142
|
-
field.startsWith('$page.')
|
|
143
|
-
) {
|
|
144
|
-
// Reactive query variables stored with full key
|
|
145
|
-
stateUpdates[field] = resolved;
|
|
146
|
-
} else {
|
|
147
|
-
handlers.setRecordValue?.(field, resolved);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (Object.keys(stateUpdates).length > 0) {
|
|
152
|
-
actionCtx.state.setMany(stateUpdates);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function handleNavigate(path: string, actionCtx: ActionContext, handlers: ActionHandlers): void {
|
|
157
|
-
const resolved = String(resolveExpressionValue(path, actionCtx) ?? path);
|
|
158
|
-
handlers.navigate?.(resolved);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function handleService(
|
|
162
|
-
action: {
|
|
163
|
-
type: 'service';
|
|
164
|
-
name: string;
|
|
165
|
-
params?: Record<string, unknown>;
|
|
166
|
-
context?: string;
|
|
167
|
-
onSuccess?: WidgetAction;
|
|
168
|
-
onError?: WidgetAction;
|
|
169
|
-
},
|
|
170
|
-
actionCtx: ActionContext,
|
|
171
|
-
handlers: ActionHandlers,
|
|
172
|
-
): Promise<void> {
|
|
173
|
-
const params: Record<string, unknown> = {};
|
|
174
|
-
if (action.params) {
|
|
175
|
-
for (const [key, value] of Object.entries(action.params)) {
|
|
176
|
-
params[key] = resolveExpressionValue(value, actionCtx);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
const response = await handlers.service?.(action.name, params);
|
|
182
|
-
if (action.onSuccess) {
|
|
183
|
-
const successCtx: ActionContext = {
|
|
184
|
-
...actionCtx,
|
|
185
|
-
response: response ?? {},
|
|
186
|
-
};
|
|
187
|
-
await dispatch(action.onSuccess, successCtx, handlers);
|
|
188
|
-
} else {
|
|
189
|
-
handlers.refreshSource?.();
|
|
190
|
-
}
|
|
191
|
-
} catch (err) {
|
|
192
|
-
if (action.onError) {
|
|
193
|
-
const errorCtx: ActionContext = {
|
|
194
|
-
...actionCtx,
|
|
195
|
-
response: err instanceof Error ? { message: err.message } : { message: String(err) },
|
|
196
|
-
};
|
|
197
|
-
await dispatch(action.onError, errorCtx, handlers);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async function handleModelCreate(
|
|
203
|
-
action: { type: 'model.create'; model: string; data: Record<string, unknown> },
|
|
204
|
-
actionCtx: ActionContext,
|
|
205
|
-
handlers: ActionHandlers,
|
|
206
|
-
): Promise<void> {
|
|
207
|
-
const data: Record<string, unknown> = {};
|
|
208
|
-
for (const [key, value] of Object.entries(action.data)) {
|
|
209
|
-
data[key] = resolveExpressionValue(value, actionCtx);
|
|
210
|
-
}
|
|
211
|
-
await handlers.modelCreate?.(action.model, data);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async function handleModelUpdate(
|
|
215
|
-
action: { type: 'model.update'; model?: string; id?: string; data: Record<string, unknown> },
|
|
216
|
-
actionCtx: ActionContext,
|
|
217
|
-
handlers: ActionHandlers,
|
|
218
|
-
): Promise<void> {
|
|
219
|
-
const model = action.model ?? actionCtx.widgetContext.model;
|
|
220
|
-
const id = action.id
|
|
221
|
-
? String(resolveExpressionValue(action.id, actionCtx))
|
|
222
|
-
: String(actionCtx.widgetContext.record['id'] ?? '');
|
|
223
|
-
const data: Record<string, unknown> = {};
|
|
224
|
-
for (const [key, value] of Object.entries(action.data)) {
|
|
225
|
-
data[key] = resolveExpressionValue(value, actionCtx);
|
|
226
|
-
}
|
|
227
|
-
await handlers.modelUpdate?.(model, id, data);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async function handleModelDelete(
|
|
231
|
-
action: { type: 'model.delete'; model?: string; id?: string },
|
|
232
|
-
actionCtx: ActionContext,
|
|
233
|
-
handlers: ActionHandlers,
|
|
234
|
-
): Promise<void> {
|
|
235
|
-
const model = action.model ?? actionCtx.widgetContext.model;
|
|
236
|
-
const id = action.id
|
|
237
|
-
? String(resolveExpressionValue(action.id, actionCtx))
|
|
238
|
-
: String(actionCtx.widgetContext.record['id'] ?? '');
|
|
239
|
-
await handlers.modelDelete?.(model, id);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async function handleModelFetch(
|
|
243
|
-
action: { type: 'model.fetch'; model: string; id: string; into: string },
|
|
244
|
-
actionCtx: ActionContext,
|
|
245
|
-
handlers: ActionHandlers,
|
|
246
|
-
): Promise<void> {
|
|
247
|
-
const id = String(resolveExpressionValue(action.id, actionCtx));
|
|
248
|
-
const result = await handlers.modelFetch?.(action.model, id);
|
|
249
|
-
if (result && action.into === '$record') {
|
|
250
|
-
for (const [key, value] of Object.entries(result)) {
|
|
251
|
-
handlers.setRecordValue?.(key, value);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async function handleModelList(
|
|
257
|
-
action: { type: 'model.list'; model: string; filters?: Record<string, unknown>; into: string },
|
|
258
|
-
actionCtx: ActionContext,
|
|
259
|
-
handlers: ActionHandlers,
|
|
260
|
-
): Promise<void> {
|
|
261
|
-
const filters: Record<string, unknown> = {};
|
|
262
|
-
if (action.filters) {
|
|
263
|
-
for (const [key, value] of Object.entries(action.filters)) {
|
|
264
|
-
filters[key] = resolveExpressionValue(value, actionCtx);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
await handlers.modelList?.(action.model, filters);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
async function handleSequence(
|
|
271
|
-
actions: WidgetAction[],
|
|
272
|
-
actionCtx: ActionContext,
|
|
273
|
-
handlers: ActionHandlers,
|
|
274
|
-
): Promise<void> {
|
|
275
|
-
for (const action of actions) {
|
|
276
|
-
await dispatch(action, actionCtx, handlers);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
async function handleConditional(
|
|
281
|
-
action: {
|
|
282
|
-
type: 'conditional';
|
|
283
|
-
condition: { field: string; operator: string; value?: unknown };
|
|
284
|
-
then: WidgetAction;
|
|
285
|
-
else?: WidgetAction;
|
|
286
|
-
},
|
|
287
|
-
actionCtx: ActionContext,
|
|
288
|
-
handlers: ActionHandlers,
|
|
289
|
-
): Promise<void> {
|
|
290
|
-
const { evaluateCondition } = await import('../condition/evaluator.js');
|
|
291
|
-
const flat = flattenContext(actionCtx.widgetContext);
|
|
292
|
-
const merged = {
|
|
293
|
-
...flat,
|
|
294
|
-
$state: Object.fromEntries(actionCtx.state.keys().map((k) => [k, actionCtx.state.get(k)])),
|
|
295
|
-
};
|
|
296
|
-
const result = evaluateCondition(
|
|
297
|
-
action.condition as Parameters<typeof evaluateCondition>[0],
|
|
298
|
-
merged,
|
|
299
|
-
);
|
|
300
|
-
if (result) {
|
|
301
|
-
await dispatch(action.then, actionCtx, handlers);
|
|
302
|
-
} else if (action.else) {
|
|
303
|
-
await dispatch(action.else, actionCtx, handlers);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export function resolveExpressionValue(value: unknown, actionCtx: ActionContext): unknown {
|
|
308
|
-
if (typeof value !== 'string') return value;
|
|
309
|
-
if (!value.includes('{{')) return value;
|
|
310
|
-
|
|
311
|
-
const flat = flattenContext(actionCtx.widgetContext);
|
|
312
|
-
const merged: Record<string, unknown> = {
|
|
313
|
-
...flat,
|
|
314
|
-
$state: Object.fromEntries(actionCtx.state.keys().map((k) => [k, actionCtx.state.get(k)])),
|
|
315
|
-
};
|
|
316
|
-
if (actionCtx.response) {
|
|
317
|
-
merged['$response'] = actionCtx.response;
|
|
318
|
-
}
|
|
319
|
-
if (actionCtx.selected) {
|
|
320
|
-
merged['$selected'] = actionCtx.selected;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const trimmed = value.trim();
|
|
324
|
-
if (trimmed.startsWith('{{') && trimmed.endsWith('}}') && !trimmed.slice(2, -2).includes('{{')) {
|
|
325
|
-
const ast = parse(trimmed);
|
|
326
|
-
return evaluate(ast, merged);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return value.replace(/\{\{(.+?)\}\}/g, (_match, expr: string) => {
|
|
330
|
-
const ast = parse(expr.trim());
|
|
331
|
-
const result = evaluate(ast, merged);
|
|
332
|
-
return String(result ?? '');
|
|
333
|
-
});
|
|
334
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { WidgetBinding } from '@rangka/shared';
|
|
2
|
-
import type { WidgetContext } from '../context/types.js';
|
|
3
|
-
import type { StateStore } from '../state/store.js';
|
|
4
|
-
import { parse, evaluate } from '../expression/index.js';
|
|
5
|
-
import { flattenContext } from '../context/types.js';
|
|
6
|
-
|
|
7
|
-
export interface FieldMeta {
|
|
8
|
-
type: string;
|
|
9
|
-
label: string;
|
|
10
|
-
required: boolean;
|
|
11
|
-
options?: unknown[];
|
|
12
|
-
readOnly: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface BindingResult {
|
|
16
|
-
value: unknown;
|
|
17
|
-
setValue?: (val: unknown) => void;
|
|
18
|
-
meta?: FieldMeta;
|
|
19
|
-
error?: string;
|
|
20
|
-
query?: { name: string; filters?: Record<string, unknown>; limit?: number };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function resolveBinding(
|
|
24
|
-
bind: WidgetBinding | undefined,
|
|
25
|
-
context: WidgetContext,
|
|
26
|
-
fieldMeta?: Record<string, FieldMeta>,
|
|
27
|
-
setValue?: (field: string, val: unknown) => void,
|
|
28
|
-
state?: StateStore,
|
|
29
|
-
): BindingResult | null {
|
|
30
|
-
if (!bind) return null;
|
|
31
|
-
|
|
32
|
-
if (bind.field) {
|
|
33
|
-
const value = context.record[bind.field];
|
|
34
|
-
const meta = fieldMeta?.[bind.field];
|
|
35
|
-
return {
|
|
36
|
-
value,
|
|
37
|
-
setValue: setValue ? (val: unknown) => setValue(bind.field!, val) : undefined,
|
|
38
|
-
meta,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (bind.expression) {
|
|
43
|
-
const flat = flattenContext(context);
|
|
44
|
-
const merged: Record<string, unknown> = { ...flat };
|
|
45
|
-
if (state) {
|
|
46
|
-
merged['$state'] = Object.fromEntries(state.keys().map((k) => [k, state.get(k)]));
|
|
47
|
-
}
|
|
48
|
-
const ast = parse(bind.expression);
|
|
49
|
-
const value = evaluate(ast, merged);
|
|
50
|
-
return { value };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (bind.model) {
|
|
54
|
-
return {
|
|
55
|
-
value: null,
|
|
56
|
-
query: bind.model,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return null;
|
|
61
|
-
}
|