@bonlineza/luxity-lib 2.39.4 → 2.39.6
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/Accordion.js +342 -352
- package/AddInterest.js +3109 -3068
- package/AddProductInterest.js +999 -7572
- package/BrandSelector.js +854 -861
- package/Checkbox.js +240 -250
- package/CheckboxButton.js +229 -236
- package/CommunicationPreferenceOptionsRenderer.js +560 -568
- package/CommunicationPreferenceSection.js +651 -659
- package/CommunicationPreferences.js +2394 -2402
- package/ConditionSelector.js +852 -859
- package/CountryCodeSelect.js +79 -6650
- package/CurrencyInputFormatter.js +86 -96
- package/CurrencySelect.js +219 -227
- package/DropdownSelector.js +212 -6821
- package/GALink.js +43 -51
- package/GenderSelector.js +355 -362
- package/GraphQLDropdownSelector.js +5308 -11832
- package/GraphQLMultiSelector.js +593 -601
- package/IconHeader.js +118 -126
- package/IconWrapper.js +17 -25
- package/InterestCatSelector.js +424 -389
- package/InterestItem.js +462 -469
- package/Interests.js +4719 -11243
- package/InterestsSubheading.js +33 -41
- package/InvertedTextButton.js +167 -175
- package/LuxityAppSideNav.js +181 -189
- package/LuxityDesktopHeader.js +1355 -1363
- package/LuxityFooter.js +2228 -2236
- package/LuxityGraphQLDropdownSelector.js +5395 -11919
- package/LuxityGraphQLMultiselector.js +778 -785
- package/LuxityMobileHeader.js +1786 -1794
- package/LuxityPageReady.js +199 -209
- package/LuxityProductInterestItem.js +359 -367
- package/LuxitySideNav.js +665 -673
- package/MaterialSelector.js +5430 -11954
- package/ModelSelector.js +5430 -11954
- package/NotificationDropDown.js +459 -7030
- package/Notify.js +20 -32
- package/OutlinePrimaryButton.js +134 -142
- package/PriceRangeSelector.js +1264 -1265
- package/PrimaryButton.js +129 -137
- package/RadioButtons.js +308 -318
- package/RangeSlider.js +1033 -1039
- package/RareStepFooter.js +257 -264
- package/RareStepHeader.js +801 -808
- package/ReactSelectWrapper.js +33 -6604
- package/SecondaryButton.js +144 -152
- package/ShoeSizeRangeSelector.js +1147 -1153
- package/ShoeSizeSelector.js +1146 -1154
- package/SimpleErrorBoundary.js +130 -138
- package/StyleSelector.js +854 -861
- package/TestComponent.js +15 -0
- package/TextButton.js +135 -143
- package/Tooltip.js +176 -184
- package/Tracking.js +15 -25
- package/WrapperButton.js +50 -58
- package/build/CountryCodeSelect/index.js +18 -0
- package/build/ReactSelectWrapper/index.js +34 -0
- package/build/bundle.css +1 -1
- package/build/bundle.js +10634 -17192
- package/build/constants.js +32 -43
- package/package.json +2 -2
- package/postcss.config.js +6 -0
- package/svg/Accessories.js +34 -42
- package/svg/Account.js +30 -38
- package/svg/Add.js +31 -39
- package/svg/ArrowRight.js +28 -36
- package/svg/Bags.js +40 -48
- package/svg/BurgerMenu.js +24 -32
- package/svg/Calendar.js +98 -106
- package/svg/CalendarCircled.js +100 -108
- package/svg/CheckCircled.js +32 -40
- package/svg/Close.js +29 -37
- package/svg/Clothing.js +36 -44
- package/svg/Dot.js +32 -40
- package/svg/Edit.js +27 -35
- package/svg/Eye.js +28 -36
- package/svg/FileAttach.js +28 -36
- package/svg/FileAttachCircled.js +30 -38
- package/svg/Jewellery.js +56 -0
- package/svg/LightBulb.js +58 -66
- package/svg/Logo.js +132 -140
- package/svg/LuxityMenu.js +28 -36
- package/svg/Minus.js +29 -37
- package/svg/MobileAvatar.js +28 -36
- package/svg/MyOrder.js +36 -44
- package/svg/MyPayment.js +36 -44
- package/svg/MyProfile.js +44 -52
- package/svg/MySale.js +31 -39
- package/svg/Notification.js +30 -38
- package/svg/PointerDown.js +28 -36
- package/svg/Search.js +27 -35
- package/svg/Shoes.js +37 -45
- package/svg/SvgWrapper.js +13 -21
- package/svg/Trash.js +32 -40
- package/svg/Watches.js +47 -55
|
@@ -1,600 +1,592 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['react', 'styled-components'], factory) :
|
|
4
|
-
(global = global || self, global.CommunicationPreferenceOptionsRenderer = factory(global.React, global.styled));
|
|
5
|
-
}(this, (function (React, styled) { 'use strict';
|
|
6
|
-
|
|
7
|
-
var React__default = 'default' in React ? React['default'] : React;
|
|
8
|
-
styled = styled && Object.prototype.hasOwnProperty.call(styled, 'default') ? 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 _objectWithoutPropertiesLoose(source, excluded) {
|
|
28
|
-
if (source == null) return {};
|
|
29
|
-
var target = {};
|
|
30
|
-
var sourceKeys = Object.keys(source);
|
|
31
|
-
var key, i;
|
|
32
|
-
|
|
33
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
34
|
-
key = sourceKeys[i];
|
|
35
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
36
|
-
target[key] = source[key];
|
|
37
|
-
}
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
38
3
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (source == null) return {};
|
|
44
|
-
|
|
45
|
-
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
46
|
-
|
|
47
|
-
var key, i;
|
|
4
|
+
function _extends() {
|
|
5
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
6
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
7
|
+
var source = arguments[i];
|
|
48
8
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
key = sourceSymbolKeys[i];
|
|
54
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
55
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
56
|
-
target[key] = source[key];
|
|
9
|
+
for (var key in source) {
|
|
10
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
11
|
+
target[key] = source[key];
|
|
12
|
+
}
|
|
57
13
|
}
|
|
58
14
|
}
|
|
59
15
|
|
|
60
16
|
return target;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function _taggedTemplateLiteral(strings, raw) {
|
|
64
|
-
if (!raw) {
|
|
65
|
-
raw = strings.slice(0);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return Object.freeze(Object.defineProperties(strings, {
|
|
69
|
-
raw: {
|
|
70
|
-
value: Object.freeze(raw)
|
|
71
|
-
}
|
|
72
|
-
}));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
76
|
-
|
|
77
|
-
var isDateObject = (value) => value instanceof Date;
|
|
78
|
-
|
|
79
|
-
var isNullOrUndefined = (value) => value == null;
|
|
80
|
-
|
|
81
|
-
const isObjectType = (value) => typeof value === 'object';
|
|
82
|
-
var isObject = (value) => !isNullOrUndefined(value) &&
|
|
83
|
-
!Array.isArray(value) &&
|
|
84
|
-
isObjectType(value) &&
|
|
85
|
-
!isDateObject(value);
|
|
86
|
-
|
|
87
|
-
var getEventValue = (event) => isObject(event) && event.target
|
|
88
|
-
? isCheckBoxInput(event.target)
|
|
89
|
-
? event.target.checked
|
|
90
|
-
: event.target.value
|
|
91
|
-
: event;
|
|
92
|
-
|
|
93
|
-
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
94
|
-
|
|
95
|
-
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
96
|
-
|
|
97
|
-
var isPlainObject = (tempObject) => {
|
|
98
|
-
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
99
|
-
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
|
100
17
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
|
116
|
-
(isArray || isObject(data))) {
|
|
117
|
-
copy = isArray ? [] : {};
|
|
118
|
-
if (!isArray && !isPlainObject(data)) {
|
|
119
|
-
copy = data;
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
for (const key in data) {
|
|
123
|
-
if (data.hasOwnProperty(key)) {
|
|
124
|
-
copy[key] = cloneObject(data[key]);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
return data;
|
|
131
|
-
}
|
|
132
|
-
return copy;
|
|
18
|
+
return _extends.apply(this, arguments);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
22
|
+
if (source == null) return {};
|
|
23
|
+
var target = {};
|
|
24
|
+
var sourceKeys = Object.keys(source);
|
|
25
|
+
var key, i;
|
|
26
|
+
|
|
27
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
28
|
+
key = sourceKeys[i];
|
|
29
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
30
|
+
target[key] = source[key];
|
|
133
31
|
}
|
|
134
32
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
var isUndefined = (val) => val === undefined;
|
|
138
|
-
|
|
139
|
-
var get = (obj, path, defaultValue) => {
|
|
140
|
-
if (!path || !isObject(obj)) {
|
|
141
|
-
return defaultValue;
|
|
142
|
-
}
|
|
143
|
-
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
|
|
144
|
-
return isUndefined(result) || result === obj
|
|
145
|
-
? isUndefined(obj[path])
|
|
146
|
-
? defaultValue
|
|
147
|
-
: obj[path]
|
|
148
|
-
: result;
|
|
149
|
-
};
|
|
33
|
+
return target;
|
|
34
|
+
}
|
|
150
35
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
FOCUS_OUT: 'focusout',
|
|
154
|
-
CHANGE: 'change',
|
|
155
|
-
};
|
|
156
|
-
const VALIDATION_MODE = {
|
|
157
|
-
onBlur: 'onBlur',
|
|
158
|
-
onChange: 'onChange',
|
|
159
|
-
onSubmit: 'onSubmit',
|
|
160
|
-
onTouched: 'onTouched',
|
|
161
|
-
all: 'all',
|
|
162
|
-
};
|
|
36
|
+
function _objectWithoutProperties(source, excluded) {
|
|
37
|
+
if (source == null) return {};
|
|
163
38
|
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
|
167
|
-
*
|
|
168
|
-
* @remarks
|
|
169
|
-
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
170
|
-
*
|
|
171
|
-
* @returns return all useForm methods
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```tsx
|
|
175
|
-
* function App() {
|
|
176
|
-
* const methods = useForm();
|
|
177
|
-
* const onSubmit = data => console.log(data);
|
|
178
|
-
*
|
|
179
|
-
* return (
|
|
180
|
-
* <FormProvider {...methods} >
|
|
181
|
-
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
182
|
-
* <NestedInput />
|
|
183
|
-
* <input type="submit" />
|
|
184
|
-
* </form>
|
|
185
|
-
* </FormProvider>
|
|
186
|
-
* );
|
|
187
|
-
* }
|
|
188
|
-
*
|
|
189
|
-
* function NestedInput() {
|
|
190
|
-
* const { register } = useFormContext(); // retrieve all hook methods
|
|
191
|
-
* return <input {...register("test")} />;
|
|
192
|
-
* }
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
const useFormContext = () => React__default.useContext(HookFormContext);
|
|
196
|
-
|
|
197
|
-
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
198
|
-
const result = {
|
|
199
|
-
defaultValues: control._defaultValues,
|
|
200
|
-
};
|
|
201
|
-
for (const key in formState) {
|
|
202
|
-
Object.defineProperty(result, key, {
|
|
203
|
-
get: () => {
|
|
204
|
-
const _key = key;
|
|
205
|
-
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
206
|
-
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
207
|
-
}
|
|
208
|
-
localProxyFormState && (localProxyFormState[_key] = true);
|
|
209
|
-
return formState[_key];
|
|
210
|
-
},
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
return result;
|
|
214
|
-
};
|
|
39
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
|
215
40
|
|
|
216
|
-
var
|
|
41
|
+
var key, i;
|
|
217
42
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const { name, ...formState } = formStateData;
|
|
221
|
-
return (isEmptyObject(formState) ||
|
|
222
|
-
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
223
|
-
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
224
|
-
(!isRoot || VALIDATION_MODE.all)));
|
|
225
|
-
};
|
|
43
|
+
if (Object.getOwnPropertySymbols) {
|
|
44
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
226
45
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
: !name ||
|
|
234
|
-
!signalName ||
|
|
235
|
-
name === signalName ||
|
|
236
|
-
convertToArrayPayload(name).some((currentName) => currentName &&
|
|
237
|
-
(currentName.startsWith(signalName) ||
|
|
238
|
-
signalName.startsWith(currentName)));
|
|
239
|
-
|
|
240
|
-
function useSubscribe(props) {
|
|
241
|
-
const _props = React__default.useRef(props);
|
|
242
|
-
_props.current = props;
|
|
243
|
-
React__default.useEffect(() => {
|
|
244
|
-
const subscription = !props.disabled &&
|
|
245
|
-
_props.current.subject &&
|
|
246
|
-
_props.current.subject.subscribe({
|
|
247
|
-
next: _props.current.next,
|
|
248
|
-
});
|
|
249
|
-
return () => {
|
|
250
|
-
subscription && subscription.unsubscribe();
|
|
251
|
-
};
|
|
252
|
-
}, [props.disabled]);
|
|
46
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
|
47
|
+
key = sourceSymbolKeys[i];
|
|
48
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
49
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
50
|
+
target[key] = source[key];
|
|
51
|
+
}
|
|
253
52
|
}
|
|
254
53
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```tsx
|
|
265
|
-
* function App() {
|
|
266
|
-
* const { register, handleSubmit, control } = useForm({
|
|
267
|
-
* defaultValues: {
|
|
268
|
-
* firstName: "firstName"
|
|
269
|
-
* }});
|
|
270
|
-
* const { dirtyFields } = useFormState({
|
|
271
|
-
* control
|
|
272
|
-
* });
|
|
273
|
-
* const onSubmit = (data) => console.log(data);
|
|
274
|
-
*
|
|
275
|
-
* return (
|
|
276
|
-
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
277
|
-
* <input {...register("firstName")} placeholder="First Name" />
|
|
278
|
-
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
|
279
|
-
* <input type="submit" />
|
|
280
|
-
* </form>
|
|
281
|
-
* );
|
|
282
|
-
* }
|
|
283
|
-
* ```
|
|
284
|
-
*/
|
|
285
|
-
function useFormState(props) {
|
|
286
|
-
const methods = useFormContext();
|
|
287
|
-
const { control = methods.control, disabled, name, exact } = props || {};
|
|
288
|
-
const [formState, updateFormState] = React__default.useState(control._formState);
|
|
289
|
-
const _mounted = React__default.useRef(true);
|
|
290
|
-
const _localProxyFormState = React__default.useRef({
|
|
291
|
-
isDirty: false,
|
|
292
|
-
isLoading: false,
|
|
293
|
-
dirtyFields: false,
|
|
294
|
-
touchedFields: false,
|
|
295
|
-
isValidating: false,
|
|
296
|
-
isValid: false,
|
|
297
|
-
errors: false,
|
|
298
|
-
});
|
|
299
|
-
const _name = React__default.useRef(name);
|
|
300
|
-
_name.current = name;
|
|
301
|
-
useSubscribe({
|
|
302
|
-
disabled,
|
|
303
|
-
next: (value) => _mounted.current &&
|
|
304
|
-
shouldSubscribeByName(_name.current, value.name, exact) &&
|
|
305
|
-
shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
|
|
306
|
-
updateFormState({
|
|
307
|
-
...control._formState,
|
|
308
|
-
...value,
|
|
309
|
-
}),
|
|
310
|
-
subject: control._subjects.state,
|
|
311
|
-
});
|
|
312
|
-
React__default.useEffect(() => {
|
|
313
|
-
_mounted.current = true;
|
|
314
|
-
_localProxyFormState.current.isValid && control._updateValid(true);
|
|
315
|
-
return () => {
|
|
316
|
-
_mounted.current = false;
|
|
317
|
-
};
|
|
318
|
-
}, [control]);
|
|
319
|
-
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
54
|
+
return target;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _taggedTemplateLiteral(strings, raw) {
|
|
58
|
+
if (!raw) {
|
|
59
|
+
raw = strings.slice(0);
|
|
320
60
|
}
|
|
321
61
|
|
|
322
|
-
|
|
62
|
+
return Object.freeze(Object.defineProperties(strings, {
|
|
63
|
+
raw: {
|
|
64
|
+
value: Object.freeze(raw)
|
|
65
|
+
}
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
323
68
|
|
|
324
|
-
|
|
325
|
-
if (isString(names)) {
|
|
326
|
-
isGlobal && _names.watch.add(names);
|
|
327
|
-
return get(formValues, names, defaultValue);
|
|
328
|
-
}
|
|
329
|
-
if (Array.isArray(names)) {
|
|
330
|
-
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
331
|
-
}
|
|
332
|
-
isGlobal && (_names.watchAll = true);
|
|
333
|
-
return formValues;
|
|
334
|
-
};
|
|
69
|
+
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
335
70
|
|
|
336
|
-
|
|
337
|
-
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
338
|
-
*
|
|
339
|
-
* @remarks
|
|
340
|
-
*
|
|
341
|
-
* [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```tsx
|
|
345
|
-
* const { watch } = useForm();
|
|
346
|
-
* const values = useWatch({
|
|
347
|
-
* name: "fieldName"
|
|
348
|
-
* control,
|
|
349
|
-
* })
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
|
-
function useWatch(props) {
|
|
353
|
-
const methods = useFormContext();
|
|
354
|
-
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
|
355
|
-
const _name = React__default.useRef(name);
|
|
356
|
-
_name.current = name;
|
|
357
|
-
useSubscribe({
|
|
358
|
-
disabled,
|
|
359
|
-
subject: control._subjects.values,
|
|
360
|
-
next: (formState) => {
|
|
361
|
-
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
|
362
|
-
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
|
363
|
-
}
|
|
364
|
-
},
|
|
365
|
-
});
|
|
366
|
-
const [value, updateValue] = React__default.useState(control._getWatch(name, defaultValue));
|
|
367
|
-
React__default.useEffect(() => control._removeUnmounted());
|
|
368
|
-
return value;
|
|
369
|
-
}
|
|
71
|
+
var isDateObject = (value) => value instanceof Date;
|
|
370
72
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
374
|
-
|
|
375
|
-
function set(object, path, value) {
|
|
376
|
-
let index = -1;
|
|
377
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
378
|
-
const length = tempPath.length;
|
|
379
|
-
const lastIndex = length - 1;
|
|
380
|
-
while (++index < length) {
|
|
381
|
-
const key = tempPath[index];
|
|
382
|
-
let newValue = value;
|
|
383
|
-
if (index !== lastIndex) {
|
|
384
|
-
const objValue = object[key];
|
|
385
|
-
newValue =
|
|
386
|
-
isObject(objValue) || Array.isArray(objValue)
|
|
387
|
-
? objValue
|
|
388
|
-
: !isNaN(+tempPath[index + 1])
|
|
389
|
-
? []
|
|
390
|
-
: {};
|
|
391
|
-
}
|
|
392
|
-
object[key] = newValue;
|
|
393
|
-
object = object[key];
|
|
394
|
-
}
|
|
395
|
-
return object;
|
|
396
|
-
}
|
|
73
|
+
var isNullOrUndefined = (value) => value == null;
|
|
397
74
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
*
|
|
404
|
-
* @param props - the path name to the form field value, and validation rules.
|
|
405
|
-
*
|
|
406
|
-
* @returns field properties, field and form state. {@link UseControllerReturn}
|
|
407
|
-
*
|
|
408
|
-
* @example
|
|
409
|
-
* ```tsx
|
|
410
|
-
* function Input(props) {
|
|
411
|
-
* const { field, fieldState, formState } = useController(props);
|
|
412
|
-
* return (
|
|
413
|
-
* <div>
|
|
414
|
-
* <input {...field} placeholder={props.name} />
|
|
415
|
-
* <p>{fieldState.isTouched && "Touched"}</p>
|
|
416
|
-
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
417
|
-
* </div>
|
|
418
|
-
* );
|
|
419
|
-
* }
|
|
420
|
-
* ```
|
|
421
|
-
*/
|
|
422
|
-
function useController(props) {
|
|
423
|
-
const methods = useFormContext();
|
|
424
|
-
const { name, disabled, control = methods.control, shouldUnregister } = props;
|
|
425
|
-
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
426
|
-
const value = useWatch({
|
|
427
|
-
control,
|
|
428
|
-
name,
|
|
429
|
-
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
|
430
|
-
exact: true,
|
|
431
|
-
});
|
|
432
|
-
const formState = useFormState({
|
|
433
|
-
control,
|
|
434
|
-
name,
|
|
435
|
-
});
|
|
436
|
-
const _registerProps = React__default.useRef(control.register(name, {
|
|
437
|
-
...props.rules,
|
|
438
|
-
value,
|
|
439
|
-
}));
|
|
440
|
-
_registerProps.current = control.register(name, props.rules);
|
|
441
|
-
React__default.useEffect(() => {
|
|
442
|
-
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
443
|
-
const updateMounted = (name, value) => {
|
|
444
|
-
const field = get(control._fields, name);
|
|
445
|
-
if (field) {
|
|
446
|
-
field._f.mount = value;
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
updateMounted(name, true);
|
|
450
|
-
if (_shouldUnregisterField) {
|
|
451
|
-
const value = cloneObject(get(control._options.defaultValues, name));
|
|
452
|
-
set(control._defaultValues, name, value);
|
|
453
|
-
if (isUndefined(get(control._formValues, name))) {
|
|
454
|
-
set(control._formValues, name, value);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
return () => {
|
|
458
|
-
(isArrayField
|
|
459
|
-
? _shouldUnregisterField && !control._state.action
|
|
460
|
-
: _shouldUnregisterField)
|
|
461
|
-
? control.unregister(name)
|
|
462
|
-
: updateMounted(name, false);
|
|
463
|
-
};
|
|
464
|
-
}, [name, control, isArrayField, shouldUnregister]);
|
|
465
|
-
React__default.useEffect(() => {
|
|
466
|
-
control._updateDisabledField({
|
|
467
|
-
disabled,
|
|
468
|
-
fields: control._fields,
|
|
469
|
-
name,
|
|
470
|
-
});
|
|
471
|
-
}, [disabled, name, control]);
|
|
472
|
-
return {
|
|
473
|
-
field: {
|
|
474
|
-
name,
|
|
475
|
-
value,
|
|
476
|
-
disabled,
|
|
477
|
-
onChange: React__default.useCallback((event) => _registerProps.current.onChange({
|
|
478
|
-
target: {
|
|
479
|
-
value: getEventValue(event),
|
|
480
|
-
name: name,
|
|
481
|
-
},
|
|
482
|
-
type: EVENTS.CHANGE,
|
|
483
|
-
}), [name]),
|
|
484
|
-
onBlur: React__default.useCallback(() => _registerProps.current.onBlur({
|
|
485
|
-
target: {
|
|
486
|
-
value: get(control._formValues, name),
|
|
487
|
-
name: name,
|
|
488
|
-
},
|
|
489
|
-
type: EVENTS.BLUR,
|
|
490
|
-
}), [name, control]),
|
|
491
|
-
ref: (elm) => {
|
|
492
|
-
const field = get(control._fields, name);
|
|
493
|
-
if (field && elm) {
|
|
494
|
-
field._f.ref = {
|
|
495
|
-
focus: () => elm.focus(),
|
|
496
|
-
select: () => elm.select(),
|
|
497
|
-
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
498
|
-
reportValidity: () => elm.reportValidity(),
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
|
-
},
|
|
502
|
-
},
|
|
503
|
-
formState,
|
|
504
|
-
fieldState: Object.defineProperties({}, {
|
|
505
|
-
invalid: {
|
|
506
|
-
enumerable: true,
|
|
507
|
-
get: () => !!get(formState.errors, name),
|
|
508
|
-
},
|
|
509
|
-
isDirty: {
|
|
510
|
-
enumerable: true,
|
|
511
|
-
get: () => !!get(formState.dirtyFields, name),
|
|
512
|
-
},
|
|
513
|
-
isTouched: {
|
|
514
|
-
enumerable: true,
|
|
515
|
-
get: () => !!get(formState.touchedFields, name),
|
|
516
|
-
},
|
|
517
|
-
error: {
|
|
518
|
-
enumerable: true,
|
|
519
|
-
get: () => get(formState.errors, name),
|
|
520
|
-
},
|
|
521
|
-
}),
|
|
522
|
-
};
|
|
523
|
-
}
|
|
75
|
+
const isObjectType = (value) => typeof value === 'object';
|
|
76
|
+
var isObject = (value) => !isNullOrUndefined(value) &&
|
|
77
|
+
!Array.isArray(value) &&
|
|
78
|
+
isObjectType(value) &&
|
|
79
|
+
!isDateObject(value);
|
|
524
80
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
});
|
|
81
|
+
var getEventValue = (event) => isObject(event) && event.target
|
|
82
|
+
? isCheckBoxInput(event.target)
|
|
83
|
+
? event.target.checked
|
|
84
|
+
: event.target.value
|
|
85
|
+
: event;
|
|
531
86
|
|
|
532
|
-
|
|
533
|
-
var RadioButtonText = styled.div(_templateObject$1 || (_templateObject$1 = _taggedTemplateLiteral(["\n display: flex;\n align-items: center;\n"])));
|
|
87
|
+
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
534
88
|
|
|
535
|
-
|
|
536
|
-
var RadioButtonCircle = styled.div(_templateObject$2 || (_templateObject$2 = _taggedTemplateLiteral(["\n height: 20px;\n width: 20px;\n border-radius: 50%;\n margin-right: 5px;\n border: 1px solid ", ";\n display: flex;\n"])), function (props) {
|
|
537
|
-
return props.theme.colors.base;
|
|
538
|
-
});
|
|
89
|
+
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
539
90
|
|
|
540
|
-
|
|
541
|
-
|
|
91
|
+
var isPlainObject = (tempObject) => {
|
|
92
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
93
|
+
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
|
94
|
+
};
|
|
542
95
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
});
|
|
96
|
+
var isWeb = typeof window !== 'undefined' &&
|
|
97
|
+
typeof window.HTMLElement !== 'undefined' &&
|
|
98
|
+
typeof document !== 'undefined';
|
|
547
99
|
|
|
548
|
-
|
|
100
|
+
function cloneObject(data) {
|
|
101
|
+
let copy;
|
|
102
|
+
const isArray = Array.isArray(data);
|
|
103
|
+
if (data instanceof Date) {
|
|
104
|
+
copy = new Date(data);
|
|
105
|
+
}
|
|
106
|
+
else if (data instanceof Set) {
|
|
107
|
+
copy = new Set(data);
|
|
108
|
+
}
|
|
109
|
+
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
|
110
|
+
(isArray || isObject(data))) {
|
|
111
|
+
copy = isArray ? [] : {};
|
|
112
|
+
if (!isArray && !isPlainObject(data)) {
|
|
113
|
+
copy = data;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
for (const key in data) {
|
|
117
|
+
if (data.hasOwnProperty(key)) {
|
|
118
|
+
copy[key] = cloneObject(data[key]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
return data;
|
|
125
|
+
}
|
|
126
|
+
return copy;
|
|
127
|
+
}
|
|
549
128
|
|
|
550
|
-
|
|
551
|
-
var OptionsRow = styled.div(_templateObject$5 || (_templateObject$5 = _taggedTemplateLiteral(["\n display: flex;\n flex-wrap: nowrap;\n flex-direction: row;\n"])));
|
|
552
|
-
var Options = styled.div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n display: flex;\n flex-wrap: nowrap;\n flex-direction: row;\n flex-basis: 60%;\n"])));
|
|
129
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
|
553
130
|
|
|
554
|
-
|
|
555
|
-
var control = _ref.control,
|
|
556
|
-
name = _ref.name,
|
|
557
|
-
isDisabled = _ref.isDisabled,
|
|
558
|
-
options = _ref.options,
|
|
559
|
-
defaultValue = _ref.defaultValue,
|
|
560
|
-
setValue = _ref.setValue;
|
|
131
|
+
var isUndefined = (val) => val === undefined;
|
|
561
132
|
|
|
562
|
-
|
|
133
|
+
var get = (obj, path, defaultValue) => {
|
|
134
|
+
if (!path || !isObject(obj)) {
|
|
135
|
+
return defaultValue;
|
|
136
|
+
}
|
|
137
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], obj);
|
|
138
|
+
return isUndefined(result) || result === obj
|
|
139
|
+
? isUndefined(obj[path])
|
|
140
|
+
? defaultValue
|
|
141
|
+
: obj[path]
|
|
142
|
+
: result;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const EVENTS = {
|
|
146
|
+
BLUR: 'blur',
|
|
147
|
+
FOCUS_OUT: 'focusout',
|
|
148
|
+
CHANGE: 'change',
|
|
149
|
+
};
|
|
150
|
+
const VALIDATION_MODE = {
|
|
151
|
+
onBlur: 'onBlur',
|
|
152
|
+
onChange: 'onChange',
|
|
153
|
+
onSubmit: 'onSubmit',
|
|
154
|
+
onTouched: 'onTouched',
|
|
155
|
+
all: 'all',
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const HookFormContext = React.createContext(null);
|
|
159
|
+
/**
|
|
160
|
+
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
164
|
+
*
|
|
165
|
+
* @returns return all useForm methods
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```tsx
|
|
169
|
+
* function App() {
|
|
170
|
+
* const methods = useForm();
|
|
171
|
+
* const onSubmit = data => console.log(data);
|
|
172
|
+
*
|
|
173
|
+
* return (
|
|
174
|
+
* <FormProvider {...methods} >
|
|
175
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
176
|
+
* <NestedInput />
|
|
177
|
+
* <input type="submit" />
|
|
178
|
+
* </form>
|
|
179
|
+
* </FormProvider>
|
|
180
|
+
* );
|
|
181
|
+
* }
|
|
182
|
+
*
|
|
183
|
+
* function NestedInput() {
|
|
184
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
|
185
|
+
* return <input {...register("test")} />;
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
const useFormContext = () => React.useContext(HookFormContext);
|
|
190
|
+
|
|
191
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
192
|
+
const result = {
|
|
193
|
+
defaultValues: control._defaultValues,
|
|
194
|
+
};
|
|
195
|
+
for (const key in formState) {
|
|
196
|
+
Object.defineProperty(result, key, {
|
|
197
|
+
get: () => {
|
|
198
|
+
const _key = key;
|
|
199
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
200
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
201
|
+
}
|
|
202
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
|
203
|
+
return formState[_key];
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
211
|
+
|
|
212
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
213
|
+
updateFormState(formStateData);
|
|
214
|
+
const { name, ...formState } = formStateData;
|
|
215
|
+
return (isEmptyObject(formState) ||
|
|
216
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
217
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
218
|
+
(!isRoot || VALIDATION_MODE.all)));
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
222
|
+
|
|
223
|
+
var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
|
|
224
|
+
? name === signalName ||
|
|
225
|
+
(Array.isArray(name) &&
|
|
226
|
+
name.some((currentName) => currentName && exact && currentName === signalName))
|
|
227
|
+
: !name ||
|
|
228
|
+
!signalName ||
|
|
229
|
+
name === signalName ||
|
|
230
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
|
231
|
+
(currentName.startsWith(signalName) ||
|
|
232
|
+
signalName.startsWith(currentName)));
|
|
233
|
+
|
|
234
|
+
function useSubscribe(props) {
|
|
235
|
+
const _props = React.useRef(props);
|
|
236
|
+
_props.current = props;
|
|
237
|
+
React.useEffect(() => {
|
|
238
|
+
const subscription = !props.disabled &&
|
|
239
|
+
_props.current.subject &&
|
|
240
|
+
_props.current.subject.subscribe({
|
|
241
|
+
next: _props.current.next,
|
|
242
|
+
});
|
|
243
|
+
return () => {
|
|
244
|
+
subscription && subscription.unsubscribe();
|
|
245
|
+
};
|
|
246
|
+
}, [props.disabled]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
|
251
|
+
*
|
|
252
|
+
* @remarks
|
|
253
|
+
* [API](https://react-hook-form.com/docs/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
|
|
254
|
+
*
|
|
255
|
+
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```tsx
|
|
259
|
+
* function App() {
|
|
260
|
+
* const { register, handleSubmit, control } = useForm({
|
|
261
|
+
* defaultValues: {
|
|
262
|
+
* firstName: "firstName"
|
|
263
|
+
* }});
|
|
264
|
+
* const { dirtyFields } = useFormState({
|
|
265
|
+
* control
|
|
266
|
+
* });
|
|
267
|
+
* const onSubmit = (data) => console.log(data);
|
|
268
|
+
*
|
|
269
|
+
* return (
|
|
270
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
271
|
+
* <input {...register("firstName")} placeholder="First Name" />
|
|
272
|
+
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
|
273
|
+
* <input type="submit" />
|
|
274
|
+
* </form>
|
|
275
|
+
* );
|
|
276
|
+
* }
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
function useFormState(props) {
|
|
280
|
+
const methods = useFormContext();
|
|
281
|
+
const { control = methods.control, disabled, name, exact } = props || {};
|
|
282
|
+
const [formState, updateFormState] = React.useState(control._formState);
|
|
283
|
+
const _mounted = React.useRef(true);
|
|
284
|
+
const _localProxyFormState = React.useRef({
|
|
285
|
+
isDirty: false,
|
|
286
|
+
isLoading: false,
|
|
287
|
+
dirtyFields: false,
|
|
288
|
+
touchedFields: false,
|
|
289
|
+
isValidating: false,
|
|
290
|
+
isValid: false,
|
|
291
|
+
errors: false,
|
|
292
|
+
});
|
|
293
|
+
const _name = React.useRef(name);
|
|
294
|
+
_name.current = name;
|
|
295
|
+
useSubscribe({
|
|
296
|
+
disabled,
|
|
297
|
+
next: (value) => _mounted.current &&
|
|
298
|
+
shouldSubscribeByName(_name.current, value.name, exact) &&
|
|
299
|
+
shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) &&
|
|
300
|
+
updateFormState({
|
|
301
|
+
...control._formState,
|
|
302
|
+
...value,
|
|
303
|
+
}),
|
|
304
|
+
subject: control._subjects.state,
|
|
305
|
+
});
|
|
306
|
+
React.useEffect(() => {
|
|
307
|
+
_mounted.current = true;
|
|
308
|
+
_localProxyFormState.current.isValid && control._updateValid(true);
|
|
309
|
+
return () => {
|
|
310
|
+
_mounted.current = false;
|
|
311
|
+
};
|
|
312
|
+
}, [control]);
|
|
313
|
+
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
var isString = (value) => typeof value === 'string';
|
|
317
|
+
|
|
318
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
|
319
|
+
if (isString(names)) {
|
|
320
|
+
isGlobal && _names.watch.add(names);
|
|
321
|
+
return get(formValues, names, defaultValue);
|
|
322
|
+
}
|
|
323
|
+
if (Array.isArray(names)) {
|
|
324
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
325
|
+
}
|
|
326
|
+
isGlobal && (_names.watchAll = true);
|
|
327
|
+
return formValues;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
332
|
+
*
|
|
333
|
+
* @remarks
|
|
334
|
+
*
|
|
335
|
+
* [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```tsx
|
|
339
|
+
* const { watch } = useForm();
|
|
340
|
+
* const values = useWatch({
|
|
341
|
+
* name: "fieldName"
|
|
342
|
+
* control,
|
|
343
|
+
* })
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
function useWatch(props) {
|
|
347
|
+
const methods = useFormContext();
|
|
348
|
+
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
|
349
|
+
const _name = React.useRef(name);
|
|
350
|
+
_name.current = name;
|
|
351
|
+
useSubscribe({
|
|
352
|
+
disabled,
|
|
353
|
+
subject: control._subjects.values,
|
|
354
|
+
next: (formState) => {
|
|
355
|
+
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
|
356
|
+
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
|
|
361
|
+
React.useEffect(() => control._removeUnmounted());
|
|
362
|
+
return value;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
var isKey = (value) => /^\w*$/.test(value);
|
|
366
|
+
|
|
367
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
|
368
|
+
|
|
369
|
+
function set(object, path, value) {
|
|
370
|
+
let index = -1;
|
|
371
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
372
|
+
const length = tempPath.length;
|
|
373
|
+
const lastIndex = length - 1;
|
|
374
|
+
while (++index < length) {
|
|
375
|
+
const key = tempPath[index];
|
|
376
|
+
let newValue = value;
|
|
377
|
+
if (index !== lastIndex) {
|
|
378
|
+
const objValue = object[key];
|
|
379
|
+
newValue =
|
|
380
|
+
isObject(objValue) || Array.isArray(objValue)
|
|
381
|
+
? objValue
|
|
382
|
+
: !isNaN(+tempPath[index + 1])
|
|
383
|
+
? []
|
|
384
|
+
: {};
|
|
385
|
+
}
|
|
386
|
+
object[key] = newValue;
|
|
387
|
+
object = object[key];
|
|
388
|
+
}
|
|
389
|
+
return object;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
|
394
|
+
*
|
|
395
|
+
* @remarks
|
|
396
|
+
* [API](https://react-hook-form.com/docs/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
|
|
397
|
+
*
|
|
398
|
+
* @param props - the path name to the form field value, and validation rules.
|
|
399
|
+
*
|
|
400
|
+
* @returns field properties, field and form state. {@link UseControllerReturn}
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```tsx
|
|
404
|
+
* function Input(props) {
|
|
405
|
+
* const { field, fieldState, formState } = useController(props);
|
|
406
|
+
* return (
|
|
407
|
+
* <div>
|
|
408
|
+
* <input {...field} placeholder={props.name} />
|
|
409
|
+
* <p>{fieldState.isTouched && "Touched"}</p>
|
|
410
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
411
|
+
* </div>
|
|
412
|
+
* );
|
|
413
|
+
* }
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
function useController(props) {
|
|
417
|
+
const methods = useFormContext();
|
|
418
|
+
const { name, disabled, control = methods.control, shouldUnregister } = props;
|
|
419
|
+
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
420
|
+
const value = useWatch({
|
|
421
|
+
control,
|
|
422
|
+
name,
|
|
423
|
+
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
|
424
|
+
exact: true,
|
|
425
|
+
});
|
|
426
|
+
const formState = useFormState({
|
|
427
|
+
control,
|
|
428
|
+
name,
|
|
429
|
+
});
|
|
430
|
+
const _registerProps = React.useRef(control.register(name, {
|
|
431
|
+
...props.rules,
|
|
432
|
+
value,
|
|
433
|
+
}));
|
|
434
|
+
_registerProps.current = control.register(name, props.rules);
|
|
435
|
+
React.useEffect(() => {
|
|
436
|
+
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
437
|
+
const updateMounted = (name, value) => {
|
|
438
|
+
const field = get(control._fields, name);
|
|
439
|
+
if (field) {
|
|
440
|
+
field._f.mount = value;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
updateMounted(name, true);
|
|
444
|
+
if (_shouldUnregisterField) {
|
|
445
|
+
const value = cloneObject(get(control._options.defaultValues, name));
|
|
446
|
+
set(control._defaultValues, name, value);
|
|
447
|
+
if (isUndefined(get(control._formValues, name))) {
|
|
448
|
+
set(control._formValues, name, value);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return () => {
|
|
452
|
+
(isArrayField
|
|
453
|
+
? _shouldUnregisterField && !control._state.action
|
|
454
|
+
: _shouldUnregisterField)
|
|
455
|
+
? control.unregister(name)
|
|
456
|
+
: updateMounted(name, false);
|
|
457
|
+
};
|
|
458
|
+
}, [name, control, isArrayField, shouldUnregister]);
|
|
459
|
+
React.useEffect(() => {
|
|
460
|
+
control._updateDisabledField({
|
|
461
|
+
disabled,
|
|
462
|
+
fields: control._fields,
|
|
463
|
+
name,
|
|
464
|
+
});
|
|
465
|
+
}, [disabled, name, control]);
|
|
466
|
+
return {
|
|
467
|
+
field: {
|
|
468
|
+
name,
|
|
469
|
+
value,
|
|
470
|
+
disabled,
|
|
471
|
+
onChange: React.useCallback((event) => _registerProps.current.onChange({
|
|
472
|
+
target: {
|
|
473
|
+
value: getEventValue(event),
|
|
474
|
+
name: name,
|
|
475
|
+
},
|
|
476
|
+
type: EVENTS.CHANGE,
|
|
477
|
+
}), [name]),
|
|
478
|
+
onBlur: React.useCallback(() => _registerProps.current.onBlur({
|
|
479
|
+
target: {
|
|
480
|
+
value: get(control._formValues, name),
|
|
481
|
+
name: name,
|
|
482
|
+
},
|
|
483
|
+
type: EVENTS.BLUR,
|
|
484
|
+
}), [name, control]),
|
|
485
|
+
ref: (elm) => {
|
|
486
|
+
const field = get(control._fields, name);
|
|
487
|
+
if (field && elm) {
|
|
488
|
+
field._f.ref = {
|
|
489
|
+
focus: () => elm.focus(),
|
|
490
|
+
select: () => elm.select(),
|
|
491
|
+
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
492
|
+
reportValidity: () => elm.reportValidity(),
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
formState,
|
|
498
|
+
fieldState: Object.defineProperties({}, {
|
|
499
|
+
invalid: {
|
|
500
|
+
enumerable: true,
|
|
501
|
+
get: () => !!get(formState.errors, name),
|
|
502
|
+
},
|
|
503
|
+
isDirty: {
|
|
504
|
+
enumerable: true,
|
|
505
|
+
get: () => !!get(formState.dirtyFields, name),
|
|
506
|
+
},
|
|
507
|
+
isTouched: {
|
|
508
|
+
enumerable: true,
|
|
509
|
+
get: () => !!get(formState.touchedFields, name),
|
|
510
|
+
},
|
|
511
|
+
error: {
|
|
512
|
+
enumerable: true,
|
|
513
|
+
get: () => get(formState.errors, name),
|
|
514
|
+
},
|
|
515
|
+
}),
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
var _templateObject;
|
|
520
|
+
var RadioButtonLabel = styled.label(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n display: flex;\n margin-bottom: ", ";\n margin-right: ", ";\n"])), function (props) {
|
|
521
|
+
return props.theme.marginPaddings[0];
|
|
522
|
+
}, function (props) {
|
|
523
|
+
return props.theme.marginPaddings[0];
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
var _templateObject$1;
|
|
527
|
+
var RadioButtonText = styled.div(_templateObject$1 || (_templateObject$1 = _taggedTemplateLiteral(["\n display: flex;\n align-items: center;\n"])));
|
|
528
|
+
|
|
529
|
+
var _templateObject$2;
|
|
530
|
+
var RadioButtonCircle = styled.div(_templateObject$2 || (_templateObject$2 = _taggedTemplateLiteral(["\n height: 20px;\n width: 20px;\n border-radius: 50%;\n margin-right: 5px;\n border: 1px solid ", ";\n display: flex;\n"])), function (props) {
|
|
531
|
+
return props.theme.colors.base;
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
var _templateObject$3;
|
|
535
|
+
var RadioButtonCircleInner = styled.div(_templateObject$3 || (_templateObject$3 = _taggedTemplateLiteral(["\n height: 8px;\n width: 8px;\n border-radius: 50%;\n margin: auto;\n"])));
|
|
536
|
+
|
|
537
|
+
var _templateObject$4;
|
|
538
|
+
var RadioButtonInput = styled.input(_templateObject$4 || (_templateObject$4 = _taggedTemplateLiteral(["\n display: none;\n :checked ~ ", " {\n ", " {\n background-color: ", ";\n }\n }\n"])), RadioButtonCircle, RadioButtonCircleInner, function (props) {
|
|
539
|
+
return props.theme.colors.base;
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
var _excluded = ["ref"];
|
|
543
|
+
|
|
544
|
+
var _templateObject$5, _templateObject2;
|
|
545
|
+
var OptionsRow = styled.div(_templateObject$5 || (_templateObject$5 = _taggedTemplateLiteral(["\n display: flex;\n flex-wrap: nowrap;\n flex-direction: row;\n"])));
|
|
546
|
+
var Options = styled.div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n display: flex;\n flex-wrap: nowrap;\n flex-direction: row;\n flex-basis: 60%;\n"])));
|
|
547
|
+
|
|
548
|
+
var CommunicationPreferenceOptionsRenderer = function CommunicationPreferenceOptionsRenderer(_ref) {
|
|
549
|
+
var control = _ref.control,
|
|
550
|
+
name = _ref.name,
|
|
551
|
+
isDisabled = _ref.isDisabled,
|
|
552
|
+
options = _ref.options,
|
|
553
|
+
defaultValue = _ref.defaultValue,
|
|
554
|
+
setValue = _ref.setValue;
|
|
555
|
+
|
|
556
|
+
var _useController = useController({
|
|
557
|
+
name: name,
|
|
558
|
+
control: control,
|
|
559
|
+
defaultValue: defaultValue
|
|
560
|
+
}),
|
|
561
|
+
_useController$field = _useController.field,
|
|
562
|
+
ref = _useController$field.ref,
|
|
563
|
+
inputProps = _objectWithoutProperties(_useController$field, _excluded),
|
|
564
|
+
_useController$fieldS = _useController.fieldState,
|
|
565
|
+
invalid = _useController$fieldS.invalid,
|
|
566
|
+
error = _useController$fieldS.error;
|
|
567
|
+
|
|
568
|
+
useEffect(function () {
|
|
569
|
+
if (defaultValue) {
|
|
570
|
+
setValue(name, defaultValue);
|
|
571
|
+
}
|
|
572
|
+
}, [defaultValue, name, setValue]);
|
|
573
|
+
return /*#__PURE__*/React.createElement(OptionsRow, null, /*#__PURE__*/React.createElement(Options, null, options.map(function (option) {
|
|
574
|
+
return /*#__PURE__*/React.createElement(RadioButtonLabel, {
|
|
575
|
+
key: option.value,
|
|
576
|
+
htmlFor: option.value
|
|
577
|
+
}, /*#__PURE__*/React.createElement(RadioButtonInput, _extends({
|
|
578
|
+
id: option.value,
|
|
579
|
+
ref: ref,
|
|
580
|
+
type: "radio",
|
|
563
581
|
name: name,
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (defaultValue) {
|
|
576
|
-
setValue(name, defaultValue);
|
|
577
|
-
}
|
|
578
|
-
}, [defaultValue, name, setValue]);
|
|
579
|
-
return /*#__PURE__*/React__default.createElement(OptionsRow, null, /*#__PURE__*/React__default.createElement(Options, null, options.map(function (option) {
|
|
580
|
-
return /*#__PURE__*/React__default.createElement(RadioButtonLabel, {
|
|
581
|
-
key: option.value,
|
|
582
|
-
htmlFor: option.value
|
|
583
|
-
}, /*#__PURE__*/React__default.createElement(RadioButtonInput, _extends({
|
|
584
|
-
id: option.value,
|
|
585
|
-
ref: ref,
|
|
586
|
-
type: "radio",
|
|
587
|
-
name: name,
|
|
588
|
-
defaultChecked: inputProps.value === option.value.toString()
|
|
589
|
-
}, inputProps, {
|
|
590
|
-
disabled: isDisabled,
|
|
591
|
-
value: option.value
|
|
592
|
-
})), /*#__PURE__*/React__default.createElement(RadioButtonCircle, null, /*#__PURE__*/React__default.createElement(RadioButtonCircleInner, null)), /*#__PURE__*/React__default.createElement(RadioButtonText, null, option.label));
|
|
593
|
-
})), invalid && /*#__PURE__*/React__default.createElement("span", {
|
|
594
|
-
className: "form__error"
|
|
595
|
-
}, error === null || error === void 0 ? void 0 : error.message));
|
|
596
|
-
};
|
|
597
|
-
|
|
598
|
-
return CommunicationPreferenceOptionsRenderer;
|
|
599
|
-
|
|
600
|
-
})));
|
|
582
|
+
defaultChecked: inputProps.value === option.value.toString()
|
|
583
|
+
}, inputProps, {
|
|
584
|
+
disabled: isDisabled,
|
|
585
|
+
value: option.value
|
|
586
|
+
})), /*#__PURE__*/React.createElement(RadioButtonCircle, null, /*#__PURE__*/React.createElement(RadioButtonCircleInner, null)), /*#__PURE__*/React.createElement(RadioButtonText, null, option.label));
|
|
587
|
+
})), invalid && /*#__PURE__*/React.createElement("span", {
|
|
588
|
+
className: "form__error"
|
|
589
|
+
}, error === null || error === void 0 ? void 0 : error.message));
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
export default CommunicationPreferenceOptionsRenderer;
|