@opengeoweb/form-fields 2.0.1

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.
Files changed (34) hide show
  1. package/README.md +11 -0
  2. package/form-fields.esm.js +802 -0
  3. package/form-fields.umd.js +836 -0
  4. package/index.d.ts +1 -0
  5. package/lib/components/ReactHookFormDateTime.d.ts +13 -0
  6. package/lib/components/ReactHookFormDateTime.spec.d.ts +1 -0
  7. package/lib/components/ReactHookFormDateTime.stories.d.ts +6 -0
  8. package/lib/components/ReactHookFormFormControl.d.ts +8 -0
  9. package/lib/components/ReactHookFormFormControl.spec.d.ts +1 -0
  10. package/lib/components/ReactHookFormHiddenInput.d.ts +8 -0
  11. package/lib/components/ReactHookFormHiddenInput.spec.d.ts +1 -0
  12. package/lib/components/ReactHookFormHiddenInput.stories.d.ts +6 -0
  13. package/lib/components/ReactHookFormNumberField.d.ts +13 -0
  14. package/lib/components/ReactHookFormNumberField.spec.d.ts +1 -0
  15. package/lib/components/ReactHookFormNumberField.stories.d.ts +6 -0
  16. package/lib/components/ReactHookFormProvider.d.ts +20 -0
  17. package/lib/components/ReactHookFormProvider.spec.d.ts +1 -0
  18. package/lib/components/ReactHookFormProvider.stories.d.ts +6 -0
  19. package/lib/components/ReactHookFormRadioGroup.d.ts +10 -0
  20. package/lib/components/ReactHookFormRadioGroup.spec.d.ts +1 -0
  21. package/lib/components/ReactHookFormRadioGroup.stories.d.ts +6 -0
  22. package/lib/components/ReactHookFormSelect.d.ts +9 -0
  23. package/lib/components/ReactHookFormSelect.spec.d.ts +1 -0
  24. package/lib/components/ReactHookFormSelect.stories.d.ts +6 -0
  25. package/lib/components/ReactHookFormTextField.d.ts +10 -0
  26. package/lib/components/ReactHookFormTextField.spec.d.ts +1 -0
  27. package/lib/components/ReactHookFormTextField.stories.d.ts +6 -0
  28. package/lib/components/formUtils.d.ts +3 -0
  29. package/lib/components/formUtils.spec.d.ts +1 -0
  30. package/lib/components/index.d.ts +11 -0
  31. package/lib/components/types.d.ts +9 -0
  32. package/lib/components/utils.d.ts +42 -0
  33. package/lib/components/utils.spec.d.ts +1 -0
  34. package/package.json +27 -0
