@opengeoweb/form-fields 9.16.0 → 9.18.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/index.esm.js CHANGED
@@ -2,13 +2,12 @@ import * as React from 'react';
2
2
  import React__default from 'react';
3
3
  import { FormControl, FormHelperText, InputLabel, Select, RadioGroup, TextField, InputAdornment } from '@mui/material';
4
4
  import { useFormContext, useController, useForm, FormProvider } from 'react-hook-form';
5
- import moment from 'moment';
6
5
  import { isEqual } from 'lodash';
7
- import moment$1 from 'moment-timezone';
6
+ import { dateUtils } from '@opengeoweb/shared';
8
7
  import { DateTimePicker } from '@mui/x-date-pickers';
9
8
  import { CalendarToday } from '@opengeoweb/theme';
10
- import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
11
9
  import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
10
+ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
12
11
 
13
12
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
14
13
 
@@ -1666,27 +1665,19 @@ const isEmpty = value => {
1666
1665
  }
1667
1666
  return false;
1668
1667
  };
1669
- const isValidDate = value => value ? moment(value).isValid() : true;
1670
- const isBefore = (ownDate, referenceDate) => ownDate ? moment.utc(ownDate) < moment.utc(referenceDate) : true;
1671
- const isAfter = (ownDate, otherDate) => ownDate ? moment.utc(ownDate).isAfter(moment.utc(otherDate)) : true;
1672
- const isBetween = (ownDate, otherDateStart, otherDateEnd) => {
1673
- return moment.utc(ownDate) >= moment.utc(otherDateStart) && moment.utc(ownDate) <= moment.utc(otherDateEnd);
1674
- };
1675
1668
  const isXHoursBefore = (ownDate, otherDate, hours = 4) => {
1676
1669
  if (isEmpty(ownDate) || isEmpty(otherDate)) {
1677
1670
  return true;
1678
1671
  }
1679
- const duration = moment.duration(moment.utc(ownDate).diff(otherDate));
1680
- const inHours = duration.asHours();
1681
- return inHours >= -hours;
1672
+ const duration = dateUtils.differenceInHours(dateUtils.utc(otherDate), dateUtils.utc(ownDate), 'ceil');
1673
+ return duration <= hours;
1682
1674
  };
1683
1675
  const isXHoursAfter = (ownDate, otherDate, hours = 4) => {
1684
1676
  if (isEmpty(ownDate) || isEmpty(otherDate)) {
1685
1677
  return true;
1686
1678
  }
1687
- const duration = moment.duration(moment.utc(ownDate).diff(otherDate));
1688
- const inHours = duration.asHours();
1689
- return inHours <= hours;
1679
+ const duration = dateUtils.differenceInHours(dateUtils.utc(ownDate), dateUtils.utc(otherDate), 'ceil');
1680
+ return duration <= hours;
1690
1681
  };
1691
1682
  const isLatitude = lat => {
1692
1683
  if (!lat) {
@@ -2978,9 +2969,11 @@ const ReactHookFormNumberField = _a => {
2978
2969
  const before = inputElement.value.slice(0, inputElement.selectionStart);
2979
2970
  const after = inputElement.value.slice(inputElement.selectionEnd);
2980
2971
  const newValue = before + clipboardText + after;
2972
+ // eslint-disable-next-line no-useless-escape
2981
2973
  if (inputMode === 'numeric' && !newValue.match(/^\-?[0-9]+$/)) {
2982
2974
  event.preventDefault();
2983
2975
  }
2976
+ // eslint-disable-next-line no-useless-escape
2984
2977
  if (inputMode === 'decimal' && !newValue.match(/^\-?[0-9]*(\.[0-9]+)?$/)) {
2985
2978
  event.preventDefault();
2986
2979
  }
@@ -3040,14 +3033,24 @@ const ReactHookFormNumberField = _a => {
3040
3033
  }, otherProps)));
3041
3034
  };
3042
3035
 
