cozy-ui 114.0.1 → 115.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.
package/.bundlemonrc CHANGED
@@ -10,8 +10,7 @@
10
10
  "maxPercentIncrease": 10
11
11
  },
12
12
  {
13
- "path": "transpiled/react/stylesheet.css",
14
- "maxPercentIncrease": 10
13
+ "path": "transpiled/react/stylesheet.css"
15
14
  }
16
15
  ],
17
16
  "groups": [
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [115.0.1](https://github.com/cozy/cozy-ui/compare/v115.0.0...v115.0.1) (2024-12-20)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Ensure use correct format locales ([5d078db](https://github.com/cozy/cozy-ui/commit/5d078db)), closes [/github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20](https://github.com//github.com/date-fns/date-fns/blob/main/CHANGELOG.md/issues/200---2019-08-20)
7
+
8
+ # [115.0.0](https://github.com/cozy/cozy-ui/compare/v114.0.1...v115.0.0) (2024-12-19)
9
+
10
+
11
+ ### Features
12
+
13
+ * Upgrade date-fns to 2.30.0 ([a89c524](https://github.com/cozy/cozy-ui/commit/a89c524))
14
+
15
+
16
+ ### BREAKING CHANGES
17
+
18
+ * The date formatting function (`f` from `useI18n`) changes,
19
+ please refer to this doc.
20
+ https://date-fns.org/v2.30.0/docs/format
21
+
1
22
  ## [114.0.1](https://github.com/cozy/cozy-ui/compare/v114.0.0...v114.0.1) (2024-12-16)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui",
3
- "version": "114.0.1",
3
+ "version": "115.0.1",
4
4
  "description": "Cozy apps UI SDK",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -162,7 +162,7 @@
162
162
  "chart.js": "3.7.1",
163
163
  "classnames": "^2.2.5",
164
164
  "cozy-interapp": "^0.5.4",
165
- "date-fns": "^1.28.5",
165
+ "date-fns": "2.30.0",
166
166
  "filesize": "8.0.7",
167
167
  "hammerjs": "^2.0.8",
168
168
  "intersection-observer": "0.11.0",
@@ -22,7 +22,7 @@ const DumbHelloWorld = translate()(({ t, f, lang }) => (
22
22
  <div>
23
23
  {t('helloworld')}
24
24
  <br />
25
- {f('2020-01-06', 'DDD MMM')}
25
+ {f('2020-01-06', 'D LLL', { useAdditionalDayOfYearTokens: true })}
26
26
  <br />
27
27
  {lang}
28
28
  </div>
@@ -26,7 +26,7 @@ const MonthButton = ({ monthNum, onClick, isSelected }) => {
26
26
  )}
27
27
  onClick={handleClick}
28
28
  >
29
- {f(d, 'MMM')}
29
+ {f(d, 'LLL')}
30
30
  </button>
31
31
  )
32
32
  }
@@ -50,7 +50,7 @@ const DateMonthPicker = ({ initialValue, onSelect }) => {
50
50
 
51
51
  const handleClickMonth = month => {
52
52
  const d = new Date(year, month, 1)
53
- onSelect(format(d, 'YYYY-MM-DD'))
53
+ onSelect(format(d, 'yyyy-LL-dd'))
54
54
  }
55
55
  return (
56
56
  <div>
@@ -93,7 +93,7 @@ const dateMonthProp = function (props, propName, componentName) {
93
93
  '` supplied to' +
94
94
  ' `' +
95
95
  componentName +
96
- '`. Should be in the form YYYY-MM.'
96
+ '`. Should be in the form yyyy-LL.'
97
97
  )
98
98
  }
99
99
  }
@@ -51,7 +51,7 @@ const FilePickerBodyItem = ({
51
51
  const Input = multiple ? Checkbox : Radio
52
52
 
53
53
  const listItemSecondaryContent = isFile(item)
54
- ? `${f(item.updated_at, 'DD MMM YYYY')} - ${filesize(item.size, {
54
+ ? `${f(item.updated_at, 'dd LLL yyyy')} - ${filesize(item.size, {
55
55
  base: 10
56
56
  })}`
57
57
  : null
@@ -12,7 +12,7 @@ const SecondaryText = ({ secondary, file }) => {
12
12
  if (secondary) return secondary
13
13
 
14
14
  const date = file?.metadata?.datetime
15
- ? f(file?.metadata?.datetime, 'DD/MM/YYYY')
15
+ ? f(file?.metadata?.datetime, 'dd/LL/yyyy')
16
16
  : null
17
17
 
18
18
  return (
@@ -1,23 +1,43 @@
1
- import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
2
1
  import format from 'date-fns/format'
2
+ import formatDistanceToNow from 'date-fns/formatDistanceToNow'
3
3
 
4
4
  import { DEFAULT_LANG } from '.'
5
5
 
6
6
  const locales = {}
7
7
  let lang = DEFAULT_LANG
8
8
 
9
+ /**
10
+ * Ensure that the locale is in the correct format for date-fns (see 2.0.0 BC https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20)
11
+ * @param {string} lang
12
+ */
13
+ const ensureLocaleFormat = lang => {
14
+ switch (lang) {
15
+ case 'en':
16
+ return 'en-US'
17
+ case 'zh_cn':
18
+ return 'zh-CN'
19
+ case 'zh_tw':
20
+ return 'zh-TW'
21
+ default:
22
+ return lang
23
+ }
24
+ }
25
+
9
26
  const getWarningMessage = lang =>
10
27
  `The "${lang}" locale isn't supported by date-fns. or has not been included in the build. Check if you have configured a ContextReplacementPlugin that is too restrictive.`
11
28
 
12
29
  export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => {
13
- lang = userLang
30
+ lang = ensureLocaleFormat(userLang)
31
+ const ensureDefaultLang = ensureLocaleFormat(defaultLang)
14
32
  try {
15
- locales[defaultLang] = require(`date-fns/locale/${defaultLang}/index.js`)
33
+ locales[
34
+ ensureDefaultLang
35
+ ] = require(`date-fns/locale/${ensureDefaultLang}/index.js`)
16
36
  } catch (err) {
17
- console.warn(getWarningMessage(defaultLang))
37
+ console.warn(getWarningMessage(ensureDefaultLang))
18
38
  }
19
39
 
20
- if (lang && lang !== defaultLang) {
40
+ if (lang && lang !== ensureDefaultLang) {
21
41
  try {
22
42
  locales[lang] = require(`date-fns/locale/${lang}/index.js`)
23
43
  } catch (e) {
@@ -29,10 +49,13 @@ export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => {
29
49
 
30
50
  export const initFormat =
31
51
  (userLang, defaultLang = DEFAULT_LANG) =>
32
- (date, formatStr) => {
52
+ (date, formatStr, opts = {}) => {
33
53
  const locale = provideDateFnsLocale(userLang, defaultLang)
34
- return format(date, formatStr, { locale })
54
+ const ensureDate = date && typeof date === 'string' ? new Date(date) : date
55
+
56
+ return format(ensureDate, formatStr, { locale, ...opts })
35
57
  }
36
58
 
37
- export const formatLocallyDistanceToNow = date =>
38
- distanceInWordsToNow(date, { locale: locales[lang] })
59
+ export const formatLocallyDistanceToNow = date => {
60
+ return formatDistanceToNow(date, { locale: locales[lang] })
61
+ }
@@ -10,6 +10,18 @@ describe('initFormat', () => {
10
10
  it('should not throw if a date-fns locale can not be found', () => {
11
11
  expect(() => initFormat('unknown-lang', 'unknown-default')).not.toThrow()
12
12
  })
13
+
14
+ it('should use the correct locale', () => {
15
+ const f = initFormat('fr', 'en')
16
+ const date = f(0, 'yyyy-LLLL-dd')
17
+ expect(date).toBe('1970-janvier-01')
18
+ })
19
+
20
+ it('should fallback english if a date-fns locale can not be found', () => {
21
+ const f = initFormat('unknown-lang', 'unknown-default')
22
+ const date = f(0, 'yyyy-LLLL-dd')
23
+ expect(date).toBe('1970-January-01')
24
+ })
13
25
  })
14
26
 
15
27
  describe('formatLocallyDistanceToNow', () => {
@@ -10,7 +10,7 @@ const DumbI18nHelloWorld = ({ t, f, lang }) => (
10
10
  <div>
11
11
  {t('helloworld')}
12
12
  <br />
13
- {f('2020-01-06', 'DDD MMM')}
13
+ {f('2020-01-06', 'D LLL', { useAdditionalDayOfYearTokens: true })}
14
14
  <br />
15
15
  {lang}
16
16
  </div>
@@ -36,7 +36,7 @@ var MonthButton = function MonthButton(_ref) {
36
36
  return /*#__PURE__*/React.createElement("button", {
37
37
  className: cx(styles.DateMonthPicker__MonthButton, isSelected ? styles['DateMonthPicker__MonthButton--selected'] : null),
38
38
  onClick: handleClick
39
- }, f(d, 'MMM'));
39
+ }, f(d, 'LLL'));
40
40
  };
41
41
 
42
42
  var useCounter = function useCounter(initialValue, min, max) {
@@ -75,7 +75,7 @@ var DateMonthPicker = function DateMonthPicker(_ref2) {
75
75
 
76
76
  var handleClickMonth = function handleClickMonth(month) {
77
77
  var d = new Date(year, month, 1);
78
- onSelect(format(d, 'YYYY-MM-DD'));
78
+ onSelect(format(d, 'yyyy-LL-dd'));
79
79
  };
80
80
 
81
81
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
@@ -108,7 +108,7 @@ var DateMonthPicker = function DateMonthPicker(_ref2) {
108
108
 
109
109
  var dateMonthProp = function dateMonthProp(props, propName, componentName) {
110
110
  if (!/^[0-9]{4}-[0-9]{2}$/.test(props[propName])) {
111
- return new Error('Invalid prop `' + propName + '` supplied to' + ' `' + componentName + '`. Should be in the form YYYY-MM.');
111
+ return new Error('Invalid prop `' + propName + '` supplied to' + ' `' + componentName + '`. Should be in the form yyyy-LL.');
112
112
  }
113
113
  };
114
114
 
@@ -50,7 +50,7 @@ var FilePickerBodyItem = function FilePickerBodyItem(_ref) {
50
50
 
51
51
  var hasChoice = isValidFile(item, itemTypesAccepted) || isValidFolder(item, itemTypesAccepted);
52
52
  var Input = multiple ? Checkbox : Radio;
53
- var listItemSecondaryContent = isFile(item) ? "".concat(f(item.updated_at, 'DD MMM YYYY'), " - ").concat(filesize(item.size, {
53
+ var listItemSecondaryContent = isFile(item) ? "".concat(f(item.updated_at, 'dd LLL yyyy'), " - ").concat(filesize(item.size, {
54
54
  base: 10
55
55
  })) : null;
56
56
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ListItem, {
@@ -14,7 +14,7 @@ var SecondaryText = function SecondaryText(_ref) {
14
14
  f = _useI18n.f;
15
15
 
16
16
  if (secondary) return secondary;
17
- var date = file !== null && file !== void 0 && (_file$metadata = file.metadata) !== null && _file$metadata !== void 0 && _file$metadata.datetime ? f(file === null || file === void 0 ? void 0 : (_file$metadata2 = file.metadata) === null || _file$metadata2 === void 0 ? void 0 : _file$metadata2.datetime, 'DD/MM/YYYY') : null;
17
+ var date = file !== null && file !== void 0 && (_file$metadata = file.metadata) !== null && _file$metadata !== void 0 && _file$metadata.datetime ? f(file === null || file === void 0 ? void 0 : (_file$metadata2 = file.metadata) === null || _file$metadata2 === void 0 ? void 0 : _file$metadata2.datetime, 'dd/LL/yyyy') : null;
18
18
  return /*#__PURE__*/React.createElement(React.Fragment, null, date ? date : '', (isExpired(file) || isExpiringSoon(file)) && /*#__PURE__*/React.createElement(React.Fragment, null, date ? ' · ' : '', /*#__PURE__*/React.createElement(ExpirationAnnotation, {
19
19
  file: file
20
20
  })));
@@ -1,8 +1,34 @@
1
- import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
2
7
  import format from 'date-fns/format';
8
+ import formatDistanceToNow from 'date-fns/formatDistanceToNow';
3
9
  import { DEFAULT_LANG } from "cozy-ui/transpiled/react/providers/I18n";
4
10
  var locales = {};
5
11
  var lang = DEFAULT_LANG;
12
+ /**
13
+ * Ensure that the locale is in the correct format for date-fns (see 2.0.0 BC https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20)
14
+ * @param {string} lang
15
+ */
16
+
17
+ var ensureLocaleFormat = function ensureLocaleFormat(lang) {
18
+ switch (lang) {
19
+ case 'en':
20
+ return 'en-US';
21
+
22
+ case 'zh_cn':
23
+ return 'zh-CN';
24
+
25
+ case 'zh_tw':
26
+ return 'zh-TW';
27
+
28
+ default:
29
+ return lang;
30
+ }
31
+ };
6
32
 
7
33
  var getWarningMessage = function getWarningMessage(lang) {
8
34
  return "The \"".concat(lang, "\" locale isn't supported by date-fns. or has not been included in the build. Check if you have configured a ContextReplacementPlugin that is too restrictive.");
@@ -10,15 +36,16 @@ var getWarningMessage = function getWarningMessage(lang) {
10
36
 
11
37
  export var provideDateFnsLocale = function provideDateFnsLocale(userLang) {
12
38
  var defaultLang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LANG;
13
- lang = userLang;
39
+ lang = ensureLocaleFormat(userLang);
40
+ var ensureDefaultLang = ensureLocaleFormat(defaultLang);
14
41
 
15
42
  try {
16
- locales[defaultLang] = require("date-fns/locale/".concat(defaultLang, "/index.js"));
43
+ locales[ensureDefaultLang] = require("date-fns/locale/".concat(ensureDefaultLang, "/index.js"));
17
44
  } catch (err) {
18
- console.warn(getWarningMessage(defaultLang));
45
+ console.warn(getWarningMessage(ensureDefaultLang));
19
46
  }
20
47
 
21
- if (lang && lang !== defaultLang) {
48
+ if (lang && lang !== ensureDefaultLang) {
22
49
  try {
23
50
  locales[lang] = require("date-fns/locale/".concat(lang, "/index.js"));
24
51
  } catch (e) {
@@ -31,14 +58,16 @@ export var provideDateFnsLocale = function provideDateFnsLocale(userLang) {
31
58
  export var initFormat = function initFormat(userLang) {
32
59
  var defaultLang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LANG;
33
60
  return function (date, formatStr) {
61
+ var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
34
62
  var locale = provideDateFnsLocale(userLang, defaultLang);
35
- return format(date, formatStr, {
63
+ var ensureDate = date && typeof date === 'string' ? new Date(date) : date;
64
+ return format(ensureDate, formatStr, _objectSpread({
36
65
  locale: locale
37
- });
66
+ }, opts));
38
67
  };
39
68
  };
40
69
  export var formatLocallyDistanceToNow = function formatLocallyDistanceToNow(date) {
41
- return distanceInWordsToNow(date, {
70
+ return formatDistanceToNow(date, {
42
71
  locale: locales[lang]
43
72
  });
44
73
  };