@lodashventure/medusa-campaign 0.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +2270 -274
- package/.medusa/server/src/admin/index.mjs +2263 -268
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@ import { defineRouteConfig } from "@medusajs/admin-sdk";
|
|
|
3
3
|
import { Sparkles, Trash } from "@medusajs/icons";
|
|
4
4
|
import { createDataTableColumnHelper, Badge, useDataTable, Container, Button, DataTable, Text, Input, Label, Textarea, Table, Select, FocusModal, toast } from "@medusajs/ui";
|
|
5
5
|
import dayjs from "dayjs";
|
|
6
|
-
import { useState, useMemo } from "react";
|
|
6
|
+
import React, { useState, useMemo } from "react";
|
|
7
7
|
import { useNavigate, useParams } from "react-router-dom";
|
|
8
8
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
9
9
|
import { debounce } from "lodash";
|
|
@@ -15,8 +15,8 @@ var axios$3 = { exports: {} };
|
|
|
15
15
|
var bind$2 = function bind(fn, thisArg) {
|
|
16
16
|
return function wrap() {
|
|
17
17
|
var args = new Array(arguments.length);
|
|
18
|
-
for (var
|
|
19
|
-
args[
|
|
18
|
+
for (var i2 = 0; i2 < args.length; i2++) {
|
|
19
|
+
args[i2] = arguments[i2];
|
|
20
20
|
}
|
|
21
21
|
return fn.apply(thisArg, args);
|
|
22
22
|
};
|
|
@@ -26,11 +26,11 @@ var toString = Object.prototype.toString;
|
|
|
26
26
|
function isArray(val) {
|
|
27
27
|
return toString.call(val) === "[object Array]";
|
|
28
28
|
}
|
|
29
|
-
function isUndefined(val) {
|
|
29
|
+
function isUndefined$1(val) {
|
|
30
30
|
return typeof val === "undefined";
|
|
31
31
|
}
|
|
32
32
|
function isBuffer(val) {
|
|
33
|
-
return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor) && typeof val.constructor.isBuffer === "function" && val.constructor.isBuffer(val);
|
|
33
|
+
return val !== null && !isUndefined$1(val) && val.constructor !== null && !isUndefined$1(val.constructor) && typeof val.constructor.isBuffer === "function" && val.constructor.isBuffer(val);
|
|
34
34
|
}
|
|
35
35
|
function isArrayBuffer(val) {
|
|
36
36
|
return toString.call(val) === "[object ArrayBuffer]";
|
|
@@ -47,16 +47,16 @@ function isArrayBufferView(val) {
|
|
|
47
47
|
}
|
|
48
48
|
return result;
|
|
49
49
|
}
|
|
50
|
-
function isString(val) {
|
|
50
|
+
function isString$1(val) {
|
|
51
51
|
return typeof val === "string";
|
|
52
52
|
}
|
|
53
53
|
function isNumber(val) {
|
|
54
54
|
return typeof val === "number";
|
|
55
55
|
}
|
|
56
|
-
function isObject(val) {
|
|
56
|
+
function isObject$1(val) {
|
|
57
57
|
return val !== null && typeof val === "object";
|
|
58
58
|
}
|
|
59
|
-
function isPlainObject(val) {
|
|
59
|
+
function isPlainObject$1(val) {
|
|
60
60
|
if (toString.call(val) !== "[object Object]") {
|
|
61
61
|
return false;
|
|
62
62
|
}
|
|
@@ -72,11 +72,11 @@ function isFile(val) {
|
|
|
72
72
|
function isBlob(val) {
|
|
73
73
|
return toString.call(val) === "[object Blob]";
|
|
74
74
|
}
|
|
75
|
-
function isFunction(val) {
|
|
75
|
+
function isFunction$1(val) {
|
|
76
76
|
return toString.call(val) === "[object Function]";
|
|
77
77
|
}
|
|
78
78
|
function isStream(val) {
|
|
79
|
-
return isObject(val) && isFunction(val.pipe);
|
|
79
|
+
return isObject$1(val) && isFunction$1(val.pipe);
|
|
80
80
|
}
|
|
81
81
|
function isURLSearchParams(val) {
|
|
82
82
|
return typeof URLSearchParams !== "undefined" && val instanceof URLSearchParams;
|
|
@@ -98,8 +98,8 @@ function forEach(obj, fn) {
|
|
|
98
98
|
obj = [obj];
|
|
99
99
|
}
|
|
100
100
|
if (isArray(obj)) {
|
|
101
|
-
for (var
|
|
102
|
-
fn.call(null, obj[
|
|
101
|
+
for (var i2 = 0, l = obj.length; i2 < l; i2++) {
|
|
102
|
+
fn.call(null, obj[i2], i2, obj);
|
|
103
103
|
}
|
|
104
104
|
} else {
|
|
105
105
|
for (var key in obj) {
|
|
@@ -112,9 +112,9 @@ function forEach(obj, fn) {
|
|
|
112
112
|
function merge() {
|
|
113
113
|
var result = {};
|
|
114
114
|
function assignValue(val, key) {
|
|
115
|
-
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
|
115
|
+
if (isPlainObject$1(result[key]) && isPlainObject$1(val)) {
|
|
116
116
|
result[key] = merge(result[key], val);
|
|
117
|
-
} else if (isPlainObject(val)) {
|
|
117
|
+
} else if (isPlainObject$1(val)) {
|
|
118
118
|
result[key] = merge({}, val);
|
|
119
119
|
} else if (isArray(val)) {
|
|
120
120
|
result[key] = val.slice();
|
|
@@ -122,20 +122,20 @@ function merge() {
|
|
|
122
122
|
result[key] = val;
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
|
-
for (var
|
|
126
|
-
forEach(arguments[
|
|
125
|
+
for (var i2 = 0, l = arguments.length; i2 < l; i2++) {
|
|
126
|
+
forEach(arguments[i2], assignValue);
|
|
127
127
|
}
|
|
128
128
|
return result;
|
|
129
129
|
}
|
|
130
|
-
function extend(
|
|
130
|
+
function extend(a2, b, thisArg) {
|
|
131
131
|
forEach(b, function assignValue(val, key) {
|
|
132
132
|
if (thisArg && typeof val === "function") {
|
|
133
|
-
|
|
133
|
+
a2[key] = bind$1(val, thisArg);
|
|
134
134
|
} else {
|
|
135
|
-
|
|
135
|
+
a2[key] = val;
|
|
136
136
|
}
|
|
137
137
|
});
|
|
138
|
-
return
|
|
138
|
+
return a2;
|
|
139
139
|
}
|
|
140
140
|
function stripBOM(content) {
|
|
141
141
|
if (content.charCodeAt(0) === 65279) {
|
|
@@ -149,15 +149,15 @@ var utils$9 = {
|
|
|
149
149
|
isBuffer,
|
|
150
150
|
isFormData,
|
|
151
151
|
isArrayBufferView,
|
|
152
|
-
isString,
|
|
152
|
+
isString: isString$1,
|
|
153
153
|
isNumber,
|
|
154
|
-
isObject,
|
|
155
|
-
isPlainObject,
|
|
156
|
-
isUndefined,
|
|
154
|
+
isObject: isObject$1,
|
|
155
|
+
isPlainObject: isPlainObject$1,
|
|
156
|
+
isUndefined: isUndefined$1,
|
|
157
157
|
isDate,
|
|
158
158
|
isFile,
|
|
159
159
|
isBlob,
|
|
160
|
-
isFunction,
|
|
160
|
+
isFunction: isFunction$1,
|
|
161
161
|
isStream,
|
|
162
162
|
isURLSearchParams,
|
|
163
163
|
isStandardBrowserEnv,
|
|
@@ -424,14 +424,14 @@ function requireParseHeaders() {
|
|
|
424
424
|
var parsed = {};
|
|
425
425
|
var key;
|
|
426
426
|
var val;
|
|
427
|
-
var
|
|
427
|
+
var i2;
|
|
428
428
|
if (!headers) {
|
|
429
429
|
return parsed;
|
|
430
430
|
}
|
|
431
431
|
utils2.forEach(headers.split("\n"), function parser(line) {
|
|
432
|
-
|
|
433
|
-
key = utils2.trim(line.substr(0,
|
|
434
|
-
val = utils2.trim(line.substr(
|
|
432
|
+
i2 = line.indexOf(":");
|
|
433
|
+
key = utils2.trim(line.substr(0, i2)).toLowerCase();
|
|
434
|
+
val = utils2.trim(line.substr(i2 + 1));
|
|
435
435
|
if (key) {
|
|
436
436
|
if (parsed[key] && ignoreDuplicateOf.indexOf(key) >= 0) {
|
|
437
437
|
return;
|
|
@@ -648,9 +648,9 @@ function stringifySafely(rawValue, parser, encoder) {
|
|
|
648
648
|
try {
|
|
649
649
|
(parser || JSON.parse)(rawValue);
|
|
650
650
|
return utils$5.trim(rawValue);
|
|
651
|
-
} catch (
|
|
652
|
-
if (
|
|
653
|
-
throw
|
|
651
|
+
} catch (e2) {
|
|
652
|
+
if (e2.name !== "SyntaxError") {
|
|
653
|
+
throw e2;
|
|
654
654
|
}
|
|
655
655
|
}
|
|
656
656
|
}
|
|
@@ -690,12 +690,12 @@ var defaults$3 = {
|
|
|
690
690
|
if (strictJSONParsing || forcedJSONParsing && utils$5.isString(data) && data.length) {
|
|
691
691
|
try {
|
|
692
692
|
return JSON.parse(data);
|
|
693
|
-
} catch (
|
|
693
|
+
} catch (e2) {
|
|
694
694
|
if (strictJSONParsing) {
|
|
695
|
-
if (
|
|
696
|
-
throw enhanceError2(
|
|
695
|
+
if (e2.name === "SyntaxError") {
|
|
696
|
+
throw enhanceError2(e2, this, "E_JSON_PARSE");
|
|
697
697
|
}
|
|
698
|
-
throw
|
|
698
|
+
throw e2;
|
|
699
699
|
}
|
|
700
700
|
}
|
|
701
701
|
}
|
|
@@ -881,9 +881,9 @@ const require$$0 = {
|
|
|
881
881
|
};
|
|
882
882
|
var pkg = require$$0;
|
|
883
883
|
var validators$1 = {};
|
|
884
|
-
["object", "boolean", "number", "function", "string", "symbol"].forEach(function(type,
|
|
884
|
+
["object", "boolean", "number", "function", "string", "symbol"].forEach(function(type, i2) {
|
|
885
885
|
validators$1[type] = function validator2(thing) {
|
|
886
|
-
return typeof thing === type || "a" + (
|
|
886
|
+
return typeof thing === type || "a" + (i2 < 1 ? "n " : " ") + type;
|
|
887
887
|
};
|
|
888
888
|
});
|
|
889
889
|
var deprecatedWarnings = {};
|
|
@@ -891,10 +891,10 @@ var currentVerArr = pkg.version.split(".");
|
|
|
891
891
|
function isOlderVersion(version2, thanVersion) {
|
|
892
892
|
var pkgVersionArr = thanVersion ? thanVersion.split(".") : currentVerArr;
|
|
893
893
|
var destVer = version2.split(".");
|
|
894
|
-
for (var
|
|
895
|
-
if (pkgVersionArr[
|
|
894
|
+
for (var i2 = 0; i2 < 3; i2++) {
|
|
895
|
+
if (pkgVersionArr[i2] > destVer[i2]) {
|
|
896
896
|
return true;
|
|
897
|
-
} else if (pkgVersionArr[
|
|
897
|
+
} else if (pkgVersionArr[i2] < destVer[i2]) {
|
|
898
898
|
return false;
|
|
899
899
|
}
|
|
900
900
|
}
|
|
@@ -926,9 +926,9 @@ function assertOptions(options, schema, allowUnknown) {
|
|
|
926
926
|
throw new TypeError("options must be an object");
|
|
927
927
|
}
|
|
928
928
|
var keys = Object.keys(options);
|
|
929
|
-
var
|
|
930
|
-
while (
|
|
931
|
-
var opt = keys[
|
|
929
|
+
var i2 = keys.length;
|
|
930
|
+
while (i2-- > 0) {
|
|
931
|
+
var opt = keys[i2];
|
|
932
932
|
var validator2 = schema[opt];
|
|
933
933
|
if (validator2) {
|
|
934
934
|
var value = options[opt];
|
|
@@ -1098,8 +1098,8 @@ function requireCancelToken() {
|
|
|
1098
1098
|
};
|
|
1099
1099
|
CancelToken.source = function source() {
|
|
1100
1100
|
var cancel;
|
|
1101
|
-
var token = new CancelToken(function executor(
|
|
1102
|
-
cancel =
|
|
1101
|
+
var token = new CancelToken(function executor(c2) {
|
|
1102
|
+
cancel = c2;
|
|
1103
1103
|
});
|
|
1104
1104
|
return {
|
|
1105
1105
|
token,
|
|
@@ -1225,7 +1225,7 @@ const FlashSalePage = () => {
|
|
|
1225
1225
|
const table = useDataTable({
|
|
1226
1226
|
data: (data == null ? void 0 : data.campaigns) || [],
|
|
1227
1227
|
columns: columns2,
|
|
1228
|
-
getRowId: (
|
|
1228
|
+
getRowId: (campaign2) => campaign2.id,
|
|
1229
1229
|
pagination: {
|
|
1230
1230
|
state: pagination,
|
|
1231
1231
|
onPaginationChange: setPagination
|
|
@@ -1253,6 +1253,1887 @@ const config$1 = defineRouteConfig({
|
|
|
1253
1253
|
label: "Flash Sale",
|
|
1254
1254
|
icon: Sparkles
|
|
1255
1255
|
});
|
|
1256
|
+
var isCheckBoxInput = (element) => element.type === "checkbox";
|
|
1257
|
+
var isDateObject = (value) => value instanceof Date;
|
|
1258
|
+
var isNullOrUndefined = (value) => value == null;
|
|
1259
|
+
const isObjectType = (value) => typeof value === "object";
|
|
1260
|
+
var isObject = (value) => !isNullOrUndefined(value) && !Array.isArray(value) && isObjectType(value) && !isDateObject(value);
|
|
1261
|
+
var getEventValue = (event) => isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
|
|
1262
|
+
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
1263
|
+
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
1264
|
+
var isPlainObject = (tempObject) => {
|
|
1265
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
1266
|
+
return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty("isPrototypeOf");
|
|
1267
|
+
};
|
|
1268
|
+
var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
|
|
1269
|
+
function cloneObject(data) {
|
|
1270
|
+
let copy;
|
|
1271
|
+
const isArray2 = Array.isArray(data);
|
|
1272
|
+
if (data instanceof Date) {
|
|
1273
|
+
copy = new Date(data);
|
|
1274
|
+
} else if (data instanceof Set) {
|
|
1275
|
+
copy = new Set(data);
|
|
1276
|
+
} else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) && (isArray2 || isObject(data))) {
|
|
1277
|
+
copy = isArray2 ? [] : {};
|
|
1278
|
+
if (!isArray2 && !isPlainObject(data)) {
|
|
1279
|
+
copy = data;
|
|
1280
|
+
} else {
|
|
1281
|
+
for (const key in data) {
|
|
1282
|
+
if (data.hasOwnProperty(key)) {
|
|
1283
|
+
copy[key] = cloneObject(data[key]);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
} else {
|
|
1288
|
+
return data;
|
|
1289
|
+
}
|
|
1290
|
+
return copy;
|
|
1291
|
+
}
|
|
1292
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
|
1293
|
+
var isUndefined = (val) => val === void 0;
|
|
1294
|
+
var get = (object, path, defaultValue) => {
|
|
1295
|
+
if (!path || !isObject(object)) {
|
|
1296
|
+
return defaultValue;
|
|
1297
|
+
}
|
|
1298
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result2, key) => isNullOrUndefined(result2) ? result2 : result2[key], object);
|
|
1299
|
+
return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
|
|
1300
|
+
};
|
|
1301
|
+
var isBoolean = (value) => typeof value === "boolean";
|
|
1302
|
+
const EVENTS = {
|
|
1303
|
+
BLUR: "blur",
|
|
1304
|
+
FOCUS_OUT: "focusout",
|
|
1305
|
+
CHANGE: "change"
|
|
1306
|
+
};
|
|
1307
|
+
const VALIDATION_MODE = {
|
|
1308
|
+
onBlur: "onBlur",
|
|
1309
|
+
onChange: "onChange",
|
|
1310
|
+
onSubmit: "onSubmit",
|
|
1311
|
+
onTouched: "onTouched",
|
|
1312
|
+
all: "all"
|
|
1313
|
+
};
|
|
1314
|
+
const INPUT_VALIDATION_RULES = {
|
|
1315
|
+
max: "max",
|
|
1316
|
+
min: "min",
|
|
1317
|
+
maxLength: "maxLength",
|
|
1318
|
+
minLength: "minLength",
|
|
1319
|
+
pattern: "pattern",
|
|
1320
|
+
required: "required",
|
|
1321
|
+
validate: "validate"
|
|
1322
|
+
};
|
|
1323
|
+
const HookFormContext = React.createContext(null);
|
|
1324
|
+
const useFormContext = () => React.useContext(HookFormContext);
|
|
1325
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
1326
|
+
const result = {
|
|
1327
|
+
defaultValues: control._defaultValues
|
|
1328
|
+
};
|
|
1329
|
+
for (const key in formState) {
|
|
1330
|
+
Object.defineProperty(result, key, {
|
|
1331
|
+
get: () => {
|
|
1332
|
+
const _key = key;
|
|
1333
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
1334
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
1335
|
+
}
|
|
1336
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
|
1337
|
+
return formState[_key];
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1341
|
+
return result;
|
|
1342
|
+
};
|
|
1343
|
+
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
1344
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
1345
|
+
updateFormState(formStateData);
|
|
1346
|
+
const { name, ...formState } = formStateData;
|
|
1347
|
+
return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key) => _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
|
|
1348
|
+
};
|
|
1349
|
+
var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
|
|
1350
|
+
var shouldSubscribeByName = (name, signalName, exact) => !name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName) => currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName)));
|
|
1351
|
+
function useSubscribe(props) {
|
|
1352
|
+
const _props = React.useRef(props);
|
|
1353
|
+
_props.current = props;
|
|
1354
|
+
React.useEffect(() => {
|
|
1355
|
+
const subscription = !props.disabled && _props.current.subject && _props.current.subject.subscribe({
|
|
1356
|
+
next: _props.current.next
|
|
1357
|
+
});
|
|
1358
|
+
return () => {
|
|
1359
|
+
subscription && subscription.unsubscribe();
|
|
1360
|
+
};
|
|
1361
|
+
}, [props.disabled]);
|
|
1362
|
+
}
|
|
1363
|
+
function useFormState(props) {
|
|
1364
|
+
const methods = useFormContext();
|
|
1365
|
+
const { control = methods.control, disabled, name, exact } = props || {};
|
|
1366
|
+
const [formState, updateFormState] = React.useState(control._formState);
|
|
1367
|
+
const _mounted = React.useRef(true);
|
|
1368
|
+
const _localProxyFormState = React.useRef({
|
|
1369
|
+
isDirty: false,
|
|
1370
|
+
isLoading: false,
|
|
1371
|
+
dirtyFields: false,
|
|
1372
|
+
touchedFields: false,
|
|
1373
|
+
isValidating: false,
|
|
1374
|
+
isValid: false,
|
|
1375
|
+
errors: false
|
|
1376
|
+
});
|
|
1377
|
+
const _name = React.useRef(name);
|
|
1378
|
+
_name.current = name;
|
|
1379
|
+
useSubscribe({
|
|
1380
|
+
disabled,
|
|
1381
|
+
next: (value) => _mounted.current && shouldSubscribeByName(_name.current, value.name, exact) && shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) && updateFormState({
|
|
1382
|
+
...control._formState,
|
|
1383
|
+
...value
|
|
1384
|
+
}),
|
|
1385
|
+
subject: control._subjects.state
|
|
1386
|
+
});
|
|
1387
|
+
React.useEffect(() => {
|
|
1388
|
+
_mounted.current = true;
|
|
1389
|
+
_localProxyFormState.current.isValid && control._updateValid(true);
|
|
1390
|
+
return () => {
|
|
1391
|
+
_mounted.current = false;
|
|
1392
|
+
};
|
|
1393
|
+
}, [control]);
|
|
1394
|
+
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
1395
|
+
}
|
|
1396
|
+
var isString = (value) => typeof value === "string";
|
|
1397
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
|
1398
|
+
if (isString(names)) {
|
|
1399
|
+
isGlobal && _names.watch.add(names);
|
|
1400
|
+
return get(formValues, names, defaultValue);
|
|
1401
|
+
}
|
|
1402
|
+
if (Array.isArray(names)) {
|
|
1403
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
1404
|
+
}
|
|
1405
|
+
isGlobal && (_names.watchAll = true);
|
|
1406
|
+
return formValues;
|
|
1407
|
+
};
|
|
1408
|
+
function useWatch(props) {
|
|
1409
|
+
const methods = useFormContext();
|
|
1410
|
+
const { control = methods.control, name, defaultValue, disabled, exact } = props || {};
|
|
1411
|
+
const _name = React.useRef(name);
|
|
1412
|
+
_name.current = name;
|
|
1413
|
+
useSubscribe({
|
|
1414
|
+
disabled,
|
|
1415
|
+
subject: control._subjects.values,
|
|
1416
|
+
next: (formState) => {
|
|
1417
|
+
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
|
1418
|
+
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
});
|
|
1422
|
+
const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
|
|
1423
|
+
React.useEffect(() => control._removeUnmounted());
|
|
1424
|
+
return value;
|
|
1425
|
+
}
|
|
1426
|
+
var isKey = (value) => /^\w*$/.test(value);
|
|
1427
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, "").split(/\.|\[/));
|
|
1428
|
+
var set = (object, path, value) => {
|
|
1429
|
+
let index = -1;
|
|
1430
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
1431
|
+
const length = tempPath.length;
|
|
1432
|
+
const lastIndex = length - 1;
|
|
1433
|
+
while (++index < length) {
|
|
1434
|
+
const key = tempPath[index];
|
|
1435
|
+
let newValue = value;
|
|
1436
|
+
if (index !== lastIndex) {
|
|
1437
|
+
const objValue = object[key];
|
|
1438
|
+
newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
|
|
1439
|
+
}
|
|
1440
|
+
object[key] = newValue;
|
|
1441
|
+
object = object[key];
|
|
1442
|
+
}
|
|
1443
|
+
return object;
|
|
1444
|
+
};
|
|
1445
|
+
function useController(props) {
|
|
1446
|
+
const methods = useFormContext();
|
|
1447
|
+
const { name, disabled, control = methods.control, shouldUnregister } = props;
|
|
1448
|
+
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
1449
|
+
const value = useWatch({
|
|
1450
|
+
control,
|
|
1451
|
+
name,
|
|
1452
|
+
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
|
1453
|
+
exact: true
|
|
1454
|
+
});
|
|
1455
|
+
const formState = useFormState({
|
|
1456
|
+
control,
|
|
1457
|
+
name
|
|
1458
|
+
});
|
|
1459
|
+
const _registerProps = React.useRef(control.register(name, {
|
|
1460
|
+
...props.rules,
|
|
1461
|
+
value,
|
|
1462
|
+
...isBoolean(props.disabled) ? { disabled: props.disabled } : {}
|
|
1463
|
+
}));
|
|
1464
|
+
React.useEffect(() => {
|
|
1465
|
+
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
1466
|
+
const updateMounted = (name2, value2) => {
|
|
1467
|
+
const field = get(control._fields, name2);
|
|
1468
|
+
if (field) {
|
|
1469
|
+
field._f.mount = value2;
|
|
1470
|
+
}
|
|
1471
|
+
};
|
|
1472
|
+
updateMounted(name, true);
|
|
1473
|
+
if (_shouldUnregisterField) {
|
|
1474
|
+
const value2 = cloneObject(get(control._options.defaultValues, name));
|
|
1475
|
+
set(control._defaultValues, name, value2);
|
|
1476
|
+
if (isUndefined(get(control._formValues, name))) {
|
|
1477
|
+
set(control._formValues, name, value2);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
return () => {
|
|
1481
|
+
(isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
|
|
1482
|
+
};
|
|
1483
|
+
}, [name, control, isArrayField, shouldUnregister]);
|
|
1484
|
+
React.useEffect(() => {
|
|
1485
|
+
if (get(control._fields, name)) {
|
|
1486
|
+
control._updateDisabledField({
|
|
1487
|
+
disabled,
|
|
1488
|
+
fields: control._fields,
|
|
1489
|
+
name,
|
|
1490
|
+
value: get(control._fields, name)._f.value
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
}, [disabled, name, control]);
|
|
1494
|
+
return {
|
|
1495
|
+
field: {
|
|
1496
|
+
name,
|
|
1497
|
+
value,
|
|
1498
|
+
...isBoolean(disabled) || isBoolean(formState.disabled) ? { disabled: formState.disabled || disabled } : {},
|
|
1499
|
+
onChange: React.useCallback((event) => _registerProps.current.onChange({
|
|
1500
|
+
target: {
|
|
1501
|
+
value: getEventValue(event),
|
|
1502
|
+
name
|
|
1503
|
+
},
|
|
1504
|
+
type: EVENTS.CHANGE
|
|
1505
|
+
}), [name]),
|
|
1506
|
+
onBlur: React.useCallback(() => _registerProps.current.onBlur({
|
|
1507
|
+
target: {
|
|
1508
|
+
value: get(control._formValues, name),
|
|
1509
|
+
name
|
|
1510
|
+
},
|
|
1511
|
+
type: EVENTS.BLUR
|
|
1512
|
+
}), [name, control]),
|
|
1513
|
+
ref: (elm) => {
|
|
1514
|
+
const field = get(control._fields, name);
|
|
1515
|
+
if (field && elm) {
|
|
1516
|
+
field._f.ref = {
|
|
1517
|
+
focus: () => elm.focus(),
|
|
1518
|
+
select: () => elm.select(),
|
|
1519
|
+
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
1520
|
+
reportValidity: () => elm.reportValidity()
|
|
1521
|
+
};
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
},
|
|
1525
|
+
formState,
|
|
1526
|
+
fieldState: Object.defineProperties({}, {
|
|
1527
|
+
invalid: {
|
|
1528
|
+
enumerable: true,
|
|
1529
|
+
get: () => !!get(formState.errors, name)
|
|
1530
|
+
},
|
|
1531
|
+
isDirty: {
|
|
1532
|
+
enumerable: true,
|
|
1533
|
+
get: () => !!get(formState.dirtyFields, name)
|
|
1534
|
+
},
|
|
1535
|
+
isTouched: {
|
|
1536
|
+
enumerable: true,
|
|
1537
|
+
get: () => !!get(formState.touchedFields, name)
|
|
1538
|
+
},
|
|
1539
|
+
error: {
|
|
1540
|
+
enumerable: true,
|
|
1541
|
+
get: () => get(formState.errors, name)
|
|
1542
|
+
}
|
|
1543
|
+
})
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
const Controller = (props) => props.render(useController(props));
|
|
1547
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria ? {
|
|
1548
|
+
...errors[name],
|
|
1549
|
+
types: {
|
|
1550
|
+
...errors[name] && errors[name].types ? errors[name].types : {},
|
|
1551
|
+
[type]: message || true
|
|
1552
|
+
}
|
|
1553
|
+
} : {};
|
|
1554
|
+
var generateId = () => {
|
|
1555
|
+
const d = typeof performance === "undefined" ? Date.now() : performance.now() * 1e3;
|
|
1556
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c2) => {
|
|
1557
|
+
const r = (Math.random() * 16 + d) % 16 | 0;
|
|
1558
|
+
return (c2 == "x" ? r : r & 3 | 8).toString(16);
|
|
1559
|
+
});
|
|
1560
|
+
};
|
|
1561
|
+
var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined(options.shouldFocus) ? options.focusName || `${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.` : "";
|
|
1562
|
+
var getValidationModes = (mode) => ({
|
|
1563
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
1564
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
1565
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
1566
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
1567
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched
|
|
1568
|
+
});
|
|
1569
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent && (_names.watchAll || _names.watch.has(name) || [..._names.watch].some((watchName) => name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length))));
|
|
1570
|
+
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
|
1571
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
|
1572
|
+
const field = get(fields, key);
|
|
1573
|
+
if (field) {
|
|
1574
|
+
const { _f, ...currentField } = field;
|
|
1575
|
+
if (_f) {
|
|
1576
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
1577
|
+
break;
|
|
1578
|
+
} else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
1579
|
+
break;
|
|
1580
|
+
} else {
|
|
1581
|
+
iterateFieldsByAction(currentField, action);
|
|
1582
|
+
}
|
|
1583
|
+
} else if (isObject(currentField)) {
|
|
1584
|
+
iterateFieldsByAction(currentField, action);
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
};
|
|
1589
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
|
1590
|
+
const fieldArrayErrors = compact(get(errors, name));
|
|
1591
|
+
set(fieldArrayErrors, "root", error[name]);
|
|
1592
|
+
set(errors, name, fieldArrayErrors);
|
|
1593
|
+
return errors;
|
|
1594
|
+
};
|
|
1595
|
+
var isFileInput = (element) => element.type === "file";
|
|
1596
|
+
var isFunction = (value) => typeof value === "function";
|
|
1597
|
+
var isHTMLElement = (value) => {
|
|
1598
|
+
if (!isWeb) {
|
|
1599
|
+
return false;
|
|
1600
|
+
}
|
|
1601
|
+
const owner = value ? value.ownerDocument : 0;
|
|
1602
|
+
return value instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
|
|
1603
|
+
};
|
|
1604
|
+
var isMessage = (value) => isString(value);
|
|
1605
|
+
var isRadioInput = (element) => element.type === "radio";
|
|
1606
|
+
var isRegex = (value) => value instanceof RegExp;
|
|
1607
|
+
const defaultResult = {
|
|
1608
|
+
value: false,
|
|
1609
|
+
isValid: false
|
|
1610
|
+
};
|
|
1611
|
+
const validResult = { value: true, isValid: true };
|
|
1612
|
+
var getCheckboxValue = (options) => {
|
|
1613
|
+
if (Array.isArray(options)) {
|
|
1614
|
+
if (options.length > 1) {
|
|
1615
|
+
const values = options.filter((option) => option && option.checked && !option.disabled).map((option) => option.value);
|
|
1616
|
+
return { value: values, isValid: !!values.length };
|
|
1617
|
+
}
|
|
1618
|
+
return options[0].checked && !options[0].disabled ? (
|
|
1619
|
+
// @ts-expect-error expected to work in the browser
|
|
1620
|
+
options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === "" ? validResult : { value: options[0].value, isValid: true } : validResult
|
|
1621
|
+
) : defaultResult;
|
|
1622
|
+
}
|
|
1623
|
+
return defaultResult;
|
|
1624
|
+
};
|
|
1625
|
+
const defaultReturn = {
|
|
1626
|
+
isValid: false,
|
|
1627
|
+
value: null
|
|
1628
|
+
};
|
|
1629
|
+
var getRadioValue = (options) => Array.isArray(options) ? options.reduce((previous, option) => option && option.checked && !option.disabled ? {
|
|
1630
|
+
isValid: true,
|
|
1631
|
+
value: option.value
|
|
1632
|
+
} : previous, defaultReturn) : defaultReturn;
|
|
1633
|
+
function getValidateError(result, ref, type = "validate") {
|
|
1634
|
+
if (isMessage(result) || Array.isArray(result) && result.every(isMessage) || isBoolean(result) && !result) {
|
|
1635
|
+
return {
|
|
1636
|
+
type,
|
|
1637
|
+
message: isMessage(result) ? result : "",
|
|
1638
|
+
ref
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData) ? validationData : {
|
|
1643
|
+
value: validationData,
|
|
1644
|
+
message: ""
|
|
1645
|
+
};
|
|
1646
|
+
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
1647
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled } = field._f;
|
|
1648
|
+
const inputValue = get(formValues, name);
|
|
1649
|
+
if (!mount || disabled) {
|
|
1650
|
+
return {};
|
|
1651
|
+
}
|
|
1652
|
+
const inputRef = refs ? refs[0] : ref;
|
|
1653
|
+
const setCustomValidity = (message) => {
|
|
1654
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
1655
|
+
inputRef.setCustomValidity(isBoolean(message) ? "" : message || "");
|
|
1656
|
+
inputRef.reportValidity();
|
|
1657
|
+
}
|
|
1658
|
+
};
|
|
1659
|
+
const error = {};
|
|
1660
|
+
const isRadio = isRadioInput(ref);
|
|
1661
|
+
const isCheckBox = isCheckBoxInput(ref);
|
|
1662
|
+
const isRadioOrCheckbox2 = isRadio || isCheckBox;
|
|
1663
|
+
const isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === "" || inputValue === "" || Array.isArray(inputValue) && !inputValue.length;
|
|
1664
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
1665
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
1666
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
1667
|
+
error[name] = {
|
|
1668
|
+
type: exceedMax ? maxType : minType,
|
|
1669
|
+
message,
|
|
1670
|
+
ref,
|
|
1671
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message)
|
|
1672
|
+
};
|
|
1673
|
+
};
|
|
1674
|
+
if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox2 && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
|
|
1675
|
+
const { value, message } = isMessage(required) ? { value: !!required, message: required } : getValueAndMessage(required);
|
|
1676
|
+
if (value) {
|
|
1677
|
+
error[name] = {
|
|
1678
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
1679
|
+
message,
|
|
1680
|
+
ref: inputRef,
|
|
1681
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message)
|
|
1682
|
+
};
|
|
1683
|
+
if (!validateAllFieldCriteria) {
|
|
1684
|
+
setCustomValidity(message);
|
|
1685
|
+
return error;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
1690
|
+
let exceedMax;
|
|
1691
|
+
let exceedMin;
|
|
1692
|
+
const maxOutput = getValueAndMessage(max);
|
|
1693
|
+
const minOutput = getValueAndMessage(min);
|
|
1694
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
1695
|
+
const valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
|
|
1696
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
1697
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
1698
|
+
}
|
|
1699
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
1700
|
+
exceedMin = valueNumber < minOutput.value;
|
|
1701
|
+
}
|
|
1702
|
+
} else {
|
|
1703
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
1704
|
+
const convertTimeToDate = (time) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + time);
|
|
1705
|
+
const isTime = ref.type == "time";
|
|
1706
|
+
const isWeek = ref.type == "week";
|
|
1707
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
1708
|
+
exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
|
|
1709
|
+
}
|
|
1710
|
+
if (isString(minOutput.value) && inputValue) {
|
|
1711
|
+
exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
if (exceedMax || exceedMin) {
|
|
1715
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
1716
|
+
if (!validateAllFieldCriteria) {
|
|
1717
|
+
setCustomValidity(error[name].message);
|
|
1718
|
+
return error;
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
|
|
1723
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
1724
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
|
1725
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
|
|
1726
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
|
|
1727
|
+
if (exceedMax || exceedMin) {
|
|
1728
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
1729
|
+
if (!validateAllFieldCriteria) {
|
|
1730
|
+
setCustomValidity(error[name].message);
|
|
1731
|
+
return error;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
1736
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
1737
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
1738
|
+
error[name] = {
|
|
1739
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
1740
|
+
message,
|
|
1741
|
+
ref,
|
|
1742
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message)
|
|
1743
|
+
};
|
|
1744
|
+
if (!validateAllFieldCriteria) {
|
|
1745
|
+
setCustomValidity(message);
|
|
1746
|
+
return error;
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
if (validate) {
|
|
1751
|
+
if (isFunction(validate)) {
|
|
1752
|
+
const result = await validate(inputValue, formValues);
|
|
1753
|
+
const validateError = getValidateError(result, inputRef);
|
|
1754
|
+
if (validateError) {
|
|
1755
|
+
error[name] = {
|
|
1756
|
+
...validateError,
|
|
1757
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message)
|
|
1758
|
+
};
|
|
1759
|
+
if (!validateAllFieldCriteria) {
|
|
1760
|
+
setCustomValidity(validateError.message);
|
|
1761
|
+
return error;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
} else if (isObject(validate)) {
|
|
1765
|
+
let validationResult = {};
|
|
1766
|
+
for (const key in validate) {
|
|
1767
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
1768
|
+
break;
|
|
1769
|
+
}
|
|
1770
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
1771
|
+
if (validateError) {
|
|
1772
|
+
validationResult = {
|
|
1773
|
+
...validateError,
|
|
1774
|
+
...appendErrorsCurry(key, validateError.message)
|
|
1775
|
+
};
|
|
1776
|
+
setCustomValidity(validateError.message);
|
|
1777
|
+
if (validateAllFieldCriteria) {
|
|
1778
|
+
error[name] = validationResult;
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
if (!isEmptyObject(validationResult)) {
|
|
1783
|
+
error[name] = {
|
|
1784
|
+
ref: inputRef,
|
|
1785
|
+
...validationResult
|
|
1786
|
+
};
|
|
1787
|
+
if (!validateAllFieldCriteria) {
|
|
1788
|
+
return error;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
setCustomValidity(true);
|
|
1794
|
+
return error;
|
|
1795
|
+
};
|
|
1796
|
+
var appendAt = (data, value) => [
|
|
1797
|
+
...data,
|
|
1798
|
+
...convertToArrayPayload(value)
|
|
1799
|
+
];
|
|
1800
|
+
var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => void 0) : void 0;
|
|
1801
|
+
function insert(data, index, value) {
|
|
1802
|
+
return [
|
|
1803
|
+
...data.slice(0, index),
|
|
1804
|
+
...convertToArrayPayload(value),
|
|
1805
|
+
...data.slice(index)
|
|
1806
|
+
];
|
|
1807
|
+
}
|
|
1808
|
+
var moveArrayAt = (data, from, to) => {
|
|
1809
|
+
if (!Array.isArray(data)) {
|
|
1810
|
+
return [];
|
|
1811
|
+
}
|
|
1812
|
+
if (isUndefined(data[to])) {
|
|
1813
|
+
data[to] = void 0;
|
|
1814
|
+
}
|
|
1815
|
+
data.splice(to, 0, data.splice(from, 1)[0]);
|
|
1816
|
+
return data;
|
|
1817
|
+
};
|
|
1818
|
+
var prependAt = (data, value) => [
|
|
1819
|
+
...convertToArrayPayload(value),
|
|
1820
|
+
...convertToArrayPayload(data)
|
|
1821
|
+
];
|
|
1822
|
+
function removeAtIndexes(data, indexes) {
|
|
1823
|
+
let i2 = 0;
|
|
1824
|
+
const temp = [...data];
|
|
1825
|
+
for (const index of indexes) {
|
|
1826
|
+
temp.splice(index - i2, 1);
|
|
1827
|
+
i2++;
|
|
1828
|
+
}
|
|
1829
|
+
return compact(temp).length ? temp : [];
|
|
1830
|
+
}
|
|
1831
|
+
var removeArrayAt = (data, index) => isUndefined(index) ? [] : removeAtIndexes(data, convertToArrayPayload(index).sort((a2, b) => a2 - b));
|
|
1832
|
+
var swapArrayAt = (data, indexA, indexB) => {
|
|
1833
|
+
[data[indexA], data[indexB]] = [data[indexB], data[indexA]];
|
|
1834
|
+
};
|
|
1835
|
+
function baseGet(object, updatePath) {
|
|
1836
|
+
const length = updatePath.slice(0, -1).length;
|
|
1837
|
+
let index = 0;
|
|
1838
|
+
while (index < length) {
|
|
1839
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
1840
|
+
}
|
|
1841
|
+
return object;
|
|
1842
|
+
}
|
|
1843
|
+
function isEmptyArray(obj) {
|
|
1844
|
+
for (const key in obj) {
|
|
1845
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
1846
|
+
return false;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
return true;
|
|
1850
|
+
}
|
|
1851
|
+
function unset(object, path) {
|
|
1852
|
+
const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path);
|
|
1853
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
1854
|
+
const index = paths.length - 1;
|
|
1855
|
+
const key = paths[index];
|
|
1856
|
+
if (childObject) {
|
|
1857
|
+
delete childObject[key];
|
|
1858
|
+
}
|
|
1859
|
+
if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
|
|
1860
|
+
unset(object, paths.slice(0, -1));
|
|
1861
|
+
}
|
|
1862
|
+
return object;
|
|
1863
|
+
}
|
|
1864
|
+
var updateAt = (fieldValues, index, value) => {
|
|
1865
|
+
fieldValues[index] = value;
|
|
1866
|
+
return fieldValues;
|
|
1867
|
+
};
|
|
1868
|
+
function useFieldArray(props) {
|
|
1869
|
+
const methods = useFormContext();
|
|
1870
|
+
const { control = methods.control, name, keyName = "id", shouldUnregister } = props;
|
|
1871
|
+
const [fields, setFields] = React.useState(control._getFieldArray(name));
|
|
1872
|
+
const ids = React.useRef(control._getFieldArray(name).map(generateId));
|
|
1873
|
+
const _fieldIds = React.useRef(fields);
|
|
1874
|
+
const _name = React.useRef(name);
|
|
1875
|
+
const _actioned = React.useRef(false);
|
|
1876
|
+
_name.current = name;
|
|
1877
|
+
_fieldIds.current = fields;
|
|
1878
|
+
control._names.array.add(name);
|
|
1879
|
+
props.rules && control.register(name, props.rules);
|
|
1880
|
+
useSubscribe({
|
|
1881
|
+
next: ({ values, name: fieldArrayName }) => {
|
|
1882
|
+
if (fieldArrayName === _name.current || !fieldArrayName) {
|
|
1883
|
+
const fieldValues = get(values, _name.current);
|
|
1884
|
+
if (Array.isArray(fieldValues)) {
|
|
1885
|
+
setFields(fieldValues);
|
|
1886
|
+
ids.current = fieldValues.map(generateId);
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
},
|
|
1890
|
+
subject: control._subjects.array
|
|
1891
|
+
});
|
|
1892
|
+
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
|
1893
|
+
_actioned.current = true;
|
|
1894
|
+
control._updateFieldArray(name, updatedFieldArrayValues);
|
|
1895
|
+
}, [control, name]);
|
|
1896
|
+
const append = (value, options) => {
|
|
1897
|
+
const appendValue = convertToArrayPayload(cloneObject(value));
|
|
1898
|
+
const updatedFieldArrayValues = appendAt(control._getFieldArray(name), appendValue);
|
|
1899
|
+
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
|
|
1900
|
+
ids.current = appendAt(ids.current, appendValue.map(generateId));
|
|
1901
|
+
updateValues(updatedFieldArrayValues);
|
|
1902
|
+
setFields(updatedFieldArrayValues);
|
|
1903
|
+
control._updateFieldArray(name, updatedFieldArrayValues, appendAt, {
|
|
1904
|
+
argA: fillEmptyArray(value)
|
|
1905
|
+
});
|
|
1906
|
+
};
|
|
1907
|
+
const prepend = (value, options) => {
|
|
1908
|
+
const prependValue = convertToArrayPayload(cloneObject(value));
|
|
1909
|
+
const updatedFieldArrayValues = prependAt(control._getFieldArray(name), prependValue);
|
|
1910
|
+
control._names.focus = getFocusFieldName(name, 0, options);
|
|
1911
|
+
ids.current = prependAt(ids.current, prependValue.map(generateId));
|
|
1912
|
+
updateValues(updatedFieldArrayValues);
|
|
1913
|
+
setFields(updatedFieldArrayValues);
|
|
1914
|
+
control._updateFieldArray(name, updatedFieldArrayValues, prependAt, {
|
|
1915
|
+
argA: fillEmptyArray(value)
|
|
1916
|
+
});
|
|
1917
|
+
};
|
|
1918
|
+
const remove = (index) => {
|
|
1919
|
+
const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
|
|
1920
|
+
ids.current = removeArrayAt(ids.current, index);
|
|
1921
|
+
updateValues(updatedFieldArrayValues);
|
|
1922
|
+
setFields(updatedFieldArrayValues);
|
|
1923
|
+
control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
|
|
1924
|
+
argA: index
|
|
1925
|
+
});
|
|
1926
|
+
};
|
|
1927
|
+
const insert$1 = (index, value, options) => {
|
|
1928
|
+
const insertValue = convertToArrayPayload(cloneObject(value));
|
|
1929
|
+
const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
|
|
1930
|
+
control._names.focus = getFocusFieldName(name, index, options);
|
|
1931
|
+
ids.current = insert(ids.current, index, insertValue.map(generateId));
|
|
1932
|
+
updateValues(updatedFieldArrayValues);
|
|
1933
|
+
setFields(updatedFieldArrayValues);
|
|
1934
|
+
control._updateFieldArray(name, updatedFieldArrayValues, insert, {
|
|
1935
|
+
argA: index,
|
|
1936
|
+
argB: fillEmptyArray(value)
|
|
1937
|
+
});
|
|
1938
|
+
};
|
|
1939
|
+
const swap = (indexA, indexB) => {
|
|
1940
|
+
const updatedFieldArrayValues = control._getFieldArray(name);
|
|
1941
|
+
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
|
|
1942
|
+
swapArrayAt(ids.current, indexA, indexB);
|
|
1943
|
+
updateValues(updatedFieldArrayValues);
|
|
1944
|
+
setFields(updatedFieldArrayValues);
|
|
1945
|
+
control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
|
|
1946
|
+
argA: indexA,
|
|
1947
|
+
argB: indexB
|
|
1948
|
+
}, false);
|
|
1949
|
+
};
|
|
1950
|
+
const move = (from, to) => {
|
|
1951
|
+
const updatedFieldArrayValues = control._getFieldArray(name);
|
|
1952
|
+
moveArrayAt(updatedFieldArrayValues, from, to);
|
|
1953
|
+
moveArrayAt(ids.current, from, to);
|
|
1954
|
+
updateValues(updatedFieldArrayValues);
|
|
1955
|
+
setFields(updatedFieldArrayValues);
|
|
1956
|
+
control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
|
|
1957
|
+
argA: from,
|
|
1958
|
+
argB: to
|
|
1959
|
+
}, false);
|
|
1960
|
+
};
|
|
1961
|
+
const update = (index, value) => {
|
|
1962
|
+
const updateValue = cloneObject(value);
|
|
1963
|
+
const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
|
|
1964
|
+
ids.current = [...updatedFieldArrayValues].map((item, i2) => !item || i2 === index ? generateId() : ids.current[i2]);
|
|
1965
|
+
updateValues(updatedFieldArrayValues);
|
|
1966
|
+
setFields([...updatedFieldArrayValues]);
|
|
1967
|
+
control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
|
|
1968
|
+
argA: index,
|
|
1969
|
+
argB: updateValue
|
|
1970
|
+
}, true, false);
|
|
1971
|
+
};
|
|
1972
|
+
const replace = (value) => {
|
|
1973
|
+
const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
|
|
1974
|
+
ids.current = updatedFieldArrayValues.map(generateId);
|
|
1975
|
+
updateValues([...updatedFieldArrayValues]);
|
|
1976
|
+
setFields([...updatedFieldArrayValues]);
|
|
1977
|
+
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
|
1978
|
+
};
|
|
1979
|
+
React.useEffect(() => {
|
|
1980
|
+
control._state.action = false;
|
|
1981
|
+
isWatched(name, control._names) && control._subjects.state.next({
|
|
1982
|
+
...control._formState
|
|
1983
|
+
});
|
|
1984
|
+
if (_actioned.current && (!getValidationModes(control._options.mode).isOnSubmit || control._formState.isSubmitted)) {
|
|
1985
|
+
if (control._options.resolver) {
|
|
1986
|
+
control._executeSchema([name]).then((result) => {
|
|
1987
|
+
const error = get(result.errors, name);
|
|
1988
|
+
const existingError = get(control._formState.errors, name);
|
|
1989
|
+
if (existingError ? !error && existingError.type || error && (existingError.type !== error.type || existingError.message !== error.message) : error && error.type) {
|
|
1990
|
+
error ? set(control._formState.errors, name, error) : unset(control._formState.errors, name);
|
|
1991
|
+
control._subjects.state.next({
|
|
1992
|
+
errors: control._formState.errors
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
});
|
|
1996
|
+
} else {
|
|
1997
|
+
const field = get(control._fields, name);
|
|
1998
|
+
if (field && field._f) {
|
|
1999
|
+
validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) && control._subjects.state.next({
|
|
2000
|
+
errors: updateFieldArrayRootError(control._formState.errors, error, name)
|
|
2001
|
+
}));
|
|
2002
|
+
}
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
control._subjects.values.next({
|
|
2006
|
+
name,
|
|
2007
|
+
values: { ...control._formValues }
|
|
2008
|
+
});
|
|
2009
|
+
control._names.focus && iterateFieldsByAction(control._fields, (ref, key) => {
|
|
2010
|
+
if (control._names.focus && key.startsWith(control._names.focus) && ref.focus) {
|
|
2011
|
+
ref.focus();
|
|
2012
|
+
return 1;
|
|
2013
|
+
}
|
|
2014
|
+
return;
|
|
2015
|
+
});
|
|
2016
|
+
control._names.focus = "";
|
|
2017
|
+
control._updateValid();
|
|
2018
|
+
_actioned.current = false;
|
|
2019
|
+
}, [fields, name, control]);
|
|
2020
|
+
React.useEffect(() => {
|
|
2021
|
+
!get(control._formValues, name) && control._updateFieldArray(name);
|
|
2022
|
+
return () => {
|
|
2023
|
+
(control._options.shouldUnregister || shouldUnregister) && control.unregister(name);
|
|
2024
|
+
};
|
|
2025
|
+
}, [name, control, keyName, shouldUnregister]);
|
|
2026
|
+
return {
|
|
2027
|
+
swap: React.useCallback(swap, [updateValues, name, control]),
|
|
2028
|
+
move: React.useCallback(move, [updateValues, name, control]),
|
|
2029
|
+
prepend: React.useCallback(prepend, [updateValues, name, control]),
|
|
2030
|
+
append: React.useCallback(append, [updateValues, name, control]),
|
|
2031
|
+
remove: React.useCallback(remove, [updateValues, name, control]),
|
|
2032
|
+
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
|
2033
|
+
update: React.useCallback(update, [updateValues, name, control]),
|
|
2034
|
+
replace: React.useCallback(replace, [updateValues, name, control]),
|
|
2035
|
+
fields: React.useMemo(() => fields.map((field, index) => ({
|
|
2036
|
+
...field,
|
|
2037
|
+
[keyName]: ids.current[index] || generateId()
|
|
2038
|
+
})), [fields, keyName])
|
|
2039
|
+
};
|
|
2040
|
+
}
|
|
2041
|
+
var createSubject = () => {
|
|
2042
|
+
let _observers = [];
|
|
2043
|
+
const next = (value) => {
|
|
2044
|
+
for (const observer of _observers) {
|
|
2045
|
+
observer.next && observer.next(value);
|
|
2046
|
+
}
|
|
2047
|
+
};
|
|
2048
|
+
const subscribe = (observer) => {
|
|
2049
|
+
_observers.push(observer);
|
|
2050
|
+
return {
|
|
2051
|
+
unsubscribe: () => {
|
|
2052
|
+
_observers = _observers.filter((o2) => o2 !== observer);
|
|
2053
|
+
}
|
|
2054
|
+
};
|
|
2055
|
+
};
|
|
2056
|
+
const unsubscribe = () => {
|
|
2057
|
+
_observers = [];
|
|
2058
|
+
};
|
|
2059
|
+
return {
|
|
2060
|
+
get observers() {
|
|
2061
|
+
return _observers;
|
|
2062
|
+
},
|
|
2063
|
+
next,
|
|
2064
|
+
subscribe,
|
|
2065
|
+
unsubscribe
|
|
2066
|
+
};
|
|
2067
|
+
};
|
|
2068
|
+
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
2069
|
+
function deepEqual(object1, object2) {
|
|
2070
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
2071
|
+
return object1 === object2;
|
|
2072
|
+
}
|
|
2073
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
2074
|
+
return object1.getTime() === object2.getTime();
|
|
2075
|
+
}
|
|
2076
|
+
const keys1 = Object.keys(object1);
|
|
2077
|
+
const keys2 = Object.keys(object2);
|
|
2078
|
+
if (keys1.length !== keys2.length) {
|
|
2079
|
+
return false;
|
|
2080
|
+
}
|
|
2081
|
+
for (const key of keys1) {
|
|
2082
|
+
const val1 = object1[key];
|
|
2083
|
+
if (!keys2.includes(key)) {
|
|
2084
|
+
return false;
|
|
2085
|
+
}
|
|
2086
|
+
if (key !== "ref") {
|
|
2087
|
+
const val2 = object2[key];
|
|
2088
|
+
if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2) : val1 !== val2) {
|
|
2089
|
+
return false;
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
return true;
|
|
2094
|
+
}
|
|
2095
|
+
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
|
2096
|
+
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
2097
|
+
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
|
2098
|
+
var objectHasFunction = (data) => {
|
|
2099
|
+
for (const key in data) {
|
|
2100
|
+
if (isFunction(data[key])) {
|
|
2101
|
+
return true;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
return false;
|
|
2105
|
+
};
|
|
2106
|
+
function markFieldsDirty(data, fields = {}) {
|
|
2107
|
+
const isParentNodeArray = Array.isArray(data);
|
|
2108
|
+
if (isObject(data) || isParentNodeArray) {
|
|
2109
|
+
for (const key in data) {
|
|
2110
|
+
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
2111
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
2112
|
+
markFieldsDirty(data[key], fields[key]);
|
|
2113
|
+
} else if (!isNullOrUndefined(data[key])) {
|
|
2114
|
+
fields[key] = true;
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
return fields;
|
|
2119
|
+
}
|
|
2120
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
|
2121
|
+
const isParentNodeArray = Array.isArray(data);
|
|
2122
|
+
if (isObject(data) || isParentNodeArray) {
|
|
2123
|
+
for (const key in data) {
|
|
2124
|
+
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
2125
|
+
if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
|
|
2126
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key]) ? markFieldsDirty(data[key], []) : { ...markFieldsDirty(data[key]) };
|
|
2127
|
+
} else {
|
|
2128
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
2129
|
+
}
|
|
2130
|
+
} else {
|
|
2131
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
return dirtyFieldsFromValues;
|
|
2136
|
+
}
|
|
2137
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
2138
|
+
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value) ? value : valueAsNumber ? value === "" ? NaN : value ? +value : value : valueAsDate && isString(value) ? new Date(value) : setValueAs ? setValueAs(value) : value;
|
|
2139
|
+
function getFieldValue(_f) {
|
|
2140
|
+
const ref = _f.ref;
|
|
2141
|
+
if (_f.refs ? _f.refs.every((ref2) => ref2.disabled) : ref.disabled) {
|
|
2142
|
+
return;
|
|
2143
|
+
}
|
|
2144
|
+
if (isFileInput(ref)) {
|
|
2145
|
+
return ref.files;
|
|
2146
|
+
}
|
|
2147
|
+
if (isRadioInput(ref)) {
|
|
2148
|
+
return getRadioValue(_f.refs).value;
|
|
2149
|
+
}
|
|
2150
|
+
if (isMultipleSelect(ref)) {
|
|
2151
|
+
return [...ref.selectedOptions].map(({ value }) => value);
|
|
2152
|
+
}
|
|
2153
|
+
if (isCheckBoxInput(ref)) {
|
|
2154
|
+
return getCheckboxValue(_f.refs).value;
|
|
2155
|
+
}
|
|
2156
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
2157
|
+
}
|
|
2158
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
|
2159
|
+
const fields = {};
|
|
2160
|
+
for (const name of fieldsNames) {
|
|
2161
|
+
const field = get(_fields, name);
|
|
2162
|
+
field && set(fields, name, field._f);
|
|
2163
|
+
}
|
|
2164
|
+
return {
|
|
2165
|
+
criteriaMode,
|
|
2166
|
+
names: [...fieldsNames],
|
|
2167
|
+
fields,
|
|
2168
|
+
shouldUseNativeValidation
|
|
2169
|
+
};
|
|
2170
|
+
};
|
|
2171
|
+
var getRuleValue = (rule) => isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
|
|
2172
|
+
var hasValidation = (options) => options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
|
|
2173
|
+
function schemaErrorLookup(errors, _fields, name) {
|
|
2174
|
+
const error = get(errors, name);
|
|
2175
|
+
if (error || isKey(name)) {
|
|
2176
|
+
return {
|
|
2177
|
+
error,
|
|
2178
|
+
name
|
|
2179
|
+
};
|
|
2180
|
+
}
|
|
2181
|
+
const names = name.split(".");
|
|
2182
|
+
while (names.length) {
|
|
2183
|
+
const fieldName = names.join(".");
|
|
2184
|
+
const field = get(_fields, fieldName);
|
|
2185
|
+
const foundError = get(errors, fieldName);
|
|
2186
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
2187
|
+
return { name };
|
|
2188
|
+
}
|
|
2189
|
+
if (foundError && foundError.type) {
|
|
2190
|
+
return {
|
|
2191
|
+
name: fieldName,
|
|
2192
|
+
error: foundError
|
|
2193
|
+
};
|
|
2194
|
+
}
|
|
2195
|
+
names.pop();
|
|
2196
|
+
}
|
|
2197
|
+
return {
|
|
2198
|
+
name
|
|
2199
|
+
};
|
|
2200
|
+
}
|
|
2201
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
|
2202
|
+
if (mode.isOnAll) {
|
|
2203
|
+
return false;
|
|
2204
|
+
} else if (!isSubmitted && mode.isOnTouch) {
|
|
2205
|
+
return !(isTouched || isBlurEvent);
|
|
2206
|
+
} else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
2207
|
+
return !isBlurEvent;
|
|
2208
|
+
} else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
2209
|
+
return isBlurEvent;
|
|
2210
|
+
}
|
|
2211
|
+
return true;
|
|
2212
|
+
};
|
|
2213
|
+
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
|
2214
|
+
const defaultOptions = {
|
|
2215
|
+
mode: VALIDATION_MODE.onSubmit,
|
|
2216
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
|
2217
|
+
shouldFocusError: true
|
|
2218
|
+
};
|
|
2219
|
+
function createFormControl(props = {}, flushRootRender) {
|
|
2220
|
+
let _options = {
|
|
2221
|
+
...defaultOptions,
|
|
2222
|
+
...props
|
|
2223
|
+
};
|
|
2224
|
+
let _formState = {
|
|
2225
|
+
submitCount: 0,
|
|
2226
|
+
isDirty: false,
|
|
2227
|
+
isLoading: isFunction(_options.defaultValues),
|
|
2228
|
+
isValidating: false,
|
|
2229
|
+
isSubmitted: false,
|
|
2230
|
+
isSubmitting: false,
|
|
2231
|
+
isSubmitSuccessful: false,
|
|
2232
|
+
isValid: false,
|
|
2233
|
+
touchedFields: {},
|
|
2234
|
+
dirtyFields: {},
|
|
2235
|
+
errors: _options.errors || {},
|
|
2236
|
+
disabled: false
|
|
2237
|
+
};
|
|
2238
|
+
let _fields = {};
|
|
2239
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
|
|
2240
|
+
let _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
|
|
2241
|
+
let _state = {
|
|
2242
|
+
action: false,
|
|
2243
|
+
mount: false,
|
|
2244
|
+
watch: false
|
|
2245
|
+
};
|
|
2246
|
+
let _names = {
|
|
2247
|
+
mount: /* @__PURE__ */ new Set(),
|
|
2248
|
+
unMount: /* @__PURE__ */ new Set(),
|
|
2249
|
+
array: /* @__PURE__ */ new Set(),
|
|
2250
|
+
watch: /* @__PURE__ */ new Set()
|
|
2251
|
+
};
|
|
2252
|
+
let delayErrorCallback;
|
|
2253
|
+
let timer = 0;
|
|
2254
|
+
const _proxyFormState = {
|
|
2255
|
+
isDirty: false,
|
|
2256
|
+
dirtyFields: false,
|
|
2257
|
+
touchedFields: false,
|
|
2258
|
+
isValidating: false,
|
|
2259
|
+
isValid: false,
|
|
2260
|
+
errors: false
|
|
2261
|
+
};
|
|
2262
|
+
const _subjects = {
|
|
2263
|
+
values: createSubject(),
|
|
2264
|
+
array: createSubject(),
|
|
2265
|
+
state: createSubject()
|
|
2266
|
+
};
|
|
2267
|
+
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
|
2268
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
2269
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
2270
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
2271
|
+
const debounce2 = (callback) => (wait) => {
|
|
2272
|
+
clearTimeout(timer);
|
|
2273
|
+
timer = setTimeout(callback, wait);
|
|
2274
|
+
};
|
|
2275
|
+
const _updateValid = async (shouldUpdateValid) => {
|
|
2276
|
+
if (_proxyFormState.isValid || shouldUpdateValid) {
|
|
2277
|
+
const isValid2 = _options.resolver ? isEmptyObject((await _executeSchema()).errors) : await executeBuiltInValidation(_fields, true);
|
|
2278
|
+
if (isValid2 !== _formState.isValid) {
|
|
2279
|
+
_subjects.state.next({
|
|
2280
|
+
isValid: isValid2
|
|
2281
|
+
});
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
};
|
|
2285
|
+
const _updateIsValidating = (value) => _proxyFormState.isValidating && _subjects.state.next({
|
|
2286
|
+
isValidating: value
|
|
2287
|
+
});
|
|
2288
|
+
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
2289
|
+
if (args && method) {
|
|
2290
|
+
_state.action = true;
|
|
2291
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
2292
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
2293
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
|
2294
|
+
}
|
|
2295
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
|
|
2296
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
2297
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
|
2298
|
+
unsetEmptyArray(_formState.errors, name);
|
|
2299
|
+
}
|
|
2300
|
+
if (_proxyFormState.touchedFields && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
|
|
2301
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
2302
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
2303
|
+
}
|
|
2304
|
+
if (_proxyFormState.dirtyFields) {
|
|
2305
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
2306
|
+
}
|
|
2307
|
+
_subjects.state.next({
|
|
2308
|
+
name,
|
|
2309
|
+
isDirty: _getDirty(name, values),
|
|
2310
|
+
dirtyFields: _formState.dirtyFields,
|
|
2311
|
+
errors: _formState.errors,
|
|
2312
|
+
isValid: _formState.isValid
|
|
2313
|
+
});
|
|
2314
|
+
} else {
|
|
2315
|
+
set(_formValues, name, values);
|
|
2316
|
+
}
|
|
2317
|
+
};
|
|
2318
|
+
const updateErrors = (name, error) => {
|
|
2319
|
+
set(_formState.errors, name, error);
|
|
2320
|
+
_subjects.state.next({
|
|
2321
|
+
errors: _formState.errors
|
|
2322
|
+
});
|
|
2323
|
+
};
|
|
2324
|
+
const _setErrors = (errors) => {
|
|
2325
|
+
_formState.errors = errors;
|
|
2326
|
+
_subjects.state.next({
|
|
2327
|
+
errors: _formState.errors,
|
|
2328
|
+
isValid: false
|
|
2329
|
+
});
|
|
2330
|
+
};
|
|
2331
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
|
2332
|
+
const field = get(_fields, name);
|
|
2333
|
+
if (field) {
|
|
2334
|
+
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
|
2335
|
+
isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
|
|
2336
|
+
_state.mount && _updateValid();
|
|
2337
|
+
}
|
|
2338
|
+
};
|
|
2339
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
|
2340
|
+
let shouldUpdateField = false;
|
|
2341
|
+
let isPreviousDirty = false;
|
|
2342
|
+
const output = {
|
|
2343
|
+
name
|
|
2344
|
+
};
|
|
2345
|
+
const disabledField = get(_fields, name) && get(_fields, name)._f.disabled;
|
|
2346
|
+
if (!isBlurEvent || shouldDirty) {
|
|
2347
|
+
if (_proxyFormState.isDirty) {
|
|
2348
|
+
isPreviousDirty = _formState.isDirty;
|
|
2349
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
2350
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
2351
|
+
}
|
|
2352
|
+
const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
|
|
2353
|
+
isPreviousDirty = !disabledField && get(_formState.dirtyFields, name);
|
|
2354
|
+
isCurrentFieldPristine || disabledField ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
|
|
2355
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
2356
|
+
shouldUpdateField = shouldUpdateField || _proxyFormState.dirtyFields && isPreviousDirty !== !isCurrentFieldPristine;
|
|
2357
|
+
}
|
|
2358
|
+
if (isBlurEvent) {
|
|
2359
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
2360
|
+
if (!isPreviousFieldTouched) {
|
|
2361
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
2362
|
+
output.touchedFields = _formState.touchedFields;
|
|
2363
|
+
shouldUpdateField = shouldUpdateField || _proxyFormState.touchedFields && isPreviousFieldTouched !== isBlurEvent;
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
2367
|
+
return shouldUpdateField ? output : {};
|
|
2368
|
+
};
|
|
2369
|
+
const shouldRenderByError = (name, isValid2, error, fieldState) => {
|
|
2370
|
+
const previousFieldError = get(_formState.errors, name);
|
|
2371
|
+
const shouldUpdateValid = _proxyFormState.isValid && isBoolean(isValid2) && _formState.isValid !== isValid2;
|
|
2372
|
+
if (props.delayError && error) {
|
|
2373
|
+
delayErrorCallback = debounce2(() => updateErrors(name, error));
|
|
2374
|
+
delayErrorCallback(props.delayError);
|
|
2375
|
+
} else {
|
|
2376
|
+
clearTimeout(timer);
|
|
2377
|
+
delayErrorCallback = null;
|
|
2378
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
2379
|
+
}
|
|
2380
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
|
|
2381
|
+
const updatedFormState = {
|
|
2382
|
+
...fieldState,
|
|
2383
|
+
...shouldUpdateValid && isBoolean(isValid2) ? { isValid: isValid2 } : {},
|
|
2384
|
+
errors: _formState.errors,
|
|
2385
|
+
name
|
|
2386
|
+
};
|
|
2387
|
+
_formState = {
|
|
2388
|
+
..._formState,
|
|
2389
|
+
...updatedFormState
|
|
2390
|
+
};
|
|
2391
|
+
_subjects.state.next(updatedFormState);
|
|
2392
|
+
}
|
|
2393
|
+
_updateIsValidating(false);
|
|
2394
|
+
};
|
|
2395
|
+
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
2396
|
+
const executeSchemaAndUpdateState = async (names) => {
|
|
2397
|
+
const { errors } = await _executeSchema(names);
|
|
2398
|
+
if (names) {
|
|
2399
|
+
for (const name of names) {
|
|
2400
|
+
const error = get(errors, name);
|
|
2401
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
2402
|
+
}
|
|
2403
|
+
} else {
|
|
2404
|
+
_formState.errors = errors;
|
|
2405
|
+
}
|
|
2406
|
+
return errors;
|
|
2407
|
+
};
|
|
2408
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
|
2409
|
+
valid: true
|
|
2410
|
+
}) => {
|
|
2411
|
+
for (const name in fields) {
|
|
2412
|
+
const field = fields[name];
|
|
2413
|
+
if (field) {
|
|
2414
|
+
const { _f, ...fieldValue } = field;
|
|
2415
|
+
if (_f) {
|
|
2416
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
2417
|
+
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
2418
|
+
if (fieldError[_f.name]) {
|
|
2419
|
+
context.valid = false;
|
|
2420
|
+
if (shouldOnlyCheckValid) {
|
|
2421
|
+
break;
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
!shouldOnlyCheckValid && (get(fieldError, _f.name) ? isFieldArrayRoot ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name) : set(_formState.errors, _f.name, fieldError[_f.name]) : unset(_formState.errors, _f.name));
|
|
2425
|
+
}
|
|
2426
|
+
fieldValue && await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context);
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
return context.valid;
|
|
2430
|
+
};
|
|
2431
|
+
const _removeUnmounted = () => {
|
|
2432
|
+
for (const name of _names.unMount) {
|
|
2433
|
+
const field = get(_fields, name);
|
|
2434
|
+
field && (field._f.refs ? field._f.refs.every((ref) => !live(ref)) : !live(field._f.ref)) && unregister(name);
|
|
2435
|
+
}
|
|
2436
|
+
_names.unMount = /* @__PURE__ */ new Set();
|
|
2437
|
+
};
|
|
2438
|
+
const _getDirty = (name, data) => (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
|
|
2439
|
+
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
|
2440
|
+
..._state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? { [names]: defaultValue } : defaultValue
|
|
2441
|
+
}, isGlobal, defaultValue);
|
|
2442
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
2443
|
+
const setFieldValue = (name, value, options = {}) => {
|
|
2444
|
+
const field = get(_fields, name);
|
|
2445
|
+
let fieldValue = value;
|
|
2446
|
+
if (field) {
|
|
2447
|
+
const fieldReference = field._f;
|
|
2448
|
+
if (fieldReference) {
|
|
2449
|
+
!fieldReference.disabled && set(_formValues, name, getFieldValueAs(value, fieldReference));
|
|
2450
|
+
fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value) ? "" : value;
|
|
2451
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
|
2452
|
+
[...fieldReference.ref.options].forEach((optionRef) => optionRef.selected = fieldValue.includes(optionRef.value));
|
|
2453
|
+
} else if (fieldReference.refs) {
|
|
2454
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
|
2455
|
+
fieldReference.refs.length > 1 ? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) && (checkboxRef.checked = Array.isArray(fieldValue) ? !!fieldValue.find((data) => data === checkboxRef.value) : fieldValue === checkboxRef.value)) : fieldReference.refs[0] && (fieldReference.refs[0].checked = !!fieldValue);
|
|
2456
|
+
} else {
|
|
2457
|
+
fieldReference.refs.forEach((radioRef) => radioRef.checked = radioRef.value === fieldValue);
|
|
2458
|
+
}
|
|
2459
|
+
} else if (isFileInput(fieldReference.ref)) {
|
|
2460
|
+
fieldReference.ref.value = "";
|
|
2461
|
+
} else {
|
|
2462
|
+
fieldReference.ref.value = fieldValue;
|
|
2463
|
+
if (!fieldReference.ref.type) {
|
|
2464
|
+
_subjects.values.next({
|
|
2465
|
+
name,
|
|
2466
|
+
values: { ..._formValues }
|
|
2467
|
+
});
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
(options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
2473
|
+
options.shouldValidate && trigger(name);
|
|
2474
|
+
};
|
|
2475
|
+
const setValues = (name, value, options) => {
|
|
2476
|
+
for (const fieldKey in value) {
|
|
2477
|
+
const fieldValue = value[fieldKey];
|
|
2478
|
+
const fieldName = `${name}.${fieldKey}`;
|
|
2479
|
+
const field = get(_fields, fieldName);
|
|
2480
|
+
(_names.array.has(name) || !isPrimitive(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
|
|
2481
|
+
}
|
|
2482
|
+
};
|
|
2483
|
+
const setValue = (name, value, options = {}) => {
|
|
2484
|
+
const field = get(_fields, name);
|
|
2485
|
+
const isFieldArray = _names.array.has(name);
|
|
2486
|
+
const cloneValue = cloneObject(value);
|
|
2487
|
+
set(_formValues, name, cloneValue);
|
|
2488
|
+
if (isFieldArray) {
|
|
2489
|
+
_subjects.array.next({
|
|
2490
|
+
name,
|
|
2491
|
+
values: { ..._formValues }
|
|
2492
|
+
});
|
|
2493
|
+
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) && options.shouldDirty) {
|
|
2494
|
+
_subjects.state.next({
|
|
2495
|
+
name,
|
|
2496
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
2497
|
+
isDirty: _getDirty(name, cloneValue)
|
|
2498
|
+
});
|
|
2499
|
+
}
|
|
2500
|
+
} else {
|
|
2501
|
+
field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
|
|
2502
|
+
}
|
|
2503
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
2504
|
+
_subjects.values.next({
|
|
2505
|
+
name,
|
|
2506
|
+
values: { ..._formValues }
|
|
2507
|
+
});
|
|
2508
|
+
!_state.mount && flushRootRender();
|
|
2509
|
+
};
|
|
2510
|
+
const onChange = async (event) => {
|
|
2511
|
+
const target = event.target;
|
|
2512
|
+
let name = target.name;
|
|
2513
|
+
let isFieldValueUpdated = true;
|
|
2514
|
+
const field = get(_fields, name);
|
|
2515
|
+
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
2516
|
+
const _updateIsFieldValueUpdated = (fieldValue) => {
|
|
2517
|
+
isFieldValueUpdated = Number.isNaN(fieldValue) || fieldValue === get(_formValues, name, fieldValue);
|
|
2518
|
+
};
|
|
2519
|
+
if (field) {
|
|
2520
|
+
let error;
|
|
2521
|
+
let isValid2;
|
|
2522
|
+
const fieldValue = getCurrentFieldValue();
|
|
2523
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
2524
|
+
const shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
2525
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
|
2526
|
+
set(_formValues, name, fieldValue);
|
|
2527
|
+
if (isBlurEvent) {
|
|
2528
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
2529
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
2530
|
+
} else if (field._f.onChange) {
|
|
2531
|
+
field._f.onChange(event);
|
|
2532
|
+
}
|
|
2533
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
|
|
2534
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
2535
|
+
!isBlurEvent && _subjects.values.next({
|
|
2536
|
+
name,
|
|
2537
|
+
type: event.type,
|
|
2538
|
+
values: { ..._formValues }
|
|
2539
|
+
});
|
|
2540
|
+
if (shouldSkipValidation) {
|
|
2541
|
+
_proxyFormState.isValid && _updateValid();
|
|
2542
|
+
return shouldRender && _subjects.state.next({ name, ...watched ? {} : fieldState });
|
|
2543
|
+
}
|
|
2544
|
+
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
|
2545
|
+
_updateIsValidating(true);
|
|
2546
|
+
if (_options.resolver) {
|
|
2547
|
+
const { errors } = await _executeSchema([name]);
|
|
2548
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
2549
|
+
if (isFieldValueUpdated) {
|
|
2550
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
2551
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
2552
|
+
error = errorLookupResult.error;
|
|
2553
|
+
name = errorLookupResult.name;
|
|
2554
|
+
isValid2 = isEmptyObject(errors);
|
|
2555
|
+
}
|
|
2556
|
+
} else {
|
|
2557
|
+
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
2558
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
2559
|
+
if (isFieldValueUpdated) {
|
|
2560
|
+
if (error) {
|
|
2561
|
+
isValid2 = false;
|
|
2562
|
+
} else if (_proxyFormState.isValid) {
|
|
2563
|
+
isValid2 = await executeBuiltInValidation(_fields, true);
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
if (isFieldValueUpdated) {
|
|
2568
|
+
field._f.deps && trigger(field._f.deps);
|
|
2569
|
+
shouldRenderByError(name, isValid2, error, fieldState);
|
|
2570
|
+
}
|
|
2571
|
+
}
|
|
2572
|
+
};
|
|
2573
|
+
const _focusInput = (ref, key) => {
|
|
2574
|
+
if (get(_formState.errors, key) && ref.focus) {
|
|
2575
|
+
ref.focus();
|
|
2576
|
+
return 1;
|
|
2577
|
+
}
|
|
2578
|
+
return;
|
|
2579
|
+
};
|
|
2580
|
+
const trigger = async (name, options = {}) => {
|
|
2581
|
+
let isValid2;
|
|
2582
|
+
let validationResult;
|
|
2583
|
+
const fieldNames = convertToArrayPayload(name);
|
|
2584
|
+
_updateIsValidating(true);
|
|
2585
|
+
if (_options.resolver) {
|
|
2586
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
|
2587
|
+
isValid2 = isEmptyObject(errors);
|
|
2588
|
+
validationResult = name ? !fieldNames.some((name2) => get(errors, name2)) : isValid2;
|
|
2589
|
+
} else if (name) {
|
|
2590
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
|
2591
|
+
const field = get(_fields, fieldName);
|
|
2592
|
+
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
|
2593
|
+
}))).every(Boolean);
|
|
2594
|
+
!(!validationResult && !_formState.isValid) && _updateValid();
|
|
2595
|
+
} else {
|
|
2596
|
+
validationResult = isValid2 = await executeBuiltInValidation(_fields);
|
|
2597
|
+
}
|
|
2598
|
+
_subjects.state.next({
|
|
2599
|
+
...!isString(name) || _proxyFormState.isValid && isValid2 !== _formState.isValid ? {} : { name },
|
|
2600
|
+
..._options.resolver || !name ? { isValid: isValid2 } : {},
|
|
2601
|
+
errors: _formState.errors,
|
|
2602
|
+
isValidating: false
|
|
2603
|
+
});
|
|
2604
|
+
options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
|
2605
|
+
return validationResult;
|
|
2606
|
+
};
|
|
2607
|
+
const getValues = (fieldNames) => {
|
|
2608
|
+
const values = {
|
|
2609
|
+
..._defaultValues,
|
|
2610
|
+
..._state.mount ? _formValues : {}
|
|
2611
|
+
};
|
|
2612
|
+
return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map((name) => get(values, name));
|
|
2613
|
+
};
|
|
2614
|
+
const getFieldState = (name, formState) => ({
|
|
2615
|
+
invalid: !!get((formState || _formState).errors, name),
|
|
2616
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
2617
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
2618
|
+
error: get((formState || _formState).errors, name)
|
|
2619
|
+
});
|
|
2620
|
+
const clearErrors = (name) => {
|
|
2621
|
+
name && convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
|
2622
|
+
_subjects.state.next({
|
|
2623
|
+
errors: name ? _formState.errors : {}
|
|
2624
|
+
});
|
|
2625
|
+
};
|
|
2626
|
+
const setError = (name, error, options) => {
|
|
2627
|
+
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
|
2628
|
+
set(_formState.errors, name, {
|
|
2629
|
+
...error,
|
|
2630
|
+
ref
|
|
2631
|
+
});
|
|
2632
|
+
_subjects.state.next({
|
|
2633
|
+
name,
|
|
2634
|
+
errors: _formState.errors,
|
|
2635
|
+
isValid: false
|
|
2636
|
+
});
|
|
2637
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
2638
|
+
};
|
|
2639
|
+
const watch = (name, defaultValue) => isFunction(name) ? _subjects.values.subscribe({
|
|
2640
|
+
next: (payload) => name(_getWatch(void 0, defaultValue), payload)
|
|
2641
|
+
}) : _getWatch(name, defaultValue, true);
|
|
2642
|
+
const unregister = (name, options = {}) => {
|
|
2643
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
|
2644
|
+
_names.mount.delete(fieldName);
|
|
2645
|
+
_names.array.delete(fieldName);
|
|
2646
|
+
if (!options.keepValue) {
|
|
2647
|
+
unset(_fields, fieldName);
|
|
2648
|
+
unset(_formValues, fieldName);
|
|
2649
|
+
}
|
|
2650
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
|
2651
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
2652
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
2653
|
+
!_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
|
|
2654
|
+
}
|
|
2655
|
+
_subjects.values.next({
|
|
2656
|
+
values: { ..._formValues }
|
|
2657
|
+
});
|
|
2658
|
+
_subjects.state.next({
|
|
2659
|
+
..._formState,
|
|
2660
|
+
...!options.keepDirty ? {} : { isDirty: _getDirty() }
|
|
2661
|
+
});
|
|
2662
|
+
!options.keepIsValid && _updateValid();
|
|
2663
|
+
};
|
|
2664
|
+
const _updateDisabledField = ({ disabled, name, field, fields, value }) => {
|
|
2665
|
+
if (isBoolean(disabled)) {
|
|
2666
|
+
const inputValue = disabled ? void 0 : isUndefined(value) ? getFieldValue(field ? field._f : get(fields, name)._f) : value;
|
|
2667
|
+
set(_formValues, name, inputValue);
|
|
2668
|
+
updateTouchAndDirty(name, inputValue, false, false, true);
|
|
2669
|
+
}
|
|
2670
|
+
};
|
|
2671
|
+
const register = (name, options = {}) => {
|
|
2672
|
+
let field = get(_fields, name);
|
|
2673
|
+
const disabledIsDefined = isBoolean(options.disabled);
|
|
2674
|
+
set(_fields, name, {
|
|
2675
|
+
...field || {},
|
|
2676
|
+
_f: {
|
|
2677
|
+
...field && field._f ? field._f : { ref: { name } },
|
|
2678
|
+
name,
|
|
2679
|
+
mount: true,
|
|
2680
|
+
...options
|
|
2681
|
+
}
|
|
2682
|
+
});
|
|
2683
|
+
_names.mount.add(name);
|
|
2684
|
+
if (field) {
|
|
2685
|
+
_updateDisabledField({
|
|
2686
|
+
field,
|
|
2687
|
+
disabled: options.disabled,
|
|
2688
|
+
name
|
|
2689
|
+
});
|
|
2690
|
+
} else {
|
|
2691
|
+
updateValidAndValue(name, true, options.value);
|
|
2692
|
+
}
|
|
2693
|
+
return {
|
|
2694
|
+
...disabledIsDefined ? { disabled: options.disabled } : {},
|
|
2695
|
+
..._options.progressive ? {
|
|
2696
|
+
required: !!options.required,
|
|
2697
|
+
min: getRuleValue(options.min),
|
|
2698
|
+
max: getRuleValue(options.max),
|
|
2699
|
+
minLength: getRuleValue(options.minLength),
|
|
2700
|
+
maxLength: getRuleValue(options.maxLength),
|
|
2701
|
+
pattern: getRuleValue(options.pattern)
|
|
2702
|
+
} : {},
|
|
2703
|
+
name,
|
|
2704
|
+
onChange,
|
|
2705
|
+
onBlur: onChange,
|
|
2706
|
+
ref: (ref) => {
|
|
2707
|
+
if (ref) {
|
|
2708
|
+
register(name, options);
|
|
2709
|
+
field = get(_fields, name);
|
|
2710
|
+
const fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll("input,select,textarea")[0] || ref : ref : ref;
|
|
2711
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
2712
|
+
const refs = field._f.refs || [];
|
|
2713
|
+
if (radioOrCheckbox ? refs.find((option) => option === fieldRef) : fieldRef === field._f.ref) {
|
|
2714
|
+
return;
|
|
2715
|
+
}
|
|
2716
|
+
set(_fields, name, {
|
|
2717
|
+
_f: {
|
|
2718
|
+
...field._f,
|
|
2719
|
+
...radioOrCheckbox ? {
|
|
2720
|
+
refs: [
|
|
2721
|
+
...refs.filter(live),
|
|
2722
|
+
fieldRef,
|
|
2723
|
+
...Array.isArray(get(_defaultValues, name)) ? [{}] : []
|
|
2724
|
+
],
|
|
2725
|
+
ref: { type: fieldRef.type, name }
|
|
2726
|
+
} : { ref: fieldRef }
|
|
2727
|
+
}
|
|
2728
|
+
});
|
|
2729
|
+
updateValidAndValue(name, false, void 0, fieldRef);
|
|
2730
|
+
} else {
|
|
2731
|
+
field = get(_fields, name, {});
|
|
2732
|
+
if (field._f) {
|
|
2733
|
+
field._f.mount = false;
|
|
2734
|
+
}
|
|
2735
|
+
(_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
};
|
|
2740
|
+
const _focusError = () => _options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
|
2741
|
+
const _disableForm = (disabled) => {
|
|
2742
|
+
if (isBoolean(disabled)) {
|
|
2743
|
+
_subjects.state.next({ disabled });
|
|
2744
|
+
iterateFieldsByAction(_fields, (ref, name) => {
|
|
2745
|
+
let requiredDisabledState = disabled;
|
|
2746
|
+
const currentField = get(_fields, name);
|
|
2747
|
+
if (currentField && isBoolean(currentField._f.disabled)) {
|
|
2748
|
+
requiredDisabledState || (requiredDisabledState = currentField._f.disabled);
|
|
2749
|
+
}
|
|
2750
|
+
ref.disabled = requiredDisabledState;
|
|
2751
|
+
}, 0, false);
|
|
2752
|
+
}
|
|
2753
|
+
};
|
|
2754
|
+
const handleSubmit = (onValid, onInvalid) => async (e2) => {
|
|
2755
|
+
if (e2) {
|
|
2756
|
+
e2.preventDefault && e2.preventDefault();
|
|
2757
|
+
e2.persist && e2.persist();
|
|
2758
|
+
}
|
|
2759
|
+
let fieldValues = cloneObject(_formValues);
|
|
2760
|
+
_subjects.state.next({
|
|
2761
|
+
isSubmitting: true
|
|
2762
|
+
});
|
|
2763
|
+
if (_options.resolver) {
|
|
2764
|
+
const { errors, values } = await _executeSchema();
|
|
2765
|
+
_formState.errors = errors;
|
|
2766
|
+
fieldValues = values;
|
|
2767
|
+
} else {
|
|
2768
|
+
await executeBuiltInValidation(_fields);
|
|
2769
|
+
}
|
|
2770
|
+
unset(_formState.errors, "root");
|
|
2771
|
+
if (isEmptyObject(_formState.errors)) {
|
|
2772
|
+
_subjects.state.next({
|
|
2773
|
+
errors: {}
|
|
2774
|
+
});
|
|
2775
|
+
await onValid(fieldValues, e2);
|
|
2776
|
+
} else {
|
|
2777
|
+
if (onInvalid) {
|
|
2778
|
+
await onInvalid({ ..._formState.errors }, e2);
|
|
2779
|
+
}
|
|
2780
|
+
_focusError();
|
|
2781
|
+
setTimeout(_focusError);
|
|
2782
|
+
}
|
|
2783
|
+
_subjects.state.next({
|
|
2784
|
+
isSubmitted: true,
|
|
2785
|
+
isSubmitting: false,
|
|
2786
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors),
|
|
2787
|
+
submitCount: _formState.submitCount + 1,
|
|
2788
|
+
errors: _formState.errors
|
|
2789
|
+
});
|
|
2790
|
+
};
|
|
2791
|
+
const resetField = (name, options = {}) => {
|
|
2792
|
+
if (get(_fields, name)) {
|
|
2793
|
+
if (isUndefined(options.defaultValue)) {
|
|
2794
|
+
setValue(name, get(_defaultValues, name));
|
|
2795
|
+
} else {
|
|
2796
|
+
setValue(name, options.defaultValue);
|
|
2797
|
+
set(_defaultValues, name, options.defaultValue);
|
|
2798
|
+
}
|
|
2799
|
+
if (!options.keepTouched) {
|
|
2800
|
+
unset(_formState.touchedFields, name);
|
|
2801
|
+
}
|
|
2802
|
+
if (!options.keepDirty) {
|
|
2803
|
+
unset(_formState.dirtyFields, name);
|
|
2804
|
+
_formState.isDirty = options.defaultValue ? _getDirty(name, get(_defaultValues, name)) : _getDirty();
|
|
2805
|
+
}
|
|
2806
|
+
if (!options.keepError) {
|
|
2807
|
+
unset(_formState.errors, name);
|
|
2808
|
+
_proxyFormState.isValid && _updateValid();
|
|
2809
|
+
}
|
|
2810
|
+
_subjects.state.next({ ..._formState });
|
|
2811
|
+
}
|
|
2812
|
+
};
|
|
2813
|
+
const _reset = (formValues, keepStateOptions = {}) => {
|
|
2814
|
+
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
2815
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
|
2816
|
+
const values = formValues && !isEmptyObject(formValues) ? cloneUpdatedValues : _defaultValues;
|
|
2817
|
+
if (!keepStateOptions.keepDefaultValues) {
|
|
2818
|
+
_defaultValues = updatedValues;
|
|
2819
|
+
}
|
|
2820
|
+
if (!keepStateOptions.keepValues) {
|
|
2821
|
+
if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
|
|
2822
|
+
for (const fieldName of _names.mount) {
|
|
2823
|
+
get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
|
|
2824
|
+
}
|
|
2825
|
+
} else {
|
|
2826
|
+
if (isWeb && isUndefined(formValues)) {
|
|
2827
|
+
for (const name of _names.mount) {
|
|
2828
|
+
const field = get(_fields, name);
|
|
2829
|
+
if (field && field._f) {
|
|
2830
|
+
const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
|
|
2831
|
+
if (isHTMLElement(fieldReference)) {
|
|
2832
|
+
const form = fieldReference.closest("form");
|
|
2833
|
+
if (form) {
|
|
2834
|
+
form.reset();
|
|
2835
|
+
break;
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
}
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
_fields = {};
|
|
2842
|
+
}
|
|
2843
|
+
_formValues = props.shouldUnregister ? keepStateOptions.keepDefaultValues ? cloneObject(_defaultValues) : {} : cloneObject(values);
|
|
2844
|
+
_subjects.array.next({
|
|
2845
|
+
values: { ...values }
|
|
2846
|
+
});
|
|
2847
|
+
_subjects.values.next({
|
|
2848
|
+
values: { ...values }
|
|
2849
|
+
});
|
|
2850
|
+
}
|
|
2851
|
+
_names = {
|
|
2852
|
+
mount: /* @__PURE__ */ new Set(),
|
|
2853
|
+
unMount: /* @__PURE__ */ new Set(),
|
|
2854
|
+
array: /* @__PURE__ */ new Set(),
|
|
2855
|
+
watch: /* @__PURE__ */ new Set(),
|
|
2856
|
+
watchAll: false,
|
|
2857
|
+
focus: ""
|
|
2858
|
+
};
|
|
2859
|
+
!_state.mount && flushRootRender();
|
|
2860
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
|
|
2861
|
+
_state.watch = !!props.shouldUnregister;
|
|
2862
|
+
_subjects.state.next({
|
|
2863
|
+
submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
|
|
2864
|
+
isDirty: keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
|
|
2865
|
+
isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
|
|
2866
|
+
dirtyFields: keepStateOptions.keepDirtyValues ? _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : {},
|
|
2867
|
+
touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
|
|
2868
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
2869
|
+
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
|
|
2870
|
+
isSubmitting: false
|
|
2871
|
+
});
|
|
2872
|
+
};
|
|
2873
|
+
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
|
|
2874
|
+
const setFocus = (name, options = {}) => {
|
|
2875
|
+
const field = get(_fields, name);
|
|
2876
|
+
const fieldReference = field && field._f;
|
|
2877
|
+
if (fieldReference) {
|
|
2878
|
+
const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
|
|
2879
|
+
if (fieldRef.focus) {
|
|
2880
|
+
fieldRef.focus();
|
|
2881
|
+
options.shouldSelect && fieldRef.select();
|
|
2882
|
+
}
|
|
2883
|
+
}
|
|
2884
|
+
};
|
|
2885
|
+
const _updateFormState = (updatedFormState) => {
|
|
2886
|
+
_formState = {
|
|
2887
|
+
..._formState,
|
|
2888
|
+
...updatedFormState
|
|
2889
|
+
};
|
|
2890
|
+
};
|
|
2891
|
+
const _resetDefaultValues = () => isFunction(_options.defaultValues) && _options.defaultValues().then((values) => {
|
|
2892
|
+
reset(values, _options.resetOptions);
|
|
2893
|
+
_subjects.state.next({
|
|
2894
|
+
isLoading: false
|
|
2895
|
+
});
|
|
2896
|
+
});
|
|
2897
|
+
return {
|
|
2898
|
+
control: {
|
|
2899
|
+
register,
|
|
2900
|
+
unregister,
|
|
2901
|
+
getFieldState,
|
|
2902
|
+
handleSubmit,
|
|
2903
|
+
setError,
|
|
2904
|
+
_executeSchema,
|
|
2905
|
+
_getWatch,
|
|
2906
|
+
_getDirty,
|
|
2907
|
+
_updateValid,
|
|
2908
|
+
_removeUnmounted,
|
|
2909
|
+
_updateFieldArray,
|
|
2910
|
+
_updateDisabledField,
|
|
2911
|
+
_getFieldArray,
|
|
2912
|
+
_reset,
|
|
2913
|
+
_resetDefaultValues,
|
|
2914
|
+
_updateFormState,
|
|
2915
|
+
_disableForm,
|
|
2916
|
+
_subjects,
|
|
2917
|
+
_proxyFormState,
|
|
2918
|
+
_setErrors,
|
|
2919
|
+
get _fields() {
|
|
2920
|
+
return _fields;
|
|
2921
|
+
},
|
|
2922
|
+
get _formValues() {
|
|
2923
|
+
return _formValues;
|
|
2924
|
+
},
|
|
2925
|
+
get _state() {
|
|
2926
|
+
return _state;
|
|
2927
|
+
},
|
|
2928
|
+
set _state(value) {
|
|
2929
|
+
_state = value;
|
|
2930
|
+
},
|
|
2931
|
+
get _defaultValues() {
|
|
2932
|
+
return _defaultValues;
|
|
2933
|
+
},
|
|
2934
|
+
get _names() {
|
|
2935
|
+
return _names;
|
|
2936
|
+
},
|
|
2937
|
+
set _names(value) {
|
|
2938
|
+
_names = value;
|
|
2939
|
+
},
|
|
2940
|
+
get _formState() {
|
|
2941
|
+
return _formState;
|
|
2942
|
+
},
|
|
2943
|
+
set _formState(value) {
|
|
2944
|
+
_formState = value;
|
|
2945
|
+
},
|
|
2946
|
+
get _options() {
|
|
2947
|
+
return _options;
|
|
2948
|
+
},
|
|
2949
|
+
set _options(value) {
|
|
2950
|
+
_options = {
|
|
2951
|
+
..._options,
|
|
2952
|
+
...value
|
|
2953
|
+
};
|
|
2954
|
+
}
|
|
2955
|
+
},
|
|
2956
|
+
trigger,
|
|
2957
|
+
register,
|
|
2958
|
+
handleSubmit,
|
|
2959
|
+
watch,
|
|
2960
|
+
setValue,
|
|
2961
|
+
getValues,
|
|
2962
|
+
reset,
|
|
2963
|
+
resetField,
|
|
2964
|
+
clearErrors,
|
|
2965
|
+
unregister,
|
|
2966
|
+
setError,
|
|
2967
|
+
setFocus,
|
|
2968
|
+
getFieldState
|
|
2969
|
+
};
|
|
2970
|
+
}
|
|
2971
|
+
function useForm(props = {}) {
|
|
2972
|
+
const _formControl = React.useRef();
|
|
2973
|
+
const _values = React.useRef();
|
|
2974
|
+
const [formState, updateFormState] = React.useState({
|
|
2975
|
+
isDirty: false,
|
|
2976
|
+
isValidating: false,
|
|
2977
|
+
isLoading: isFunction(props.defaultValues),
|
|
2978
|
+
isSubmitted: false,
|
|
2979
|
+
isSubmitting: false,
|
|
2980
|
+
isSubmitSuccessful: false,
|
|
2981
|
+
isValid: false,
|
|
2982
|
+
submitCount: 0,
|
|
2983
|
+
dirtyFields: {},
|
|
2984
|
+
touchedFields: {},
|
|
2985
|
+
errors: props.errors || {},
|
|
2986
|
+
disabled: false,
|
|
2987
|
+
defaultValues: isFunction(props.defaultValues) ? void 0 : props.defaultValues
|
|
2988
|
+
});
|
|
2989
|
+
if (!_formControl.current) {
|
|
2990
|
+
_formControl.current = {
|
|
2991
|
+
...createFormControl(props, () => updateFormState((formState2) => ({ ...formState2 }))),
|
|
2992
|
+
formState
|
|
2993
|
+
};
|
|
2994
|
+
}
|
|
2995
|
+
const control = _formControl.current.control;
|
|
2996
|
+
control._options = props;
|
|
2997
|
+
useSubscribe({
|
|
2998
|
+
subject: control._subjects.state,
|
|
2999
|
+
next: (value) => {
|
|
3000
|
+
if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
|
|
3001
|
+
updateFormState({ ...control._formState });
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
});
|
|
3005
|
+
React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
3006
|
+
React.useEffect(() => {
|
|
3007
|
+
if (control._proxyFormState.isDirty) {
|
|
3008
|
+
const isDirty2 = control._getDirty();
|
|
3009
|
+
if (isDirty2 !== formState.isDirty) {
|
|
3010
|
+
control._subjects.state.next({
|
|
3011
|
+
isDirty: isDirty2
|
|
3012
|
+
});
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
}, [control, formState.isDirty]);
|
|
3016
|
+
React.useEffect(() => {
|
|
3017
|
+
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
3018
|
+
control._reset(props.values, control._options.resetOptions);
|
|
3019
|
+
_values.current = props.values;
|
|
3020
|
+
updateFormState((state) => ({ ...state }));
|
|
3021
|
+
} else {
|
|
3022
|
+
control._resetDefaultValues();
|
|
3023
|
+
}
|
|
3024
|
+
}, [props.values, control]);
|
|
3025
|
+
React.useEffect(() => {
|
|
3026
|
+
if (props.errors) {
|
|
3027
|
+
control._setErrors(props.errors);
|
|
3028
|
+
}
|
|
3029
|
+
}, [props.errors, control]);
|
|
3030
|
+
React.useEffect(() => {
|
|
3031
|
+
if (!control._state.mount) {
|
|
3032
|
+
control._updateValid();
|
|
3033
|
+
control._state.mount = true;
|
|
3034
|
+
}
|
|
3035
|
+
if (control._state.watch) {
|
|
3036
|
+
control._state.watch = false;
|
|
3037
|
+
control._subjects.state.next({ ...control._formState });
|
|
3038
|
+
}
|
|
3039
|
+
control._removeUnmounted();
|
|
3040
|
+
});
|
|
3041
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
|
3042
|
+
return _formControl.current;
|
|
3043
|
+
}
|
|
3044
|
+
var t$1 = function(t2, n2, e2) {
|
|
3045
|
+
if (t2 && "reportValidity" in t2) {
|
|
3046
|
+
var i2 = get(e2, n2);
|
|
3047
|
+
t2.setCustomValidity(i2 && i2.message || ""), t2.reportValidity();
|
|
3048
|
+
}
|
|
3049
|
+
}, n$1 = function(r, n2) {
|
|
3050
|
+
var e2 = function(e3) {
|
|
3051
|
+
var i3 = n2.fields[e3];
|
|
3052
|
+
i3 && i3.ref && "reportValidity" in i3.ref ? t$1(i3.ref, e3, r) : i3.refs && i3.refs.forEach(function(n3) {
|
|
3053
|
+
return t$1(n3, e3, r);
|
|
3054
|
+
});
|
|
3055
|
+
};
|
|
3056
|
+
for (var i2 in n2.fields) e2(i2);
|
|
3057
|
+
}, e = function(r) {
|
|
3058
|
+
return r instanceof Date;
|
|
3059
|
+
}, i = function(r) {
|
|
3060
|
+
return null == r;
|
|
3061
|
+
}, a = function(r) {
|
|
3062
|
+
return "object" == typeof r;
|
|
3063
|
+
}, o = function(r) {
|
|
3064
|
+
return !i(r) && !Array.isArray(r) && a(r) && !e(r);
|
|
3065
|
+
}, f = function(r) {
|
|
3066
|
+
return /^\w*$/.test(r);
|
|
3067
|
+
}, s = function(r, t2, n2) {
|
|
3068
|
+
for (var e2 = -1, i2 = f(t2) ? [t2] : function(r2) {
|
|
3069
|
+
return t3 = r2.replace(/["|']|\]/g, "").split(/\.|\[/), Array.isArray(t3) ? t3.filter(Boolean) : [];
|
|
3070
|
+
var t3;
|
|
3071
|
+
}(t2), a2 = i2.length, s2 = a2 - 1; ++e2 < a2; ) {
|
|
3072
|
+
var u2 = i2[e2], c2 = n2;
|
|
3073
|
+
if (e2 !== s2) {
|
|
3074
|
+
var l = r[u2];
|
|
3075
|
+
c2 = o(l) || Array.isArray(l) ? l : isNaN(+i2[e2 + 1]) ? {} : [];
|
|
3076
|
+
}
|
|
3077
|
+
r[u2] = c2, r = r[u2];
|
|
3078
|
+
}
|
|
3079
|
+
return r;
|
|
3080
|
+
}, u = function(t2, e2) {
|
|
3081
|
+
e2.shouldUseNativeValidation && n$1(t2, e2);
|
|
3082
|
+
var i2 = {};
|
|
3083
|
+
for (var a2 in t2) {
|
|
3084
|
+
var o2 = get(e2.fields, a2), f2 = Object.assign(t2[a2] || {}, { ref: o2 && o2.ref });
|
|
3085
|
+
if (c(e2.names || Object.keys(t2), a2)) {
|
|
3086
|
+
var u2 = Object.assign({}, get(i2, a2));
|
|
3087
|
+
s(u2, "root", f2), s(i2, a2, u2);
|
|
3088
|
+
} else s(i2, a2, f2);
|
|
3089
|
+
}
|
|
3090
|
+
return i2;
|
|
3091
|
+
}, c = function(r, t2) {
|
|
3092
|
+
return r.some(function(r2) {
|
|
3093
|
+
return r2.startsWith(t2 + ".");
|
|
3094
|
+
});
|
|
3095
|
+
};
|
|
3096
|
+
var n = function(e2, o2) {
|
|
3097
|
+
for (var n2 = {}; e2.length; ) {
|
|
3098
|
+
var t2 = e2[0], s2 = t2.code, i2 = t2.message, a2 = t2.path.join(".");
|
|
3099
|
+
if (!n2[a2]) if ("unionErrors" in t2) {
|
|
3100
|
+
var u2 = t2.unionErrors[0].errors[0];
|
|
3101
|
+
n2[a2] = { message: u2.message, type: u2.code };
|
|
3102
|
+
} else n2[a2] = { message: i2, type: s2 };
|
|
3103
|
+
if ("unionErrors" in t2 && t2.unionErrors.forEach(function(r) {
|
|
3104
|
+
return r.errors.forEach(function(r2) {
|
|
3105
|
+
return e2.push(r2);
|
|
3106
|
+
});
|
|
3107
|
+
}), o2) {
|
|
3108
|
+
var c2 = n2[a2].types, f2 = c2 && c2[t2.code];
|
|
3109
|
+
n2[a2] = appendErrors(a2, o2, n2, s2, f2 ? [].concat(f2, t2.message) : t2.message);
|
|
3110
|
+
}
|
|
3111
|
+
e2.shift();
|
|
3112
|
+
}
|
|
3113
|
+
return n2;
|
|
3114
|
+
}, t = function(r, t2, s2) {
|
|
3115
|
+
return void 0 === s2 && (s2 = {}), function(i2, a2, u$1) {
|
|
3116
|
+
try {
|
|
3117
|
+
return Promise.resolve(function(o2, n2) {
|
|
3118
|
+
try {
|
|
3119
|
+
var a3 = Promise.resolve(r["sync" === s2.mode ? "parse" : "parseAsync"](i2, t2)).then(function(r2) {
|
|
3120
|
+
return u$1.shouldUseNativeValidation && n$1({}, u$1), { errors: {}, values: s2.raw ? i2 : r2 };
|
|
3121
|
+
});
|
|
3122
|
+
} catch (r2) {
|
|
3123
|
+
return n2(r2);
|
|
3124
|
+
}
|
|
3125
|
+
return a3 && a3.then ? a3.then(void 0, n2) : a3;
|
|
3126
|
+
}(0, function(r2) {
|
|
3127
|
+
if (function(r3) {
|
|
3128
|
+
return null != r3.errors;
|
|
3129
|
+
}(r2)) return { values: {}, errors: u(n(r2.errors, !u$1.shouldUseNativeValidation && "all" === u$1.criteriaMode), u$1) };
|
|
3130
|
+
throw r2;
|
|
3131
|
+
}));
|
|
3132
|
+
} catch (r2) {
|
|
3133
|
+
return Promise.reject(r2);
|
|
3134
|
+
}
|
|
3135
|
+
};
|
|
3136
|
+
};
|
|
1256
3137
|
var util;
|
|
1257
3138
|
(function(util2) {
|
|
1258
3139
|
util2.assertEqual = (_) => {
|
|
@@ -1280,8 +3161,8 @@ var util;
|
|
|
1280
3161
|
return util2.objectValues(filtered);
|
|
1281
3162
|
};
|
|
1282
3163
|
util2.objectValues = (obj) => {
|
|
1283
|
-
return util2.objectKeys(obj).map(function(
|
|
1284
|
-
return obj[
|
|
3164
|
+
return util2.objectKeys(obj).map(function(e2) {
|
|
3165
|
+
return obj[e2];
|
|
1285
3166
|
});
|
|
1286
3167
|
};
|
|
1287
3168
|
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
@@ -1345,8 +3226,8 @@ const ZodParsedType = util.arrayToEnum([
|
|
|
1345
3226
|
"set"
|
|
1346
3227
|
]);
|
|
1347
3228
|
const getParsedType = (data) => {
|
|
1348
|
-
const
|
|
1349
|
-
switch (
|
|
3229
|
+
const t2 = typeof data;
|
|
3230
|
+
switch (t2) {
|
|
1350
3231
|
case "undefined":
|
|
1351
3232
|
return ZodParsedType.undefined;
|
|
1352
3233
|
case "string":
|
|
@@ -1446,10 +3327,10 @@ class ZodError extends Error {
|
|
|
1446
3327
|
fieldErrors._errors.push(mapper(issue));
|
|
1447
3328
|
} else {
|
|
1448
3329
|
let curr = fieldErrors;
|
|
1449
|
-
let
|
|
1450
|
-
while (
|
|
1451
|
-
const el = issue.path[
|
|
1452
|
-
const terminal =
|
|
3330
|
+
let i2 = 0;
|
|
3331
|
+
while (i2 < issue.path.length) {
|
|
3332
|
+
const el = issue.path[i2];
|
|
3333
|
+
const terminal = i2 === issue.path.length - 1;
|
|
1453
3334
|
if (!terminal) {
|
|
1454
3335
|
curr[el] = curr[el] || { _errors: [] };
|
|
1455
3336
|
} else {
|
|
@@ -1457,7 +3338,7 @@ class ZodError extends Error {
|
|
|
1457
3338
|
curr[el]._errors.push(mapper(issue));
|
|
1458
3339
|
}
|
|
1459
3340
|
curr = curr[el];
|
|
1460
|
-
|
|
3341
|
+
i2++;
|
|
1461
3342
|
}
|
|
1462
3343
|
}
|
|
1463
3344
|
}
|
|
@@ -1667,12 +3548,12 @@ class ParseStatus {
|
|
|
1667
3548
|
}
|
|
1668
3549
|
static mergeArray(status, results) {
|
|
1669
3550
|
const arrayValue = [];
|
|
1670
|
-
for (const
|
|
1671
|
-
if (
|
|
3551
|
+
for (const s2 of results) {
|
|
3552
|
+
if (s2.status === "aborted")
|
|
1672
3553
|
return INVALID;
|
|
1673
|
-
if (
|
|
3554
|
+
if (s2.status === "dirty")
|
|
1674
3555
|
status.dirty();
|
|
1675
|
-
arrayValue.push(
|
|
3556
|
+
arrayValue.push(s2.value);
|
|
1676
3557
|
}
|
|
1677
3558
|
return { status: status.value, value: arrayValue };
|
|
1678
3559
|
}
|
|
@@ -3424,14 +5305,14 @@ class ZodArray extends ZodType {
|
|
|
3424
5305
|
}
|
|
3425
5306
|
}
|
|
3426
5307
|
if (ctx.common.async) {
|
|
3427
|
-
return Promise.all([...ctx.data].map((item,
|
|
3428
|
-
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path,
|
|
5308
|
+
return Promise.all([...ctx.data].map((item, i2) => {
|
|
5309
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i2));
|
|
3429
5310
|
})).then((result2) => {
|
|
3430
5311
|
return ParseStatus.mergeArray(status, result2);
|
|
3431
5312
|
});
|
|
3432
5313
|
}
|
|
3433
|
-
const result = [...ctx.data].map((item,
|
|
3434
|
-
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path,
|
|
5314
|
+
const result = [...ctx.data].map((item, i2) => {
|
|
5315
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i2));
|
|
3435
5316
|
});
|
|
3436
5317
|
return ParseStatus.mergeArray(status, result);
|
|
3437
5318
|
}
|
|
@@ -4033,17 +5914,17 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
4033
5914
|
});
|
|
4034
5915
|
}
|
|
4035
5916
|
}
|
|
4036
|
-
function mergeValues(
|
|
4037
|
-
const aType = getParsedType(
|
|
5917
|
+
function mergeValues(a2, b) {
|
|
5918
|
+
const aType = getParsedType(a2);
|
|
4038
5919
|
const bType = getParsedType(b);
|
|
4039
|
-
if (
|
|
4040
|
-
return { valid: true, data:
|
|
5920
|
+
if (a2 === b) {
|
|
5921
|
+
return { valid: true, data: a2 };
|
|
4041
5922
|
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
4042
5923
|
const bKeys = util.objectKeys(b);
|
|
4043
|
-
const sharedKeys = util.objectKeys(
|
|
4044
|
-
const newObj = { ...
|
|
5924
|
+
const sharedKeys = util.objectKeys(a2).filter((key) => bKeys.indexOf(key) !== -1);
|
|
5925
|
+
const newObj = { ...a2, ...b };
|
|
4045
5926
|
for (const key of sharedKeys) {
|
|
4046
|
-
const sharedValue = mergeValues(
|
|
5927
|
+
const sharedValue = mergeValues(a2[key], b[key]);
|
|
4047
5928
|
if (!sharedValue.valid) {
|
|
4048
5929
|
return { valid: false };
|
|
4049
5930
|
}
|
|
@@ -4051,12 +5932,12 @@ function mergeValues(a, b) {
|
|
|
4051
5932
|
}
|
|
4052
5933
|
return { valid: true, data: newObj };
|
|
4053
5934
|
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
4054
|
-
if (
|
|
5935
|
+
if (a2.length !== b.length) {
|
|
4055
5936
|
return { valid: false };
|
|
4056
5937
|
}
|
|
4057
5938
|
const newArray = [];
|
|
4058
|
-
for (let index = 0; index <
|
|
4059
|
-
const itemA =
|
|
5939
|
+
for (let index = 0; index < a2.length; index++) {
|
|
5940
|
+
const itemA = a2[index];
|
|
4060
5941
|
const itemB = b[index];
|
|
4061
5942
|
const sharedValue = mergeValues(itemA, itemB);
|
|
4062
5943
|
if (!sharedValue.valid) {
|
|
@@ -4065,8 +5946,8 @@ function mergeValues(a, b) {
|
|
|
4065
5946
|
newArray.push(sharedValue.data);
|
|
4066
5947
|
}
|
|
4067
5948
|
return { valid: true, data: newArray };
|
|
4068
|
-
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +
|
|
4069
|
-
return { valid: true, data:
|
|
5949
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a2 === +b) {
|
|
5950
|
+
return { valid: true, data: a2 };
|
|
4070
5951
|
} else {
|
|
4071
5952
|
return { valid: false };
|
|
4072
5953
|
}
|
|
@@ -4360,7 +6241,7 @@ class ZodSet extends ZodType {
|
|
|
4360
6241
|
}
|
|
4361
6242
|
return { status: status.value, value: parsedSet };
|
|
4362
6243
|
}
|
|
4363
|
-
const elements = [...ctx.data.values()].map((item,
|
|
6244
|
+
const elements = [...ctx.data.values()].map((item, i2) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i2)));
|
|
4364
6245
|
if (ctx.common.async) {
|
|
4365
6246
|
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
4366
6247
|
} else {
|
|
@@ -4438,13 +6319,13 @@ class ZodFunction extends ZodType {
|
|
|
4438
6319
|
const me = this;
|
|
4439
6320
|
return OK(async function(...args) {
|
|
4440
6321
|
const error = new ZodError([]);
|
|
4441
|
-
const parsedArgs = await me._def.args.parseAsync(args, params).catch((
|
|
4442
|
-
error.addIssue(makeArgsIssue(args,
|
|
6322
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e2) => {
|
|
6323
|
+
error.addIssue(makeArgsIssue(args, e2));
|
|
4443
6324
|
throw error;
|
|
4444
6325
|
});
|
|
4445
6326
|
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
4446
|
-
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((
|
|
4447
|
-
error.addIssue(makeReturnsIssue(result,
|
|
6327
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e2) => {
|
|
6328
|
+
error.addIssue(makeReturnsIssue(result, e2));
|
|
4448
6329
|
throw error;
|
|
4449
6330
|
});
|
|
4450
6331
|
return parsedReturns;
|
|
@@ -5019,9 +6900,9 @@ class ZodPipeline extends ZodType {
|
|
|
5019
6900
|
}
|
|
5020
6901
|
}
|
|
5021
6902
|
}
|
|
5022
|
-
static create(
|
|
6903
|
+
static create(a2, b) {
|
|
5023
6904
|
return new ZodPipeline({
|
|
5024
|
-
in:
|
|
6905
|
+
in: a2,
|
|
5025
6906
|
out: b,
|
|
5026
6907
|
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
5027
6908
|
});
|
|
@@ -5338,7 +7219,7 @@ const ProductSelector = ({
|
|
|
5338
7219
|
{
|
|
5339
7220
|
className: "text-lg py-2",
|
|
5340
7221
|
placeholder: "Search products...",
|
|
5341
|
-
onChange: (
|
|
7222
|
+
onChange: (e2) => debouncedSearchHandler(e2.target.value)
|
|
5342
7223
|
}
|
|
5343
7224
|
),
|
|
5344
7225
|
/* @__PURE__ */ jsx(DataTable, { instance: table, children: /* @__PURE__ */ jsx(DataTable.Table, {}) })
|
|
@@ -5377,207 +7258,321 @@ const FlashSaleForm = ({
|
|
|
5377
7258
|
onCancel,
|
|
5378
7259
|
disabled = false
|
|
5379
7260
|
}) => {
|
|
5380
|
-
|
|
5381
|
-
|
|
5382
|
-
|
|
5383
|
-
|
|
7261
|
+
var _a;
|
|
7262
|
+
const {
|
|
7263
|
+
control,
|
|
7264
|
+
register,
|
|
7265
|
+
handleSubmit,
|
|
7266
|
+
watch,
|
|
7267
|
+
setValue,
|
|
7268
|
+
formState: { errors }
|
|
7269
|
+
} = useForm({
|
|
7270
|
+
resolver: t(campaignDataSchema),
|
|
7271
|
+
defaultValues: {
|
|
7272
|
+
name: (initialData == null ? void 0 : initialData.name) || "",
|
|
7273
|
+
description: (initialData == null ? void 0 : initialData.description) || "",
|
|
5384
7274
|
type: "flash-sale",
|
|
5385
|
-
starts_at: "",
|
|
5386
|
-
ends_at: ""
|
|
7275
|
+
starts_at: (initialData == null ? void 0 : initialData.starts_at) || "",
|
|
7276
|
+
ends_at: (initialData == null ? void 0 : initialData.ends_at) || "",
|
|
7277
|
+
products: initialProducts ? Array.from(initialProducts.values()) : []
|
|
7278
|
+
}
|
|
7279
|
+
});
|
|
7280
|
+
const { fields, append, remove } = useFieldArray({
|
|
7281
|
+
control,
|
|
7282
|
+
name: "products"
|
|
7283
|
+
});
|
|
7284
|
+
const [openProductModal, setOpenProductModal] = useState(false);
|
|
7285
|
+
const startsAt = watch("starts_at");
|
|
7286
|
+
const endsAt = watch("ends_at");
|
|
7287
|
+
const handleDateTimeChange = (field, type, value) => {
|
|
7288
|
+
const currentValue = watch(field);
|
|
7289
|
+
if (type === "date") {
|
|
7290
|
+
const time = currentValue ? dayjs(currentValue).format("HH:mm") : field === "starts_at" ? "00:00" : "23:59";
|
|
7291
|
+
setValue(field, `${value}T${time}`);
|
|
7292
|
+
} else {
|
|
7293
|
+
const date = currentValue ? dayjs(currentValue).format("YYYY-MM-DD") : dayjs().format("YYYY-MM-DD");
|
|
7294
|
+
setValue(field, `${date}T${value}`);
|
|
5387
7295
|
}
|
|
5388
|
-
);
|
|
5389
|
-
const handleCampaignChange = (name, value) => {
|
|
5390
|
-
setCampaign((prev) => ({ ...prev, [name]: value }));
|
|
5391
7296
|
};
|
|
5392
|
-
const
|
|
5393
|
-
|
|
5394
|
-
);
|
|
5395
|
-
const handleProductChange = (product) => {
|
|
5396
|
-
setProducts((prev) => {
|
|
5397
|
-
const newProducts = new Map(prev);
|
|
5398
|
-
newProducts.set(product.product.id, product);
|
|
5399
|
-
return newProducts;
|
|
5400
|
-
});
|
|
7297
|
+
const onFormSubmit = (data) => {
|
|
7298
|
+
onSubmit(data);
|
|
5401
7299
|
};
|
|
5402
|
-
const
|
|
5403
|
-
|
|
5404
|
-
|
|
5405
|
-
|
|
5406
|
-
|
|
5407
|
-
|
|
7300
|
+
const onFormError = () => {
|
|
7301
|
+
const errorMessages = Object.entries(errors).map(([key, value]) => {
|
|
7302
|
+
var _a2;
|
|
7303
|
+
if (key === "products" && Array.isArray(value)) {
|
|
7304
|
+
return value.map(
|
|
7305
|
+
(item, index) => item ? `Product ${index + 1}: ${Object.values(item).join(", ")}` : ""
|
|
7306
|
+
).filter(Boolean).join(", ");
|
|
7307
|
+
}
|
|
7308
|
+
return (value == null ? void 0 : value.message) || ((_a2 = value == null ? void 0 : value.root) == null ? void 0 : _a2.message);
|
|
7309
|
+
}).filter(Boolean).join(", ");
|
|
7310
|
+
toast.error("Invalid data", {
|
|
7311
|
+
description: errorMessages
|
|
5408
7312
|
});
|
|
5409
|
-
if (!campaignData.success) {
|
|
5410
|
-
toast.error("Invalid data", {
|
|
5411
|
-
description: campaignData.error.issues.map((error) => error.message).join(", ")
|
|
5412
|
-
});
|
|
5413
|
-
return;
|
|
5414
|
-
}
|
|
5415
|
-
onSubmit(campaignData.data);
|
|
5416
7313
|
};
|
|
5417
7314
|
return /* @__PURE__ */ jsxs(Container, { children: [
|
|
5418
7315
|
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
5419
7316
|
/* @__PURE__ */ jsx("h1", { className: "text-xl font-semibold", children: "Flash sale campaign" }),
|
|
5420
7317
|
/* @__PURE__ */ jsx(Button, { variant: "transparent", onClick: onCancel, children: "Cancel" })
|
|
5421
7318
|
] }),
|
|
5422
|
-
/* @__PURE__ */ jsxs(
|
|
5423
|
-
|
|
5424
|
-
|
|
5425
|
-
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
|
|
5430
|
-
|
|
5431
|
-
|
|
5432
|
-
|
|
5433
|
-
|
|
5434
|
-
|
|
5435
|
-
|
|
5436
|
-
|
|
5437
|
-
|
|
5438
|
-
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
|
|
5444
|
-
|
|
5445
|
-
|
|
5446
|
-
|
|
5447
|
-
|
|
5448
|
-
|
|
5449
|
-
|
|
5450
|
-
|
|
5451
|
-
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
|
|
5457
|
-
|
|
5458
|
-
|
|
5459
|
-
|
|
5460
|
-
|
|
5461
|
-
|
|
5462
|
-
|
|
5463
|
-
|
|
5464
|
-
|
|
5465
|
-
|
|
5466
|
-
|
|
5467
|
-
|
|
5468
|
-
|
|
5469
|
-
|
|
5470
|
-
|
|
5471
|
-
|
|
5472
|
-
|
|
5473
|
-
|
|
5474
|
-
|
|
5475
|
-
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
5499
|
-
|
|
5500
|
-
|
|
5501
|
-
|
|
5502
|
-
|
|
5503
|
-
children: [
|
|
5504
|
-
/* @__PURE__ */ jsx(
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5513
|
-
|
|
5514
|
-
|
|
5515
|
-
|
|
5516
|
-
|
|
5517
|
-
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
|
|
5521
|
-
|
|
5522
|
-
|
|
5523
|
-
|
|
5524
|
-
|
|
5525
|
-
|
|
5526
|
-
|
|
5527
|
-
|
|
5528
|
-
|
|
5529
|
-
|
|
5530
|
-
|
|
5531
|
-
|
|
5532
|
-
|
|
5533
|
-
|
|
5534
|
-
|
|
5535
|
-
|
|
5536
|
-
|
|
5537
|
-
|
|
5538
|
-
|
|
5539
|
-
|
|
5540
|
-
|
|
5541
|
-
|
|
5542
|
-
|
|
5543
|
-
|
|
5544
|
-
|
|
5545
|
-
|
|
5546
|
-
|
|
5547
|
-
|
|
5548
|
-
|
|
5549
|
-
|
|
5550
|
-
|
|
5551
|
-
|
|
5552
|
-
|
|
5553
|
-
|
|
5554
|
-
|
|
5555
|
-
}
|
|
5556
|
-
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
|
|
7319
|
+
/* @__PURE__ */ jsxs(
|
|
7320
|
+
"form",
|
|
7321
|
+
{
|
|
7322
|
+
onSubmit: handleSubmit(onFormSubmit, onFormError),
|
|
7323
|
+
className: "space-y-4 my-8",
|
|
7324
|
+
children: [
|
|
7325
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
7326
|
+
/* @__PURE__ */ jsx(Label, { children: "Name" }),
|
|
7327
|
+
/* @__PURE__ */ jsx(Input, { ...register("name"), disabled }),
|
|
7328
|
+
errors.name && /* @__PURE__ */ jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.name.message })
|
|
7329
|
+
] }),
|
|
7330
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
7331
|
+
/* @__PURE__ */ jsx(Label, { children: "Description" }),
|
|
7332
|
+
/* @__PURE__ */ jsx(Textarea, { ...register("description"), disabled }),
|
|
7333
|
+
errors.description && /* @__PURE__ */ jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.description.message })
|
|
7334
|
+
] }),
|
|
7335
|
+
/* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
7336
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
7337
|
+
/* @__PURE__ */ jsx(Label, { children: "Start Date" }),
|
|
7338
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
|
|
7339
|
+
/* @__PURE__ */ jsx(
|
|
7340
|
+
Input,
|
|
7341
|
+
{
|
|
7342
|
+
type: "date",
|
|
7343
|
+
name: "start_date",
|
|
7344
|
+
value: campaign.starts_at ? dayjs(campaign.starts_at).format("YYYY-MM-DD") : "",
|
|
7345
|
+
onChange: (e2) => {
|
|
7346
|
+
const time = campaign.starts_at ? dayjs(campaign.starts_at).format("HH:mm") : "00:00";
|
|
7347
|
+
handleCampaignChange(
|
|
7348
|
+
"starts_at",
|
|
7349
|
+
`${e2.target.value}T${time}`
|
|
7350
|
+
);
|
|
7351
|
+
},
|
|
7352
|
+
disabled,
|
|
7353
|
+
className: "flex-1"
|
|
7354
|
+
}
|
|
7355
|
+
),
|
|
7356
|
+
/* @__PURE__ */ jsx(
|
|
7357
|
+
Input,
|
|
7358
|
+
{
|
|
7359
|
+
type: "time",
|
|
7360
|
+
name: "start_time",
|
|
7361
|
+
value: campaign.starts_at ? dayjs(campaign.starts_at).format("HH:mm") : "",
|
|
7362
|
+
onChange: (e2) => {
|
|
7363
|
+
const date = campaign.starts_at ? dayjs(campaign.starts_at).format("YYYY-MM-DD") : dayjs().format("YYYY-MM-DD");
|
|
7364
|
+
handleCampaignChange(
|
|
7365
|
+
"starts_at",
|
|
7366
|
+
`${date}T${e2.target.value}`
|
|
7367
|
+
);
|
|
7368
|
+
},
|
|
7369
|
+
disabled,
|
|
7370
|
+
className: "w-32"
|
|
7371
|
+
}
|
|
7372
|
+
)
|
|
7373
|
+
] }),
|
|
7374
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
|
|
7375
|
+
/* @__PURE__ */ jsx(
|
|
7376
|
+
Input,
|
|
7377
|
+
{
|
|
7378
|
+
type: "date",
|
|
7379
|
+
value: startsAt ? dayjs(startsAt).format("YYYY-MM-DD") : "",
|
|
7380
|
+
onChange: (e2) => handleDateTimeChange("starts_at", "date", e2.target.value),
|
|
7381
|
+
disabled,
|
|
7382
|
+
className: "flex-1"
|
|
7383
|
+
}
|
|
7384
|
+
),
|
|
7385
|
+
/* @__PURE__ */ jsx(
|
|
7386
|
+
Input,
|
|
7387
|
+
{
|
|
7388
|
+
type: "time",
|
|
7389
|
+
value: startsAt ? dayjs(startsAt).format("HH:mm") : "",
|
|
7390
|
+
onChange: (e2) => handleDateTimeChange("starts_at", "time", e2.target.value),
|
|
7391
|
+
disabled,
|
|
7392
|
+
className: "w-32"
|
|
7393
|
+
}
|
|
7394
|
+
)
|
|
7395
|
+
] }),
|
|
7396
|
+
errors.starts_at && /* @__PURE__ */ jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.starts_at.message })
|
|
7397
|
+
] }),
|
|
7398
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
7399
|
+
/* @__PURE__ */ jsx(Label, { children: "End Date" }),
|
|
7400
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
|
|
7401
|
+
/* @__PURE__ */ jsx(
|
|
7402
|
+
Input,
|
|
7403
|
+
{
|
|
7404
|
+
type: "date",
|
|
7405
|
+
name: "end_date",
|
|
7406
|
+
value: campaign.ends_at ? dayjs(campaign.ends_at).format("YYYY-MM-DD") : "",
|
|
7407
|
+
onChange: (e2) => {
|
|
7408
|
+
const time = campaign.ends_at ? dayjs(campaign.ends_at).format("HH:mm") : "23:59";
|
|
7409
|
+
handleCampaignChange("ends_at", `${e2.target.value}T${time}`);
|
|
7410
|
+
},
|
|
7411
|
+
disabled,
|
|
7412
|
+
className: "flex-1"
|
|
7413
|
+
}
|
|
7414
|
+
),
|
|
7415
|
+
/* @__PURE__ */ jsx(
|
|
7416
|
+
Input,
|
|
7417
|
+
{
|
|
7418
|
+
type: "time",
|
|
7419
|
+
name: "end_time",
|
|
7420
|
+
value: campaign.ends_at ? dayjs(campaign.ends_at).format("HH:mm") : "",
|
|
7421
|
+
onChange: (e2) => {
|
|
7422
|
+
const date = campaign.ends_at ? dayjs(campaign.ends_at).format("YYYY-MM-DD") : dayjs().format("YYYY-MM-DD");
|
|
7423
|
+
handleCampaignChange("ends_at", `${date}T${e2.target.value}`);
|
|
7424
|
+
},
|
|
7425
|
+
disabled,
|
|
7426
|
+
className: "w-32"
|
|
7427
|
+
}
|
|
7428
|
+
)
|
|
7429
|
+
] }),
|
|
7430
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
|
|
7431
|
+
/* @__PURE__ */ jsx(
|
|
7432
|
+
Input,
|
|
7433
|
+
{
|
|
7434
|
+
type: "date",
|
|
7435
|
+
value: endsAt ? dayjs(endsAt).format("YYYY-MM-DD") : "",
|
|
7436
|
+
onChange: (e2) => handleDateTimeChange("ends_at", "date", e2.target.value),
|
|
7437
|
+
disabled,
|
|
7438
|
+
className: "flex-1"
|
|
7439
|
+
}
|
|
7440
|
+
),
|
|
7441
|
+
/* @__PURE__ */ jsx(
|
|
7442
|
+
Input,
|
|
7443
|
+
{
|
|
7444
|
+
type: "time",
|
|
7445
|
+
value: endsAt ? dayjs(endsAt).format("HH:mm") : "",
|
|
7446
|
+
onChange: (e2) => handleDateTimeChange("ends_at", "time", e2.target.value),
|
|
7447
|
+
disabled,
|
|
7448
|
+
className: "w-32"
|
|
7449
|
+
}
|
|
7450
|
+
)
|
|
7451
|
+
] }),
|
|
7452
|
+
errors.ends_at && /* @__PURE__ */ jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.ends_at.message })
|
|
7453
|
+
] })
|
|
7454
|
+
] }),
|
|
7455
|
+
/* @__PURE__ */ jsxs("div", { className: "flex justify-between items-center", children: [
|
|
7456
|
+
/* @__PURE__ */ jsx(Label, { children: "Products" }),
|
|
7457
|
+
/* @__PURE__ */ jsx(
|
|
7458
|
+
Button,
|
|
7459
|
+
{
|
|
7460
|
+
type: "button",
|
|
7461
|
+
variant: "secondary",
|
|
7462
|
+
onClick: () => setOpenProductModal(true),
|
|
7463
|
+
disabled,
|
|
7464
|
+
children: "Add Product"
|
|
7465
|
+
}
|
|
7466
|
+
)
|
|
7467
|
+
] }),
|
|
7468
|
+
((_a = errors.products) == null ? void 0 : _a.root) && /* @__PURE__ */ jsx("p", { className: "text-red-500 text-sm", children: errors.products.root.message }),
|
|
7469
|
+
/* @__PURE__ */ jsxs(Table, { children: [
|
|
7470
|
+
/* @__PURE__ */ jsx(Table.Header, { children: /* @__PURE__ */ jsxs(Table.Row, { children: [
|
|
7471
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Product" }),
|
|
7472
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Discount Type" }),
|
|
7473
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Discount Value" }),
|
|
7474
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Limit" }),
|
|
7475
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Max Qty per Order" }),
|
|
7476
|
+
/* @__PURE__ */ jsx(Table.HeaderCell, { children: "Actions" })
|
|
7477
|
+
] }) }),
|
|
7478
|
+
/* @__PURE__ */ jsx(Table.Body, { children: fields.map((field, index) => /* @__PURE__ */ jsxs(Table.Row, { children: [
|
|
7479
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: field.product.title }),
|
|
7480
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: /* @__PURE__ */ jsx(
|
|
7481
|
+
Controller,
|
|
7482
|
+
{
|
|
7483
|
+
name: `products.${index}.discountType`,
|
|
7484
|
+
control,
|
|
7485
|
+
render: ({ field: field2 }) => /* @__PURE__ */ jsxs(
|
|
7486
|
+
Select,
|
|
7487
|
+
{
|
|
7488
|
+
value: field2.value,
|
|
7489
|
+
onValueChange: field2.onChange,
|
|
7490
|
+
disabled,
|
|
7491
|
+
children: [
|
|
7492
|
+
/* @__PURE__ */ jsx(Select.Trigger, { children: /* @__PURE__ */ jsx(Select.Value, { placeholder: "Select discount type" }) }),
|
|
7493
|
+
/* @__PURE__ */ jsx(Select.Content, { children: /* @__PURE__ */ jsx(Select.Item, { value: "percentage", children: "Percentage" }) })
|
|
7494
|
+
]
|
|
7495
|
+
}
|
|
7496
|
+
)
|
|
7497
|
+
}
|
|
7498
|
+
) }),
|
|
7499
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: /* @__PURE__ */ jsx(
|
|
7500
|
+
Controller,
|
|
7501
|
+
{
|
|
7502
|
+
name: `products.${index}.discountValue`,
|
|
7503
|
+
control,
|
|
7504
|
+
render: ({ field: field2 }) => /* @__PURE__ */ jsx(
|
|
7505
|
+
Input,
|
|
7506
|
+
{
|
|
7507
|
+
type: "number",
|
|
7508
|
+
value: field2.value,
|
|
7509
|
+
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
7510
|
+
disabled
|
|
7511
|
+
}
|
|
7512
|
+
)
|
|
7513
|
+
}
|
|
7514
|
+
) }),
|
|
7515
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: /* @__PURE__ */ jsx(
|
|
7516
|
+
Controller,
|
|
7517
|
+
{
|
|
7518
|
+
name: `products.${index}.limit`,
|
|
7519
|
+
control,
|
|
7520
|
+
render: ({ field: field2 }) => /* @__PURE__ */ jsx(
|
|
7521
|
+
Input,
|
|
7522
|
+
{
|
|
7523
|
+
type: "number",
|
|
7524
|
+
value: field2.value,
|
|
7525
|
+
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
7526
|
+
disabled
|
|
7527
|
+
}
|
|
7528
|
+
)
|
|
7529
|
+
}
|
|
7530
|
+
) }),
|
|
7531
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: /* @__PURE__ */ jsx(
|
|
7532
|
+
Controller,
|
|
7533
|
+
{
|
|
7534
|
+
name: `products.${index}.maxQty`,
|
|
7535
|
+
control,
|
|
7536
|
+
render: ({ field: field2 }) => /* @__PURE__ */ jsx(
|
|
7537
|
+
Input,
|
|
7538
|
+
{
|
|
7539
|
+
type: "number",
|
|
7540
|
+
value: field2.value,
|
|
7541
|
+
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
7542
|
+
disabled
|
|
7543
|
+
}
|
|
7544
|
+
)
|
|
7545
|
+
}
|
|
7546
|
+
) }),
|
|
7547
|
+
/* @__PURE__ */ jsx(Table.Cell, { children: /* @__PURE__ */ jsx(
|
|
7548
|
+
Button,
|
|
7549
|
+
{
|
|
7550
|
+
type: "button",
|
|
7551
|
+
variant: "danger",
|
|
7552
|
+
onClick: () => remove(index),
|
|
7553
|
+
disabled,
|
|
7554
|
+
children: /* @__PURE__ */ jsx(Trash, {})
|
|
7555
|
+
}
|
|
7556
|
+
) })
|
|
7557
|
+
] }, field.id)) })
|
|
7558
|
+
] }),
|
|
7559
|
+
/* @__PURE__ */ jsx(Button, { type: "submit", disabled, children: "Save" })
|
|
7560
|
+
]
|
|
7561
|
+
}
|
|
7562
|
+
),
|
|
5564
7563
|
/* @__PURE__ */ jsx(FocusModal, { open: openProductModal, onOpenChange: setOpenProductModal, children: /* @__PURE__ */ jsxs(FocusModal.Content, { children: [
|
|
5565
7564
|
/* @__PURE__ */ jsx(FocusModal.Header, { title: "Add Product" }),
|
|
5566
7565
|
/* @__PURE__ */ jsx(FocusModal.Body, { children: /* @__PURE__ */ jsx(
|
|
5567
7566
|
ProductSelector,
|
|
5568
7567
|
{
|
|
5569
|
-
selectedProductIds:
|
|
7568
|
+
selectedProductIds: fields.map((f2) => f2.product.id),
|
|
5570
7569
|
onSelectProduct: (product) => {
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
|
|
5577
|
-
limit: 10,
|
|
5578
|
-
maxQty: 1
|
|
5579
|
-
});
|
|
5580
|
-
return newProducts;
|
|
7570
|
+
append({
|
|
7571
|
+
product,
|
|
7572
|
+
discountType: "percentage",
|
|
7573
|
+
discountValue: 10,
|
|
7574
|
+
limit: 10,
|
|
7575
|
+
maxQty: 1
|
|
5581
7576
|
});
|
|
5582
7577
|
setOpenProductModal(false);
|
|
5583
7578
|
}
|