3043
- const DEFAULT_DATE_FORMAT = 'DD/MM/YYYY HH:mm';
3044
- const DATE_FORMAT_FNS = 'dd/MM/yyyy HH:mm';
3045
3036
  const OpenPicker = () => /*#__PURE__*/React.createElement(CalendarToday, null);
3046
- // TODO: Place this default setting somewhere more appropriate, should only need to fire once https://gitlab.com/opengeoweb/opengeoweb/-/issues/331
3047
- moment$1.tz.setDefault('Etc/GMT-0');
3037
+ const getTimezoneOffset = value => value.getTimezoneOffset() * 60000;
3038
+ const makeLocalAppearUTC = value => new Date(value.getTime() + getTimezoneOffset(value));
3039
+ const localToUTC = dateTime => new Date(dateTime.getTime() - getTimezoneOffset(dateTime));
3048
3040
  const getFormattedValue = value => {
3049
- const isMoment = moment$1(value).isValid();
3050
- return isMoment ? moment$1.utc(value).format() : value;
3041
+ var _a;
3042
+ return (_a = dateUtils.dateToIsoString(value)) !== null && _a !== void 0 ? _a : null;
3043
+ };
3044
+ const getDateValue = value => {
3045
+ var _a, _b;
3046
+ if (value) {
3047
+ const dateValue = (_b = (_a = dateUtils.stringToDate(value, 'yyyy/MM/dd HH:mm', false)) !== null && _a !== void 0 ? _a : dateUtils.isoStringToDate(value, false)) !== null && _b !== void 0 ? _b : null;
3048
+ if (dateValue) {
3049
+ return makeLocalAppearUTC(dateValue);
3050
+ }
3051
+ return null;
3052
+ }
3053
+ return null;
3051
3054
  };
3052
3055
  const ReactHookKeyboardDateTimePicker = _a => {
3053
3056
  var {
@@ -3055,7 +3058,7 @@ const ReactHookKeyboardDateTimePicker = _a => {
3055
3058
  rules,
3056
3059
  disabled,
3057
3060
  label = 'Select date and time',
3058
- format = DEFAULT_DATE_FORMAT,
3061
+ format = dateUtils.DATE_FORMAT_DATEPICKER,
3059
3062
  openTo = 'hours',
3060
3063
  defaultNullValue = null,
3061
3064
  helperText = '',
@@ -3064,9 +3067,11 @@ const ReactHookKeyboardDateTimePicker = _a => {
3064
3067
  sx,
3065
3068
  isReadOnly,
3066
3069
  inputAdornment,
3067
- shouldHideUTC
3070
+ shouldHideUTC,
3071
+ disablePast,
3072
+ disableFuture
3068
3073
  } = _a,
3069
- otherProps = __rest(_a, ["name", "rules", "disabled", "label", "format", "openTo", "defaultNullValue", "helperText", "onChange", "className", "sx", "isReadOnly", "inputAdornment", "shouldHideUTC"]);
3074
+ otherProps = __rest(_a, ["name", "rules", "disabled", "label", "format", "openTo", "defaultNullValue", "helperText", "onChange", "className", "sx", "isReadOnly", "inputAdornment", "shouldHideUTC", "disablePast", "disableFuture"]);
3070
3075
  const {
3071
3076
  control
3072
3077
  } = useFormContext();
@@ -3086,8 +3091,11 @@ const ReactHookKeyboardDateTimePicker = _a => {
3086
3091
  defaultValue: defaultNullValue
3087
3092
  }, defaultProps));
3088
3093
  const errors = getErrors(name, formErrors);
3089
- // Ensure value is a Moment object
3090
- const dateValue = value ? moment$1(value) : null;
3094
+ // Ensure value is a Date object
3095
+ const dateValue = getDateValue(value);
3096
+ const now = makeLocalAppearUTC(dateUtils.utc());
3097
+ const minDateTime = disablePast ? now : null;
3098
+ const maxDateTime = disableFuture ? now : null;
3091
3099
  return /*#__PURE__*/React.createElement(ReactHookFormFormControl, {
3092
3100
  disabled: disabled,
3093
3101
  errors: errors,
@@ -3103,8 +3111,14 @@ const ReactHookKeyboardDateTimePicker = _a => {
3103
3111
  openTo: openTo,
3104
3112
  disabled: disabled,
3105
3113
  onChange: value => {
3106
- onChangeField(getFormattedValue(value));
3107
- onChange(getFormattedValue(value));
3114
+ if (!value) {
3115
+ onChange(null);
3116
+ onChangeField(null);
3117
+ return;
3118
+ }
3119
+ const formattedDate = getFormattedValue(localToUTC(value));
3120
+ onChangeField(formattedDate);
3121
+ onChange(formattedDate);
3108
3122
  },
3109
3123
  inputRef: ref,
3110
3124
  slotProps: {
@@ -3143,7 +3157,9 @@ const ReactHookKeyboardDateTimePicker = _a => {
3143
3157
  },
3144
3158
  timeSteps: {
3145
3159
  minutes: 1
3146
- }
3160
+ },
3161
+ minDateTime: minDateTime,
3162
+ maxDateTime: maxDateTime
3147
3163
  }, otherProps)));