@@ -0,0 +1,802 @@
1
+ import * as React from 'react';
2
+ import React__default from 'react';
3
+ import { FormControl, FormHelperText, InputLabel, Select, RadioGroup, TextField, InputAdornment } from '@material-ui/core';
4
+ import { useFormContext, Controller, useForm, FormProvider } from 'react-hook-form';
5
+ import moment from 'moment';
6
+ import { isEqual } from 'lodash';
7
+ import { KeyboardDateTimePicker } from '@material-ui/pickers';
8
+ import moment$1 from 'moment-timezone';
9
+
10
+ /*! *****************************************************************************
11
+ Copyright (c) Microsoft Corporation.
12
+
13
+ Permission to use, copy, modify, and/or distribute this software for any
14
+ purpose with or without fee is hereby granted.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
+ PERFORMANCE OF THIS SOFTWARE.
23
+ ***************************************************************************** */
24
+
25
+ function __rest(s, e) {
26
+ var t = {};
27
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
28
+ t[p] = s[p];
29
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
30
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
31
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
32
+ t[p[i]] = s[p[i]];
33
+ }
34
+ return t;
35
+ }
36
+
37
+ /* *
38
+ * Licensed under the Apache License, Version 2.0 (the "License");
39
+ * you may not use this file except in compliance with the License.
40
+ * You may obtain a copy of the License at
41
+ *
42
+ * http://www.apache.org/licenses/LICENSE-2.0
43
+ *
44
+ * Unless required by applicable law or agreed to in writing, software
45
+ * distributed under the License is distributed on an "AS IS" BASIS,
46
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47
+ * See the License for the specific language governing permissions and
48
+ * limitations under the License.
49
+ *
50
+ * Copyright 2021 - Koninklijk Nederlands Meteorologisch Instituut (KNMI)
51
+ * Copyright 2021 - Finnish Meteorological Institute (FMI)
52
+ * */
53
+ var errorMessages = {
54
+ required: 'This field is required',
55
+ isValidDate: 'Not a valid date',
56
+ isAfter: 'Date should not be before',
57
+ isXHoursBefore: 'Date is too far in the past',
58
+ isXHoursAfter: 'Date is too far in the future',
59
+ isBefore: 'Timestamp too late',
60
+ minLevel: 'The minimum level is 1',
61
+ isLatitude: 'Invalid latitude, expected number with maximum two decimal degrees between -90.00 and 90.00',
62
+ isLongitude: 'Invalid longitude, expected number with maximum two decimal degrees between -180.00 and 180.00',
63
+ isValidMax: 'The maximum value is exceeded',
64
+ isLevelLower: 'The lower level has to be below the upper level',
65
+ isNonOrBothCoordinates: 'Please enter both coordinates',
66
+ isInteger: 'Invalid entry, enter a non-decimal number',
67
+ isNumeric: 'Invalid entry, enter a numeric value',
68
+ containsNoCommas: 'Please use a dot (.) as decimal separator',
69
+ minVisibility: 'The minimum visibility is 0',
70
+ maxVisibility: 'The maximum visibility is 4900'
71
+ };
72
+ var regexMaxTwoDecimals = /^\s*-?\d+(\.\d{1,2})?\s*$/; // validations
73
+
74
+ var isEmpty = function isEmpty(value) {
75
+ if (typeof value === 'string' && !value.trim().length || value === null || value === undefined) {
76
+ return true;
77
+ }
78
+
79
+ return false;
80
+ };
81
+ var isValidDate = function isValidDate(value) {
82
+ return value ? moment(value).isValid() : true;
83
+ };
84
+ var isBefore = function isBefore(ownDate, referenceDate) {
85
+ return ownDate ? moment.utc(ownDate) < moment.utc(referenceDate) : true;
86
+ };
87
+ var isAfter = function isAfter(ownDate, otherDate) {
88
+ return ownDate ? moment.utc(ownDate).isAfter(moment.utc(otherDate)) : true;
89
+ };
90
+ var isBetween = function isBetween(ownDate, otherDateStart, otherDateEnd) {
91
+ return moment.utc(ownDate) >= moment.utc(otherDateStart) && moment.utc(ownDate) <= moment.utc(otherDateEnd);
92
+ };
93
+ var isXHoursBefore = function isXHoursBefore(ownDate, otherDate) {
94
+ var hours = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 4;
95
+
96
+ if (isEmpty(ownDate) || isEmpty(otherDate)) {
97
+ return true;
98
+ }
99
+
100
+ var duration = moment.duration(moment.utc(ownDate).diff(otherDate));
101
+ var inHours = duration.asHours();
102
+ return inHours >= -hours;
103
+ };
104
+ var isXHoursAfter = function isXHoursAfter(ownDate, otherDate) {
105
+ var hours = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 4;
106
+
107
+ if (isEmpty(ownDate) || isEmpty(otherDate)) {
108
+ return true;
109
+ }
110
+
111
+ var duration = moment.duration(moment.utc(ownDate).diff(otherDate));
112
+ var inHours = duration.asHours();
113
+ return inHours <= hours;
114
+ };
115
+ var isLatitude = function isLatitude(lat) {
116
+ if (!lat) return true;
117
+ return isFinite(lat) && Math.abs(lat) <= 90 && regexMaxTwoDecimals.test(lat.toString());
118
+ };
119
+ var isLongitude = function isLongitude(lng) {
120
+ if (!lng) return true;
121
+ return isFinite(lng) && Math.abs(lng) <= 180 && lng >= -180 && regexMaxTwoDecimals.test(lng.toString());
122
+ };
123
+ var isValidMax = function isValidMax(value, maxValue) {
124
+ if (isEmpty(value)) {
125
+ return true;
126
+ }
127
+
128
+ return !(value > maxValue);
129
+ };
130
+ var isValidMin = function isValidMin(value, minValue) {
131
+ if (isEmpty(value)) {
132
+ return true;
133
+ }
134
+
135
+ return !(value < minValue);
136
+ };
137
+ var hasValidGeometry = function hasValidGeometry(geojson) {
138
+ if (!geojson || !geojson.features || !geojson.features.length || !geojson.features[0].geometry || // eslint-disable-next-line @typescript-eslint/ban-ts-comment
139
+ // @ts-ignore
140
+ !geojson.features[0].geometry.coordinates || !geojson.features[0].geometry.type) {
141
+ return false;
142
+ }
143
+
144
+ return true;
145
+ };
146
+ var isValidGeoJsonCoordinates = function isValidGeoJsonCoordinates(geojson) {
147
+ var hasGeometry = hasValidGeometry(geojson);
148
+
149
+ if (!hasGeometry) {
150
+ return false;
151
+ } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
152
+ // @ts-ignore
153
+
154
+
155
+ var _geojson$features$0$g = geojson.features[0].geometry,
156
+ coordinates = _geojson$features$0$g.coordinates,
157
+ type = _geojson$features$0$g.type; // For type POINT coordinates is an array of 2
158
+
159
+ if (coordinates.length === 2 && type === 'Point') {
160
+ return true;
161
+ } // For type POLYGON coordinates is an array of arrays
162
+
163
+
164
+ if (coordinates.length === 1 && coordinates[0].length > 0 && type === 'Polygon') {
165
+ return true;
166
+ }
167
+
168
+ return false;
169
+ };
170
+ var isMaximumOneDrawing = function isMaximumOneDrawing(geojson) {
171
+ var hasGeometry = hasValidGeometry(geojson);
172
+
173
+ if (!hasGeometry) {
174
+ return true; // no need to check further when there is no drawing yet
175
+ } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
176
+ // @ts-ignore
177
+
178
+
179
+ var _geojson$features$0$g2 = geojson.features[0].geometry,
180
+ coordinates = _geojson$features$0$g2.coordinates,
181
+ type = _geojson$features$0$g2.type; // For type POLYGON only one array of coordinates is allowed
182
+
183
+ if (type === 'Polygon' && coordinates.length > 1) {
184
+ return false;
185
+ }
186
+
187
+ return true;
188
+ };
189
+ var hasIntersectionWithFIR = function hasIntersectionWithFIR(geojson, intersection) {
190
+ if (!hasValidGeometry(geojson) || // eslint-disable-next-line @typescript-eslint/ban-ts-comment
191
+ // @ts-ignore
192
+ !geojson.features[0].geometry.coordinates.length || geojson.features[0].geometry.type === 'Polygon' && !geojson.features[0].geometry.coordinates[0].length) {
193
+ return true; // no need to check further when there is no drawing yet
194
+ } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
195
+ // @ts-ignore
196
+
197
+
198
+ var coordinates = intersection.features[0].geometry.coordinates;
199
+
200
+ if (coordinates.length > 0 && coordinates[0].length === 0) {
201
+ return false;
202
+ }
203
+
204
+ return true;
205
+ };
206
+ var hasMaxFeaturePoints = function hasMaxFeaturePoints(geojson) {
207
+ var maxPoints = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 7;
208
+ var hasGeometry = hasValidGeometry(geojson);
209
+
210
+ if (!hasGeometry) {
211
+ return false;
212
+ } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
213
+ // @ts-ignore
214
+
215
+
216
+ var _geojson$features$0$g3 = geojson.features[0].geometry,
217
+ coordinates = _geojson$features$0$g3.coordinates,
218
+ type = _geojson$features$0$g3.type;
219
+
220
+ if (type === 'Polygon' && coordinates[0].length > maxPoints) {
221
+ return true;
222
+ }
223
+
224
+ return false;
225
+ };
226
+ var hasMulitpleIntersections = function hasMulitpleIntersections(geojson) {
227
+ var hasGeometry = hasValidGeometry(geojson);
228
+
229
+ if (!hasGeometry) {
230
+ return false;
231
+ } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
232
+ // @ts-ignore
233
+
234
+
235
+ var _geojson$features$0$g4 = geojson.features[0].geometry,
236
+ coordinates = _geojson$features$0$g4.coordinates,
237
+ type = _geojson$features$0$g4.type;
238
+
239
+ if (type === 'MultiPolygon' || coordinates.length > 1 && type !== 'Point') {
240
+ return true;
241
+ }
242
+
243
+ return false;
244
+ };
245
+ var isGeometryDirty = function isGeometryDirty(geoJSON, formGeoJSON) {
246
+ if (geoJSON === (undefined) || formGeoJSON === (undefined) || !hasValidGeometry(geoJSON) || !hasValidGeometry(formGeoJSON)) {
247
+ return false;
248
+ }
249
+
250
+ return !isEqual( // eslint-disable-next-line @typescript-eslint/ban-ts-comment
251
+ // @ts-ignore
252
+ geoJSON.features[0].geometry.coordinates, // eslint-disable-next-line @typescript-eslint/ban-ts-comment
253
+ // @ts-ignore
254
+ formGeoJSON.features[0].geometry.coordinates);
255
+ };
256
+ var isInteger = function isInteger(value) {
257
+ if (isEmpty(value)) {
258
+ return true;
259
+ }
260
+
261
+ return Number.isInteger(value);
262
+ }; // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
263
+ var isNonOrBothCoordinates = function isNonOrBothCoordinates(coordinate, otherCoordinate) {
264
+ if (coordinate && otherCoordinate || !coordinate && !otherCoordinate) {
265
+ return true;
266
+ }
267
+
268
+ if (coordinate) {
269
+ return true;
270
+ }
271
+
272
+ return false;
273
+ };
274
+ var containsNoCommas = function containsNoCommas(val) {
275
+ if (typeof val === 'string' && val.includes(',')) {
276
+ return false;
277
+ }
278
+
279
+ return true;
280
+ };
281
+
282
+ var ReactHookFormFormControl = function ReactHookFormFormControl(_a) {
283
+ var children = _a.children,
284
+ errors = _a.errors,
285
+ props = __rest(_a, ["children", "errors"]);
286
+
287
+ return /*#__PURE__*/React.createElement(FormControl, Object.assign({
288
+ fullWidth: true
289
+ }, props, {
290
+ error: !!errors
291
+ }), children, errors && /*#__PURE__*/React.createElement(FormHelperText, {
292
+ variant: "filled"
293
+ }, errors.message || errorMessages[errors.type]));
294
+ };
295
+
296
+ function getDeepProperty(p, o) {
297
+ return p.reduce(function (xs, x) {
298
+ return xs && xs[x] ? xs[x] : null;
299
+ }, o);
300
+ }
301
+ var getErrors = function getErrors(name, errors) {
302
+ var nameAsArray = name.split('.');
303
+ return getDeepProperty(nameAsArray, errors);
304
+ };
305
+
306
+ var ReactHookFormSelect = function ReactHookFormSelect(_a) {
307
+ var name = _a.name,
308
+ label = _a.label,
309
+ _a$defaultValue = _a.defaultValue,
310
+ defaultValue = _a$defaultValue === void 0 ? '' : _a$defaultValue,
311
+ children = _a.children,
312
+ rules = _a.rules,
313
+ _a$onChange = _a.onChange,
314
+ _onChange = _a$onChange === void 0 ? function () {} : _a$onChange,
315
+ disabled = _a.disabled,
316
+ className = _a.className,
317
+ otherProps = __rest(_a, ["name", "label", "defaultValue", "children", "rules", "onChange", "disabled", "className"]);
318
+
319
+ var labelId = "".concat(name, "-label");
320
+ var inputRef = React.useRef(null);
321
+
322
+ var _useFormContext = useFormContext(),
323
+ control = _useFormContext.control,
324
+ formErrors = _useFormContext.errors;
325
+
326
+ var errors = getErrors(name, formErrors);
327
+ return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
328
+ className: className,
329
+ disabled: disabled,
330
+ errors: errors
331
+ }, /*#__PURE__*/React.createElement(InputLabel, {
332
+ variant: "filled",
333
+ id: labelId
334
+ }, label), /*#__PURE__*/React.createElement(Controller, {
335
+ render: function render(_props) {
336
+ return /*#__PURE__*/React.createElement(Select, Object.assign({
337
+ labelId: labelId,
338
+ label: label,
339
+ inputRef: inputRef,
340
+ name: name,
341
+ onChange: function onChange(changeEvent) {
342
+ _props.onChange(changeEvent.target.value); // default props
343
+
344
+
345
+ _onChange(changeEvent);
346
+ },
347
+ value: _props.value,
348
+ variant: "filled"
349
+ }, otherProps), children);
350
+ },
351
+ name: name,
352
+ control: control,
353
+ defaultValue: defaultValue,
354
+ rules: rules,
355
+ onFocus: function onFocus() {
356
+ var _a;
357
+
358
+ inputRef.current.disabled = false;
359
+ (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
360
+ }
361
+ }));
362
+ };
363
+
364
+ var ReactHookFormRadioGroup = function ReactHookFormRadioGroup(_a) {
365
+ var name = _a.name,
366
+ label = _a.label,
367
+ _a$defaultValue = _a.defaultValue,
368
+ defaultValue = _a$defaultValue === void 0 ? '' : _a$defaultValue,
369
+ children = _a.children,
370
+ rules = _a.rules,
371
+ _a$disabled = _a.disabled,
372
+ disabled = _a$disabled === void 0 ? false : _a$disabled,
373
+ _a$onChange = _a.onChange,
374
+ _onChange = _a$onChange === void 0 ? function () {
375
+ return null;
376
+ } : _a$onChange,
377
+ otherProps = __rest(_a, ["name", "label", "defaultValue", "children", "rules", "disabled", "onChange"]);
378
+
379
+ var labelId = "".concat(name, "-label");
380
+ var inputRef = React.useRef(null);
381
+
382
+ var _useFormContext = useFormContext(),
383
+ control = _useFormContext.control,
384
+ formErrors = _useFormContext.errors;
385
+
386
+ var errors = getErrors(name, formErrors);
387
+ return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
388
+ disabled: disabled,
389
+ errors: errors
390
+ }, /*#__PURE__*/React.createElement(InputLabel, {
391
+ id: labelId
392
+ }, label), /*#__PURE__*/React.createElement(Controller, {
393
+ render: function render(_props) {
394
+ return /*#__PURE__*/React.createElement(RadioGroup, Object.assign({
395
+ value: _props.value,
396
+ onChange: function onChange(changeEvent) {
397
+ _props.onChange(changeEvent.target.value);
398
+
399
+ _onChange(changeEvent, changeEvent.target.value);
400
+ },
401
+ innerRef: inputRef,
402
+ name: name
403
+ }, otherProps), children);
404
+ },
405
+ name: name,
406
+ control: control,
407
+ defaultValue: defaultValue,
408
+ rules: rules,
409
+ onFocus: function onFocus() {
410
+ // move focus to first radio on error
411
+ var firstElement = inputRef.current.querySelector('input');
412
+
413
+ if (firstElement) {
414
+ firstElement.disabled = false;
415
+ firstElement === null || firstElement === void 0 ? void 0 : firstElement.focus();
416
+ }
417
+ }
418
+ }));
419
+ };
420
+
421
+ var styles = {
422
+ textEditor: {
423
+ backgroundColor: 'rgba(0, 117, 169, 0.05)'
424
+ }
425
+ };
426
+ var convertTextInputValue = function convertTextInputValue(value, inCapitals) {
427
+ return inCapitals ? value.toUpperCase() : value;
428
+ };
429
+
430
+ var ReactHookFormTextField = function ReactHookFormTextField(_a) {
431
+ var name = _a.name,
432
+ label = _a.label,
433
+ _a$defaultValue = _a.defaultValue,
434
+ defaultValue = _a$defaultValue === void 0 ? '' : _a$defaultValue,
435
+ rules = _a.rules,
436
+ _a$disabled = _a.disabled,
437
+ disabled = _a$disabled === void 0 ? false : _a$disabled,
438
+ _a$upperCase = _a.upperCase,
439
+ upperCase = _a$upperCase === void 0 ? false : _a$upperCase,
440
+ _a$helperText = _a.helperText,
441
+ helperText = _a$helperText === void 0 ? '' : _a$helperText,
442
+ className = _a.className,
443
+ _a$onChange = _a.onChange,
444
+ _onChange = _a$onChange === void 0 ? function () {
445
+ return null;
446
+ } : _a$onChange,
447
+ otherProps = __rest(_a, ["name", "label", "defaultValue", "rules", "disabled", "upperCase", "helperText", "className", "onChange"]);
448
+
449
+ var inputRef = React.useRef(null);
450
+
451
+ var _useFormContext = useFormContext(),
452
+ control = _useFormContext.control,
453
+ formErrors = _useFormContext.errors;
454
+
455
+ var errors = getErrors(name, formErrors);
456
+ return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
457
+ className: className,
458
+ disabled: disabled,
459
+ errors: errors
460
+ }, /*#__PURE__*/React.createElement(Controller, {
461
+ render: function render(_props) {
462
+ return /*#__PURE__*/React.createElement(TextField, Object.assign({
463
+ label: label,
464
+ style: otherProps.multiline ? styles.textEditor : {},
465
+ error: !!errors,
466
+ helperText: helperText,
467
+ value: _props.value,
468
+ variant: "filled",
469
+ type: "text",
470
+ name: name,
471
+ onChange: function onChange(evt) {
472
+ var value = evt.target.value;
473
+ var convertedValue = convertTextInputValue(value, upperCase);
474
+
475
+ _props.onChange(convertedValue);
476
+
477
+ _onChange(null);
478
+ },
479
+ disabled: disabled,
480
+ inputRef: inputRef
481
+ }, otherProps));
482
+ },
483
+ name: name,
484
+ control: control,
485
+ defaultValue: defaultValue,
486
+ rules: rules,
487
+ disabled: disabled,
488
+ onFocus: function onFocus() {
489
+ var _a;
490
+
491
+ inputRef.current.disabled = false;
492
+ (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
493
+ }
494
+ }));
495
+ };
496
+
497
+ function _toConsumableArray(arr) {
498
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
499
+ }
500
+
501
+ function _arrayWithoutHoles(arr) {
502
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
503
+ }
504
+
505
+ function _iterableToArray(iter) {
506
+ if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
507
+ }
508
+
509
+ function _unsupportedIterableToArray(o, minLen) {
510
+ if (!o) return;
511
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
512
+ var n = Object.prototype.toString.call(o).slice(8, -1);
513
+ if (n === "Object" && o.constructor) n = o.constructor.name;
514
+ if (n === "Map" || n === "Set") return Array.from(o);
515
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
516
+ }
517
+
518
+ function _arrayLikeToArray(arr, len) {
519
+ if (len == null || len > arr.length) len = arr.length;
520
+
521
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
522
+
523
+ return arr2;
524
+ }
525
+
526
+ function _nonIterableSpread() {
527
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
528
+ }
529
+
530
+ var convertNumericInputValue = function convertNumericInputValue(value, inputMode) {
531
+ if (value !== '' && !isNaN(parseFloat(value))) {
532
+ // float is returned as string, option 'valueAsNumber: true' will take care of the parsing
533
+ return inputMode === 'numeric' ? parseInt(value, 10) : value;
534
+ }
535
+
536
+ return value;
537
+ };
538
+ var parseInput = function parseInput(value) {
539
+ // allow '-' at the start of the value
540
+ if (typeof value === 'string' && value.includes('-')) {
541
+ return value.split('-').reduce(function (list, el) {
542
+ if (!el.length && !list.length) {
543
+ return '-';
544
+ }
545
+
546
+ return list.concat(el);
547
+ }, '');
548
+ } // return value when '.' is given
549
+
550
+
551
+ if (typeof value === 'string' && value.includes('.')) {
552
+ return value;
553
+ }
554
+
555
+ return value == null || isNaN(value) ? '' : value.toString();
556
+ };
557
+ var getAllowedKeys = function getAllowedKeys() {
558
+ var inputMode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'numeric';
559
+ var allowedKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', 'Backspace', 'ArrowRight', 'ArrowLeft', 'Tab', 'Home', 'End', 'PageDown', 'PageUp', 'Delete'].concat(_toConsumableArray(inputMode === 'decimal' ? ['.'] : []));
560
+ return allowedKeys;
561
+ };
562
+
563
+ var ReactHookFormNumberField = function ReactHookFormNumberField(_a) {
564
+ var name = _a.name,
565
+ label = _a.label,
566
+ _a$defaultValue = _a.defaultValue,
567
+ defaultValue = _a$defaultValue === void 0 ? '' : _a$defaultValue,
568
+ rules = _a.rules,
569
+ _a$disabled = _a.disabled,
570
+ disabled = _a$disabled === void 0 ? false : _a$disabled,
571
+ _a$inputMode = _a.inputMode,
572
+ inputMode = _a$inputMode === void 0 ? 'numeric' : _a$inputMode,
573
+ _a$helperText = _a.helperText,
574
+ helperText = _a$helperText === void 0 ? '' : _a$helperText,
575
+ className = _a.className,
576
+ _a$onChange = _a.onChange,
577
+ _onChange = _a$onChange === void 0 ? function () {
578
+ return null;
579
+ } : _a$onChange,
580
+ otherProps = __rest(_a, ["name", "label", "defaultValue", "rules", "disabled", "inputMode", "helperText", "className", "onChange"]);
581
+
582
+ var inputRef = React.useRef(null);
583
+
584
+ var _useFormContext = useFormContext(),
585
+ control = _useFormContext.control,
586
+ formErrors = _useFormContext.errors;
587
+
588
+ var errors = getErrors(name, formErrors);
589
+ var validationRules = Object.assign(Object.assign({}, rules), {
590
+ setValueAs: function setValueAs(value) {
591
+ return parseFloat(value);
592
+ }
593
+ });
594
+ var allowedKeyes = getAllowedKeys(inputMode);
595
+
596
+ var onKeyDown = function onKeyDown(event) {
597
+ if (!allowedKeyes.includes(event.key) || // only allow one seperator
598
+ event.key === '.' && event.target.value.includes('.') || // only allow one -
599
+ event.key === '-' && event.target.value.includes('-')) {
600
+ event.preventDefault();
601
+ }
602
+ };
603
+
604
+ return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
605
+ className: className,
606
+ disabled: disabled,
607
+ errors: errors
608
+ }, /*#__PURE__*/React.createElement(Controller, {
609
+ render: function render(_props) {
610
+ return /*#__PURE__*/React.createElement(TextField, Object.assign({
611
+ label: label,
612
+ error: !!errors,
613
+ helperText: helperText,
614
+ inputMode: inputMode,
615
+ value: parseInput(_props.value),
616
+ variant: "filled",
617
+ type: "text",
618
+ inputProps: {
619
+ inputMode: inputMode
620
+ },
621
+ name: name,
622
+ onChange: function onChange(evt) {
623
+ _props.onChange(convertNumericInputValue(evt.target.value, inputMode));
624
+
625
+ _onChange(null);
626
+ },
627
+ onKeyDown: onKeyDown,
628
+ disabled: disabled,
629
+ inputRef: inputRef
630
+ }, otherProps));
631
+ },
632
+ name: name,
633
+ control: control,
634
+ defaultValue: defaultValue,
635
+ rules: validationRules,
636
+ disabled: disabled,
637
+ onFocus: function onFocus() {
638
+ var _a;
639
+
640
+ inputRef.current.disabled = false;
641
+ (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
642
+ }
643
+ }));
644
+ };
645
+
646
+ moment$1.tz.setDefault('Etc/GMT-0');
647
+ var getFormattedValue = function getFormattedValue(value) {
648
+ var isMoment = moment$1(value).isValid();
649
+ return isMoment ? moment$1.utc(value).format() : value;
650
+ };
651
+
652
+ var ReactHookKeyboardDateTimePicker = function ReactHookKeyboardDateTimePicker(_a) {
653
+ var name = _a.name,
654
+ rules = _a.rules,
655
+ disabled = _a.disabled,
656
+ _a$label = _a.label,
657
+ label = _a$label === void 0 ? 'Select date and time' : _a$label,
658
+ _a$format = _a.format,
659
+ format = _a$format === void 0 ? 'YYYY/MM/DD HH:mm' : _a$format,
660
+ _a$openTo = _a.openTo,
661
+ openTo = _a$openTo === void 0 ? 'hours' : _a$openTo,
662
+ _a$defaultNullValue = _a.defaultNullValue,
663
+ defaultNullValue = _a$defaultNullValue === void 0 ? null : _a$defaultNullValue,
664
+ _a$helperText = _a.helperText,
665
+ helperText = _a$helperText === void 0 ? '' : _a$helperText,
666
+ _a$onChange = _a.onChange,
667
+ _onChange = _a$onChange === void 0 ? function () {
668
+ return null;
669
+ } : _a$onChange,
670
+ className = _a.className,
671
+ otherProps = __rest(_a, ["name", "rules", "disabled", "label", "format", "openTo", "defaultNullValue", "helperText", "onChange", "className"]);
672
+
673
+ var inputRef = React.useRef(null);
674
+
675
+ var _useFormContext = useFormContext(),
676
+ control = _useFormContext.control,
677
+ formErrors = _useFormContext.errors,
678
+ getValues = _useFormContext.getValues;
679
+
680
+ var errors = getErrors(name, formErrors);
681
+ return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
682
+ disabled: disabled,
683
+ errors: errors,
684
+ className: className
685
+ }, /*#__PURE__*/React.createElement(Controller, {
686
+ render: function render(_props) {
687
+ return /*#__PURE__*/React.createElement(KeyboardDateTimePicker, Object.assign({
688
+ autoOk: true,
689
+ name: name,
690
+ ampm: false,
691
+ format: format,
692
+ label: label,
693
+ value: _props.value,
694
+ openTo: openTo,
695
+ error: !!errors,
696
+ disabled: disabled,
697
+ onChange: function onChange(value) {
698
+ _props.onChange(getFormattedValue(value));
699
+
700
+ _onChange(getFormattedValue(value));
701
+ },
702
+ invalidDateMessage: null,
703
+ inputVariant: "filled",
704
+ inputRef: inputRef,
705
+ InputAdornmentProps: {
706
+ position: 'start'
707
+ },
708
+ helperText: helperText,
709
+ InputProps: {
710
+ endAdornment: /*#__PURE__*/React.createElement(InputAdornment, {
711
+ style: {
712
+ paddingTop: '16px'
713
+ },
714
+ position: "end"
715
+ }, "UTC")
716
+ }
717
+ }, otherProps));
718
+ },
719
+ name: name,
720
+ control: control,
721
+ rules: rules,
722
+ defaultValue: getValues(name) || defaultNullValue,
723
+ onFocus: function onFocus() {
724
+ var _a;
725
+
726
+ if (inputRef.current) {
727
+ inputRef.current.disabled = false;
728
+ (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
729
+ }
730
+ }
731
+ }));
732
+ };
733
+
734
+ /* *
735
+ * Licensed under the Apache License, Version 2.0 (the "License");
736
+ * you may not use this file except in compliance with the License.
737
+ * You may obtain a copy of the License at
738
+ *
739
+ * http://www.apache.org/licenses/LICENSE-2.0
740
+ *
741
+ * Unless required by applicable law or agreed to in writing, software
742
+ * distributed under the License is distributed on an "AS IS" BASIS,
743
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
744
+ * See the License for the specific language governing permissions and
745
+ * limitations under the License.
746
+ *
747
+ * Copyright 2021 - Koninklijk Nederlands Meteorologisch Instituut (KNMI)
748
+ * Copyright 2021 - Finnish Meteorological Institute (FMI)
749
+ * */
750
+ var defaultFormOptions = {
751
+ mode: 'onChange',
752
+ reValidateMode: 'onChange'
753
+ };
754
+
755
+ var ReactHookFormProvider = function ReactHookFormProvider(_ref) {
756
+ var children = _ref.children,
757
+ _ref$options = _ref.options,
758
+ options = _ref$options === void 0 ? defaultFormOptions : _ref$options;
759
+ var formMethods = useForm(options);
760
+ return /*#__PURE__*/React__default.createElement(FormProvider, Object.assign({}, formMethods), children);
761
+ };
762
+
763
+ /* *
764
+ * Licensed under the Apache License, Version 2.0 (the "License");
765
+ * you may not use this file except in compliance with the License.
766
+ * You may obtain a copy of the License at
767
+ *
768
+ * http://www.apache.org/licenses/LICENSE-2.0
769
+ *
770
+ * Unless required by applicable law or agreed to in writing, software
771
+ * distributed under the License is distributed on an "AS IS" BASIS,
772
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
773
+ * See the License for the specific language governing permissions and
774
+ * limitations under the License.
775
+ *
776
+ * Copyright 2021 - Koninklijk Nederlands Meteorologisch Instituut (KNMI)
777
+ * Copyright 2021 - Finnish Meteorological Institute (FMI)
778
+ * */
779
+
780
+ var ReactHookFormHiddenInput = function ReactHookFormHiddenInput(_ref) {
781
+ var name = _ref.name,
782
+ _ref$defaultValue = _ref.defaultValue,
783
+ defaultValue = _ref$defaultValue === void 0 ? null : _ref$defaultValue,
784
+ rules = _ref.rules;
785
+
786
+ var _useFormContext = useFormContext(),
787
+ control = _useFormContext.control;
788
+
789
+ return /*#__PURE__*/React.createElement(Controller, {
790
+ render: function render(field) {
791
+ return field.value !== null ? /*#__PURE__*/React.createElement("input", Object.assign({}, field, {
792
+ type: "hidden"
793
+ })) : null;
794
+ },
795
+ name: name,
796
+ control: control,
797
+ defaultValue: defaultValue,
798
+ rules: rules
799
+ });
800
+ };
801
+
802
+ export { ReactHookKeyboardDateTimePicker as ReactHookFormDateTime, ReactHookFormFormControl, ReactHookFormHiddenInput, ReactHookFormNumberField, ReactHookFormProvider, ReactHookFormRadioGroup, ReactHookFormSelect, ReactHookFormTextField, containsNoCommas, defaultFormOptions, errorMessages, getDeepProperty, hasIntersectionWithFIR, hasMaxFeaturePoints, hasMulitpleIntersections, isAfter, isBefore, isBetween, isEmpty, isGeometryDirty, isInteger, isLatitude, isLongitude, isMaximumOneDrawing, isNonOrBothCoordinates, isValidDate, isValidGeoJsonCoordinates, isValidMax, isValidMin, isXHoursAfter, isXHoursBefore };