@arkcit/engine 0.3.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/README.md +68 -0
- package/dist/UIEngine.d.ts +6 -0
- package/dist/UIEngine.js +3235 -0
- package/dist/bindings.d.ts +8 -0
- package/dist/bindings.js +146 -0
- package/dist/components/index.d.ts +17 -0
- package/dist/components/index.js +143 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +9 -0
- package/dist/form/engineFormValidation.d.ts +2 -0
- package/dist/form/engineFormValidation.js +241 -0
- package/dist/form.d.ts +67 -0
- package/dist/form.js +241 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +3336 -0
- package/dist/layout.d.ts +6 -0
- package/dist/layout.js +82 -0
- package/dist/preview/index.d.ts +63 -0
- package/dist/preview/index.js +1514 -0
- package/dist/react-web/index.d.ts +8 -0
- package/dist/react-web/index.js +3246 -0
- package/dist/registry.d.ts +2 -0
- package/dist/registry.js +0 -0
- package/dist/render-layer/index.d.ts +1 -0
- package/dist/render-layer/index.js +13 -0
- package/dist/renderStudioForm-CPQEzvT7.d.ts +75 -0
- package/dist/renderers/index.d.ts +8 -0
- package/dist/renderers/index.js +1570 -0
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +0 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +0 -0
- package/dist/studio-bridge/index.d.ts +501 -0
- package/dist/studio-bridge/index.js +2840 -0
- package/dist/studioProps.d.ts +8 -0
- package/dist/studioProps.js +97 -0
- package/dist/types-9TZ2lQDP.d.ts +60 -0
- package/dist/types-CyAE6ZLH.d.ts +19 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +0 -0
- package/dist/wizardEditingHandlers-D50tR-6n.d.ts +163 -0
- package/package.json +160 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UINode } from '@arkcit/engine-schema';
|
|
2
|
+
import { UIRuntime } from '@arkcit/engine-runtime';
|
|
3
|
+
|
|
4
|
+
declare const resolveUIValue: (value: unknown, runtime: UIRuntime) => unknown;
|
|
5
|
+
declare const resolveNodeProps: (props: Record<string, unknown> | undefined, runtime: UIRuntime) => Record<string, unknown>;
|
|
6
|
+
declare const bindNodeEvents: (events: UINode["events"], runtime: UIRuntime) => Partial<Record<keyof NonNullable<UINode["events"]>, (...args: unknown[]) => void>>;
|
|
7
|
+
|
|
8
|
+
export { bindNodeEvents, resolveNodeProps, resolveUIValue };
|
package/dist/bindings.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/runtime/bindings.ts
|
|
22
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
23
|
+
var MISSING_TRANSLATION_FALLBACK = "Lorem ipsum dolor sit amet. (fallback)";
|
|
24
|
+
var MISSING_REF_FALLBACK = "Lorem ipsum dolor sit amet. (fallback)";
|
|
25
|
+
var isPlainObject = (value) => {
|
|
26
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
27
|
+
const prototype = Object.getPrototypeOf(value);
|
|
28
|
+
return prototype === Object.prototype || prototype === null;
|
|
29
|
+
};
|
|
30
|
+
var isReactElementLike = (value) => isPlainObject(value) && typeof value.$$typeof === "symbol" && "props" in value;
|
|
31
|
+
var isTranslationValue = (value) => isPlainObject(value) && typeof value.$t === "string";
|
|
32
|
+
var isRefValue = (value) => isPlainObject(value) && typeof value.$ref === "string";
|
|
33
|
+
var isExprValue = (value) => isPlainObject(value) && typeof value.$expr === "string";
|
|
34
|
+
var parseStructuredString = (value) => {
|
|
35
|
+
if (typeof value !== "string") return value;
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return value;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(trimmed);
|
|
40
|
+
if (!isPlainObject(parsed)) return value;
|
|
41
|
+
if ((typeof parsed.$ref === "string" || typeof parsed.$t === "string") && Object.keys(parsed).length >= 1) {
|
|
42
|
+
return parsed;
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
} catch (e) {
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var resolveUIValue = (value, runtime) => {
|
|
50
|
+
const normalizedValue = parseStructuredString(value);
|
|
51
|
+
if (isTranslationValue(normalizedValue)) {
|
|
52
|
+
const fallback = typeof normalizedValue.defaultValue === "string" && normalizedValue.defaultValue.trim() ? normalizedValue.defaultValue : normalizedValue.$t;
|
|
53
|
+
if (!runtime.t) return fallback || MISSING_TRANSLATION_FALLBACK;
|
|
54
|
+
const translated = runtime.t(normalizedValue.$t, normalizedValue.values);
|
|
55
|
+
if (typeof translated !== "string") return fallback || MISSING_TRANSLATION_FALLBACK;
|
|
56
|
+
if (!translated.trim()) return fallback || MISSING_TRANSLATION_FALLBACK;
|
|
57
|
+
if (translated === normalizedValue.$t) return fallback || MISSING_TRANSLATION_FALLBACK;
|
|
58
|
+
return translated;
|
|
59
|
+
}
|
|
60
|
+
if (isRefValue(normalizedValue)) {
|
|
61
|
+
const resolved = runtime.get(normalizedValue.$ref);
|
|
62
|
+
if (resolved == null) return MISSING_REF_FALLBACK;
|
|
63
|
+
if (typeof resolved === "string" && !resolved.trim()) return MISSING_REF_FALLBACK;
|
|
64
|
+
return resolved;
|
|
65
|
+
}
|
|
66
|
+
if (isExprValue(normalizedValue)) {
|
|
67
|
+
return void 0;
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(normalizedValue)) {
|
|
70
|
+
return normalizedValue.map((item) => resolveUIValue(item, runtime));
|
|
71
|
+
}
|
|
72
|
+
if (isReactElementLike(normalizedValue)) {
|
|
73
|
+
return normalizedValue;
|
|
74
|
+
}
|
|
75
|
+
if (isPlainObject(normalizedValue)) {
|
|
76
|
+
const next = {};
|
|
77
|
+
Object.entries(normalizedValue).forEach(([key, childValue]) => {
|
|
78
|
+
if (DANGEROUS_KEYS.has(key)) return;
|
|
79
|
+
next[key] = resolveUIValue(childValue, runtime);
|
|
80
|
+
});
|
|
81
|
+
return next;
|
|
82
|
+
}
|
|
83
|
+
return normalizedValue;
|
|
84
|
+
};
|
|
85
|
+
var resolveNodeProps = (props, runtime) => {
|
|
86
|
+
if (!props) return {};
|
|
87
|
+
const resolved = {};
|
|
88
|
+
Object.entries(props).forEach(([key, value]) => {
|
|
89
|
+
if (DANGEROUS_KEYS.has(key)) return;
|
|
90
|
+
resolved[key] = resolveUIValue(value, runtime);
|
|
91
|
+
});
|
|
92
|
+
return resolved;
|
|
93
|
+
};
|
|
94
|
+
var bindNodeEvents = (events, runtime) => {
|
|
95
|
+
if (!events) return {};
|
|
96
|
+
const handlers = {};
|
|
97
|
+
const extractDynamicPayload = (eventName, args) => {
|
|
98
|
+
var _a;
|
|
99
|
+
if (eventName !== "onChange") return void 0;
|
|
100
|
+
const firstArg = args[0];
|
|
101
|
+
if (!firstArg || typeof firstArg !== "object" || !("target" in firstArg)) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
const target = firstArg.target;
|
|
105
|
+
if (!target) return void 0;
|
|
106
|
+
const isCheckboxType = String((_a = target.type) != null ? _a : "").toLowerCase() === "checkbox";
|
|
107
|
+
const query = isCheckboxType ? target.checked : target.value;
|
|
108
|
+
return {
|
|
109
|
+
query,
|
|
110
|
+
value: target.value,
|
|
111
|
+
checked: target.checked
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
Object.entries(events).forEach(([eventName, action]) => {
|
|
115
|
+
if (!action) return;
|
|
116
|
+
handlers[eventName] = (...args) => {
|
|
117
|
+
const actionRef = action;
|
|
118
|
+
const dynamicPayload = extractDynamicPayload(eventName, args);
|
|
119
|
+
if (!dynamicPayload) {
|
|
120
|
+
runtime.dispatch(actionRef);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if ("id" in actionRef) {
|
|
124
|
+
const basePayload = actionRef.payload && typeof actionRef.payload === "object" ? actionRef.payload : {};
|
|
125
|
+
runtime.dispatch(__spreadProps(__spreadValues({}, actionRef), {
|
|
126
|
+
payload: __spreadValues(__spreadValues({}, basePayload), dynamicPayload)
|
|
127
|
+
}));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if ("type" in actionRef) {
|
|
131
|
+
const basePayload = actionRef.payload && typeof actionRef.payload === "object" ? actionRef.payload : {};
|
|
132
|
+
runtime.dispatch(__spreadProps(__spreadValues({}, actionRef), {
|
|
133
|
+
payload: __spreadValues(__spreadValues({}, basePayload), dynamicPayload)
|
|
134
|
+
}));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
runtime.dispatch(actionRef);
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
return handlers;
|
|
141
|
+
};
|
|
142
|
+
export {
|
|
143
|
+
bindNodeEvents,
|
|
144
|
+
resolveNodeProps,
|
|
145
|
+
resolveUIValue
|
|
146
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { EngineWarningFallback, NodeErrorBoundary } from '@arkcit/engine-react';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import { a as UIInlineEditorState } from '../types-CyAE6ZLH.js';
|
|
4
|
+
import { UINodeWrapperProps } from '@arkcit/engine-core';
|
|
5
|
+
import 'react';
|
|
6
|
+
|
|
7
|
+
type InlineTextEditorProps = {
|
|
8
|
+
editing: UIInlineEditorState;
|
|
9
|
+
onChange: (value: string) => void;
|
|
10
|
+
onCancel: () => void;
|
|
11
|
+
onCommit: () => void;
|
|
12
|
+
};
|
|
13
|
+
declare const InlineTextEditor: ({ editing, onChange, onCancel, onCommit, }: InlineTextEditorProps) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
declare const StudioNodeWrapper: ({ nodeId, wrapperClassName, cursorClassName, isSelected, allowResizeHandle, style, onClick, onDoubleClick, onTouchStart, onTouchMove, onTouchEnd, showResizeHandle, onResizeMouseDown, isInlineEditing, preserveContentWhileInlineEditing, children, inlineEditor, }: UINodeWrapperProps) => react_jsx_runtime.JSX.Element;
|
|
16
|
+
|
|
17
|
+
export { InlineTextEditor, StudioNodeWrapper };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/react-web/engine/EngineWarningFallback.tsx
|
|
19
|
+
import { EngineWarningFallback } from "@arkcit/engine-react";
|
|
20
|
+
|
|
21
|
+
// src/react-web/engine/NodeErrorBoundary.tsx
|
|
22
|
+
import { NodeErrorBoundary } from "@arkcit/engine-react";
|
|
23
|
+
|
|
24
|
+
// src/studio-bridge/inline/InlineTextEditor.tsx
|
|
25
|
+
import { jsx } from "react/jsx-runtime";
|
|
26
|
+
var InlineTextEditor = ({
|
|
27
|
+
editing,
|
|
28
|
+
onChange,
|
|
29
|
+
onCancel,
|
|
30
|
+
onCommit
|
|
31
|
+
}) => editing.multiline ? /* @__PURE__ */ jsx(
|
|
32
|
+
"textarea",
|
|
33
|
+
{
|
|
34
|
+
className: "absolute z-[260] h-full w-full resize-none rounded-md border border-primary/70 bg-background/95 px-2 py-1 text-sm text-foreground outline-none ring-0 cursor-text shadow-lg",
|
|
35
|
+
style: editing.editorStyle,
|
|
36
|
+
value: editing.value,
|
|
37
|
+
autoFocus: true,
|
|
38
|
+
"aria-label": "Inline text editor",
|
|
39
|
+
onChange: (event) => onChange(event.target.value),
|
|
40
|
+
onBlur: onCommit,
|
|
41
|
+
onKeyDown: (event) => {
|
|
42
|
+
if (event.key === "Escape") {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
onCancel();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
onCommit();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
) : /* @__PURE__ */ jsx(
|
|
54
|
+
"input",
|
|
55
|
+
{
|
|
56
|
+
className: "absolute z-[260] h-full w-full rounded-md border border-primary/70 bg-background/95 px-2 text-sm text-foreground outline-none ring-0 cursor-text shadow-lg",
|
|
57
|
+
style: editing.editorStyle,
|
|
58
|
+
value: editing.value,
|
|
59
|
+
autoFocus: true,
|
|
60
|
+
"aria-label": "Inline text editor",
|
|
61
|
+
onChange: (event) => onChange(event.target.value),
|
|
62
|
+
onBlur: onCommit,
|
|
63
|
+
onKeyDown: (event) => {
|
|
64
|
+
if (event.key === "Escape") {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
onCancel();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (event.key === "Enter") {
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
onCommit();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// src/studio-bridge/nodes/StudioNodeWrapper.tsx
|
|
78
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
79
|
+
var StudioNodeWrapper = ({
|
|
80
|
+
nodeId,
|
|
81
|
+
wrapperClassName = "",
|
|
82
|
+
cursorClassName = "",
|
|
83
|
+
isSelected,
|
|
84
|
+
allowResizeHandle = true,
|
|
85
|
+
style,
|
|
86
|
+
onClick,
|
|
87
|
+
onDoubleClick,
|
|
88
|
+
onTouchStart,
|
|
89
|
+
onTouchMove,
|
|
90
|
+
onTouchEnd,
|
|
91
|
+
showResizeHandle,
|
|
92
|
+
onResizeMouseDown,
|
|
93
|
+
isInlineEditing,
|
|
94
|
+
preserveContentWhileInlineEditing = false,
|
|
95
|
+
children,
|
|
96
|
+
inlineEditor
|
|
97
|
+
}) => /* @__PURE__ */ jsxs(
|
|
98
|
+
"div",
|
|
99
|
+
{
|
|
100
|
+
"data-node-id": nodeId,
|
|
101
|
+
className: [
|
|
102
|
+
"relative block w-fit min-w-0 max-w-full rounded-md border border-transparent transition-colors",
|
|
103
|
+
wrapperClassName,
|
|
104
|
+
cursorClassName,
|
|
105
|
+
isSelected ? "border-dashed border-primary/60 bg-primary/5" : ""
|
|
106
|
+
].filter(Boolean).join(" "),
|
|
107
|
+
style: __spreadValues({
|
|
108
|
+
position: "relative"
|
|
109
|
+
}, style != null ? style : {}),
|
|
110
|
+
onTouchStart,
|
|
111
|
+
onTouchMove,
|
|
112
|
+
onTouchEnd,
|
|
113
|
+
onClick,
|
|
114
|
+
onDoubleClick,
|
|
115
|
+
children: [
|
|
116
|
+
allowResizeHandle && showResizeHandle && onResizeMouseDown ? /* @__PURE__ */ jsx2(
|
|
117
|
+
"button",
|
|
118
|
+
{
|
|
119
|
+
type: "button",
|
|
120
|
+
className: "absolute -right-2 -top-2 z-20 inline-flex h-7 w-7 cursor-nwse-resize items-center justify-center rounded-md border border-primary bg-primary text-xs text-primary-foreground shadow-sm transition-colors hover:bg-primary-hover hover:border-primary-hover",
|
|
121
|
+
"aria-label": "Resize node",
|
|
122
|
+
onMouseDown: onResizeMouseDown,
|
|
123
|
+
children: "\u2922"
|
|
124
|
+
}
|
|
125
|
+
) : null,
|
|
126
|
+
/* @__PURE__ */ jsx2(
|
|
127
|
+
"div",
|
|
128
|
+
{
|
|
129
|
+
className: isInlineEditing && !preserveContentWhileInlineEditing ? "pointer-events-none opacity-0" : void 0,
|
|
130
|
+
children
|
|
131
|
+
}
|
|
132
|
+
),
|
|
133
|
+
isInlineEditing ? inlineEditor : null
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
nodeId
|
|
137
|
+
);
|
|
138
|
+
export {
|
|
139
|
+
EngineWarningFallback,
|
|
140
|
+
InlineTextEditor,
|
|
141
|
+
NodeErrorBoundary,
|
|
142
|
+
StudioNodeWrapper
|
|
143
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ComponentRegistry, ComponentRegistryEntry, ReactWebRendererProps, ResolvedNode, ResolvedNodeBase, ResolvedNodeChildDescriptor, ResolvedNodeContentDescriptor, RuntimeRenderInput, SchemaRenderInput, StudioAuthoringProps, UIEngineProps, UINodeDropPreview, UINodeDropState, UINodeDropTarget, UINodeSize, UINodeWrapperProps, createResolvedNode, createResolvedNodeBase } from '@arkcit/engine-core';
|
|
2
|
+
import '@arkcit/engine-render-layer';
|
|
3
|
+
import '@arkcit/engine-runtime';
|
|
4
|
+
import '@arkcit/engine-schema';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { D as DeclarativeValidatorConfig, a as DeclarativeValidatorType, F as FormErrors, b as FormState, c as FormTouched, d as FormValues, V as ValidationIssue, createFormState, defaultDeclarativeValidatorRegistry, hasDeclarativeValidators, resolveBindableText, resolveIssueMessage, validateFormValues } from '../form.js';
|
|
2
|
+
import '../types-9TZ2lQDP.js';
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/form/engineFormValidation.ts
|
|
19
|
+
var EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
20
|
+
var isEmptyValue = (value) => {
|
|
21
|
+
if (value == null) return true;
|
|
22
|
+
if (typeof value === "string") return value.trim().length === 0;
|
|
23
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
24
|
+
return false;
|
|
25
|
+
};
|
|
26
|
+
var toNumber = (value) => {
|
|
27
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
const parsed = Number(value);
|
|
30
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
};
|
|
34
|
+
var getPathValue = (source, ref) => {
|
|
35
|
+
const normalizedRef = ref.replace(/^@/, "").trim();
|
|
36
|
+
if (!normalizedRef) return void 0;
|
|
37
|
+
return normalizedRef.split(".").filter(Boolean).reduce((accumulator, segment) => {
|
|
38
|
+
if (!accumulator || typeof accumulator !== "object") return void 0;
|
|
39
|
+
return accumulator[segment];
|
|
40
|
+
}, source);
|
|
41
|
+
};
|
|
42
|
+
var resolveBindableText = (value, options) => {
|
|
43
|
+
var _a;
|
|
44
|
+
if (typeof value === "string") {
|
|
45
|
+
const trimmed = value.trim();
|
|
46
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
47
|
+
try {
|
|
48
|
+
const parsed = JSON.parse(trimmed);
|
|
49
|
+
return resolveBindableText(parsed, options);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
57
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return "";
|
|
58
|
+
const candidate = value;
|
|
59
|
+
if (typeof candidate.$t === "string") {
|
|
60
|
+
const translated = (_a = options == null ? void 0 : options.t) == null ? void 0 : _a.call(
|
|
61
|
+
options,
|
|
62
|
+
candidate.$t,
|
|
63
|
+
candidate.values && typeof candidate.values === "object" ? candidate.values : void 0
|
|
64
|
+
);
|
|
65
|
+
if (typeof translated === "string" && translated.trim().length > 0) return translated;
|
|
66
|
+
return candidate.$t;
|
|
67
|
+
}
|
|
68
|
+
if (typeof candidate.$ref === "string" && (options == null ? void 0 : options.values)) {
|
|
69
|
+
const resolved = getPathValue(options.values, candidate.$ref);
|
|
70
|
+
if (resolved == null) return "";
|
|
71
|
+
return String(resolved);
|
|
72
|
+
}
|
|
73
|
+
return "";
|
|
74
|
+
};
|
|
75
|
+
var defaultDeclarativeValidatorRegistry = {
|
|
76
|
+
required: (value) => isEmptyValue(value) ? { code: "required" } : null,
|
|
77
|
+
minLength: (value, params) => {
|
|
78
|
+
var _a;
|
|
79
|
+
if (isEmptyValue(value)) return null;
|
|
80
|
+
const min = toNumber((_a = params == null ? void 0 : params.min) != null ? _a : params == null ? void 0 : params.value);
|
|
81
|
+
if (min == null) return null;
|
|
82
|
+
return String(value).length < min ? { code: "minLength", params: { min } } : null;
|
|
83
|
+
},
|
|
84
|
+
maxLength: (value, params) => {
|
|
85
|
+
var _a;
|
|
86
|
+
if (isEmptyValue(value)) return null;
|
|
87
|
+
const max = toNumber((_a = params == null ? void 0 : params.max) != null ? _a : params == null ? void 0 : params.value);
|
|
88
|
+
if (max == null) return null;
|
|
89
|
+
return String(value).length > max ? { code: "maxLength", params: { max } } : null;
|
|
90
|
+
},
|
|
91
|
+
email: (value) => {
|
|
92
|
+
if (isEmptyValue(value)) return null;
|
|
93
|
+
return EMAIL_REGEX.test(String(value)) ? null : { code: "email" };
|
|
94
|
+
},
|
|
95
|
+
pattern: (value, params) => {
|
|
96
|
+
var _a, _b;
|
|
97
|
+
if (isEmptyValue(value)) return null;
|
|
98
|
+
const rawPattern = String((_b = (_a = params == null ? void 0 : params.pattern) != null ? _a : params == null ? void 0 : params.value) != null ? _b : "").trim();
|
|
99
|
+
if (!rawPattern) return null;
|
|
100
|
+
try {
|
|
101
|
+
const regex = new RegExp(rawPattern);
|
|
102
|
+
return regex.test(String(value)) ? null : { code: "pattern", params: { pattern: rawPattern } };
|
|
103
|
+
} catch (e) {
|
|
104
|
+
return { code: "pattern" };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var buildLegacyValidators = (field) => {
|
|
109
|
+
var _a, _b, _c;
|
|
110
|
+
const validators = [];
|
|
111
|
+
if (field.required) {
|
|
112
|
+
validators.push({
|
|
113
|
+
id: `${String((_a = field.name) != null ? _a : "field")}:required`,
|
|
114
|
+
type: "required",
|
|
115
|
+
stopOnFailure: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (String((_b = field.pattern) != null ? _b : "").trim()) {
|
|
119
|
+
validators.push({
|
|
120
|
+
id: `${String((_c = field.name) != null ? _c : "field")}:pattern`,
|
|
121
|
+
type: "pattern",
|
|
122
|
+
params: { pattern: String(field.pattern) }
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return validators;
|
|
126
|
+
};
|
|
127
|
+
var normalizeValidators = (field, options) => {
|
|
128
|
+
const explicit = Array.isArray(field.validators) ? field.validators : [];
|
|
129
|
+
if (explicit.length > 0) return explicit;
|
|
130
|
+
if (!(options == null ? void 0 : options.useLegacyFieldConstraints)) return [];
|
|
131
|
+
return buildLegacyValidators(field);
|
|
132
|
+
};
|
|
133
|
+
var toIssueArray = (issue) => {
|
|
134
|
+
if (!issue) return [];
|
|
135
|
+
return Array.isArray(issue) ? issue : [issue];
|
|
136
|
+
};
|
|
137
|
+
var validateFieldWithConfig = (field, value, values, config, registry) => {
|
|
138
|
+
if (config.enabled === false) return [];
|
|
139
|
+
const validate = registry[config.type];
|
|
140
|
+
if (!validate) return [];
|
|
141
|
+
const baseIssues = toIssueArray(
|
|
142
|
+
validate(value, config.params, {
|
|
143
|
+
field,
|
|
144
|
+
values
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
return baseIssues.map((baseIssue) => {
|
|
148
|
+
var _a, _b, _c, _d, _e;
|
|
149
|
+
return {
|
|
150
|
+
field: String((_a = field.name) != null ? _a : ""),
|
|
151
|
+
validatorId: config.id || baseIssue.validatorId,
|
|
152
|
+
code: baseIssue.code,
|
|
153
|
+
messageKey: (_b = baseIssue.messageKey) != null ? _b : config.messageKey,
|
|
154
|
+
message: (_c = baseIssue.message) != null ? _c : config.message,
|
|
155
|
+
params: __spreadValues(__spreadValues({}, (_d = config.params) != null ? _d : {}), (_e = baseIssue.params) != null ? _e : {})
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
var hasDeclarativeValidators = (fields) => fields.some((field) => Array.isArray(field.validators) && field.validators.length > 0);
|
|
160
|
+
var resolveIssueMessage = (issue, t) => {
|
|
161
|
+
const defaultMessageByCode = () => {
|
|
162
|
+
var _a, _b, _c, _d, _e, _f;
|
|
163
|
+
switch (issue.code) {
|
|
164
|
+
case "required":
|
|
165
|
+
return "This field is required";
|
|
166
|
+
case "email":
|
|
167
|
+
return "Invalid email address";
|
|
168
|
+
case "pattern":
|
|
169
|
+
return "Invalid format";
|
|
170
|
+
case "minLength": {
|
|
171
|
+
const min = (_c = (_a = issue.params) == null ? void 0 : _a.min) != null ? _c : (_b = issue.params) == null ? void 0 : _b.value;
|
|
172
|
+
return min != null ? `Minimum ${String(min)} characters` : "Value is too short";
|
|
173
|
+
}
|
|
174
|
+
case "maxLength": {
|
|
175
|
+
const max = (_f = (_d = issue.params) == null ? void 0 : _d.max) != null ? _f : (_e = issue.params) == null ? void 0 : _e.value;
|
|
176
|
+
return max != null ? `Maximum ${String(max)} characters` : "Value is too long";
|
|
177
|
+
}
|
|
178
|
+
default:
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
if (issue.messageKey && t) {
|
|
183
|
+
const translated = t(issue.messageKey, issue.params);
|
|
184
|
+
if (typeof translated === "string" && translated.trim().length > 0) {
|
|
185
|
+
return translated;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (issue.message && issue.message.trim().length > 0) return issue.message;
|
|
189
|
+
if (issue.messageKey && issue.messageKey.trim().length > 0) return issue.messageKey;
|
|
190
|
+
return defaultMessageByCode();
|
|
191
|
+
};
|
|
192
|
+
var validateFormValues = (fields, values, options) => {
|
|
193
|
+
var _a;
|
|
194
|
+
const mergedRegistry = __spreadValues(__spreadValues({}, defaultDeclarativeValidatorRegistry), (_a = options == null ? void 0 : options.registry) != null ? _a : {});
|
|
195
|
+
const issues = [];
|
|
196
|
+
fields.forEach((field) => {
|
|
197
|
+
var _a2;
|
|
198
|
+
const fieldName = String((_a2 = field.name) != null ? _a2 : "").trim();
|
|
199
|
+
if (!fieldName) return;
|
|
200
|
+
const fieldValue = values[fieldName];
|
|
201
|
+
const validators = normalizeValidators(field, options);
|
|
202
|
+
if (validators.length === 0) return;
|
|
203
|
+
for (const validatorConfig of validators) {
|
|
204
|
+
const fieldIssues = validateFieldWithConfig(
|
|
205
|
+
field,
|
|
206
|
+
fieldValue,
|
|
207
|
+
values,
|
|
208
|
+
validatorConfig,
|
|
209
|
+
mergedRegistry
|
|
210
|
+
);
|
|
211
|
+
if (fieldIssues.length > 0) {
|
|
212
|
+
issues.push(...fieldIssues);
|
|
213
|
+
if (validatorConfig.stopOnFailure) break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
const errors = {};
|
|
218
|
+
issues.forEach((issue) => {
|
|
219
|
+
if (errors[issue.field]) return;
|
|
220
|
+
errors[issue.field] = resolveIssueMessage(issue, options == null ? void 0 : options.t);
|
|
221
|
+
});
|
|
222
|
+
return { issues, errors };
|
|
223
|
+
};
|
|
224
|
+
var createFormState = (input) => ({
|
|
225
|
+
values: input.values,
|
|
226
|
+
touched: input.touched,
|
|
227
|
+
issues: input.issues,
|
|
228
|
+
errors: input.errors,
|
|
229
|
+
dirty: input.dirty,
|
|
230
|
+
isValid: Object.values(input.errors).every((error) => !error),
|
|
231
|
+
isSubmitting: input.isSubmitting,
|
|
232
|
+
submitCount: input.submitCount
|
|
233
|
+
});
|
|
234
|
+
export {
|
|
235
|
+
createFormState,
|
|
236
|
+
defaultDeclarativeValidatorRegistry,
|
|
237
|
+
hasDeclarativeValidators,
|
|
238
|
+
resolveBindableText,
|
|
239
|
+
resolveIssueMessage,
|
|
240
|
+
validateFormValues
|
|
241
|
+
};
|
package/dist/form.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { F as FieldConfig, a as Field } from './types-9TZ2lQDP.js';
|
|
2
|
+
export { D as DeclarativeValidatorConfig, b as DeclarativeValidatorType, c as FieldOption, d as FormErrors, e as FormState, f as FormTouched, g as FormValues, V as ValidationIssue } from './types-9TZ2lQDP.js';
|
|
3
|
+
|
|
4
|
+
type FormValues = Record<string, unknown>;
|
|
5
|
+
type FormErrors = Record<string, string | undefined>;
|
|
6
|
+
type FormTouched = Record<string, boolean | undefined>;
|
|
7
|
+
type ValidationIssue = {
|
|
8
|
+
field: string;
|
|
9
|
+
validatorId?: string;
|
|
10
|
+
code?: string;
|
|
11
|
+
message?: string;
|
|
12
|
+
messageKey?: string;
|
|
13
|
+
params?: Record<string, unknown>;
|
|
14
|
+
};
|
|
15
|
+
type FormState = {
|
|
16
|
+
values: FormValues;
|
|
17
|
+
errors: FormErrors;
|
|
18
|
+
touched: FormTouched;
|
|
19
|
+
dirty: boolean;
|
|
20
|
+
isValid: boolean;
|
|
21
|
+
isSubmitting: boolean;
|
|
22
|
+
submitCount: number;
|
|
23
|
+
issues: ValidationIssue[];
|
|
24
|
+
};
|
|
25
|
+
type DeclarativeValidatorType = "required" | "minLength" | "maxLength" | "email" | "pattern";
|
|
26
|
+
type DeclarativeValidatorConfig = {
|
|
27
|
+
id: string;
|
|
28
|
+
type: DeclarativeValidatorType;
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
params?: Record<string, unknown>;
|
|
31
|
+
message?: string;
|
|
32
|
+
messageKey?: string;
|
|
33
|
+
stopOnFailure?: boolean;
|
|
34
|
+
};
|
|
35
|
+
type ValidatorContext = {
|
|
36
|
+
field: FieldConfig;
|
|
37
|
+
values: FormValues;
|
|
38
|
+
};
|
|
39
|
+
type ValidationIssueDraft = Omit<ValidationIssue, "field">;
|
|
40
|
+
type ValidatorFn = (value: unknown, params: Record<string, unknown> | undefined, context: ValidatorContext) => ValidationIssueDraft | ValidationIssueDraft[] | null;
|
|
41
|
+
type ValidatorRegistry = Record<DeclarativeValidatorType, ValidatorFn>;
|
|
42
|
+
declare const resolveBindableText: (value: unknown, options?: {
|
|
43
|
+
t?: (key: string, values?: Record<string, unknown>) => string;
|
|
44
|
+
values?: Record<string, unknown>;
|
|
45
|
+
}) => string;
|
|
46
|
+
declare const defaultDeclarativeValidatorRegistry: ValidatorRegistry;
|
|
47
|
+
declare const hasDeclarativeValidators: (fields: FieldConfig[]) => boolean;
|
|
48
|
+
declare const resolveIssueMessage: (issue: ValidationIssue, t?: (key: string, values?: Record<string, unknown>) => string) => string | undefined;
|
|
49
|
+
declare const validateFormValues: (fields: FieldConfig[], values: Record<string, Field | unknown>, options?: {
|
|
50
|
+
t?: (key: string, values?: Record<string, unknown>) => string;
|
|
51
|
+
useLegacyFieldConstraints?: boolean;
|
|
52
|
+
registry?: Partial<ValidatorRegistry>;
|
|
53
|
+
}) => {
|
|
54
|
+
issues: ValidationIssue[];
|
|
55
|
+
errors: FormErrors;
|
|
56
|
+
};
|
|
57
|
+
declare const createFormState: (input: {
|
|
58
|
+
values: FormValues;
|
|
59
|
+
touched: FormTouched;
|
|
60
|
+
issues: ValidationIssue[];
|
|
61
|
+
errors: FormErrors;
|
|
62
|
+
dirty: boolean;
|
|
63
|
+
isSubmitting: boolean;
|
|
64
|
+
submitCount: number;
|
|
65
|
+
}) => FormState;
|
|
66
|
+
|
|
67
|
+
export { type DeclarativeValidatorConfig as D, type FormErrors as F, Field, FieldConfig, type ValidationIssue as V, type DeclarativeValidatorType as a, type FormState as b, type FormTouched as c, createFormState, type FormValues as d, defaultDeclarativeValidatorRegistry, hasDeclarativeValidators, resolveBindableText, resolveIssueMessage, validateFormValues };
|