@geomak/ui 5.6.0 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +447 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +534 -188
- package/dist/index.d.ts +534 -188
- package/dist/index.js +436 -4
- package/dist/index.js.map +1 -1
- package/dist/styles.css +18 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { colors_default } from './chunk-GKXP6OJJ.js';
|
|
2
2
|
export { colors_default as COLORS, PALETTE as palette, semanticTokens, vars } from './chunk-GKXP6OJJ.js';
|
|
3
3
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
4
|
-
import React8, { createContext, useState, useEffect, useMemo, useCallback, useContext, useRef, useId, useLayoutEffect } from 'react';
|
|
4
|
+
import React8, { createContext, useState, useEffect, useMemo, useCallback, useContext, useRef, useId, useLayoutEffect, useSyncExternalStore } from 'react';
|
|
5
5
|
import { createPortal } from 'react-dom';
|
|
6
6
|
import * as AvatarPrimitive from '@radix-ui/react-avatar';
|
|
7
7
|
import * as Dialog from '@radix-ui/react-dialog';
|
|
@@ -40,7 +40,7 @@ var FleetIcon = ({ color = "#fff", size = 30 }) => /* @__PURE__ */ jsxs("svg", {
|
|
|
40
40
|
/* @__PURE__ */ jsx("defs", { children: /* @__PURE__ */ jsx("clipPath", { id: "clip0_327_836", children: /* @__PURE__ */ jsx("rect", { width: "30", height: "30", fill: color }) }) })
|
|
41
41
|
] });
|
|
42
42
|
var Performance = ({ color = "#fff", size = 30 }) => /* @__PURE__ */ jsx("svg", { width: size, height: size, viewBox: "0 0 30 30", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ jsx("path", { d: "M5.64 25.515H24.36C26.0709 23.3842 27.0024 20.7327 27 18C27 11.385 21.63 6 15 6C8.37 6 3 11.385 3 18C3 20.85 3.99 23.445 5.64 25.515ZM13.5 9C13.5 8.175 14.175 7.5 15 7.5C15.825 7.5 16.5 8.175 16.5 9C16.5 9.84 15.825 10.5 15 10.5C14.175 10.5 13.5 9.84 13.5 9ZM6 12C6 11.175 6.675 10.5 7.5 10.5C8.325 10.5 9 11.175 9 12C9 12.84 8.325 13.5 7.5 13.5C6.675 13.5 6 12.84 6 12ZM12.78 17.1C14.04 15.855 22.545 11.85 22.545 11.85C22.545 11.85 18.555 20.37 17.31 21.615C16.05 22.875 14.04 22.875 12.78 21.615C12.1819 21.016 11.8459 20.204 11.8459 19.3575C11.8459 18.511 12.1819 17.699 12.78 17.1ZM4.5 19.5C4.5 18.675 5.175 18 6 18C6.825 18 7.5 18.675 7.5 19.5C7.5 20.34 6.825 21 6 21C5.175 21 4.5 20.34 4.5 19.5ZM13.5 19.5C13.5 18.675 14.175 18 15 18C15.825 18 16.5 18.675 16.5 19.5C16.5 20.34 15.825 21 15 21C14.175 21 13.5 20.34 13.5 19.5ZM22.5 19.5C22.5 18.675 23.175 18 24 18C24.825 18 25.5 18.675 25.5 19.5C25.5 20.34 24.825 21 24 21C23.175 21 22.5 20.34 22.5 19.5Z", fill: color }) });
|
|
43
|
-
var
|
|
43
|
+
var Map2 = ({ color = "#fff", size = 30 }) => /* @__PURE__ */ jsxs("svg", { width: size, height: size, viewBox: "0 0 34 30", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [
|
|
44
44
|
/* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_327_830)", children: /* @__PURE__ */ jsx("path", { d: "M16.875 0C12.7975 0 9.49219 3.30527 9.49219 7.38281C9.49219 10.6793 14.3174 16.6875 16.166 18.8684C16.5404 19.3102 17.2102 19.3102 17.584 18.8684C19.4326 16.6875 24.2578 10.6793 24.2578 7.38281C24.2578 3.30527 20.9525 0 16.875 0ZM16.875 9.84375C15.5156 9.84375 14.4141 8.74219 14.4141 7.38281C14.4141 6.02344 15.5156 4.92188 16.875 4.92188C18.2344 4.92188 19.3359 6.02344 19.3359 7.38281C19.3359 8.74219 18.2344 9.84375 16.875 9.84375ZM1.17891 12.6533C0.830964 12.7925 0.532694 13.0327 0.322564 13.343C0.112435 13.6533 8.33325e-05 14.0194 0 14.3941L0 29.0613C0 29.7246 0.669727 30.1781 1.28555 29.932L9.375 26.25V12.593C8.85703 11.6566 8.4334 10.7449 8.12988 9.87305L1.17891 12.6533ZM16.875 21.0744C16.0506 21.0744 15.2707 20.7123 14.7357 20.0807C13.5838 18.7213 12.3586 17.1732 11.25 15.5854V26.2494L22.5 29.9994V15.5859C21.3914 17.1732 20.1668 18.7219 19.0143 20.0812C18.4793 20.7123 17.6994 21.0744 16.875 21.0744ZM32.4645 9.44297L24.375 13.125V30L32.5711 26.7217C32.9191 26.5826 33.2174 26.3424 33.4275 26.0321C33.6377 25.7218 33.75 25.3556 33.75 24.9809V10.3137C33.75 9.65039 33.0803 9.19688 32.4645 9.44297Z", fill: color }) }),
|
|
45
45
|
/* @__PURE__ */ jsx("defs", { children: /* @__PURE__ */ jsx("clipPath", { id: "clip0_327_830", children: /* @__PURE__ */ jsx("rect", { width: "33.75", height: "30", fill: color }) }) })
|
|
46
46
|
] });
|
|
@@ -127,7 +127,7 @@ Icon.Minus = Minus;
|
|
|
127
127
|
Icon.Dashboard = Dashboard;
|
|
128
128
|
Icon.FleetIcon = FleetIcon;
|
|
129
129
|
Icon.Performance = Performance;
|
|
130
|
-
Icon.Map =
|
|
130
|
+
Icon.Map = Map2;
|
|
131
131
|
Icon.CharterParty = CharterParty;
|
|
132
132
|
Icon.Compliance = Compliance;
|
|
133
133
|
Icon.Applications = Applications;
|
|
@@ -5885,6 +5885,438 @@ function ColorPicker({
|
|
|
5885
5885
|
] });
|
|
5886
5886
|
}
|
|
5887
5887
|
|
|
5888
|
-
|
|
5888
|
+
// src/form/path.ts
|
|
5889
|
+
function deepClone(v) {
|
|
5890
|
+
if (v === null || typeof v !== "object") return v;
|
|
5891
|
+
if (v instanceof Date) return new Date(v.getTime());
|
|
5892
|
+
if (Array.isArray(v)) return v.map(deepClone);
|
|
5893
|
+
const out = {};
|
|
5894
|
+
for (const k in v) out[k] = deepClone(v[k]);
|
|
5895
|
+
return out;
|
|
5896
|
+
}
|
|
5897
|
+
function getPath(obj, path) {
|
|
5898
|
+
if (!path) return obj;
|
|
5899
|
+
const parts = path.split(".");
|
|
5900
|
+
let cur = obj;
|
|
5901
|
+
for (const p of parts) {
|
|
5902
|
+
if (cur == null || typeof cur !== "object") return void 0;
|
|
5903
|
+
cur = cur[p];
|
|
5904
|
+
}
|
|
5905
|
+
return cur;
|
|
5906
|
+
}
|
|
5907
|
+
function setPath(obj, path, value) {
|
|
5908
|
+
const parts = path.split(".");
|
|
5909
|
+
const root = Array.isArray(obj) ? [...obj] : { ...obj ?? {} };
|
|
5910
|
+
let cur = root;
|
|
5911
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
5912
|
+
const p = parts[i];
|
|
5913
|
+
const nextIsIndex = /^\d+$/.test(parts[i + 1]);
|
|
5914
|
+
const child = cur[p];
|
|
5915
|
+
const next = Array.isArray(child) ? [...child] : child && typeof child === "object" ? { ...child } : nextIsIndex ? [] : {};
|
|
5916
|
+
cur[p] = next;
|
|
5917
|
+
cur = next;
|
|
5918
|
+
}
|
|
5919
|
+
cur[parts[parts.length - 1]] = value;
|
|
5920
|
+
return root;
|
|
5921
|
+
}
|
|
5922
|
+
|
|
5923
|
+
// src/form/validate.ts
|
|
5924
|
+
function isEmpty(v) {
|
|
5925
|
+
return v == null || v === "" || v === false || Array.isArray(v) && v.length === 0;
|
|
5926
|
+
}
|
|
5927
|
+
var boundValue = (b) => typeof b === "number" ? b : b.value;
|
|
5928
|
+
var boundMessage = (b, fallback) => typeof b === "number" ? fallback : b.message ?? fallback;
|
|
5929
|
+
function isRequired(rules) {
|
|
5930
|
+
if (!rules) return false;
|
|
5931
|
+
const list = Array.isArray(rules) ? rules : [rules];
|
|
5932
|
+
return list.some((r) => !!r.required);
|
|
5933
|
+
}
|
|
5934
|
+
async function runFieldRules(value, rules, values) {
|
|
5935
|
+
if (!rules) return void 0;
|
|
5936
|
+
const list = Array.isArray(rules) ? rules : [rules];
|
|
5937
|
+
for (const rule of list) {
|
|
5938
|
+
if (rule.required && isEmpty(value)) {
|
|
5939
|
+
return typeof rule.required === "string" ? rule.required : rule.message ?? "This field is required";
|
|
5940
|
+
}
|
|
5941
|
+
if (isEmpty(value)) {
|
|
5942
|
+
if (rule.validate) {
|
|
5943
|
+
const res = await rule.validate(value, values);
|
|
5944
|
+
if (res) return typeof res === "string" ? res : rule.message ?? "Invalid value";
|
|
5945
|
+
}
|
|
5946
|
+
continue;
|
|
5947
|
+
}
|
|
5948
|
+
if (rule.pattern) {
|
|
5949
|
+
const re = rule.pattern instanceof RegExp ? rule.pattern : rule.pattern.value;
|
|
5950
|
+
const msg = rule.pattern instanceof RegExp ? rule.message ?? "Invalid format" : rule.pattern.message ?? rule.message ?? "Invalid format";
|
|
5951
|
+
if (typeof value === "string" && !re.test(value)) return msg;
|
|
5952
|
+
}
|
|
5953
|
+
if (rule.min != null && typeof value === "number") {
|
|
5954
|
+
const m = boundValue(rule.min);
|
|
5955
|
+
if (value < m) return boundMessage(rule.min, rule.message ?? `Must be at least ${m}`);
|
|
5956
|
+
}
|
|
5957
|
+
if (rule.max != null && typeof value === "number") {
|
|
5958
|
+
const m = boundValue(rule.max);
|
|
5959
|
+
if (value > m) return boundMessage(rule.max, rule.message ?? `Must be at most ${m}`);
|
|
5960
|
+
}
|
|
5961
|
+
if (rule.minLength != null) {
|
|
5962
|
+
const len = value?.length;
|
|
5963
|
+
const m = boundValue(rule.minLength);
|
|
5964
|
+
if (typeof len === "number" && len < m)
|
|
5965
|
+
return boundMessage(rule.minLength, rule.message ?? `Must be at least ${m} characters`);
|
|
5966
|
+
}
|
|
5967
|
+
if (rule.maxLength != null) {
|
|
5968
|
+
const len = value?.length;
|
|
5969
|
+
const m = boundValue(rule.maxLength);
|
|
5970
|
+
if (typeof len === "number" && len > m)
|
|
5971
|
+
return boundMessage(rule.maxLength, rule.message ?? `Must be at most ${m} characters`);
|
|
5972
|
+
}
|
|
5973
|
+
if (rule.validate) {
|
|
5974
|
+
const res = await rule.validate(value, values);
|
|
5975
|
+
if (res) return typeof res === "string" ? res : rule.message ?? "Invalid value";
|
|
5976
|
+
}
|
|
5977
|
+
}
|
|
5978
|
+
return void 0;
|
|
5979
|
+
}
|
|
5980
|
+
var patterns = {
|
|
5981
|
+
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
5982
|
+
url: /^https?:\/\/[^\s/$.?#].[^\s]*$/i,
|
|
5983
|
+
// Loose international phone: + and 7-15 digits, spaces/dashes allowed.
|
|
5984
|
+
phone: /^\+?[\d\s-]{7,15}$/,
|
|
5985
|
+
// Digits only.
|
|
5986
|
+
digits: /^\d+$/
|
|
5987
|
+
};
|
|
5988
|
+
|
|
5989
|
+
// src/form/store.ts
|
|
5990
|
+
var FormStore = class {
|
|
5991
|
+
listeners = /* @__PURE__ */ new Set();
|
|
5992
|
+
fieldCache = /* @__PURE__ */ new Map();
|
|
5993
|
+
values;
|
|
5994
|
+
errors = {};
|
|
5995
|
+
touched = {};
|
|
5996
|
+
submitted = false;
|
|
5997
|
+
validating = false;
|
|
5998
|
+
initialValues;
|
|
5999
|
+
rules;
|
|
6000
|
+
validateOn;
|
|
6001
|
+
// Field-array key bookkeeping (stable React keys across reorder/removal).
|
|
6002
|
+
keys = {};
|
|
6003
|
+
keySeq = 1;
|
|
6004
|
+
// Root snapshot — a new ref on every change, for form-level subscribers.
|
|
6005
|
+
rootSnap = { v: 0 };
|
|
6006
|
+
constructor(opts = {}) {
|
|
6007
|
+
this.initialValues = deepClone(opts.initialValues ?? {});
|
|
6008
|
+
this.values = deepClone(opts.initialValues ?? {});
|
|
6009
|
+
this.rules = { ...opts.rules ?? {} };
|
|
6010
|
+
this.validateOn = opts.validateOn ?? ["onChange", "onBlur", "onSubmit"];
|
|
6011
|
+
}
|
|
6012
|
+
// ── subscription ────────────────────────────────────────────────────────
|
|
6013
|
+
subscribe = (l) => {
|
|
6014
|
+
this.listeners.add(l);
|
|
6015
|
+
return () => {
|
|
6016
|
+
this.listeners.delete(l);
|
|
6017
|
+
};
|
|
6018
|
+
};
|
|
6019
|
+
emit() {
|
|
6020
|
+
this.rootSnap = { v: this.rootSnap.v + 1 };
|
|
6021
|
+
this.listeners.forEach((l) => l());
|
|
6022
|
+
}
|
|
6023
|
+
getRootSnapshot = () => this.rootSnap;
|
|
6024
|
+
getFieldSnapshot = (name) => {
|
|
6025
|
+
const value = getPath(this.values, name);
|
|
6026
|
+
const error = this.errors[name];
|
|
6027
|
+
const showError = (!!this.touched[name] || this.submitted) && error != null;
|
|
6028
|
+
const prev = this.fieldCache.get(name);
|
|
6029
|
+
if (prev && Object.is(prev.value, value) && prev.error === error && prev.showError === showError) {
|
|
6030
|
+
return prev.snap;
|
|
6031
|
+
}
|
|
6032
|
+
const snap = { value, error, showError };
|
|
6033
|
+
this.fieldCache.set(name, { value, error, showError, snap });
|
|
6034
|
+
return snap;
|
|
6035
|
+
};
|
|
6036
|
+
// ── rule registry (for dynamic / array fields) ──────────────────────────
|
|
6037
|
+
setRule(name, rules) {
|
|
6038
|
+
if (rules == null) delete this.rules[name];
|
|
6039
|
+
else this.rules[name] = rules;
|
|
6040
|
+
}
|
|
6041
|
+
removeRule(name) {
|
|
6042
|
+
delete this.rules[name];
|
|
6043
|
+
}
|
|
6044
|
+
getRule(name) {
|
|
6045
|
+
return this.rules[name];
|
|
6046
|
+
}
|
|
6047
|
+
// ── reads ────────────────────────────────────────────────────────────────
|
|
6048
|
+
getValues = () => this.values;
|
|
6049
|
+
getValue = (name) => getPath(this.values, name);
|
|
6050
|
+
get isValid() {
|
|
6051
|
+
return Object.values(this.errors).every((e) => !e);
|
|
6052
|
+
}
|
|
6053
|
+
// ── writes ────────────────────────────────────────────────────────────────
|
|
6054
|
+
setValue = (name, value, opts = {}) => {
|
|
6055
|
+
this.values = setPath(this.values, name, value);
|
|
6056
|
+
if (opts.touch) this.touched = { ...this.touched, [name]: true };
|
|
6057
|
+
this.emit();
|
|
6058
|
+
const shouldValidate = opts.validate ?? this.validateOn.includes("onChange");
|
|
6059
|
+
if (shouldValidate) void this.validateField(name);
|
|
6060
|
+
};
|
|
6061
|
+
setValues = (patch, opts = {}) => {
|
|
6062
|
+
for (const k of Object.keys(patch)) this.values = setPath(this.values, k, patch[k]);
|
|
6063
|
+
this.emit();
|
|
6064
|
+
if (opts.validate) void this.validateAll();
|
|
6065
|
+
};
|
|
6066
|
+
setError = (name, error) => {
|
|
6067
|
+
if (this.errors[name] === error) return;
|
|
6068
|
+
this.errors = { ...this.errors, [name]: error || void 0 };
|
|
6069
|
+
this.emit();
|
|
6070
|
+
};
|
|
6071
|
+
touch = (name, opts = {}) => {
|
|
6072
|
+
if (!this.touched[name]) {
|
|
6073
|
+
this.touched = { ...this.touched, [name]: true };
|
|
6074
|
+
this.emit();
|
|
6075
|
+
}
|
|
6076
|
+
if (opts.validate ?? this.validateOn.includes("onBlur")) void this.validateField(name);
|
|
6077
|
+
};
|
|
6078
|
+
setSubmitted = (v) => {
|
|
6079
|
+
this.submitted = v;
|
|
6080
|
+
this.emit();
|
|
6081
|
+
};
|
|
6082
|
+
// ── validation ─────────────────────────────────────────────────────────────
|
|
6083
|
+
async validateField(name) {
|
|
6084
|
+
const err = await runFieldRules(getPath(this.values, name), this.rules[name], this.values);
|
|
6085
|
+
this.setError(name, err);
|
|
6086
|
+
return err;
|
|
6087
|
+
}
|
|
6088
|
+
async validateAll() {
|
|
6089
|
+
this.validating = true;
|
|
6090
|
+
this.emit();
|
|
6091
|
+
const names = Object.keys(this.rules);
|
|
6092
|
+
const entries = await Promise.all(
|
|
6093
|
+
names.map(async (n) => [n, await runFieldRules(getPath(this.values, n), this.rules[n], this.values)])
|
|
6094
|
+
);
|
|
6095
|
+
const errors = {};
|
|
6096
|
+
for (const [n, e] of entries) errors[n] = e;
|
|
6097
|
+
this.errors = errors;
|
|
6098
|
+
this.validating = false;
|
|
6099
|
+
this.emit();
|
|
6100
|
+
return errors;
|
|
6101
|
+
}
|
|
6102
|
+
reset = (values) => {
|
|
6103
|
+
this.values = deepClone(values ?? this.initialValues);
|
|
6104
|
+
this.errors = {};
|
|
6105
|
+
this.touched = {};
|
|
6106
|
+
this.submitted = false;
|
|
6107
|
+
this.keys = {};
|
|
6108
|
+
this.fieldCache.clear();
|
|
6109
|
+
this.emit();
|
|
6110
|
+
};
|
|
6111
|
+
// ── field arrays ───────────────────────────────────────────────────────────
|
|
6112
|
+
getKeys(name) {
|
|
6113
|
+
const arr = getPath(this.values, name) ?? [];
|
|
6114
|
+
let keys = this.keys[name];
|
|
6115
|
+
if (!keys || keys.length !== arr.length) {
|
|
6116
|
+
keys = arr.map((_, i) => keys && keys[i] != null ? keys[i] : this.keySeq++);
|
|
6117
|
+
this.keys[name] = keys;
|
|
6118
|
+
}
|
|
6119
|
+
return keys;
|
|
6120
|
+
}
|
|
6121
|
+
arrayAppend = (name, item = {}) => {
|
|
6122
|
+
const arr = [...getPath(this.values, name) ?? []];
|
|
6123
|
+
arr.push(item);
|
|
6124
|
+
this.keys[name] = [...this.getKeys(name), this.keySeq++];
|
|
6125
|
+
this.setValue(name, arr, { validate: false });
|
|
6126
|
+
};
|
|
6127
|
+
arrayRemove = (name, index) => {
|
|
6128
|
+
const arr = [...getPath(this.values, name) ?? []];
|
|
6129
|
+
arr.splice(index, 1);
|
|
6130
|
+
const k = [...this.getKeys(name)];
|
|
6131
|
+
k.splice(index, 1);
|
|
6132
|
+
this.keys[name] = k;
|
|
6133
|
+
this.clearBranch(name);
|
|
6134
|
+
this.setValue(name, arr, { validate: false });
|
|
6135
|
+
};
|
|
6136
|
+
arrayMove = (name, from, to) => {
|
|
6137
|
+
const arr = [...getPath(this.values, name) ?? []];
|
|
6138
|
+
if (from < 0 || to < 0 || from >= arr.length || to >= arr.length) return;
|
|
6139
|
+
const [moved] = arr.splice(from, 1);
|
|
6140
|
+
arr.splice(to, 0, moved);
|
|
6141
|
+
const k = [...this.getKeys(name)];
|
|
6142
|
+
const [mk] = k.splice(from, 1);
|
|
6143
|
+
k.splice(to, 0, mk);
|
|
6144
|
+
this.keys[name] = k;
|
|
6145
|
+
this.clearBranch(name);
|
|
6146
|
+
this.setValue(name, arr, { validate: false });
|
|
6147
|
+
};
|
|
6148
|
+
/** Drop any errors/touched flags under `name.` — used when an array shifts. */
|
|
6149
|
+
clearBranch(name) {
|
|
6150
|
+
const prefix = name + ".";
|
|
6151
|
+
const errors = {};
|
|
6152
|
+
for (const k of Object.keys(this.errors)) if (!k.startsWith(prefix)) errors[k] = this.errors[k];
|
|
6153
|
+
const touched = {};
|
|
6154
|
+
for (const k of Object.keys(this.touched)) if (!k.startsWith(prefix)) touched[k] = this.touched[k];
|
|
6155
|
+
this.errors = errors;
|
|
6156
|
+
this.touched = touched;
|
|
6157
|
+
}
|
|
6158
|
+
};
|
|
6159
|
+
|
|
6160
|
+
// src/form/bindings.ts
|
|
6161
|
+
var getTarget = (arg) => {
|
|
6162
|
+
const t = arg?.target;
|
|
6163
|
+
return t && typeof t === "object" ? t : void 0;
|
|
6164
|
+
};
|
|
6165
|
+
var ADAPTERS = {
|
|
6166
|
+
value: { prop: "value", toValue: (v) => v, applyEmpty: false, empty: void 0 },
|
|
6167
|
+
native: { prop: "value", toValue: (e) => getTarget(e)?.value, applyEmpty: true, empty: "" },
|
|
6168
|
+
checked: { prop: "checked", toValue: (e) => getTarget(e)?.checked, applyEmpty: true, empty: false },
|
|
6169
|
+
target: { prop: "value", toValue: (e) => getTarget(e)?.value, applyEmpty: false, empty: void 0 }
|
|
6170
|
+
};
|
|
6171
|
+
function buildBindings(store, name, kind, snap) {
|
|
6172
|
+
const a = ADAPTERS[kind];
|
|
6173
|
+
const raw = snap.value;
|
|
6174
|
+
const value = a.applyEmpty ? raw ?? a.empty : raw;
|
|
6175
|
+
return {
|
|
6176
|
+
name,
|
|
6177
|
+
id: name,
|
|
6178
|
+
htmlFor: name,
|
|
6179
|
+
required: isRequired(store.getRule(name)) || void 0,
|
|
6180
|
+
errorMessage: snap.showError ? snap.error : void 0,
|
|
6181
|
+
[a.prop]: value,
|
|
6182
|
+
onChange: (arg) => store.setValue(name, a.toValue(arg), { touch: true }),
|
|
6183
|
+
onBlur: () => store.touch(name)
|
|
6184
|
+
};
|
|
6185
|
+
}
|
|
6186
|
+
|
|
6187
|
+
// src/form/useForm.ts
|
|
6188
|
+
function useForm(options = {}) {
|
|
6189
|
+
const ref = useRef(null);
|
|
6190
|
+
if (ref.current === null) ref.current = new FormStore(options);
|
|
6191
|
+
const store = ref.current;
|
|
6192
|
+
useSyncExternalStore(store.subscribe, store.getRootSnapshot, store.getRootSnapshot);
|
|
6193
|
+
const make = useCallback(
|
|
6194
|
+
(kind) => (name, rules) => {
|
|
6195
|
+
if (rules !== void 0) store.setRule(name, rules);
|
|
6196
|
+
return buildBindings(store, name, kind, store.getFieldSnapshot(name));
|
|
6197
|
+
},
|
|
6198
|
+
[store]
|
|
6199
|
+
);
|
|
6200
|
+
return {
|
|
6201
|
+
store,
|
|
6202
|
+
values: store.values,
|
|
6203
|
+
errors: store.errors,
|
|
6204
|
+
touched: store.touched,
|
|
6205
|
+
submitted: store.submitted,
|
|
6206
|
+
isSubmitting: store.validating,
|
|
6207
|
+
isValid: store.isValid,
|
|
6208
|
+
getValue: store.getValue,
|
|
6209
|
+
getValues: store.getValues,
|
|
6210
|
+
setValue: (name, value) => store.setValue(name, value),
|
|
6211
|
+
setValues: (patch) => store.setValues(patch),
|
|
6212
|
+
setError: store.setError,
|
|
6213
|
+
validateField: (name) => store.validateField(name),
|
|
6214
|
+
validateAll: () => store.validateAll(),
|
|
6215
|
+
reset: store.reset,
|
|
6216
|
+
field: make("value"),
|
|
6217
|
+
fieldNative: make("native"),
|
|
6218
|
+
fieldChecked: make("checked"),
|
|
6219
|
+
fieldTarget: make("target")
|
|
6220
|
+
};
|
|
6221
|
+
}
|
|
6222
|
+
var FormContext = createContext(null);
|
|
6223
|
+
function useFormStore() {
|
|
6224
|
+
const store = useContext(FormContext);
|
|
6225
|
+
if (!store) {
|
|
6226
|
+
throw new Error("useFormStore must be used within a <Form>. Did you forget to wrap your fields?");
|
|
6227
|
+
}
|
|
6228
|
+
return store;
|
|
6229
|
+
}
|
|
6230
|
+
function Form({
|
|
6231
|
+
form,
|
|
6232
|
+
onFinish,
|
|
6233
|
+
onFinishFailed,
|
|
6234
|
+
action,
|
|
6235
|
+
children,
|
|
6236
|
+
...rest
|
|
6237
|
+
}) {
|
|
6238
|
+
const ref = useRef(null);
|
|
6239
|
+
const bypass = useRef(false);
|
|
6240
|
+
const handleSubmit = async (e) => {
|
|
6241
|
+
if (bypass.current) {
|
|
6242
|
+
bypass.current = false;
|
|
6243
|
+
return;
|
|
6244
|
+
}
|
|
6245
|
+
e.preventDefault();
|
|
6246
|
+
const store = form.store;
|
|
6247
|
+
store.setSubmitted(true);
|
|
6248
|
+
const errors = await store.validateAll();
|
|
6249
|
+
const hasError = Object.values(errors).some(Boolean);
|
|
6250
|
+
if (hasError) {
|
|
6251
|
+
onFinishFailed?.(errors, store.getValues());
|
|
6252
|
+
focusFirstError(ref.current, errors);
|
|
6253
|
+
return;
|
|
6254
|
+
}
|
|
6255
|
+
if (onFinish) {
|
|
6256
|
+
await onFinish(store.getValues());
|
|
6257
|
+
return;
|
|
6258
|
+
}
|
|
6259
|
+
if (typeof action === "function") {
|
|
6260
|
+
action(new FormData(ref.current));
|
|
6261
|
+
return;
|
|
6262
|
+
}
|
|
6263
|
+
if (typeof action === "string") {
|
|
6264
|
+
bypass.current = true;
|
|
6265
|
+
ref.current.requestSubmit();
|
|
6266
|
+
}
|
|
6267
|
+
};
|
|
6268
|
+
return /* @__PURE__ */ jsx(FormContext.Provider, { value: form.store, children: /* @__PURE__ */ jsx(
|
|
6269
|
+
"form",
|
|
6270
|
+
{
|
|
6271
|
+
ref,
|
|
6272
|
+
noValidate: true,
|
|
6273
|
+
action: typeof action === "string" ? action : void 0,
|
|
6274
|
+
onSubmit: handleSubmit,
|
|
6275
|
+
...rest,
|
|
6276
|
+
children
|
|
6277
|
+
}
|
|
6278
|
+
) });
|
|
6279
|
+
}
|
|
6280
|
+
function focusFirstError(formEl, errors) {
|
|
6281
|
+
if (!formEl) return;
|
|
6282
|
+
const firstName = Object.keys(errors).find((k) => errors[k]);
|
|
6283
|
+
if (!firstName) return;
|
|
6284
|
+
const el = formEl.querySelector(`[name="${CSS.escape(firstName)}"], #${CSS.escape(firstName)}`);
|
|
6285
|
+
el?.focus();
|
|
6286
|
+
}
|
|
6287
|
+
function useFormField(name, options = {}) {
|
|
6288
|
+
const store = useFormStore();
|
|
6289
|
+
const { kind = "value", rules } = options;
|
|
6290
|
+
if (rules !== void 0 && store.getRule(name) !== rules) store.setRule(name, rules);
|
|
6291
|
+
useEffect(() => {
|
|
6292
|
+
return () => {
|
|
6293
|
+
if (rules !== void 0) store.removeRule(name);
|
|
6294
|
+
};
|
|
6295
|
+
}, [store, name]);
|
|
6296
|
+
const snap = useSyncExternalStore(
|
|
6297
|
+
store.subscribe,
|
|
6298
|
+
() => store.getFieldSnapshot(name)
|
|
6299
|
+
);
|
|
6300
|
+
return buildBindings(store, name, kind, snap);
|
|
6301
|
+
}
|
|
6302
|
+
function FormField({ name, kind, rules, children }) {
|
|
6303
|
+
const field = useFormField(name, { kind, rules });
|
|
6304
|
+
return /* @__PURE__ */ jsx(Fragment, { children: children(field) });
|
|
6305
|
+
}
|
|
6306
|
+
function useFieldArray(name) {
|
|
6307
|
+
const store = useFormStore();
|
|
6308
|
+
useSyncExternalStore(store.subscribe, store.getRootSnapshot, store.getRootSnapshot);
|
|
6309
|
+
const arr = store.getValue(name) ?? [];
|
|
6310
|
+
const keys = store.getKeys(name);
|
|
6311
|
+
return {
|
|
6312
|
+
fields: arr.map((_, i) => ({ key: keys[i], name: `${name}.${i}`, index: i })),
|
|
6313
|
+
append: (item = {}) => store.arrayAppend(name, item),
|
|
6314
|
+
remove: (index) => store.arrayRemove(name, index),
|
|
6315
|
+
move: (from, to) => store.arrayMove(name, from, to),
|
|
6316
|
+
replace: (items) => store.setValue(name, items, { validate: false })
|
|
6317
|
+
};
|
|
6318
|
+
}
|
|
6319
|
+
|
|
6320
|
+
export { AppShell, AutoComplete, Avatar, Box, Button, Catalog, CatalogCarousel, CatalogGrid, Checkbox, ColorPicker, ContextMenu, DateRangePicker, Drawer, Dropdown, FadingBase, Field, FieldHelpIcon, FieldLabel, FileInput, Flex, Form, FormContext, FormField, FormStore, Grid2 as Grid, GridCard, icons_default as Icon, IconButton, List2 as List, LoadingSpinner, Modal, NotificationProvider, NumberInput, OpaqueGridCard, OtpInput, Password, Portal, RadioGroup, Rating, ScalableContainer, SearchInput_default as SearchInput, SegmentedControl, Sidebar, SkeletonBox, SkeletonCard, SkeletonCircle, SkeletonText, Slider, Switch, Table, Tabs, TagsInput, DatePicker as Temporal, TextArea, TextInput, ThemeProvider, ThemeSwitch, TimePicker, Tooltip, TooltipProvider, TopBar, Tree, TreeSelect, Typography, Wizard, fieldShell, isRequired, patterns, runFieldRules, useFieldArray, useForm, useFormField, useFormStore, useNotification };
|
|
5889
6321
|
//# sourceMappingURL=index.js.map
|
|
5890
6322
|
//# sourceMappingURL=index.js.map
|