@lumx/react 3.5.4-alpha-remove-moment.0 → 3.5.4-alpha-optimize-lumx-react-bundle.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.d.ts CHANGED
@@ -542,7 +542,7 @@ interface DatePickerProps extends GenericProps {
542
542
  /** Default month. */
543
543
  defaultMonth?: Date;
544
544
  /** Locale (language or region) to use. */
545
- locale?: string;
545
+ locale: string;
546
546
  /** Date after which dates can't be selected. */
547
547
  maxDate?: Date;
548
548
  /** Date before which dates can't be selected. */
@@ -597,7 +597,7 @@ interface DatePickerFieldProps extends GenericProps {
597
597
  /** Whether the component is disabled or not. */
598
598
  isDisabled?: boolean;
599
599
  /** Locale (language or region) to use. */
600
- locale?: string;
600
+ locale: string;
601
601
  /** Date after which dates can't be selected. */
602
602
  maxDate?: Date;
603
603
  /** Date before which dates can't be selected. */
package/index.js CHANGED
@@ -5,6 +5,9 @@ import isEmpty from 'lodash/isEmpty';
5
5
  import noop from 'lodash/noop';
6
6
  import get from 'lodash/get';
7
7
  import isFunction from 'lodash/isFunction';
8
+ import moment$1 from 'moment';
9
+ import range from 'lodash/range';
10
+ import { extendMoment } from 'moment-range';
8
11
  import last from 'lodash/last';
9
12
  import pull from 'lodash/pull';
10
13
  import { createPortal } from 'react-dom';
@@ -20,7 +23,6 @@ import isInteger from 'lodash/isInteger';
20
23
  import isObject from 'lodash/isObject';
21
24
  import take from 'lodash/take';
22
25
  import uniqueId from 'lodash/uniqueId';
23
- import range from 'lodash/range';
24
26
  import chunk from 'lodash/chunk';
25
27
  import isUndefined from 'lodash/isUndefined';
26
28
  import set from 'lodash/set';
@@ -2024,21 +2026,6 @@ CommentBlock.displayName = COMPONENT_NAME$c;
2024
2026
  CommentBlock.className = CLASSNAME$b;
2025
2027
  CommentBlock.defaultProps = DEFAULT_PROPS$b;
2026
2028
 
2027
- /**
2028
- * Add a number of months from a date while resetting the day to prevent month length mismatches.
2029
- */
2030
- function addMonthResetDay(date, monthOffset) {
2031
- const newDate = new Date(date.getTime());
2032
- newDate.setDate(1);
2033
- newDate.setMonth(date.getMonth() + monthOffset);
2034
- return newDate;
2035
- }
2036
-
2037
- /**
2038
- * Check if given date is valid.
2039
- */
2040
- const isDateValid = date => date instanceof Date && !Number.isNaN(date.getTime());
2041
-
2042
2029
  /**
2043
2030
  * Component display name.
2044
2031
  */
@@ -2049,169 +2036,56 @@ const COMPONENT_NAME$d = 'DatePicker';
2049
2036
  */
2050
2037
  const CLASSNAME$c = getRootClassName(COMPONENT_NAME$d);
2051
2038
 
2039
+ const moment = extendMoment(moment$1);
2040
+ const DAYS_PER_WEEK = 7;
2052
2041
  /**
2053
- * Get current browser locale.
2054
- */
2055
- const getCurrentLocale = () => {
2056
- var _navigator$languages;
2057
- return ((_navigator$languages = navigator.languages) === null || _navigator$languages === void 0 ? void 0 : _navigator$languages[0]) || navigator.language;
2058
- };
2059
-
2060
- /**
2061
- * Get the language part of a locale code.
2062
- * ex: `en-us` => `en`
2042
+ * Get the list of days in a week based on locale.
2043
+ *
2044
+ * @param locale The locale using to generate the order of days in a week.
2045
+ * @return The list of days in a week based on locale.
2063
2046
  */
2064
- const getLocaleLang = locale => locale.split('-')[0];
2065
-
2066
- /** Get first day of week for locale from the browser API */
2067
- const getFromBrowser = locale => {
2068
- try {
2069
- var _localeMetadata$getWe;
2070
- const localeMetadata = new Intl.Locale(locale);
2071
- const {
2072
- firstDay
2073
- } = ((_localeMetadata$getWe = localeMetadata.getWeekInfo) === null || _localeMetadata$getWe === void 0 ? void 0 : _localeMetadata$getWe.call(localeMetadata)) || localeMetadata.weekInfo;
2074
- // Sunday is represented as `0` in Date.getDay()
2075
- if (firstDay === 7) return 0;
2076
- return firstDay;
2077
- } catch (e) {
2078
- return undefined;
2079
- }
2080
- };
2081
-
2082
- /** List first day for each locale (could be removed when all browser implement Locale weekInfo) */
2083
- const FIRST_DAY_FOR_LOCALES = [{
2084
- // Locales with Sunday as the first day of the week
2085
- localeRX: /^(af|ar-(dz|eg|sa)|bn|cy|en-(ca|us|za)|fr-ca|gd|he|hi|ja|km|ko|pt-br|te|th|ug|zh-hk)$/i,
2086
- firstDay: 0
2087
- }, {
2088
- // Locales with Monday as the first day of the week
2089
- localeRX: /^(ar-(ma|tn)|az|be|bg|bs|ca|cs|da|de|el|en-(au|gb|ie|in|nz)|eo|es|et|eu|fi|fr|fy|gl|gu|hr|ht|hu|hy|id|is|it|ka|kk|kn|lb|lt|lv|mk|mn|ms|mt|nb|nl|nn|oc|pl|pt|ro|ru|sk|sl|sq|sr|sv|ta|tr|uk|uz|vi|zh-(cn|tw))$/i,
2090
- firstDay: 1
2091
- }, {
2092
- // Locales with Saturday as the first day of the week
2093
- localeRX: /^(ar|fa-ir)$/i,
2094
- firstDay: 6
2095
- }];
2096
-
2097
- /** Find first day of week for locale from the constant */
2098
- const getFromConstant = locale => {
2099
- // Search for locale (lang + region)
2100
- for (const {
2101
- localeRX,
2102
- firstDay
2103
- } of FIRST_DAY_FOR_LOCALES) {
2104
- if (localeRX.test(locale)) return firstDay;
2105
- }
2106
- // Fallback search for locale lang
2107
- const localeLang = getLocaleLang(locale);
2108
- if (localeLang !== locale) {
2109
- return getFromConstant(localeLang);
2110
- }
2111
- return undefined;
2112
- };
2047
+ function getWeekDays(locale) {
2048
+ return range(DAYS_PER_WEEK).map((_, i) => moment().locale(locale).weekday(i));
2049
+ }
2113
2050
 
2114
2051
  /**
2115
- * Get first day of the week for the given locale code (language + region).
2052
+ * Get month calendar based on locale and start date.
2053
+ *
2054
+ * @param locale The locale using to generate the order of days in a week.
2055
+ * @param selectedMonth The selected month.
2056
+ * @return The list of days in a week based on locale.
2116
2057
  */
2117
- const getFirstDayOfWeek = locale => {
2118
- // Get from browser API
2119
- const firstDay = getFromBrowser(locale);
2120
- if (firstDay !== undefined) return firstDay;
2121
-
2122
- // Get from constant
2123
- return getFromConstant(locale);
2124
- };
2125
-
2126
- const DAYS_PER_WEEK = 7;
2058
+ function getMonthCalendar(locale, selectedMonth) {
2059
+ const firstDayOfMonth = moment(selectedMonth).startOf('month');
2060
+ const endDayOfMonth = moment(selectedMonth).endOf('month');
2061
+ // The first day of the week depends on the locale used. In FR the first day is a monday but in EN the first day is sunday
2062
+ const firstDay = firstDayOfMonth.locale(locale).startOf('week');
2063
+ const monthRange = moment.range(firstDay.toDate(), endDayOfMonth.toDate());
2064
+ return Array.from(monthRange.by('day'));
2065
+ }
2127
2066
 
2128
2067
  /**
2129
- * List week days (based on locale) with the week day letter (ex: "M" for "Monday") and week day number
2130
- * (0-based index starting on Sunday).
2068
+ * Get month calendar based on locale and start date.
2069
+ * Each day is annotated to know if they are displayed and/or clickable.
2070
+ *
2071
+ * @param locale The locale using to generate the order of days in a week.
2072
+ * @param minDate The first selectable date.
2073
+ * @param maxDate The last selectable date.
2074
+ * @param selectedMonth The selected month.
2075
+ * @return The list of days in a week based on locale.
2131
2076
  */
2132
- const getWeekDays = function () {
2133
- var _getFirstDayOfWeek;
2134
- let locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getCurrentLocale();
2135
- let referenceDate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date();
2136
- const iterDate = new Date(referenceDate.getTime());
2137
- const firstDay = (_getFirstDayOfWeek = getFirstDayOfWeek(locale)) !== null && _getFirstDayOfWeek !== void 0 ? _getFirstDayOfWeek : 1;
2138
-
2139
- // Go to start of the week
2140
- const offset = firstDay - iterDate.getDay();
2141
- iterDate.setDate(iterDate.getDate() + offset);
2142
-
2143
- // Iterate through the days of the week
2144
- const weekDays = [];
2145
- for (let i = 0; i < DAYS_PER_WEEK; i++) {
2146
- // Single letter week day (ex: "M" for "Monday", "L" for "Lundi", etc.)
2147
- const letter = iterDate.toLocaleDateString(locale, {
2148
- weekday: 'narrow'
2149
- });
2150
- // Day number (1-based index starting on Monday)
2151
- const number = iterDate.getDay();
2152
- weekDays.push({
2153
- letter,
2154
- number
2155
- });
2156
- iterDate.setDate(iterDate.getDate() + 1);
2157
- }
2158
- return weekDays;
2159
- };
2160
-
2161
- /**
2162
- * Get month calendar.
2163
- * A list of weeks with days indexed by week day number
2164
- */
2165
- const getMonthCalendar = function () {
2166
- let locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getCurrentLocale();
2167
- let referenceDate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date();
2168
- let rangeMinDate = arguments.length > 2 ? arguments[2] : undefined;
2169
- let rangeMaxDate = arguments.length > 3 ? arguments[3] : undefined;
2170
- const month = referenceDate.getMonth();
2171
- const iterDate = new Date(referenceDate.getTime());
2172
- iterDate.setDate(1);
2173
- const weekDays = getWeekDays(locale, referenceDate);
2174
- const lastDayOfWeek = last(weekDays);
2175
- const weeks = [];
2176
- let week = {};
2177
- while (iterDate.getMonth() === month) {
2178
- const weekDayNumber = iterDate.getDay();
2179
- const day = {
2180
- date: new Date(iterDate.getTime())
2077
+ function getAnnotatedMonthCalendar(locale, minDate, maxDate, selectedMonth) {
2078
+ const month = moment(selectedMonth).locale(locale).month();
2079
+ const clickableRange = moment.range(minDate, maxDate);
2080
+ return getMonthCalendar(locale, selectedMonth).map(date => {
2081
+ return {
2082
+ date,
2083
+ isClickable: clickableRange.contains(date),
2084
+ isDisplayed: date.month() === month,
2085
+ isToday: date.isSame(moment(), 'day')
2181
2086
  };
2182
-
2183
- // If a range is specified, check if the day is out of range.
2184
- if (rangeMinDate && iterDate <= rangeMinDate || rangeMaxDate && iterDate >= rangeMaxDate) {
2185
- day.isOutOfRange = true;
2186
- }
2187
- week[weekDayNumber] = day;
2188
- if (weekDayNumber === lastDayOfWeek.number) {
2189
- weeks.push(week);
2190
- week = {};
2191
- }
2192
- iterDate.setDate(iterDate.getDate() + 1);
2193
- }
2194
- if (Object.keys(week).length) weeks.push(week);
2195
- return {
2196
- weeks,
2197
- weekDays
2198
- };
2199
- };
2200
-
2201
- /**
2202
- * Check `date1` is on the same day as `date2`.
2203
- */
2204
- const isSameDay = (date1, date2) => date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear();
2205
-
2206
- const formatMonthYear = (date, locale) => date.toLocaleDateString(locale, {
2207
- year: 'numeric',
2208
- month: 'long'
2209
- });
2210
-
2211
- /** Format date month day number */
2212
- const formatDayNumber = (date, locale) => date.toLocaleDateString(locale, {
2213
- day: 'numeric'
2214
- });
2087
+ });
2088
+ }
2215
2089
 
2216
2090
  /**
2217
2091
  * Defines the props of the component.
@@ -2231,7 +2105,7 @@ const COMPONENT_NAME$e = 'DatePickerControlled';
2231
2105
  */
2232
2106
  const DatePickerControlled = /*#__PURE__*/forwardRef((props, ref) => {
2233
2107
  const {
2234
- locale = getCurrentLocale(),
2108
+ locale,
2235
2109
  maxDate,
2236
2110
  minDate,
2237
2111
  nextButtonProps,
@@ -2243,12 +2117,12 @@ const DatePickerControlled = /*#__PURE__*/forwardRef((props, ref) => {
2243
2117
  todayOrSelectedDateRef,
2244
2118
  value
2245
2119
  } = props;
2246
- const {
2247
- weeks,
2248
- weekDays
2249
- } = React.useMemo(() => {
2250
- return getMonthCalendar(locale, selectedMonth, minDate, maxDate);
2120
+ const days = React.useMemo(() => {
2121
+ return getAnnotatedMonthCalendar(locale, minDate, maxDate, moment$1(selectedMonth));
2251
2122
  }, [locale, minDate, maxDate, selectedMonth]);
2123
+ const weekDays = React.useMemo(() => {
2124
+ return getWeekDays(locale);
2125
+ }, [locale]);
2252
2126
  return /*#__PURE__*/React.createElement("div", {
2253
2127
  ref: ref,
2254
2128
  className: `${CLASSNAME$c}`
@@ -2266,46 +2140,37 @@ const DatePickerControlled = /*#__PURE__*/forwardRef((props, ref) => {
2266
2140
  })),
2267
2141
  label: /*#__PURE__*/React.createElement("span", {
2268
2142
  className: `${CLASSNAME$c}__month`
2269
- }, formatMonthYear(selectedMonth, locale))
2143
+ }, moment$1(selectedMonth).locale(locale).format('MMMM YYYY'))
2270
2144
  }), /*#__PURE__*/React.createElement("div", {
2271
2145
  className: `${CLASSNAME$c}__calendar`
2272
2146
  }, /*#__PURE__*/React.createElement("div", {
2273
2147
  className: `${CLASSNAME$c}__week-days ${CLASSNAME$c}__days-wrapper`
2274
- }, weekDays.map(_ref => {
2275
- let {
2276
- letter,
2277
- number
2278
- } = _ref;
2279
- return /*#__PURE__*/React.createElement("div", {
2280
- key: number,
2281
- className: `${CLASSNAME$c}__day-wrapper`
2282
- }, /*#__PURE__*/React.createElement("span", {
2283
- className: `${CLASSNAME$c}__week-day`
2284
- }, letter.toLocaleUpperCase()));
2285
- })), /*#__PURE__*/React.createElement("div", {
2148
+ }, weekDays.map(weekDay => /*#__PURE__*/React.createElement("div", {
2149
+ key: weekDay.unix(),
2150
+ className: `${CLASSNAME$c}__day-wrapper`
2151
+ }, /*#__PURE__*/React.createElement("span", {
2152
+ className: `${CLASSNAME$c}__week-day`
2153
+ }, weekDay.format('dddd').slice(0, 1).toLocaleUpperCase())))), /*#__PURE__*/React.createElement("div", {
2286
2154
  className: `${CLASSNAME$c}__month-days ${CLASSNAME$c}__days-wrapper`
2287
- }, weeks.flatMap((week, weekIndex) => {
2288
- return weekDays.map((weekDay, dayIndex) => {
2289
- const {
2290
- date,
2291
- isOutOfRange
2292
- } = week[weekDay.number] || {};
2293
- const key = `${weekIndex}-${dayIndex}`;
2294
- const isToday = !isOutOfRange && date && isSameDay(date, new Date());
2295
- const isSelected = date && value && isSameDay(value, date);
2155
+ }, days.map(annotatedDate => {
2156
+ if (annotatedDate.isDisplayed) {
2296
2157
  return /*#__PURE__*/React.createElement("div", {
2297
- key: key,
2158
+ key: annotatedDate.date.unix(),
2298
2159
  className: `${CLASSNAME$c}__day-wrapper`
2299
- }, date && /*#__PURE__*/React.createElement("button", {
2300
- ref: isSelected || !value && isToday ? todayOrSelectedDateRef : null,
2160
+ }, /*#__PURE__*/React.createElement("button", {
2161
+ ref: value && annotatedDate.date.isSame(value, 'day') || !value && annotatedDate.isToday ? todayOrSelectedDateRef : null,
2301
2162
  className: classnames(`${CLASSNAME$c}__month-day`, {
2302
- [`${CLASSNAME$c}__month-day--is-selected`]: isSelected,
2303
- [`${CLASSNAME$c}__month-day--is-today`]: isToday
2163
+ [`${CLASSNAME$c}__month-day--is-selected`]: value && annotatedDate.date.isSame(value, 'day'),
2164
+ [`${CLASSNAME$c}__month-day--is-today`]: annotatedDate.isClickable && annotatedDate.isToday
2304
2165
  }),
2305
- disabled: isOutOfRange,
2166
+ disabled: !annotatedDate.isClickable,
2306
2167
  type: "button",
2307
- onClick: () => onChange(date)
2308
- }, /*#__PURE__*/React.createElement("span", null, formatDayNumber(date, locale))));
2168
+ onClick: () => onChange(moment$1(annotatedDate.date).toDate())
2169
+ }, /*#__PURE__*/React.createElement("span", null, annotatedDate.date.format('DD'))));
2170
+ }
2171
+ return /*#__PURE__*/React.createElement("div", {
2172
+ key: annotatedDate.date.unix(),
2173
+ className: `${CLASSNAME$c}__day-wrapper`
2309
2174
  });
2310
2175
  }))));
2311
2176
  });
@@ -2328,12 +2193,17 @@ const DatePicker = /*#__PURE__*/forwardRef((props, ref) => {
2328
2193
  onChange
2329
2194
  } = props,
2330
2195
  forwardedProps = _objectWithoutProperties(props, _excluded$f);
2331
- let referenceDate = value || defaultMonth || new Date();
2332
- if (!isDateValid(referenceDate)) {
2196
+ let castedValue;
2197
+ if (value) {
2198
+ castedValue = moment$1(value);
2199
+ } else if (defaultMonth) {
2200
+ castedValue = moment$1(defaultMonth);
2201
+ }
2202
+ if (castedValue && !castedValue.isValid()) {
2333
2203
  // eslint-disable-next-line no-console
2334
- console.warn(`[@lumx/react/DatePicker] Invalid date provided ${referenceDate}`);
2335
- referenceDate = new Date();
2204
+ console.warn(`[@lumx/react/DatePicker] Invalid date provided ${castedValue}`);
2336
2205
  }
2206
+ const selectedDay = castedValue && castedValue.isValid() ? castedValue : moment$1();
2337
2207
  const [monthOffset, setMonthOffset] = useState(0);
2338
2208
  const setPrevMonth = () => setMonthOffset(monthOffset - 1);
2339
2209
  const setNextMonth = () => setMonthOffset(monthOffset + 1);
@@ -2341,7 +2211,7 @@ const DatePicker = /*#__PURE__*/forwardRef((props, ref) => {
2341
2211
  onChange(newDate);
2342
2212
  setMonthOffset(0);
2343
2213
  };
2344
- const selectedMonth = addMonthResetDay(referenceDate, monthOffset);
2214
+ const selectedMonth = moment$1(selectedDay).locale(locale).add(monthOffset, 'months').toDate();
2345
2215
  return /*#__PURE__*/React.createElement(DatePickerControlled, _extends({
2346
2216
  ref: ref
2347
2217
  }, forwardedProps, {
@@ -2499,15 +2369,6 @@ function useFocusTrap(focusZoneElement, focusElement) {
2499
2369
  }, [focusElement, focusZoneElement]);
2500
2370
  }
2501
2371
 
2502
- /**
2503
- * Standard date format based on locale.
2504
- */
2505
- const formatDate = (value, locale) => value.toLocaleDateString(locale, {
2506
- year: 'numeric',
2507
- month: 'long',
2508
- day: 'numeric'
2509
- });
2510
-
2511
2372
  const _excluded$g = ["defaultMonth", "disabled", "isDisabled", "locale", "maxDate", "minDate", "name", "nextButtonProps", "onChange", "previousButtonProps", "value"];
2512
2373
 
2513
2374
  /**
@@ -2531,7 +2392,7 @@ const DatePickerField = /*#__PURE__*/forwardRef((props, ref) => {
2531
2392
  defaultMonth,
2532
2393
  disabled,
2533
2394
  isDisabled = disabled,
2534
- locale = getCurrentLocale(),
2395
+ locale,
2535
2396
  maxDate,
2536
2397
  minDate,
2537
2398
  name,
@@ -2575,7 +2436,7 @@ const DatePickerField = /*#__PURE__*/forwardRef((props, ref) => {
2575
2436
  name: name,
2576
2437
  forceFocusStyle: isOpen,
2577
2438
  textFieldRef: anchorRef,
2578
- value: value ? formatDate(value, locale) : '',
2439
+ value: value ? moment$1(value).locale(locale).format('LL') : '',
2579
2440
  onClick: toggleSimpleMenu,
2580
2441
  onChange: onTextFieldChange,
2581
2442
  onKeyPress: handleKeyboardNav,