3148
3164
  };
3149
3165
 
@@ -3165,7 +3181,7 @@ const ReactHookFormProviderWrapper = ({
3165
3181
  children,
3166
3182
  options: _options2 = defaultFormOptions
3167
3183
  }) => ( /*#__PURE__*/React__default.createElement(LocalizationProvider, {
3168
- dateAdapter: AdapterMoment
3184
+ dateAdapter: AdapterDateFns
3169
3185
  }, /*#__PURE__*/React__default.createElement(ReactHookFormProvider, {
3170
3186
  options: _options2
3171
3187
  }, children)));
@@ -3228,4 +3244,4 @@ const useDraftFormHelpers = (isDefaultDraft = false) => {
3228
3244
  };
3229
3245
  };
3230
3246
 
3231
- export { DATE_FORMAT_FNS, HIDDEN_INPUT_HELPER_IS_DRAFT, ReactHookKeyboardDateTimePicker as ReactHookFormDateTime, ReactHookFormFormControl, ReactHookFormHiddenInput, ReactHookFormNumberField, ReactHookFormProviderWrapper as ReactHookFormProvider, ReactHookFormRadioGroup, ReactHookFormSelect, ReactHookFormTextField, defaultFormOptions, errorMessages, getDeepProperty, getFormattedValue, hasIntersectionWithFIR, hasMaxFeaturePoints, hasMulitpleIntersections, isAfter, isBefore, isBetween, isEmpty, isGeometryDirty, isInteger, isLatitude, isLongitude, isMaximumOneDrawing, isNonOrBothCoordinates, isValidDate, isValidGeoJsonCoordinates, isValidMax, isValidMin, isXHoursAfter, isXHoursBefore, useDraftFormHelpers };
3247
+ export { HIDDEN_INPUT_HELPER_IS_DRAFT, ReactHookKeyboardDateTimePicker as ReactHookFormDateTime, ReactHookFormFormControl, ReactHookFormHiddenInput, ReactHookFormNumberField, ReactHookFormProviderWrapper as ReactHookFormProvider, ReactHookFormRadioGroup, ReactHookFormSelect, ReactHookFormTextField, defaultFormOptions, errorMessages, getDateValue, getDeepProperty, getFormattedValue, hasIntersectionWithFIR, hasMaxFeaturePoints, hasMulitpleIntersections, isEmpty, isGeometryDirty, isInteger, isLatitude, isLongitude, isMaximumOneDrawing, isNonOrBothCoordinates, isValidGeoJsonCoordinates, isValidMax, isValidMin, isXHoursAfter, isXHoursBefore, useDraftFormHelpers };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeoweb/form-fields",
3
- "version": "9.16.0",
3
+ "version": "9.18.0",
4
4
  "description": "GeoWeb form-fields library for the opengeoweb project",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -11,10 +11,9 @@
11
11
  "@mui/material": "~5.15.11",
12
12
  "@opengeoweb/theme": "*",
13
13
  "react-hook-form": "^7.50.1",
