@bonlineza/luxity-lib 2.30.3 → 2.31.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/CHANGELOG.md +14 -0
- package/GALink.js +60 -0
- package/LuxityDesktopHeader.js +373 -0
- package/LuxityFooter.js +2026 -0
- package/LuxityGraphQLDropdownSelector.js +6 -1
- package/LuxityHeader.js +369 -0
- package/LuxityMobileHeader.js +567 -0
- package/LuxitySideNav.js +424 -0
- package/MaterialSelector.js +6 -1
- package/ModelSelector.js +6 -1
- package/Tracking.js +27 -0
- package/build/assets/.DS_Store +0 -0
- package/build/bundle.css +1 -0
- package/build/bundle.js +997 -0
- package/package.json +5 -5
package/LuxityFooter.js
ADDED
|
@@ -0,0 +1,2026 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react'), require('styled-components')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['react', 'styled-components'], factory) :
|
|
4
|
+
(global = global || self, global.LuxityFooter = factory(global.React, global.styled));
|
|
5
|
+
}(this, (function (React, styled) { 'use strict';
|
|
6
|
+
|
|
7
|
+
var React__default = 'default' in React ? React['default'] : React;
|
|
8
|
+
var styled__default = 'default' in styled ? styled['default'] : styled;
|
|
9
|
+
|
|
10
|
+
function _extends() {
|
|
11
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
12
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
13
|
+
var source = arguments[i];
|
|
14
|
+
|
|
15
|
+
for (var key in source) {
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
17
|
+
target[key] = source[key];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return target;
|
|
23
|
+
};
|
|
24
|
+
return _extends.apply(this, arguments);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function _taggedTemplateLiteral(strings, raw) {
|
|
28
|
+
if (!raw) {
|
|
29
|
+
raw = strings.slice(0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return Object.freeze(Object.defineProperties(strings, {
|
|
33
|
+
raw: {
|
|
34
|
+
value: Object.freeze(raw)
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
40
|
+
|
|
41
|
+
var isDateObject = (value) => value instanceof Date;
|
|
42
|
+
|
|
43
|
+
var isNullOrUndefined = (value) => value == null;
|
|
44
|
+
|
|
45
|
+
const isObjectType = (value) => typeof value === 'object';
|
|
46
|
+
var isObject = (value) => !isNullOrUndefined(value) &&
|
|
47
|
+
!Array.isArray(value) &&
|
|
48
|
+
isObjectType(value) &&
|
|
49
|
+
!isDateObject(value);
|
|
50
|
+
|
|
51
|
+
var getEventValue = (event) => isObject(event) && event.target
|
|
52
|
+
? isCheckBoxInput(event.target)
|
|
53
|
+
? event.target.checked
|
|
54
|
+
: event.target.value
|
|
55
|
+
: event;
|
|
56
|
+
|
|
57
|
+
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
58
|
+
|
|
59
|
+
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
60
|
+
|
|
61
|
+
var isPlainObject = (tempObject) => {
|
|
62
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
63
|
+
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
var isWeb = typeof window !== 'undefined' &&
|
|
67
|
+
typeof window.HTMLElement !== 'undefined' &&
|
|
68
|
+
typeof document !== 'undefined';
|
|
69
|
+
|
|
70
|
+
function cloneObject(data) {
|
|
71
|
+
let copy;
|
|
72
|
+
const isArray = Array.isArray(data);
|
|
73
|
+
if (data instanceof Date) {
|
|
74
|
+
copy = new Date(data);
|
|
75
|
+
}
|
|
76
|
+
else if (data instanceof Set) {
|
|
77
|
+
copy = new Set(data);
|
|
78
|
+
}
|
|
79
|
+
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
|
80
|
+
(isArray || isObject(data))) {
|
|
81
|
+
copy = isArray ? [] : {};
|
|
82
|
+
if (!isArray && !isPlainObject(data)) {
|
|
83
|
+
copy = data;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
for (const key in data) {
|
|
87
|
+
if (data.hasOwnProperty(key)) {
|
|
88
|
+
copy[key] = cloneObject(data[key]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return data;
|
|
95
|
+
}
|
|
96
|
+
return copy;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
|
100
|
+
|
|
101
|
+
var isUndefined = (val) => val === undefined;
|
|
102
|
+
|
|
103
|
+
var get = (obj, path, defaultValue) => {
|
|
104
|
+
if (!path || !isObject(obj)) {
|
|
105
|
+
return defaultValue;
|
|
106
|
+
}
|
|
107
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
|
|
108
|
+
return isUndefined(result) || result === obj
|
|
109
|
+
? isUndefined(obj[path])
|
|
110
|
+
? defaultValue
|
|
111
|
+
: obj[path]
|
|
112
|
+
: result;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const EVENTS = {
|
|
116
|
+
BLUR: 'blur',
|
|
117
|
+
FOCUS_OUT: 'focusout',
|
|
118
|
+
CHANGE: 'change',
|
|
119
|
+
};
|
|
120
|
+
const VALIDATION_MODE = {
|
|
121
|
+
onBlur: 'onBlur',
|
|
122
|
+
onChange: 'onChange',
|
|
123
|
+
onSubmit: 'onSubmit',
|
|
124
|
+
onTouched: 'onTouched',
|
|
125
|
+
all: 'all',
|
|
126
|
+
};
|
|
127
|
+
const INPUT_VALIDATION_RULES = {
|
|
128
|
+
max: 'max',
|
|
129
|
+
min: 'min',
|
|
130
|
+
maxLength: 'maxLength',
|
|
131
|
+
minLength: 'minLength',
|
|
132
|
+
pattern: 'pattern',
|
|
133
|
+
required: 'required',
|
|
134
|
+
validate: 'validate',
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const HookFormContext = React__default.createContext(null);
|
|
138
|
+
|
|
139
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
140
|
+
const result = {
|
|
141
|
+
defaultValues: control._defaultValues,
|
|
142
|
+
};
|
|
143
|
+
for (const key in formState) {
|
|
144
|
+
Object.defineProperty(result, key, {
|
|
145
|
+
get: () => {
|
|
146
|
+
const _key = key;
|
|
147
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
148
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
149
|
+
}
|
|
150
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
|
151
|
+
return formState[_key];
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
159
|
+
|
|
160
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
161
|
+
updateFormState(formStateData);
|
|
162
|
+
const { name, ...formState } = formStateData;
|
|
163
|
+
return (isEmptyObject(formState) ||
|
|
164
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
165
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
166
|
+
(!isRoot || VALIDATION_MODE.all)));
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
170
|
+
|
|
171
|
+
function useSubscribe(props) {
|
|
172
|
+
const _props = React__default.useRef(props);
|
|
173
|
+
_props.current = props;
|
|
174
|
+
React__default.useEffect(() => {
|
|
175
|
+
const subscription = !props.disabled &&
|
|
176
|
+
_props.current.subject &&
|
|
177
|
+
_props.current.subject.subscribe({
|
|
178
|
+
next: _props.current.next,
|
|
179
|
+
});
|
|
180
|
+
return () => {
|
|
181
|
+
subscription && subscription.unsubscribe();
|
|
182
|
+
};
|
|
183
|
+
}, [props.disabled]);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
var isString = (value) => typeof value === 'string';
|
|
187
|
+
|
|
188
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
|
189
|
+
if (isString(names)) {
|
|
190
|
+
isGlobal && _names.watch.add(names);
|
|
191
|
+
return get(formValues, names, defaultValue);
|
|
192
|
+
}
|
|
193
|
+
if (Array.isArray(names)) {
|
|
194
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
195
|
+
}
|
|
196
|
+
isGlobal && (_names.watchAll = true);
|
|
197
|
+
return formValues;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
var isKey = (value) => /^\w*$/.test(value);
|
|
201
|
+
|
|
202
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
203
|
+
|
|
204
|
+
function set(object, path, value) {
|
|
205
|
+
let index = -1;
|
|
206
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
207
|
+
const length = tempPath.length;
|
|
208
|
+
const lastIndex = length - 1;
|
|
209
|
+
while (++index < length) {
|
|
210
|
+
const key = tempPath[index];
|
|
211
|
+
let newValue = value;
|
|
212
|
+
if (index !== lastIndex) {
|
|
213
|
+
const objValue = object[key];
|
|
214
|
+
newValue =
|
|
215
|
+
isObject(objValue) || Array.isArray(objValue)
|
|
216
|
+
? objValue
|
|
217
|
+
: !isNaN(+tempPath[index + 1])
|
|
218
|
+
? []
|
|
219
|
+
: {};
|
|
220
|
+
}
|
|
221
|
+
object[key] = newValue;
|
|
222
|
+
object = object[key];
|
|
223
|
+
}
|
|
224
|
+
return object;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
|
228
|
+
? {
|
|
229
|
+
...errors[name],
|
|
230
|
+
types: {
|
|
231
|
+
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
|
232
|
+
[type]: message || true,
|
|
233
|
+
},
|
|
234
|
+
}
|
|
235
|
+
: {};
|
|
236
|
+
|
|
237
|
+
const focusFieldBy = (fields, callback, fieldsNames) => {
|
|
238
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
|
239
|
+
const field = get(fields, key);
|
|
240
|
+
if (field) {
|
|
241
|
+
const { _f, ...currentField } = field;
|
|
242
|
+
if (_f && callback(_f.name)) {
|
|
243
|
+
if (_f.ref.focus) {
|
|
244
|
+
_f.ref.focus();
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
else if (_f.refs && _f.refs[0].focus) {
|
|
248
|
+
_f.refs[0].focus();
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
else if (isObject(currentField)) {
|
|
253
|
+
focusFieldBy(currentField, callback);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
var getValidationModes = (mode) => ({
|
|
260
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
261
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
262
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
263
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
264
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
268
|
+
(_names.watchAll ||
|
|
269
|
+
_names.watch.has(name) ||
|
|
270
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
|
271
|
+
/^\.\w+/.test(name.slice(watchName.length))));
|
|
272
|
+
|
|
273
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
|
274
|
+
const fieldArrayErrors = compact(get(errors, name));
|
|
275
|
+
set(fieldArrayErrors, 'root', error[name]);
|
|
276
|
+
set(errors, name, fieldArrayErrors);
|
|
277
|
+
return errors;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
var isBoolean = (value) => typeof value === 'boolean';
|
|
281
|
+
|
|
282
|
+
var isFileInput = (element) => element.type === 'file';
|
|
283
|
+
|
|
284
|
+
var isFunction = (value) => typeof value === 'function';
|
|
285
|
+
|
|
286
|
+
var isHTMLElement = (value) => {
|
|
287
|
+
if (!isWeb) {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
const owner = value ? value.ownerDocument : 0;
|
|
291
|
+
return (value instanceof
|
|
292
|
+
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
var isMessage = (value) => isString(value);
|
|
296
|
+
|
|
297
|
+
var isRadioInput = (element) => element.type === 'radio';
|
|
298
|
+
|
|
299
|
+
var isRegex = (value) => value instanceof RegExp;
|
|
300
|
+
|
|
301
|
+
const defaultResult = {
|
|
302
|
+
value: false,
|
|
303
|
+
isValid: false,
|
|
304
|
+
};
|
|
305
|
+
const validResult = { value: true, isValid: true };
|
|
306
|
+
var getCheckboxValue = (options) => {
|
|
307
|
+
if (Array.isArray(options)) {
|
|
308
|
+
if (options.length > 1) {
|
|
309
|
+
const values = options
|
|
310
|
+
.filter((option) => option && option.checked && !option.disabled)
|
|
311
|
+
.map((option) => option.value);
|
|
312
|
+
return { value: values, isValid: !!values.length };
|
|
313
|
+
}
|
|
314
|
+
return options[0].checked && !options[0].disabled
|
|
315
|
+
? // @ts-expect-error expected to work in the browser
|
|
316
|
+
options[0].attributes && !isUndefined(options[0].attributes.value)
|
|
317
|
+
? isUndefined(options[0].value) || options[0].value === ''
|
|
318
|
+
? validResult
|
|
319
|
+
: { value: options[0].value, isValid: true }
|
|
320
|
+
: validResult
|
|
321
|
+
: defaultResult;
|
|
322
|
+
}
|
|
323
|
+
return defaultResult;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const defaultReturn = {
|
|
327
|
+
isValid: false,
|
|
328
|
+
value: null,
|
|
329
|
+
};
|
|
330
|
+
var getRadioValue = (options) => Array.isArray(options)
|
|
331
|
+
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
|
332
|
+
? {
|
|
333
|
+
isValid: true,
|
|
334
|
+
value: option.value,
|
|
335
|
+
}
|
|
336
|
+
: previous, defaultReturn)
|
|
337
|
+
: defaultReturn;
|
|
338
|
+
|
|
339
|
+
function getValidateError(result, ref, type = 'validate') {
|
|
340
|
+
if (isMessage(result) ||
|
|
341
|
+
(Array.isArray(result) && result.every(isMessage)) ||
|
|
342
|
+
(isBoolean(result) && !result)) {
|
|
343
|
+
return {
|
|
344
|
+
type,
|
|
345
|
+
message: isMessage(result) ? result : '',
|
|
346
|
+
ref,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
|
352
|
+
? validationData
|
|
353
|
+
: {
|
|
354
|
+
value: validationData,
|
|
355
|
+
message: '',
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
359
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
|
|
360
|
+
const inputValue = get(formValues, name);
|
|
361
|
+
if (!mount || disabled) {
|
|
362
|
+
return {};
|
|
363
|
+
}
|
|
364
|
+
const inputRef = refs ? refs[0] : ref;
|
|
365
|
+
const setCustomValidity = (message) => {
|
|
366
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
367
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
|
368
|
+
inputRef.reportValidity();
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
const error = {};
|
|
372
|
+
const isRadio = isRadioInput(ref);
|
|
373
|
+
const isCheckBox = isCheckBoxInput(ref);
|
|
374
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
375
|
+
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
|
376
|
+
isUndefined(ref.value) &&
|
|
377
|
+
isUndefined(inputValue)) ||
|
|
378
|
+
(isHTMLElement(ref) && ref.value === '') ||
|
|
379
|
+
inputValue === '' ||
|
|
380
|
+
(Array.isArray(inputValue) && !inputValue.length);
|
|
381
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
382
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
383
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
384
|
+
error[name] = {
|
|
385
|
+
type: exceedMax ? maxType : minType,
|
|
386
|
+
message,
|
|
387
|
+
ref,
|
|
388
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
if (isFieldArray
|
|
392
|
+
? !Array.isArray(inputValue) || !inputValue.length
|
|
393
|
+
: required &&
|
|
394
|
+
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
|
395
|
+
(isBoolean(inputValue) && !inputValue) ||
|
|
396
|
+
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
|
397
|
+
(isRadio && !getRadioValue(refs).isValid))) {
|
|
398
|
+
const { value, message } = isMessage(required)
|
|
399
|
+
? { value: !!required, message: required }
|
|
400
|
+
: getValueAndMessage(required);
|
|
401
|
+
if (value) {
|
|
402
|
+
error[name] = {
|
|
403
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
404
|
+
message,
|
|
405
|
+
ref: inputRef,
|
|
406
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
|
407
|
+
};
|
|
408
|
+
if (!validateAllFieldCriteria) {
|
|
409
|
+
setCustomValidity(message);
|
|
410
|
+
return error;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
415
|
+
let exceedMax;
|
|
416
|
+
let exceedMin;
|
|
417
|
+
const maxOutput = getValueAndMessage(max);
|
|
418
|
+
const minOutput = getValueAndMessage(min);
|
|
419
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
420
|
+
const valueNumber = ref.valueAsNumber ||
|
|
421
|
+
(inputValue ? +inputValue : inputValue);
|
|
422
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
423
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
424
|
+
}
|
|
425
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
426
|
+
exceedMin = valueNumber < minOutput.value;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
431
|
+
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
|
432
|
+
const isTime = ref.type == 'time';
|
|
433
|
+
const isWeek = ref.type == 'week';
|
|
434
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
435
|
+
exceedMax = isTime
|
|
436
|
+
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
|
437
|
+
: isWeek
|
|
438
|
+
? inputValue > maxOutput.value
|
|
439
|
+
: valueDate > new Date(maxOutput.value);
|
|
440
|
+
}
|
|
441
|
+
if (isString(minOutput.value) && inputValue) {
|
|
442
|
+
exceedMin = isTime
|
|
443
|
+
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
|
444
|
+
: isWeek
|
|
445
|
+
? inputValue < minOutput.value
|
|
446
|
+
: valueDate < new Date(minOutput.value);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (exceedMax || exceedMin) {
|
|
450
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
451
|
+
if (!validateAllFieldCriteria) {
|
|
452
|
+
setCustomValidity(error[name].message);
|
|
453
|
+
return error;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if ((maxLength || minLength) &&
|
|
458
|
+
!isEmpty &&
|
|
459
|
+
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
|
460
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
461
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
|
462
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
|
463
|
+
inputValue.length > +maxLengthOutput.value;
|
|
464
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
|
465
|
+
inputValue.length < +minLengthOutput.value;
|
|
466
|
+
if (exceedMax || exceedMin) {
|
|
467
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
468
|
+
if (!validateAllFieldCriteria) {
|
|
469
|
+
setCustomValidity(error[name].message);
|
|
470
|
+
return error;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
475
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
476
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
477
|
+
error[name] = {
|
|
478
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
479
|
+
message,
|
|
480
|
+
ref,
|
|
481
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
|
482
|
+
};
|
|
483
|
+
if (!validateAllFieldCriteria) {
|
|
484
|
+
setCustomValidity(message);
|
|
485
|
+
return error;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (validate) {
|
|
490
|
+
if (isFunction(validate)) {
|
|
491
|
+
const result = await validate(inputValue, formValues);
|
|
492
|
+
const validateError = getValidateError(result, inputRef);
|
|
493
|
+
if (validateError) {
|
|
494
|
+
error[name] = {
|
|
495
|
+
...validateError,
|
|
496
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
|
497
|
+
};
|
|
498
|
+
if (!validateAllFieldCriteria) {
|
|
499
|
+
setCustomValidity(validateError.message);
|
|
500
|
+
return error;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
else if (isObject(validate)) {
|
|
505
|
+
let validationResult = {};
|
|
506
|
+
for (const key in validate) {
|
|
507
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
511
|
+
if (validateError) {
|
|
512
|
+
validationResult = {
|
|
513
|
+
...validateError,
|
|
514
|
+
...appendErrorsCurry(key, validateError.message),
|
|
515
|
+
};
|
|
516
|
+
setCustomValidity(validateError.message);
|
|
517
|
+
if (validateAllFieldCriteria) {
|
|
518
|
+
error[name] = validationResult;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!isEmptyObject(validationResult)) {
|
|
523
|
+
error[name] = {
|
|
524
|
+
ref: inputRef,
|
|
525
|
+
...validationResult,
|
|
526
|
+
};
|
|
527
|
+
if (!validateAllFieldCriteria) {
|
|
528
|
+
return error;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
setCustomValidity(true);
|
|
534
|
+
return error;
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
function baseGet(object, updatePath) {
|
|
538
|
+
const length = updatePath.slice(0, -1).length;
|
|
539
|
+
let index = 0;
|
|
540
|
+
while (index < length) {
|
|
541
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
542
|
+
}
|
|
543
|
+
return object;
|
|
544
|
+
}
|
|
545
|
+
function isEmptyArray(obj) {
|
|
546
|
+
for (const key in obj) {
|
|
547
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
function unset(object, path) {
|
|
554
|
+
const paths = Array.isArray(path)
|
|
555
|
+
? path
|
|
556
|
+
: isKey(path)
|
|
557
|
+
? [path]
|
|
558
|
+
: stringToPath(path);
|
|
559
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
560
|
+
const index = paths.length - 1;
|
|
561
|
+
const key = paths[index];
|
|
562
|
+
if (childObject) {
|
|
563
|
+
delete childObject[key];
|
|
564
|
+
}
|
|
565
|
+
if (index !== 0 &&
|
|
566
|
+
((isObject(childObject) && isEmptyObject(childObject)) ||
|
|
567
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
|
568
|
+
unset(object, paths.slice(0, -1));
|
|
569
|
+
}
|
|
570
|
+
return object;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function createSubject() {
|
|
574
|
+
let _observers = [];
|
|
575
|
+
const next = (value) => {
|
|
576
|
+
for (const observer of _observers) {
|
|
577
|
+
observer.next && observer.next(value);
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
const subscribe = (observer) => {
|
|
581
|
+
_observers.push(observer);
|
|
582
|
+
return {
|
|
583
|
+
unsubscribe: () => {
|
|
584
|
+
_observers = _observers.filter((o) => o !== observer);
|
|
585
|
+
},
|
|
586
|
+
};
|
|
587
|
+
};
|
|
588
|
+
const unsubscribe = () => {
|
|
589
|
+
_observers = [];
|
|
590
|
+
};
|
|
591
|
+
return {
|
|
592
|
+
get observers() {
|
|
593
|
+
return _observers;
|
|
594
|
+
},
|
|
595
|
+
next,
|
|
596
|
+
subscribe,
|
|
597
|
+
unsubscribe,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
602
|
+
|
|
603
|
+
function deepEqual(object1, object2) {
|
|
604
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
605
|
+
return object1 === object2;
|
|
606
|
+
}
|
|
607
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
608
|
+
return object1.getTime() === object2.getTime();
|
|
609
|
+
}
|
|
610
|
+
const keys1 = Object.keys(object1);
|
|
611
|
+
const keys2 = Object.keys(object2);
|
|
612
|
+
if (keys1.length !== keys2.length) {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
for (const key of keys1) {
|
|
616
|
+
const val1 = object1[key];
|
|
617
|
+
if (!keys2.includes(key)) {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
if (key !== 'ref') {
|
|
621
|
+
const val2 = object2[key];
|
|
622
|
+
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
623
|
+
(isObject(val1) && isObject(val2)) ||
|
|
624
|
+
(Array.isArray(val1) && Array.isArray(val2))
|
|
625
|
+
? !deepEqual(val1, val2)
|
|
626
|
+
: val1 !== val2) {
|
|
627
|
+
return false;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
|
635
|
+
|
|
636
|
+
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
637
|
+
|
|
638
|
+
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
|
639
|
+
|
|
640
|
+
var objectHasFunction = (data) => {
|
|
641
|
+
for (const key in data) {
|
|
642
|
+
if (isFunction(data[key])) {
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return false;
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
function markFieldsDirty(data, fields = {}) {
|
|
650
|
+
const isParentNodeArray = Array.isArray(data);
|
|
651
|
+
if (isObject(data) || isParentNodeArray) {
|
|
652
|
+
for (const key in data) {
|
|
653
|
+
if (Array.isArray(data[key]) ||
|
|
654
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
|
655
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
656
|
+
markFieldsDirty(data[key], fields[key]);
|
|
657
|
+
}
|
|
658
|
+
else if (!isNullOrUndefined(data[key])) {
|
|
659
|
+
fields[key] = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return fields;
|
|
664
|
+
}
|
|
665
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
|
666
|
+
const isParentNodeArray = Array.isArray(data);
|
|
667
|
+
if (isObject(data) || isParentNodeArray) {
|
|
668
|
+
for (const key in data) {
|
|
669
|
+
if (Array.isArray(data[key]) ||
|
|
670
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
|
671
|
+
if (isUndefined(formValues) ||
|
|
672
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
|
673
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
|
674
|
+
? markFieldsDirty(data[key], [])
|
|
675
|
+
: { ...markFieldsDirty(data[key]) };
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return dirtyFieldsFromValues;
|
|
687
|
+
}
|
|
688
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
689
|
+
|
|
690
|
+
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
|
691
|
+
? value
|
|
692
|
+
: valueAsNumber
|
|
693
|
+
? value === ''
|
|
694
|
+
? NaN
|
|
695
|
+
: value
|
|
696
|
+
? +value
|
|
697
|
+
: value
|
|
698
|
+
: valueAsDate && isString(value)
|
|
699
|
+
? new Date(value)
|
|
700
|
+
: setValueAs
|
|
701
|
+
? setValueAs(value)
|
|
702
|
+
: value;
|
|
703
|
+
|
|
704
|
+
function getFieldValue(_f) {
|
|
705
|
+
const ref = _f.ref;
|
|
706
|
+
if (_f.refs ? _f.refs.every((ref) => ref.disabled) : ref.disabled) {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (isFileInput(ref)) {
|
|
710
|
+
return ref.files;
|
|
711
|
+
}
|
|
712
|
+
if (isRadioInput(ref)) {
|
|
713
|
+
return getRadioValue(_f.refs).value;
|
|
714
|
+
}
|
|
715
|
+
if (isMultipleSelect(ref)) {
|
|
716
|
+
return [...ref.selectedOptions].map(({ value }) => value);
|
|
717
|
+
}
|
|
718
|
+
if (isCheckBoxInput(ref)) {
|
|
719
|
+
return getCheckboxValue(_f.refs).value;
|
|
720
|
+
}
|
|
721
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
|
725
|
+
const fields = {};
|
|
726
|
+
for (const name of fieldsNames) {
|
|
727
|
+
const field = get(_fields, name);
|
|
728
|
+
field && set(fields, name, field._f);
|
|
729
|
+
}
|
|
730
|
+
return {
|
|
731
|
+
criteriaMode,
|
|
732
|
+
names: [...fieldsNames],
|
|
733
|
+
fields,
|
|
734
|
+
shouldUseNativeValidation,
|
|
735
|
+
};
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
var getRuleValue = (rule) => isUndefined(rule)
|
|
739
|
+
? rule
|
|
740
|
+
: isRegex(rule)
|
|
741
|
+
? rule.source
|
|
742
|
+
: isObject(rule)
|
|
743
|
+
? isRegex(rule.value)
|
|
744
|
+
? rule.value.source
|
|
745
|
+
: rule.value
|
|
746
|
+
: rule;
|
|
747
|
+
|
|
748
|
+
var hasValidation = (options) => options.mount &&
|
|
749
|
+
(options.required ||
|
|
750
|
+
options.min ||
|
|
751
|
+
options.max ||
|
|
752
|
+
options.maxLength ||
|
|
753
|
+
options.minLength ||
|
|
754
|
+
options.pattern ||
|
|
755
|
+
options.validate);
|
|
756
|
+
|
|
757
|
+
function schemaErrorLookup(errors, _fields, name) {
|
|
758
|
+
const error = get(errors, name);
|
|
759
|
+
if (error || isKey(name)) {
|
|
760
|
+
return {
|
|
761
|
+
error,
|
|
762
|
+
name,
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
const names = name.split('.');
|
|
766
|
+
while (names.length) {
|
|
767
|
+
const fieldName = names.join('.');
|
|
768
|
+
const field = get(_fields, fieldName);
|
|
769
|
+
const foundError = get(errors, fieldName);
|
|
770
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
771
|
+
return { name };
|
|
772
|
+
}
|
|
773
|
+
if (foundError && foundError.type) {
|
|
774
|
+
return {
|
|
775
|
+
name: fieldName,
|
|
776
|
+
error: foundError,
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
names.pop();
|
|
780
|
+
}
|
|
781
|
+
return {
|
|
782
|
+
name,
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
|
787
|
+
if (mode.isOnAll) {
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
else if (!isSubmitted && mode.isOnTouch) {
|
|
791
|
+
return !(isTouched || isBlurEvent);
|
|
792
|
+
}
|
|
793
|
+
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
794
|
+
return !isBlurEvent;
|
|
795
|
+
}
|
|
796
|
+
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
797
|
+
return isBlurEvent;
|
|
798
|
+
}
|
|
799
|
+
return true;
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
|
803
|
+
|
|
804
|
+
const defaultOptions = {
|
|
805
|
+
mode: VALIDATION_MODE.onSubmit,
|
|
806
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
|
807
|
+
shouldFocusError: true,
|
|
808
|
+
};
|
|
809
|
+
function createFormControl(props = {}, flushRootRender) {
|
|
810
|
+
let _options = {
|
|
811
|
+
...defaultOptions,
|
|
812
|
+
...props,
|
|
813
|
+
};
|
|
814
|
+
let _formState = {
|
|
815
|
+
submitCount: 0,
|
|
816
|
+
isDirty: false,
|
|
817
|
+
isLoading: isFunction(_options.defaultValues),
|
|
818
|
+
isValidating: false,
|
|
819
|
+
isSubmitted: false,
|
|
820
|
+
isSubmitting: false,
|
|
821
|
+
isSubmitSuccessful: false,
|
|
822
|
+
isValid: false,
|
|
823
|
+
touchedFields: {},
|
|
824
|
+
dirtyFields: {},
|
|
825
|
+
errors: {},
|
|
826
|
+
};
|
|
827
|
+
let _fields = {};
|
|
828
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
|
829
|
+
? cloneObject(_options.defaultValues || _options.values) || {}
|
|
830
|
+
: {};
|
|
831
|
+
let _formValues = _options.shouldUnregister
|
|
832
|
+
? {}
|
|
833
|
+
: cloneObject(_defaultValues);
|
|
834
|
+
let _state = {
|
|
835
|
+
action: false,
|
|
836
|
+
mount: false,
|
|
837
|
+
watch: false,
|
|
838
|
+
};
|
|
839
|
+
let _names = {
|
|
840
|
+
mount: new Set(),
|
|
841
|
+
unMount: new Set(),
|
|
842
|
+
array: new Set(),
|
|
843
|
+
watch: new Set(),
|
|
844
|
+
};
|
|
845
|
+
let delayErrorCallback;
|
|
846
|
+
let timer = 0;
|
|
847
|
+
const _proxyFormState = {
|
|
848
|
+
isDirty: false,
|
|
849
|
+
dirtyFields: false,
|
|
850
|
+
touchedFields: false,
|
|
851
|
+
isValidating: false,
|
|
852
|
+
isValid: false,
|
|
853
|
+
errors: false,
|
|
854
|
+
};
|
|
855
|
+
const _subjects = {
|
|
856
|
+
values: createSubject(),
|
|
857
|
+
array: createSubject(),
|
|
858
|
+
state: createSubject(),
|
|
859
|
+
};
|
|
860
|
+
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
|
861
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
862
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
863
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
864
|
+
const debounce = (callback) => (wait) => {
|
|
865
|
+
clearTimeout(timer);
|
|
866
|
+
timer = setTimeout(callback, wait);
|
|
867
|
+
};
|
|
868
|
+
const _updateValid = async (shouldUpdateValid) => {
|
|
869
|
+
if (_proxyFormState.isValid || shouldUpdateValid) {
|
|
870
|
+
const isValid = _options.resolver
|
|
871
|
+
? isEmptyObject((await _executeSchema()).errors)
|
|
872
|
+
: await executeBuiltInValidation(_fields, true);
|
|
873
|
+
if (isValid !== _formState.isValid) {
|
|
874
|
+
_subjects.state.next({
|
|
875
|
+
isValid,
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
const _updateIsValidating = (value) => _proxyFormState.isValidating &&
|
|
881
|
+
_subjects.state.next({
|
|
882
|
+
isValidating: value,
|
|
883
|
+
});
|
|
884
|
+
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
885
|
+
if (args && method) {
|
|
886
|
+
_state.action = true;
|
|
887
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
888
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
889
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
|
890
|
+
}
|
|
891
|
+
if (shouldUpdateFieldsAndState &&
|
|
892
|
+
Array.isArray(get(_formState.errors, name))) {
|
|
893
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
894
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
|
895
|
+
unsetEmptyArray(_formState.errors, name);
|
|
896
|
+
}
|
|
897
|
+
if (_proxyFormState.touchedFields &&
|
|
898
|
+
shouldUpdateFieldsAndState &&
|
|
899
|
+
Array.isArray(get(_formState.touchedFields, name))) {
|
|
900
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
901
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
902
|
+
}
|
|
903
|
+
if (_proxyFormState.dirtyFields) {
|
|
904
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
905
|
+
}
|
|
906
|
+
_subjects.state.next({
|
|
907
|
+
name,
|
|
908
|
+
isDirty: _getDirty(name, values),
|
|
909
|
+
dirtyFields: _formState.dirtyFields,
|
|
910
|
+
errors: _formState.errors,
|
|
911
|
+
isValid: _formState.isValid,
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
set(_formValues, name, values);
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
const updateErrors = (name, error) => {
|
|
919
|
+
set(_formState.errors, name, error);
|
|
920
|
+
_subjects.state.next({
|
|
921
|
+
errors: _formState.errors,
|
|
922
|
+
});
|
|
923
|
+
};
|
|
924
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
|
925
|
+
const field = get(_fields, name);
|
|
926
|
+
if (field) {
|
|
927
|
+
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
|
928
|
+
isUndefined(defaultValue) ||
|
|
929
|
+
(ref && ref.defaultChecked) ||
|
|
930
|
+
shouldSkipSetValueAs
|
|
931
|
+
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
|
932
|
+
: setFieldValue(name, defaultValue);
|
|
933
|
+
_state.mount && _updateValid();
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
|
937
|
+
let shouldUpdateField = false;
|
|
938
|
+
let isPreviousDirty = false;
|
|
939
|
+
const output = {
|
|
940
|
+
name,
|
|
941
|
+
};
|
|
942
|
+
if (!isBlurEvent || shouldDirty) {
|
|
943
|
+
if (_proxyFormState.isDirty) {
|
|
944
|
+
isPreviousDirty = _formState.isDirty;
|
|
945
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
946
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
947
|
+
}
|
|
948
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
|
949
|
+
isPreviousDirty = get(_formState.dirtyFields, name);
|
|
950
|
+
isCurrentFieldPristine
|
|
951
|
+
? unset(_formState.dirtyFields, name)
|
|
952
|
+
: set(_formState.dirtyFields, name, true);
|
|
953
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
954
|
+
shouldUpdateField =
|
|
955
|
+
shouldUpdateField ||
|
|
956
|
+
(_proxyFormState.dirtyFields &&
|
|
957
|
+
isPreviousDirty !== !isCurrentFieldPristine);
|
|
958
|
+
}
|
|
959
|
+
if (isBlurEvent) {
|
|
960
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
961
|
+
if (!isPreviousFieldTouched) {
|
|
962
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
963
|
+
output.touchedFields = _formState.touchedFields;
|
|
964
|
+
shouldUpdateField =
|
|
965
|
+
shouldUpdateField ||
|
|
966
|
+
(_proxyFormState.touchedFields &&
|
|
967
|
+
isPreviousFieldTouched !== isBlurEvent);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
971
|
+
return shouldUpdateField ? output : {};
|
|
972
|
+
};
|
|
973
|
+
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
|
974
|
+
const previousFieldError = get(_formState.errors, name);
|
|
975
|
+
const shouldUpdateValid = _proxyFormState.isValid &&
|
|
976
|
+
isBoolean(isValid) &&
|
|
977
|
+
_formState.isValid !== isValid;
|
|
978
|
+
if (props.delayError && error) {
|
|
979
|
+
delayErrorCallback = debounce(() => updateErrors(name, error));
|
|
980
|
+
delayErrorCallback(props.delayError);
|
|
981
|
+
}
|
|
982
|
+
else {
|
|
983
|
+
clearTimeout(timer);
|
|
984
|
+
delayErrorCallback = null;
|
|
985
|
+
error
|
|
986
|
+
? set(_formState.errors, name, error)
|
|
987
|
+
: unset(_formState.errors, name);
|
|
988
|
+
}
|
|
989
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
|
|
990
|
+
!isEmptyObject(fieldState) ||
|
|
991
|
+
shouldUpdateValid) {
|
|
992
|
+
const updatedFormState = {
|
|
993
|
+
...fieldState,
|
|
994
|
+
...(shouldUpdateValid && isBoolean(isValid) ? { isValid } : {}),
|
|
995
|
+
errors: _formState.errors,
|
|
996
|
+
name,
|
|
997
|
+
};
|
|
998
|
+
_formState = {
|
|
999
|
+
..._formState,
|
|
1000
|
+
...updatedFormState,
|
|
1001
|
+
};
|
|
1002
|
+
_subjects.state.next(updatedFormState);
|
|
1003
|
+
}
|
|
1004
|
+
_updateIsValidating(false);
|
|
1005
|
+
};
|
|
1006
|
+
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
1007
|
+
const executeSchemaAndUpdateState = async (names) => {
|
|
1008
|
+
const { errors } = await _executeSchema(names);
|
|
1009
|
+
if (names) {
|
|
1010
|
+
for (const name of names) {
|
|
1011
|
+
const error = get(errors, name);
|
|
1012
|
+
error
|
|
1013
|
+
? set(_formState.errors, name, error)
|
|
1014
|
+
: unset(_formState.errors, name);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
_formState.errors = errors;
|
|
1019
|
+
}
|
|
1020
|
+
return errors;
|
|
1021
|
+
};
|
|
1022
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
|
1023
|
+
valid: true,
|
|
1024
|
+
}) => {
|
|
1025
|
+
for (const name in fields) {
|
|
1026
|
+
const field = fields[name];
|
|
1027
|
+
if (field) {
|
|
1028
|
+
const { _f, ...fieldValue } = field;
|
|
1029
|
+
if (_f) {
|
|
1030
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
1031
|
+
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
1032
|
+
if (fieldError[_f.name]) {
|
|
1033
|
+
context.valid = false;
|
|
1034
|
+
if (shouldOnlyCheckValid) {
|
|
1035
|
+
break;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
!shouldOnlyCheckValid &&
|
|
1039
|
+
(get(fieldError, _f.name)
|
|
1040
|
+
? isFieldArrayRoot
|
|
1041
|
+
? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
|
|
1042
|
+
: set(_formState.errors, _f.name, fieldError[_f.name])
|
|
1043
|
+
: unset(_formState.errors, _f.name));
|
|
1044
|
+
}
|
|
1045
|
+
fieldValue &&
|
|
1046
|
+
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return context.valid;
|
|
1050
|
+
};
|
|
1051
|
+
const _removeUnmounted = () => {
|
|
1052
|
+
for (const name of _names.unMount) {
|
|
1053
|
+
const field = get(_fields, name);
|
|
1054
|
+
field &&
|
|
1055
|
+
(field._f.refs
|
|
1056
|
+
? field._f.refs.every((ref) => !live(ref))
|
|
1057
|
+
: !live(field._f.ref)) &&
|
|
1058
|
+
unregister(name);
|
|
1059
|
+
}
|
|
1060
|
+
_names.unMount = new Set();
|
|
1061
|
+
};
|
|
1062
|
+
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
|
|
1063
|
+
!deepEqual(getValues(), _defaultValues));
|
|
1064
|
+
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
|
1065
|
+
...(_state.mount
|
|
1066
|
+
? _formValues
|
|
1067
|
+
: isUndefined(defaultValue)
|
|
1068
|
+
? _defaultValues
|
|
1069
|
+
: isString(names)
|
|
1070
|
+
? { [names]: defaultValue }
|
|
1071
|
+
: defaultValue),
|
|
1072
|
+
}, isGlobal, defaultValue);
|
|
1073
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1074
|
+
const setFieldValue = (name, value, options = {}) => {
|
|
1075
|
+
const field = get(_fields, name);
|
|
1076
|
+
let fieldValue = value;
|
|
1077
|
+
if (field) {
|
|
1078
|
+
const fieldReference = field._f;
|
|
1079
|
+
if (fieldReference) {
|
|
1080
|
+
!fieldReference.disabled &&
|
|
1081
|
+
set(_formValues, name, getFieldValueAs(value, fieldReference));
|
|
1082
|
+
fieldValue =
|
|
1083
|
+
isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
|
1084
|
+
? ''
|
|
1085
|
+
: value;
|
|
1086
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
|
1087
|
+
[...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
|
|
1088
|
+
}
|
|
1089
|
+
else if (fieldReference.refs) {
|
|
1090
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
|
1091
|
+
fieldReference.refs.length > 1
|
|
1092
|
+
? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) &&
|
|
1093
|
+
(checkboxRef.checked = Array.isArray(fieldValue)
|
|
1094
|
+
? !!fieldValue.find((data) => data === checkboxRef.value)
|
|
1095
|
+
: fieldValue === checkboxRef.value))
|
|
1096
|
+
: fieldReference.refs[0] &&
|
|
1097
|
+
(fieldReference.refs[0].checked = !!fieldValue);
|
|
1098
|
+
}
|
|
1099
|
+
else {
|
|
1100
|
+
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
else if (isFileInput(fieldReference.ref)) {
|
|
1104
|
+
fieldReference.ref.value = '';
|
|
1105
|
+
}
|
|
1106
|
+
else {
|
|
1107
|
+
fieldReference.ref.value = fieldValue;
|
|
1108
|
+
if (!fieldReference.ref.type) {
|
|
1109
|
+
_subjects.values.next({
|
|
1110
|
+
name,
|
|
1111
|
+
values: { ..._formValues },
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
(options.shouldDirty || options.shouldTouch) &&
|
|
1118
|
+
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
1119
|
+
options.shouldValidate && trigger(name);
|
|
1120
|
+
};
|
|
1121
|
+
const setValues = (name, value, options) => {
|
|
1122
|
+
for (const fieldKey in value) {
|
|
1123
|
+
const fieldValue = value[fieldKey];
|
|
1124
|
+
const fieldName = `${name}.${fieldKey}`;
|
|
1125
|
+
const field = get(_fields, fieldName);
|
|
1126
|
+
(_names.array.has(name) ||
|
|
1127
|
+
!isPrimitive(fieldValue) ||
|
|
1128
|
+
(field && !field._f)) &&
|
|
1129
|
+
!isDateObject(fieldValue)
|
|
1130
|
+
? setValues(fieldName, fieldValue, options)
|
|
1131
|
+
: setFieldValue(fieldName, fieldValue, options);
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
const setValue = (name, value, options = {}) => {
|
|
1135
|
+
const field = get(_fields, name);
|
|
1136
|
+
const isFieldArray = _names.array.has(name);
|
|
1137
|
+
const cloneValue = cloneObject(value);
|
|
1138
|
+
set(_formValues, name, cloneValue);
|
|
1139
|
+
if (isFieldArray) {
|
|
1140
|
+
_subjects.array.next({
|
|
1141
|
+
name,
|
|
1142
|
+
values: { ..._formValues },
|
|
1143
|
+
});
|
|
1144
|
+
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
|
|
1145
|
+
options.shouldDirty) {
|
|
1146
|
+
_subjects.state.next({
|
|
1147
|
+
name,
|
|
1148
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
1149
|
+
isDirty: _getDirty(name, cloneValue),
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
else {
|
|
1154
|
+
field && !field._f && !isNullOrUndefined(cloneValue)
|
|
1155
|
+
? setValues(name, cloneValue, options)
|
|
1156
|
+
: setFieldValue(name, cloneValue, options);
|
|
1157
|
+
}
|
|
1158
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1159
|
+
_subjects.values.next({
|
|
1160
|
+
name,
|
|
1161
|
+
values: { ..._formValues },
|
|
1162
|
+
});
|
|
1163
|
+
!_state.mount && flushRootRender();
|
|
1164
|
+
};
|
|
1165
|
+
const onChange = async (event) => {
|
|
1166
|
+
const target = event.target;
|
|
1167
|
+
let name = target.name;
|
|
1168
|
+
let isFieldValueUpdated = true;
|
|
1169
|
+
const field = get(_fields, name);
|
|
1170
|
+
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
1171
|
+
if (field) {
|
|
1172
|
+
let error;
|
|
1173
|
+
let isValid;
|
|
1174
|
+
const fieldValue = getCurrentFieldValue();
|
|
1175
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
1176
|
+
const shouldSkipValidation = (!hasValidation(field._f) &&
|
|
1177
|
+
!_options.resolver &&
|
|
1178
|
+
!get(_formState.errors, name) &&
|
|
1179
|
+
!field._f.deps) ||
|
|
1180
|
+
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
1181
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
|
1182
|
+
set(_formValues, name, fieldValue);
|
|
1183
|
+
if (isBlurEvent) {
|
|
1184
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
1185
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
1186
|
+
}
|
|
1187
|
+
else if (field._f.onChange) {
|
|
1188
|
+
field._f.onChange(event);
|
|
1189
|
+
}
|
|
1190
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
|
|
1191
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
1192
|
+
!isBlurEvent &&
|
|
1193
|
+
_subjects.values.next({
|
|
1194
|
+
name,
|
|
1195
|
+
type: event.type,
|
|
1196
|
+
values: { ..._formValues },
|
|
1197
|
+
});
|
|
1198
|
+
if (shouldSkipValidation) {
|
|
1199
|
+
_proxyFormState.isValid && _updateValid();
|
|
1200
|
+
return (shouldRender &&
|
|
1201
|
+
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
|
1202
|
+
}
|
|
1203
|
+
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
|
1204
|
+
_updateIsValidating(true);
|
|
1205
|
+
if (_options.resolver) {
|
|
1206
|
+
const { errors } = await _executeSchema([name]);
|
|
1207
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
1208
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
1209
|
+
error = errorLookupResult.error;
|
|
1210
|
+
name = errorLookupResult.name;
|
|
1211
|
+
isValid = isEmptyObject(errors);
|
|
1212
|
+
}
|
|
1213
|
+
else {
|
|
1214
|
+
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1215
|
+
isFieldValueUpdated =
|
|
1216
|
+
isNaN(fieldValue) ||
|
|
1217
|
+
fieldValue === get(_formValues, name, fieldValue);
|
|
1218
|
+
if (isFieldValueUpdated) {
|
|
1219
|
+
if (error) {
|
|
1220
|
+
isValid = false;
|
|
1221
|
+
}
|
|
1222
|
+
else if (_proxyFormState.isValid) {
|
|
1223
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
if (isFieldValueUpdated) {
|
|
1228
|
+
field._f.deps &&
|
|
1229
|
+
trigger(field._f.deps);
|
|
1230
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
const trigger = async (name, options = {}) => {
|
|
1235
|
+
let isValid;
|
|
1236
|
+
let validationResult;
|
|
1237
|
+
const fieldNames = convertToArrayPayload(name);
|
|
1238
|
+
_updateIsValidating(true);
|
|
1239
|
+
if (_options.resolver) {
|
|
1240
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
|
1241
|
+
isValid = isEmptyObject(errors);
|
|
1242
|
+
validationResult = name
|
|
1243
|
+
? !fieldNames.some((name) => get(errors, name))
|
|
1244
|
+
: isValid;
|
|
1245
|
+
}
|
|
1246
|
+
else if (name) {
|
|
1247
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
|
1248
|
+
const field = get(_fields, fieldName);
|
|
1249
|
+
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
|
1250
|
+
}))).every(Boolean);
|
|
1251
|
+
!(!validationResult && !_formState.isValid) && _updateValid();
|
|
1252
|
+
}
|
|
1253
|
+
else {
|
|
1254
|
+
validationResult = isValid = await executeBuiltInValidation(_fields);
|
|
1255
|
+
}
|
|
1256
|
+
_subjects.state.next({
|
|
1257
|
+
...(!isString(name) ||
|
|
1258
|
+
(_proxyFormState.isValid && isValid !== _formState.isValid)
|
|
1259
|
+
? {}
|
|
1260
|
+
: { name }),
|
|
1261
|
+
...(_options.resolver || !name ? { isValid } : {}),
|
|
1262
|
+
errors: _formState.errors,
|
|
1263
|
+
isValidating: false,
|
|
1264
|
+
});
|
|
1265
|
+
options.shouldFocus &&
|
|
1266
|
+
!validationResult &&
|
|
1267
|
+
focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
|
|
1268
|
+
return validationResult;
|
|
1269
|
+
};
|
|
1270
|
+
const getValues = (fieldNames) => {
|
|
1271
|
+
const values = {
|
|
1272
|
+
..._defaultValues,
|
|
1273
|
+
...(_state.mount ? _formValues : {}),
|
|
1274
|
+
};
|
|
1275
|
+
return isUndefined(fieldNames)
|
|
1276
|
+
? values
|
|
1277
|
+
: isString(fieldNames)
|
|
1278
|
+
? get(values, fieldNames)
|
|
1279
|
+
: fieldNames.map((name) => get(values, name));
|
|
1280
|
+
};
|
|
1281
|
+
const getFieldState = (name, formState) => ({
|
|
1282
|
+
invalid: !!get((formState || _formState).errors, name),
|
|
1283
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
1284
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
1285
|
+
error: get((formState || _formState).errors, name),
|
|
1286
|
+
});
|
|
1287
|
+
const clearErrors = (name) => {
|
|
1288
|
+
name &&
|
|
1289
|
+
convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
|
1290
|
+
_subjects.state.next({
|
|
1291
|
+
errors: name ? _formState.errors : {},
|
|
1292
|
+
});
|
|
1293
|
+
};
|
|
1294
|
+
const setError = (name, error, options) => {
|
|
1295
|
+
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
|
1296
|
+
set(_formState.errors, name, {
|
|
1297
|
+
...error,
|
|
1298
|
+
ref,
|
|
1299
|
+
});
|
|
1300
|
+
_subjects.state.next({
|
|
1301
|
+
name,
|
|
1302
|
+
errors: _formState.errors,
|
|
1303
|
+
isValid: false,
|
|
1304
|
+
});
|
|
1305
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1306
|
+
};
|
|
1307
|
+
const watch = (name, defaultValue) => isFunction(name)
|
|
1308
|
+
? _subjects.values.subscribe({
|
|
1309
|
+
next: (payload) => name(_getWatch(undefined, defaultValue), payload),
|
|
1310
|
+
})
|
|
1311
|
+
: _getWatch(name, defaultValue, true);
|
|
1312
|
+
const unregister = (name, options = {}) => {
|
|
1313
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
|
1314
|
+
_names.mount.delete(fieldName);
|
|
1315
|
+
_names.array.delete(fieldName);
|
|
1316
|
+
if (!options.keepValue) {
|
|
1317
|
+
unset(_fields, fieldName);
|
|
1318
|
+
unset(_formValues, fieldName);
|
|
1319
|
+
}
|
|
1320
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
|
1321
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
1322
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
1323
|
+
!_options.shouldUnregister &&
|
|
1324
|
+
!options.keepDefaultValue &&
|
|
1325
|
+
unset(_defaultValues, fieldName);
|
|
1326
|
+
}
|
|
1327
|
+
_subjects.values.next({
|
|
1328
|
+
values: { ..._formValues },
|
|
1329
|
+
});
|
|
1330
|
+
_subjects.state.next({
|
|
1331
|
+
..._formState,
|
|
1332
|
+
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
|
1333
|
+
});
|
|
1334
|
+
!options.keepIsValid && _updateValid();
|
|
1335
|
+
};
|
|
1336
|
+
const _updateDisabledField = ({ disabled, name, field, fields, }) => {
|
|
1337
|
+
if (isBoolean(disabled)) {
|
|
1338
|
+
const value = disabled
|
|
1339
|
+
? undefined
|
|
1340
|
+
: get(_formValues, name, getFieldValue(field ? field._f : get(fields, name)._f));
|
|
1341
|
+
set(_formValues, name, value);
|
|
1342
|
+
updateTouchAndDirty(name, value, false, false, true);
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
const register = (name, options = {}) => {
|
|
1346
|
+
let field = get(_fields, name);
|
|
1347
|
+
const disabledIsDefined = isBoolean(options.disabled);
|
|
1348
|
+
set(_fields, name, {
|
|
1349
|
+
...(field || {}),
|
|
1350
|
+
_f: {
|
|
1351
|
+
...(field && field._f ? field._f : { ref: { name } }),
|
|
1352
|
+
name,
|
|
1353
|
+
mount: true,
|
|
1354
|
+
...options,
|
|
1355
|
+
},
|
|
1356
|
+
});
|
|
1357
|
+
_names.mount.add(name);
|
|
1358
|
+
if (field) {
|
|
1359
|
+
_updateDisabledField({
|
|
1360
|
+
field,
|
|
1361
|
+
disabled: options.disabled,
|
|
1362
|
+
name,
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
else {
|
|
1366
|
+
updateValidAndValue(name, true, options.value);
|
|
1367
|
+
}
|
|
1368
|
+
return {
|
|
1369
|
+
...(disabledIsDefined ? { disabled: options.disabled } : {}),
|
|
1370
|
+
...(_options.progressive
|
|
1371
|
+
? {
|
|
1372
|
+
required: !!options.required,
|
|
1373
|
+
min: getRuleValue(options.min),
|
|
1374
|
+
max: getRuleValue(options.max),
|
|
1375
|
+
minLength: getRuleValue(options.minLength),
|
|
1376
|
+
maxLength: getRuleValue(options.maxLength),
|
|
1377
|
+
pattern: getRuleValue(options.pattern),
|
|
1378
|
+
}
|
|
1379
|
+
: {}),
|
|
1380
|
+
name,
|
|
1381
|
+
onChange,
|
|
1382
|
+
onBlur: onChange,
|
|
1383
|
+
ref: (ref) => {
|
|
1384
|
+
if (ref) {
|
|
1385
|
+
register(name, options);
|
|
1386
|
+
field = get(_fields, name);
|
|
1387
|
+
const fieldRef = isUndefined(ref.value)
|
|
1388
|
+
? ref.querySelectorAll
|
|
1389
|
+
? ref.querySelectorAll('input,select,textarea')[0] || ref
|
|
1390
|
+
: ref
|
|
1391
|
+
: ref;
|
|
1392
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
1393
|
+
const refs = field._f.refs || [];
|
|
1394
|
+
if (radioOrCheckbox
|
|
1395
|
+
? refs.find((option) => option === fieldRef)
|
|
1396
|
+
: fieldRef === field._f.ref) {
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1399
|
+
set(_fields, name, {
|
|
1400
|
+
_f: {
|
|
1401
|
+
...field._f,
|
|
1402
|
+
...(radioOrCheckbox
|
|
1403
|
+
? {
|
|
1404
|
+
refs: [
|
|
1405
|
+
...refs.filter(live),
|
|
1406
|
+
fieldRef,
|
|
1407
|
+
...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
|
|
1408
|
+
],
|
|
1409
|
+
ref: { type: fieldRef.type, name },
|
|
1410
|
+
}
|
|
1411
|
+
: { ref: fieldRef }),
|
|
1412
|
+
},
|
|
1413
|
+
});
|
|
1414
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
|
1415
|
+
}
|
|
1416
|
+
else {
|
|
1417
|
+
field = get(_fields, name, {});
|
|
1418
|
+
if (field._f) {
|
|
1419
|
+
field._f.mount = false;
|
|
1420
|
+
}
|
|
1421
|
+
(_options.shouldUnregister || options.shouldUnregister) &&
|
|
1422
|
+
!(isNameInFieldArray(_names.array, name) && _state.action) &&
|
|
1423
|
+
_names.unMount.add(name);
|
|
1424
|
+
}
|
|
1425
|
+
},
|
|
1426
|
+
};
|
|
1427
|
+
};
|
|
1428
|
+
const _focusError = () => _options.shouldFocusError &&
|
|
1429
|
+
focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
|
|
1430
|
+
const handleSubmit = (onValid, onInvalid) => async (e) => {
|
|
1431
|
+
if (e) {
|
|
1432
|
+
e.preventDefault && e.preventDefault();
|
|
1433
|
+
e.persist && e.persist();
|
|
1434
|
+
}
|
|
1435
|
+
let fieldValues = cloneObject(_formValues);
|
|
1436
|
+
_subjects.state.next({
|
|
1437
|
+
isSubmitting: true,
|
|
1438
|
+
});
|
|
1439
|
+
if (_options.resolver) {
|
|
1440
|
+
const { errors, values } = await _executeSchema();
|
|
1441
|
+
_formState.errors = errors;
|
|
1442
|
+
fieldValues = values;
|
|
1443
|
+
}
|
|
1444
|
+
else {
|
|
1445
|
+
await executeBuiltInValidation(_fields);
|
|
1446
|
+
}
|
|
1447
|
+
unset(_formState.errors, 'root');
|
|
1448
|
+
if (isEmptyObject(_formState.errors)) {
|
|
1449
|
+
_subjects.state.next({
|
|
1450
|
+
errors: {},
|
|
1451
|
+
});
|
|
1452
|
+
await onValid(fieldValues, e);
|
|
1453
|
+
}
|
|
1454
|
+
else {
|
|
1455
|
+
if (onInvalid) {
|
|
1456
|
+
await onInvalid({ ..._formState.errors }, e);
|
|
1457
|
+
}
|
|
1458
|
+
_focusError();
|
|
1459
|
+
setTimeout(_focusError);
|
|
1460
|
+
}
|
|
1461
|
+
_subjects.state.next({
|
|
1462
|
+
isSubmitted: true,
|
|
1463
|
+
isSubmitting: false,
|
|
1464
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors),
|
|
1465
|
+
submitCount: _formState.submitCount + 1,
|
|
1466
|
+
errors: _formState.errors,
|
|
1467
|
+
});
|
|
1468
|
+
};
|
|
1469
|
+
const resetField = (name, options = {}) => {
|
|
1470
|
+
if (get(_fields, name)) {
|
|
1471
|
+
if (isUndefined(options.defaultValue)) {
|
|
1472
|
+
setValue(name, get(_defaultValues, name));
|
|
1473
|
+
}
|
|
1474
|
+
else {
|
|
1475
|
+
setValue(name, options.defaultValue);
|
|
1476
|
+
set(_defaultValues, name, options.defaultValue);
|
|
1477
|
+
}
|
|
1478
|
+
if (!options.keepTouched) {
|
|
1479
|
+
unset(_formState.touchedFields, name);
|
|
1480
|
+
}
|
|
1481
|
+
if (!options.keepDirty) {
|
|
1482
|
+
unset(_formState.dirtyFields, name);
|
|
1483
|
+
_formState.isDirty = options.defaultValue
|
|
1484
|
+
? _getDirty(name, get(_defaultValues, name))
|
|
1485
|
+
: _getDirty();
|
|
1486
|
+
}
|
|
1487
|
+
if (!options.keepError) {
|
|
1488
|
+
unset(_formState.errors, name);
|
|
1489
|
+
_proxyFormState.isValid && _updateValid();
|
|
1490
|
+
}
|
|
1491
|
+
_subjects.state.next({ ..._formState });
|
|
1492
|
+
}
|
|
1493
|
+
};
|
|
1494
|
+
const _reset = (formValues, keepStateOptions = {}) => {
|
|
1495
|
+
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
1496
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
|
1497
|
+
const values = formValues && !isEmptyObject(formValues)
|
|
1498
|
+
? cloneUpdatedValues
|
|
1499
|
+
: _defaultValues;
|
|
1500
|
+
if (!keepStateOptions.keepDefaultValues) {
|
|
1501
|
+
_defaultValues = updatedValues;
|
|
1502
|
+
}
|
|
1503
|
+
if (!keepStateOptions.keepValues) {
|
|
1504
|
+
if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
|
|
1505
|
+
for (const fieldName of _names.mount) {
|
|
1506
|
+
get(_formState.dirtyFields, fieldName)
|
|
1507
|
+
? set(values, fieldName, get(_formValues, fieldName))
|
|
1508
|
+
: setValue(fieldName, get(values, fieldName));
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
else {
|
|
1512
|
+
if (isWeb && isUndefined(formValues)) {
|
|
1513
|
+
for (const name of _names.mount) {
|
|
1514
|
+
const field = get(_fields, name);
|
|
1515
|
+
if (field && field._f) {
|
|
1516
|
+
const fieldReference = Array.isArray(field._f.refs)
|
|
1517
|
+
? field._f.refs[0]
|
|
1518
|
+
: field._f.ref;
|
|
1519
|
+
if (isHTMLElement(fieldReference)) {
|
|
1520
|
+
const form = fieldReference.closest('form');
|
|
1521
|
+
if (form) {
|
|
1522
|
+
form.reset();
|
|
1523
|
+
break;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
_fields = {};
|
|
1530
|
+
}
|
|
1531
|
+
_formValues = props.shouldUnregister
|
|
1532
|
+
? keepStateOptions.keepDefaultValues
|
|
1533
|
+
? cloneObject(_defaultValues)
|
|
1534
|
+
: {}
|
|
1535
|
+
: cloneObject(values);
|
|
1536
|
+
_subjects.array.next({
|
|
1537
|
+
values: { ...values },
|
|
1538
|
+
});
|
|
1539
|
+
_subjects.values.next({
|
|
1540
|
+
values: { ...values },
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
_names = {
|
|
1544
|
+
mount: new Set(),
|
|
1545
|
+
unMount: new Set(),
|
|
1546
|
+
array: new Set(),
|
|
1547
|
+
watch: new Set(),
|
|
1548
|
+
watchAll: false,
|
|
1549
|
+
focus: '',
|
|
1550
|
+
};
|
|
1551
|
+
!_state.mount && flushRootRender();
|
|
1552
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
|
|
1553
|
+
_state.watch = !!props.shouldUnregister;
|
|
1554
|
+
_subjects.state.next({
|
|
1555
|
+
submitCount: keepStateOptions.keepSubmitCount
|
|
1556
|
+
? _formState.submitCount
|
|
1557
|
+
: 0,
|
|
1558
|
+
isDirty: keepStateOptions.keepDirty
|
|
1559
|
+
? _formState.isDirty
|
|
1560
|
+
: !!(keepStateOptions.keepDefaultValues &&
|
|
1561
|
+
!deepEqual(formValues, _defaultValues)),
|
|
1562
|
+
isSubmitted: keepStateOptions.keepIsSubmitted
|
|
1563
|
+
? _formState.isSubmitted
|
|
1564
|
+
: false,
|
|
1565
|
+
dirtyFields: keepStateOptions.keepDirtyValues
|
|
1566
|
+
? _formState.dirtyFields
|
|
1567
|
+
: keepStateOptions.keepDefaultValues && formValues
|
|
1568
|
+
? getDirtyFields(_defaultValues, formValues)
|
|
1569
|
+
: {},
|
|
1570
|
+
touchedFields: keepStateOptions.keepTouched
|
|
1571
|
+
? _formState.touchedFields
|
|
1572
|
+
: {},
|
|
1573
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
1574
|
+
isSubmitting: false,
|
|
1575
|
+
isSubmitSuccessful: false,
|
|
1576
|
+
});
|
|
1577
|
+
};
|
|
1578
|
+
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
|
|
1579
|
+
? formValues(_formValues)
|
|
1580
|
+
: formValues, keepStateOptions);
|
|
1581
|
+
const setFocus = (name, options = {}) => {
|
|
1582
|
+
const field = get(_fields, name);
|
|
1583
|
+
const fieldReference = field && field._f;
|
|
1584
|
+
if (fieldReference) {
|
|
1585
|
+
const fieldRef = fieldReference.refs
|
|
1586
|
+
? fieldReference.refs[0]
|
|
1587
|
+
: fieldReference.ref;
|
|
1588
|
+
if (fieldRef.focus) {
|
|
1589
|
+
fieldRef.focus();
|
|
1590
|
+
options.shouldSelect && fieldRef.select();
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
const _updateFormState = (updatedFormState) => {
|
|
1595
|
+
_formState = {
|
|
1596
|
+
..._formState,
|
|
1597
|
+
...updatedFormState,
|
|
1598
|
+
};
|
|
1599
|
+
};
|
|
1600
|
+
const _resetDefaultValues = () => isFunction(_options.defaultValues) &&
|
|
1601
|
+
_options.defaultValues().then((values) => {
|
|
1602
|
+
reset(values, _options.resetOptions);
|
|
1603
|
+
_subjects.state.next({
|
|
1604
|
+
isLoading: false,
|
|
1605
|
+
});
|
|
1606
|
+
});
|
|
1607
|
+
return {
|
|
1608
|
+
control: {
|
|
1609
|
+
register,
|
|
1610
|
+
unregister,
|
|
1611
|
+
getFieldState,
|
|
1612
|
+
handleSubmit,
|
|
1613
|
+
setError,
|
|
1614
|
+
_executeSchema,
|
|
1615
|
+
_getWatch,
|
|
1616
|
+
_getDirty,
|
|
1617
|
+
_updateValid,
|
|
1618
|
+
_removeUnmounted,
|
|
1619
|
+
_updateFieldArray,
|
|
1620
|
+
_updateDisabledField,
|
|
1621
|
+
_getFieldArray,
|
|
1622
|
+
_reset,
|
|
1623
|
+
_resetDefaultValues,
|
|
1624
|
+
_updateFormState,
|
|
1625
|
+
_subjects,
|
|
1626
|
+
_proxyFormState,
|
|
1627
|
+
get _fields() {
|
|
1628
|
+
return _fields;
|
|
1629
|
+
},
|
|
1630
|
+
get _formValues() {
|
|
1631
|
+
return _formValues;
|
|
1632
|
+
},
|
|
1633
|
+
get _state() {
|
|
1634
|
+
return _state;
|
|
1635
|
+
},
|
|
1636
|
+
set _state(value) {
|
|
1637
|
+
_state = value;
|
|
1638
|
+
},
|
|
1639
|
+
get _defaultValues() {
|
|
1640
|
+
return _defaultValues;
|
|
1641
|
+
},
|
|
1642
|
+
get _names() {
|
|
1643
|
+
return _names;
|
|
1644
|
+
},
|
|
1645
|
+
set _names(value) {
|
|
1646
|
+
_names = value;
|
|
1647
|
+
},
|
|
1648
|
+
get _formState() {
|
|
1649
|
+
return _formState;
|
|
1650
|
+
},
|
|
1651
|
+
set _formState(value) {
|
|
1652
|
+
_formState = value;
|
|
1653
|
+
},
|
|
1654
|
+
get _options() {
|
|
1655
|
+
return _options;
|
|
1656
|
+
},
|
|
1657
|
+
set _options(value) {
|
|
1658
|
+
_options = {
|
|
1659
|
+
..._options,
|
|
1660
|
+
...value,
|
|
1661
|
+
};
|
|
1662
|
+
},
|
|
1663
|
+
},
|
|
1664
|
+
trigger,
|
|
1665
|
+
register,
|
|
1666
|
+
handleSubmit,
|
|
1667
|
+
watch,
|
|
1668
|
+
setValue,
|
|
1669
|
+
getValues,
|
|
1670
|
+
reset,
|
|
1671
|
+
resetField,
|
|
1672
|
+
clearErrors,
|
|
1673
|
+
unregister,
|
|
1674
|
+
setError,
|
|
1675
|
+
setFocus,
|
|
1676
|
+
getFieldState,
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* Custom hook to manage the entire form.
|
|
1682
|
+
*
|
|
1683
|
+
* @remarks
|
|
1684
|
+
* [API](https://react-hook-form.com/docs/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
|
|
1685
|
+
*
|
|
1686
|
+
* @param props - form configuration and validation parameters.
|
|
1687
|
+
*
|
|
1688
|
+
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
|
1689
|
+
*
|
|
1690
|
+
* @example
|
|
1691
|
+
* ```tsx
|
|
1692
|
+
* function App() {
|
|
1693
|
+
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
|
1694
|
+
* const onSubmit = data => console.log(data);
|
|
1695
|
+
*
|
|
1696
|
+
* console.log(watch("example"));
|
|
1697
|
+
*
|
|
1698
|
+
* return (
|
|
1699
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
1700
|
+
* <input defaultValue="test" {...register("example")} />
|
|
1701
|
+
* <input {...register("exampleRequired", { required: true })} />
|
|
1702
|
+
* {errors.exampleRequired && <span>This field is required</span>}
|
|
1703
|
+
* <button>Submit</button>
|
|
1704
|
+
* </form>
|
|
1705
|
+
* );
|
|
1706
|
+
* }
|
|
1707
|
+
* ```
|
|
1708
|
+
*/
|
|
1709
|
+
function useForm(props = {}) {
|
|
1710
|
+
const _formControl = React__default.useRef();
|
|
1711
|
+
const _values = React__default.useRef();
|
|
1712
|
+
const [formState, updateFormState] = React__default.useState({
|
|
1713
|
+
isDirty: false,
|
|
1714
|
+
isValidating: false,
|
|
1715
|
+
isLoading: isFunction(props.defaultValues),
|
|
1716
|
+
isSubmitted: false,
|
|
1717
|
+
isSubmitting: false,
|
|
1718
|
+
isSubmitSuccessful: false,
|
|
1719
|
+
isValid: false,
|
|
1720
|
+
submitCount: 0,
|
|
1721
|
+
dirtyFields: {},
|
|
1722
|
+
touchedFields: {},
|
|
1723
|
+
errors: {},
|
|
1724
|
+
defaultValues: isFunction(props.defaultValues)
|
|
1725
|
+
? undefined
|
|
1726
|
+
: props.defaultValues,
|
|
1727
|
+
});
|
|
1728
|
+
if (!_formControl.current) {
|
|
1729
|
+
_formControl.current = {
|
|
1730
|
+
...createFormControl(props, () => updateFormState((formState) => ({ ...formState }))),
|
|
1731
|
+
formState,
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
const control = _formControl.current.control;
|
|
1735
|
+
control._options = props;
|
|
1736
|
+
useSubscribe({
|
|
1737
|
+
subject: control._subjects.state,
|
|
1738
|
+
next: (value) => {
|
|
1739
|
+
if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
|
|
1740
|
+
updateFormState({ ...control._formState });
|
|
1741
|
+
}
|
|
1742
|
+
},
|
|
1743
|
+
});
|
|
1744
|
+
React__default.useEffect(() => {
|
|
1745
|
+
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
1746
|
+
control._reset(props.values, control._options.resetOptions);
|
|
1747
|
+
_values.current = props.values;
|
|
1748
|
+
}
|
|
1749
|
+
else {
|
|
1750
|
+
control._resetDefaultValues();
|
|
1751
|
+
}
|
|
1752
|
+
}, [props.values, control]);
|
|
1753
|
+
React__default.useEffect(() => {
|
|
1754
|
+
if (!control._state.mount) {
|
|
1755
|
+
control._updateValid();
|
|
1756
|
+
control._state.mount = true;
|
|
1757
|
+
}
|
|
1758
|
+
if (control._state.watch) {
|
|
1759
|
+
control._state.watch = false;
|
|
1760
|
+
control._subjects.state.next({ ...control._formState });
|
|
1761
|
+
}
|
|
1762
|
+
control._removeUnmounted();
|
|
1763
|
+
});
|
|
1764
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
|
1765
|
+
return _formControl.current;
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
var theme = {
|
|
1769
|
+
breakpoints: ['40em', '52em', '64em'],
|
|
1770
|
+
space: ['0px', '4px', '8px', '16px', '32px', '64px', '128px', '256px', '512px'],
|
|
1771
|
+
fontSizes: ['12px', '14px', '16px', '20px', '24px', '32px', '48px', '64px', '72px'],
|
|
1772
|
+
marginPaddings: ['0.5rem', '1rem', '1.5rem', '2rem', '2.5rem', '3rem', '3.5rem', '4rem', '4.5rem', '5rem', '5.5rem', '6rem'],
|
|
1773
|
+
letterSpacings: ['-0.5px', '0px', '+0.5px'],
|
|
1774
|
+
radii: ['0px', '2px', '4px', '8px'],
|
|
1775
|
+
borderRadius: '6px',
|
|
1776
|
+
colors: {
|
|
1777
|
+
primary: '#000',
|
|
1778
|
+
base: '#4e4949',
|
|
1779
|
+
baseLt: '#817c7c',
|
|
1780
|
+
borderColor: '#d8d8d8',
|
|
1781
|
+
ntrlMin: '#fff',
|
|
1782
|
+
ntrlMinTen: '#e6e6e6',
|
|
1783
|
+
gray: '#efefef',
|
|
1784
|
+
darkerGray: '#454141'
|
|
1785
|
+
},
|
|
1786
|
+
rareStepColors: {
|
|
1787
|
+
primary: '#202b58',
|
|
1788
|
+
secondary: '#ef5c2d',
|
|
1789
|
+
ntrlMin: '#ffffff',
|
|
1790
|
+
ntrl40: '#f4f4f4',
|
|
1791
|
+
ntrl50: '#808080',
|
|
1792
|
+
ntrl60: '#e8e8e8',
|
|
1793
|
+
ntrl70: '#252525',
|
|
1794
|
+
ntrlMax: '#000000'
|
|
1795
|
+
},
|
|
1796
|
+
rareStepFontWeights: {
|
|
1797
|
+
normal: 400,
|
|
1798
|
+
medium: 500,
|
|
1799
|
+
semiBold: 600,
|
|
1800
|
+
bold: 700
|
|
1801
|
+
},
|
|
1802
|
+
rareStepFontSize: {
|
|
1803
|
+
h1: '36px',
|
|
1804
|
+
h2: '30px',
|
|
1805
|
+
h3: '24px',
|
|
1806
|
+
h4: '20px',
|
|
1807
|
+
h5: '16px',
|
|
1808
|
+
h6: '14px',
|
|
1809
|
+
footerTitle: '16px'
|
|
1810
|
+
},
|
|
1811
|
+
rareStepPageSizes: {
|
|
1812
|
+
mobile: '500px',
|
|
1813
|
+
tablet: '768px',
|
|
1814
|
+
desktop: '992px',
|
|
1815
|
+
screenl: '1280px',
|
|
1816
|
+
screenxl: '1680px',
|
|
1817
|
+
screenxxl: '1920px',
|
|
1818
|
+
container: '1600px'
|
|
1819
|
+
}
|
|
1820
|
+
};
|
|
1821
|
+
|
|
1822
|
+
var themed = (function (Component) {
|
|
1823
|
+
return /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
1824
|
+
return /*#__PURE__*/React__default.createElement(styled.ThemeProvider, {
|
|
1825
|
+
theme: theme
|
|
1826
|
+
}, /*#__PURE__*/React__default.createElement(Component, _extends({}, props, {
|
|
1827
|
+
ref: ref
|
|
1828
|
+
})));
|
|
1829
|
+
});
|
|
1830
|
+
});
|
|
1831
|
+
|
|
1832
|
+
var _templateObject;
|
|
1833
|
+
var FooterWrapper = styled__default.div(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n {\n background-color: ", ";\n }\n"])), function (props) {
|
|
1834
|
+
return props.theme.colors.gray;
|
|
1835
|
+
});
|
|
1836
|
+
|
|
1837
|
+
var Event = function Event(instance, track) {
|
|
1838
|
+
var category = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
1839
|
+
var action = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
1840
|
+
var label = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '';
|
|
1841
|
+
var value = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : '';
|
|
1842
|
+
|
|
1843
|
+
if (track) {
|
|
1844
|
+
instance.event({
|
|
1845
|
+
category: category,
|
|
1846
|
+
action: action,
|
|
1847
|
+
label: label,
|
|
1848
|
+
value: value
|
|
1849
|
+
});
|
|
1850
|
+
}
|
|
1851
|
+
};
|
|
1852
|
+
|
|
1853
|
+
var GALink = function GALink(props) {
|
|
1854
|
+
var gaOnClick = function gaOnClick(event) {
|
|
1855
|
+
Event(props.instance, props.track, 'Link', "Click on ".concat(props.href), props.label);
|
|
1856
|
+
|
|
1857
|
+
if (typeof props.onClick === 'function') {
|
|
1858
|
+
props.onClick(event);
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
|
|
1862
|
+
return /*#__PURE__*/React__default.createElement("a", _extends({
|
|
1863
|
+
href: "/"
|
|
1864
|
+
}, props, {
|
|
1865
|
+
onClick: gaOnClick
|
|
1866
|
+
}), props.children);
|
|
1867
|
+
};
|
|
1868
|
+
|
|
1869
|
+
var LuxityFooter = function LuxityFooter(_ref) {
|
|
1870
|
+
var _ref$showNewsletter = _ref.showNewsletter,
|
|
1871
|
+
showNewsletter = _ref$showNewsletter === void 0 ? true : _ref$showNewsletter,
|
|
1872
|
+
_ref$buyLinks = _ref.buyLinks,
|
|
1873
|
+
buyLinks = _ref$buyLinks === void 0 ? [] : _ref$buyLinks,
|
|
1874
|
+
_ref$sellLinks = _ref.sellLinks,
|
|
1875
|
+
sellLinks = _ref$sellLinks === void 0 ? [] : _ref$sellLinks,
|
|
1876
|
+
_ref$helpLinks = _ref.helpLinks,
|
|
1877
|
+
helpLinks = _ref$helpLinks === void 0 ? [] : _ref$helpLinks,
|
|
1878
|
+
_ref$companyLinks = _ref.companyLinks,
|
|
1879
|
+
companyLinks = _ref$companyLinks === void 0 ? [] : _ref$companyLinks,
|
|
1880
|
+
_ref$socialLinks = _ref.socialLinks,
|
|
1881
|
+
socialLinks = _ref$socialLinks === void 0 ? [] : _ref$socialLinks,
|
|
1882
|
+
_ref$contactLinks = _ref.contactLinks,
|
|
1883
|
+
contactLinks = _ref$contactLinks === void 0 ? [] : _ref$contactLinks,
|
|
1884
|
+
_ref$quickiesLinks = _ref.quickiesLinks,
|
|
1885
|
+
quickiesLinks = _ref$quickiesLinks === void 0 ? [] : _ref$quickiesLinks,
|
|
1886
|
+
visaImgSrc = _ref.visaImgSrc,
|
|
1887
|
+
masterCardSrc = _ref.masterCardSrc,
|
|
1888
|
+
logoSrc = _ref.logoSrc,
|
|
1889
|
+
formIconSrc = _ref.formIconSrc,
|
|
1890
|
+
onSubmit = _ref.onSubmit;
|
|
1891
|
+
var currentYear = new Date().getFullYear();
|
|
1892
|
+
|
|
1893
|
+
var _useForm = useForm(),
|
|
1894
|
+
handleSubmit = _useForm.handleSubmit,
|
|
1895
|
+
register = _useForm.register,
|
|
1896
|
+
errors = _useForm.formState.errors;
|
|
1897
|
+
|
|
1898
|
+
var renderLinks = function renderLinks(links, title) {
|
|
1899
|
+
return /*#__PURE__*/React__default.createElement("div", {
|
|
1900
|
+
className: "footer__link-container"
|
|
1901
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1902
|
+
className: "footer__link-title"
|
|
1903
|
+
}, title), /*#__PURE__*/React__default.createElement("div", {
|
|
1904
|
+
className: "footer__links"
|
|
1905
|
+
}, !!links.length && links.map(function (link) {
|
|
1906
|
+
return /*#__PURE__*/React__default.createElement(GALink, {
|
|
1907
|
+
key: link.label,
|
|
1908
|
+
instance: link.instance,
|
|
1909
|
+
href: link.href,
|
|
1910
|
+
label: link.label,
|
|
1911
|
+
className: "footer__link",
|
|
1912
|
+
rel: "noopener noreferrer",
|
|
1913
|
+
track: link === null || link === void 0 ? void 0 : link.track,
|
|
1914
|
+
target: link.target
|
|
1915
|
+
}, link.iconClassName && /*#__PURE__*/React__default.createElement("i", {
|
|
1916
|
+
className: link.iconClassName
|
|
1917
|
+
}), link.label);
|
|
1918
|
+
})));
|
|
1919
|
+
};
|
|
1920
|
+
|
|
1921
|
+
return /*#__PURE__*/React__default.createElement(FooterWrapper, null, showNewsletter && /*#__PURE__*/React__default.createElement("div", {
|
|
1922
|
+
className: "footer__newsletter-section"
|
|
1923
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1924
|
+
className: "footer__title-wrapper"
|
|
1925
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1926
|
+
className: "footer__newsletter-title"
|
|
1927
|
+
}, "Sign up to our news letter"), /*#__PURE__*/React__default.createElement("p", {
|
|
1928
|
+
className: "footer__newsletter-text"
|
|
1929
|
+
}, "By signing up you agree to our terms & conditions and privacy policy. You can unsubscribe anytime.")), /*#__PURE__*/React__default.createElement("div", {
|
|
1930
|
+
className: "footer__newsletter-form"
|
|
1931
|
+
}, /*#__PURE__*/React__default.createElement("form", {
|
|
1932
|
+
onSubmit: handleSubmit(onSubmit)
|
|
1933
|
+
}, /*#__PURE__*/React__default.createElement("input", _extends({}, register('email', {
|
|
1934
|
+
required: 'The email field is required.',
|
|
1935
|
+
pattern: {
|
|
1936
|
+
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
|
|
1937
|
+
message: 'Invalid email address'
|
|
1938
|
+
}
|
|
1939
|
+
}), {
|
|
1940
|
+
placeholder: "Your Sexy Email",
|
|
1941
|
+
className: "footer__form-input"
|
|
1942
|
+
})), errors.email && /*#__PURE__*/React__default.createElement("span", {
|
|
1943
|
+
className: "footer__form-errors"
|
|
1944
|
+
}, errors.email.message), /*#__PURE__*/React__default.createElement("button", {
|
|
1945
|
+
type: "submit"
|
|
1946
|
+
}, /*#__PURE__*/React__default.createElement("img", {
|
|
1947
|
+
className: "footer__newsletter-icon",
|
|
1948
|
+
src: formIconSrc,
|
|
1949
|
+
alt: "submit"
|
|
1950
|
+
}))))), /*#__PURE__*/React__default.createElement("div", {
|
|
1951
|
+
className: "footer__wrapper footer__wrapper--desktop"
|
|
1952
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1953
|
+
className: "footer__main-section"
|
|
1954
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1955
|
+
className: "footer__logo-wrapper"
|
|
1956
|
+
}, /*#__PURE__*/React__default.createElement("img", {
|
|
1957
|
+
className: "footer__logo",
|
|
1958
|
+
src: logoSrc,
|
|
1959
|
+
alt: "luxity-logo"
|
|
1960
|
+
})), renderLinks(buyLinks, 'Buy'), /*#__PURE__*/React__default.createElement("div", null, renderLinks(sellLinks, 'Sell'), /*#__PURE__*/React__default.createElement("div", {
|
|
1961
|
+
className: "footer__link-child"
|
|
1962
|
+
}, renderLinks(companyLinks, 'Company'))), renderLinks(helpLinks, 'Help'), renderLinks(socialLinks, 'Follow us'), /*#__PURE__*/React__default.createElement("div", {
|
|
1963
|
+
className: "footer__link-container"
|
|
1964
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1965
|
+
className: "footer__link-title"
|
|
1966
|
+
}, "Contact"), /*#__PURE__*/React__default.createElement("div", {
|
|
1967
|
+
className: "footer__links"
|
|
1968
|
+
}, !!contactLinks.map && contactLinks.map(function (link) {
|
|
1969
|
+
return !link.isContact ? /*#__PURE__*/React__default.createElement(GALink, {
|
|
1970
|
+
key: link.label,
|
|
1971
|
+
instance: link.instance,
|
|
1972
|
+
href: link.href,
|
|
1973
|
+
label: link.label,
|
|
1974
|
+
className: "footer__link",
|
|
1975
|
+
rel: "noopener noreferrer",
|
|
1976
|
+
track: link === null || link === void 0 ? void 0 : link.track,
|
|
1977
|
+
target: link.target
|
|
1978
|
+
}, link.label) : /*#__PURE__*/React__default.createElement("div", {
|
|
1979
|
+
className: "footer__text"
|
|
1980
|
+
}, link.label);
|
|
1981
|
+
})), /*#__PURE__*/React__default.createElement("div", {
|
|
1982
|
+
className: "footer__store-hours"
|
|
1983
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1984
|
+
className: "footer__text"
|
|
1985
|
+
}, "Monday - Friday"), /*#__PURE__*/React__default.createElement("div", {
|
|
1986
|
+
className: "footer__text"
|
|
1987
|
+
}, "8:30am - 5pm")))), /*#__PURE__*/React__default.createElement("div", {
|
|
1988
|
+
className: "footer__copyrights-section"
|
|
1989
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
1990
|
+
className: "footer__payment-icons"
|
|
1991
|
+
}, /*#__PURE__*/React__default.createElement("img", {
|
|
1992
|
+
alt: "visa",
|
|
1993
|
+
src: masterCardSrc
|
|
1994
|
+
}), /*#__PURE__*/React__default.createElement("img", {
|
|
1995
|
+
src: visaImgSrc,
|
|
1996
|
+
alt: "button"
|
|
1997
|
+
})), /*#__PURE__*/React__default.createElement("p", {
|
|
1998
|
+
className: "footer__copyright"
|
|
1999
|
+
}, "Copyright ", currentYear, " | Luxity (Pty) LTD | All Rights Reserved | Website Developed by B Online"))), /*#__PURE__*/React__default.createElement("div", {
|
|
2000
|
+
className: "footer__wrapper footer__wrapper--mobile"
|
|
2001
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
2002
|
+
className: "footer__section-row"
|
|
2003
|
+
}, renderLinks(quickiesLinks, 'Quickies'), renderLinks(socialLinks, 'Follow us')), /*#__PURE__*/React__default.createElement("div", {
|
|
2004
|
+
className: "footer__section-row"
|
|
2005
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
2006
|
+
className: "footer__link-container"
|
|
2007
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
2008
|
+
className: "footer__link-title"
|
|
2009
|
+
}, "We love"), /*#__PURE__*/React__default.createElement("div", {
|
|
2010
|
+
className: "footer__links"
|
|
2011
|
+
}, /*#__PURE__*/React__default.createElement("div", {
|
|
2012
|
+
className: "footer__text"
|
|
2013
|
+
}, "Visa"), /*#__PURE__*/React__default.createElement("div", {
|
|
2014
|
+
className: "footer__text"
|
|
2015
|
+
}, "Mastercard"), /*#__PURE__*/React__default.createElement("div", {
|
|
2016
|
+
className: "footer__text"
|
|
2017
|
+
}, "EFT"))), renderLinks(helpLinks, 'Legal Stuff')), /*#__PURE__*/React__default.createElement("p", {
|
|
2018
|
+
className: "footer__copyright footer__copyright--mobile"
|
|
2019
|
+
}, "Copyright ", currentYear, " | Luxity (Pty) LTD | All Rights Reserved | Website Developed by B Online")));
|
|
2020
|
+
};
|
|
2021
|
+
|
|
2022
|
+
var index = themed(LuxityFooter);
|
|
2023
|
+
|
|
2024
|
+
return index;
|
|
2025
|
+
|
|
2026
|
+
})));
|