carbon-react 125.0.1 → 125.0.2

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.
@@ -22,13 +22,52 @@ import Logger from "../../__internal__/utils/logger";
22
22
  let deprecateUncontrolledWarnTriggered = false;
23
23
  export const ALLOWED_DATE_FORMATS = [["dd", "mm", "yyyy"], ["mm", "dd", "yyyy"], ["yyyy", "mm", "dd"], ["dd", "mm"], ["mm", "dd"], ["mm", "yyyy"]];
24
24
  const incorrectDateFormatMessage = "Forbidden prop dateFormat supplied to NumeralDate. " + "Only one of these date formats is allowed: " + "['dd', 'mm', 'yyyy'], " + "['mm', 'dd', 'yyyy'], " + "['yyyy', 'mm', 'dd'], " + "['dd', 'mm'], " + "['mm', 'dd'], " + "['mm', 'yyyy']";
25
- const isDayValid = day => day ? +day > 0 && +day < 32 : true;
26
- const isMonthValid = month => month ? +month > 0 && +month < 13 : true;
27
- const isYearValid = year => year ? +year > 1799 && +year < 2201 : true;
28
- const validations = {
29
- dd: isDayValid,
30
- mm: isMonthValid,
31
- yyyy: isYearValid
25
+ const getMonthsForLocale = localeName => {
26
+ const year = new Date().getFullYear();
27
+ const {
28
+ format
29
+ } = new Intl.DateTimeFormat(localeName, {
30
+ month: "long"
31
+ });
32
+ return [...Array(12).keys()].map(m => format(new Date(Date.UTC(year, m))));
33
+ };
34
+ const validationMessages = (locale, month, daysInMonth) => ({
35
+ dd: locale.numeralDate.validation.day(month ? getMonthsForLocale(locale.locale())[+month - 1] : undefined, daysInMonth),
36
+ mm: locale.numeralDate.validation.month(),
37
+ yyyy: locale.numeralDate.validation.year()
38
+ });
39
+ const getDaysInMonth = (month, year) => {
40
+ if (month && (+month > 12 || +month < 1)) {
41
+ return 31;
42
+ }
43
+ const currentDate = new Date();
44
+ const computedYear = +(year || currentDate.getFullYear());
45
+ const computedMonth = +(month || currentDate.getMonth() + 1);
46
+
47
+ // passing 0 as the third argument ensures we handle for months being 0 indexed
48
+ return new Date(computedYear, computedMonth, 0).getDate();
49
+ };
50
+ const validate = (locale, {
51
+ dd,
52
+ mm,
53
+ yyyy
54
+ }) => {
55
+ const failed = {
56
+ dd: "",
57
+ mm: "",
58
+ yyyy: ""
59
+ };
60
+ const daysInMonth = getDaysInMonth(mm, yyyy);
61
+ if (dd && (+dd > daysInMonth || +dd < 1)) {
62
+ failed.dd = validationMessages(locale, mm, String(daysInMonth)).dd;
63
+ }
64
+ if (mm && (+mm > 12 || +mm < 1)) {
65
+ failed.mm = validationMessages(locale).mm;
66
+ }
67
+ if (yyyy && (+yyyy < 1800 || +yyyy > 2200)) {
68
+ failed.yyyy = validationMessages(locale).yyyy;
69
+ }
70
+ return failed;
32
71
  };
33
72
  const getDateLabel = (datePart, locale) => {
34
73
  switch (datePart) {
@@ -99,11 +138,6 @@ export const NumeralDate = ({
99
138
  const modeSwitchedMessage = "Input elements should not switch from uncontrolled to controlled (or vice versa). " + "Decide between using a controlled or uncontrolled input element for the lifetime of the component";
100
139
  !(isControlled.current === (value !== undefined)) ? process.env.NODE_ENV !== "production" ? invariant(false, modeSwitchedMessage) : invariant(false) : void 0;
101
140
  }, [value]);
102
- const validationMessages = {
103
- dd: locale.numeralDate.validation.day(),
104
- mm: locale.numeralDate.validation.month(),
105
- yyyy: locale.numeralDate.validation.year()
106
- };
107
141
  const [dateValue, setDateValue] = useState({
108
142
  ...(initialValue || Object.fromEntries(dateFormat.map(datePart => [datePart, ""])))
109
143
  });
@@ -137,15 +171,13 @@ export const NumeralDate = ({
137
171
  }
138
172
  }
139
173
  };
140
- const handleBlur = datePart => {
174
+ const handleBlur = () => {
141
175
  const internalValidationEnabled = enableInternalError || enableInternalWarning;
142
176
  /* istanbul ignore else */
143
177
  if (internalValidationEnabled) {
144
- const newDatePart = dateValue[datePart];
145
- const errorMessage = validations[datePart](newDatePart) ? "" : validationMessages[datePart];
146
178
  setInternalMessages(prev => ({
147
179
  ...prev,
148
- [datePart]: errorMessage
180
+ ...validate(locale, dateValue)
149
181
  }));
150
182
  }
151
183
  setTimeout(() => {
@@ -239,6 +271,7 @@ export const NumeralDate = ({
239
271
  readOnly: readOnly,
240
272
  value: dateValue[datePart],
241
273
  onChange: e => handleChange(e, datePart),
274
+ onBlur: handleBlur,
242
275
  ref: element => {
243
276
  refs.current[index] = element;
244
277
  if (!inputRef) {
@@ -250,7 +283,6 @@ export const NumeralDate = ({
250
283
  inputRef.current = element;
251
284
  }
252
285
  },
253
- onBlur: () => handleBlur(datePart),
254
286
  error: !!internalError,
255
287
  warning: !!internalWarning,
256
288
  info: !!info
@@ -126,7 +126,12 @@ const plPL = {
126
126
  },
127
127
  numeralDate: {
128
128
  validation: {
129
- day: () => "Dzień musi być liczbą w zakresie 1-31.",
129
+ day: (month, daysInMonth) => {
130
+ if (month && daysInMonth) {
131
+ return `Dzień W ${month} musi być liczbą zakresie 1-${daysInMonth}.`;
132
+ }
133
+ return "Dzień musi być liczbą w zakresie 1-31.";
134
+ },
130
135
  month: () => "Miesiąć musi być liczbą w zakresie 1-12.",
131
136
  year: () => "Rok musi być liczbą w zakresie 1800-2200."
132
137
  },
@@ -91,7 +91,12 @@ const enGB = {
91
91
  },
92
92
  numeralDate: {
93
93
  validation: {
94
- day: () => "Day should be a number within a 1-31 range.",
94
+ day: (month, daysInMonth) => {
95
+ if (month && daysInMonth) {
96
+ return `Day in ${month} should be a number within 1-${daysInMonth}.`;
97
+ }
98
+ return "Day should be a number within a 1-31 range.";
99
+ },
95
100
  month: () => "Month should be a number within a 1-12 range.",
96
101
  year: () => "Year should be a number within a 1800-2200 range."
97
102
  },
@@ -74,7 +74,7 @@ interface Locale {
74
74
  };
75
75
  numeralDate: {
76
76
  validation: {
77
- day: () => string;
77
+ day: (month?: string, daysInMonth?: string) => string;
78
78
  month: () => string;
79
79
  year: () => string;
80
80
  };
@@ -31,13 +31,52 @@ function _extends() { _extends = Object.assign ? Object.assign.bind() : function
31
31
  let deprecateUncontrolledWarnTriggered = false;
32
32
  const ALLOWED_DATE_FORMATS = exports.ALLOWED_DATE_FORMATS = [["dd", "mm", "yyyy"], ["mm", "dd", "yyyy"], ["yyyy", "mm", "dd"], ["dd", "mm"], ["mm", "dd"], ["mm", "yyyy"]];
33
33
  const incorrectDateFormatMessage = "Forbidden prop dateFormat supplied to NumeralDate. " + "Only one of these date formats is allowed: " + "['dd', 'mm', 'yyyy'], " + "['mm', 'dd', 'yyyy'], " + "['yyyy', 'mm', 'dd'], " + "['dd', 'mm'], " + "['mm', 'dd'], " + "['mm', 'yyyy']";
34
- const isDayValid = day => day ? +day > 0 && +day < 32 : true;
35
- const isMonthValid = month => month ? +month > 0 && +month < 13 : true;
36
- const isYearValid = year => year ? +year > 1799 && +year < 2201 : true;
37
- const validations = {
38
- dd: isDayValid,
39
- mm: isMonthValid,
40
- yyyy: isYearValid
34
+ const getMonthsForLocale = localeName => {
35
+ const year = new Date().getFullYear();
36
+ const {
37
+ format
38
+ } = new Intl.DateTimeFormat(localeName, {
39
+ month: "long"
40
+ });
41
+ return [...Array(12).keys()].map(m => format(new Date(Date.UTC(year, m))));
42
+ };
43
+ const validationMessages = (locale, month, daysInMonth) => ({
44
+ dd: locale.numeralDate.validation.day(month ? getMonthsForLocale(locale.locale())[+month - 1] : undefined, daysInMonth),
45
+ mm: locale.numeralDate.validation.month(),
46
+ yyyy: locale.numeralDate.validation.year()
47
+ });
48
+ const getDaysInMonth = (month, year) => {
49
+ if (month && (+month > 12 || +month < 1)) {
50
+ return 31;
51
+ }
52
+ const currentDate = new Date();
53
+ const computedYear = +(year || currentDate.getFullYear());
54
+ const computedMonth = +(month || currentDate.getMonth() + 1);
55
+
56
+ // passing 0 as the third argument ensures we handle for months being 0 indexed
57
+ return new Date(computedYear, computedMonth, 0).getDate();
58
+ };
59
+ const validate = (locale, {
60
+ dd,
61
+ mm,
62
+ yyyy
63
+ }) => {
64
+ const failed = {
65
+ dd: "",
66
+ mm: "",
67
+ yyyy: ""
68
+ };
69
+ const daysInMonth = getDaysInMonth(mm, yyyy);
70
+ if (dd && (+dd > daysInMonth || +dd < 1)) {
71
+ failed.dd = validationMessages(locale, mm, String(daysInMonth)).dd;
72
+ }
73
+ if (mm && (+mm > 12 || +mm < 1)) {
74
+ failed.mm = validationMessages(locale).mm;
75
+ }
76
+ if (yyyy && (+yyyy < 1800 || +yyyy > 2200)) {
77
+ failed.yyyy = validationMessages(locale).yyyy;
78
+ }
79
+ return failed;
41
80
  };
42
81
  const getDateLabel = (datePart, locale) => {
43
82
  switch (datePart) {
@@ -108,11 +147,6 @@ const NumeralDate = ({
108
147
  const modeSwitchedMessage = "Input elements should not switch from uncontrolled to controlled (or vice versa). " + "Decide between using a controlled or uncontrolled input element for the lifetime of the component";
109
148
  !(isControlled.current === (value !== undefined)) ? process.env.NODE_ENV !== "production" ? (0, _invariant.default)(false, modeSwitchedMessage) : (0, _invariant.default)(false) : void 0;
110
149
  }, [value]);
111
- const validationMessages = {
112
- dd: locale.numeralDate.validation.day(),
113
- mm: locale.numeralDate.validation.month(),
114
- yyyy: locale.numeralDate.validation.year()
115
- };
116
150
  const [dateValue, setDateValue] = (0, _react.useState)({
117
151
  ...(initialValue || Object.fromEntries(dateFormat.map(datePart => [datePart, ""])))
118
152
  });
@@ -146,15 +180,13 @@ const NumeralDate = ({
146
180
  }
147
181
  }
148
182
  };
149
- const handleBlur = datePart => {
183
+ const handleBlur = () => {
150
184
  const internalValidationEnabled = enableInternalError || enableInternalWarning;
151
185
  /* istanbul ignore else */
152
186
  if (internalValidationEnabled) {
153
- const newDatePart = dateValue[datePart];
154
- const errorMessage = validations[datePart](newDatePart) ? "" : validationMessages[datePart];
155
187
  setInternalMessages(prev => ({
156
188
  ...prev,
157
- [datePart]: errorMessage
189
+ ...validate(locale, dateValue)
158
190
  }));
159
191
  }
160
192
  setTimeout(() => {
@@ -248,6 +280,7 @@ const NumeralDate = ({
248
280
  readOnly: readOnly,
249
281
  value: dateValue[datePart],
250
282
  onChange: e => handleChange(e, datePart),
283
+ onBlur: handleBlur,
251
284
  ref: element => {
252
285
  refs.current[index] = element;
253
286
  if (!inputRef) {
@@ -259,7 +292,6 @@ const NumeralDate = ({
259
292
  inputRef.current = element;
260
293
  }
261
294
  },
262
- onBlur: () => handleBlur(datePart),
263
295
  error: !!internalError,
264
296
  warning: !!internalWarning,
265
297
  info: !!info
@@ -133,7 +133,12 @@ const plPL = {
133
133
  },
134
134
  numeralDate: {
135
135
  validation: {
136
- day: () => "Dzień musi być liczbą w zakresie 1-31.",
136
+ day: (month, daysInMonth) => {
137
+ if (month && daysInMonth) {
138
+ return `Dzień W ${month} musi być liczbą zakresie 1-${daysInMonth}.`;
139
+ }
140
+ return "Dzień musi być liczbą w zakresie 1-31.";
141
+ },
137
142
  month: () => "Miesiąć musi być liczbą w zakresie 1-12.",
138
143
  year: () => "Rok musi być liczbą w zakresie 1800-2200."
139
144
  },
@@ -97,7 +97,12 @@ const enGB = {
97
97
  },
98
98
  numeralDate: {
99
99
  validation: {
100
- day: () => "Day should be a number within a 1-31 range.",
100
+ day: (month, daysInMonth) => {
101
+ if (month && daysInMonth) {
102
+ return `Day in ${month} should be a number within 1-${daysInMonth}.`;
103
+ }
104
+ return "Day should be a number within a 1-31 range.";
105
+ },
101
106
  month: () => "Month should be a number within a 1-12 range.",
102
107
  year: () => "Year should be a number within a 1800-2200 range."
103
108
  },
@@ -74,7 +74,7 @@ interface Locale {
74
74
  };
75
75
  numeralDate: {
76
76
  validation: {
77
- day: () => string;
77
+ day: (month?: string, daysInMonth?: string) => string;
78
78
  month: () => string;
79
79
  year: () => string;
80
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "125.0.1",
3
+ "version": "125.0.2",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "files": [
6
6
  "lib",