14
- "moment": "^2.29.4",
15
- "moment-timezone": "^0.5.43",
16
14
  "@mui/x-date-pickers": "^6.18.5",
17
- "lodash": "^4.17.21"
15
+ "lodash": "^4.17.21",
16
+ "@opengeoweb/shared": "9.18.0"
18
17
  },
19
18
  "peerDependencies": {
20
19
  "react": "18"
@@ -1,14 +1,13 @@
1
1
  import * as React from 'react';
2
2
  import { SxProps, Theme } from '@mui/material';
3
- import { Moment } from 'moment-timezone';
4
3
  import { DateTimePickerProps, DateTimePickerSlotsComponentsProps } from '@mui/x-date-pickers';
5
4
  import { ReactHookFormInput } from './types';
6
- export declare const DATE_FORMAT_FNS = "dd/MM/yyyy HH:mm";
7
- export declare const getFormattedValue: (value: Moment | string) => string | Moment;
8
- type ReactHookKeyboardDateTimePickerProps = DateTimePickerProps<Moment> & DateTimePickerSlotsComponentsProps<Moment> & ReactHookFormInput<{
5
+ export declare const getFormattedValue: (value: Date) => string | null;
6
+ export declare const getDateValue: (value: string) => Date | null;
7
+ type ReactHookKeyboardDateTimePickerProps = DateTimePickerProps<Date> & DateTimePickerSlotsComponentsProps<Date> & ReactHookFormInput<{
9
8
  defaultNullValue?: string | null;
10
9
  value?: string;
11
- onChange?: (value: string | Moment) => void;
10
+ onChange?: (value: Date | string | null) => void;
12
11
  format?: string;
13
12
  sx?: SxProps<Theme>;
14
13
  }> & {
@@ -8,5 +8,5 @@ export * from './ReactHookFormDateTime';
8
8
  export { default as ReactHookFormProvider } from './ReactHookFormProvider';
9
9
  export { default as ReactHookFormHiddenInput } from './ReactHookFormHiddenInput';
10
10
  export { defaultFormOptions } from './ReactHookFormProvider';
11
- export { errorMessages, isMaximumOneDrawing, isGeometryDirty, isValidGeoJsonCoordinates, hasIntersectionWithFIR, isValidMax, isValidMin, isInteger, isAfter, isBefore, isBetween, isValidDate, hasMaxFeaturePoints, hasMulitpleIntersections, isXHoursBefore, isXHoursAfter, isLatitude, isLongitude, isNonOrBothCoordinates, isEmpty, } from './utils';
11
+ export { errorMessages, isMaximumOneDrawing, isGeometryDirty, isValidGeoJsonCoordinates, hasIntersectionWithFIR, isValidMax, isValidMin, isInteger, hasMaxFeaturePoints, hasMulitpleIntersections, isLatitude, isLongitude, isNonOrBothCoordinates, isEmpty, isXHoursAfter, isXHoursBefore, } from './utils';
12
12
  export { getDeepProperty } from './formUtils';
@@ -1,4 +1,3 @@
1
- import { Moment } from 'moment';
2
1
  export declare const defaultProps: {
3
2
  shouldUnregister: boolean;
4
3
  };
@@ -19,10 +18,6 @@ export declare const errorMessages: {
19
18
  isNumeric: string;
20
19
  };
21
20
  export declare const isEmpty: (value?: number | string | null) => boolean;
22
- export declare const isValidDate: (value?: Moment | string | null) => boolean;
23
- export declare const isBefore: (ownDate: string, referenceDate: string) => boolean;
24
- export declare const isAfter: (ownDate: string, otherDate: string) => boolean;
25
- export declare const isBetween: (ownDate: string, otherDateStart: string, otherDateEnd: string) => boolean;
26
21
  export declare const isXHoursBefore: (ownDate: string, otherDate: string, hours?: number) => boolean;
27
22
  export declare const isXHoursAfter: (ownDate: string, otherDate: string, hours?: number) => boolean;
28
23
  export declare const isLatitude: (lat?: number | null) => boolean;