@transferwise/components 0.0.0-experimental-3ede9cc → 0.0.0-experimental-e477c03

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/build/index.js CHANGED
@@ -3188,8 +3188,10 @@ const DateInput = ({
3188
3188
  return null;
3189
3189
  };
3190
3190
  const [day, setDay] = React.useState(() => getInitialDate('day'));
3191
+ const [displayDay, setDisplayDay] = React.useState(day?.toString());
3191
3192
  const [month, setMonth] = React.useState(() => getInitialDate('month'));
3192
3193
  const [year, setYear] = React.useState(() => getInitialDate('year'));
3194
+ const [displayYear, setDisplayYear] = React.useState(year?.toString());
3193
3195
  const [lastBroadcastedValue, setLastBroadcastedValue] = React.useState(getDateObject);
3194
3196
  const monthNames = getMonthNames(locale, monthFormat);
3195
3197
  dayLabel ||= formatMessage(messages$9.dayLabel);
@@ -3215,8 +3217,8 @@ const DateInput = ({
3215
3217
  const getSelectElement = () => {
3216
3218
  return /*#__PURE__*/jsxRuntime.jsxs("label", {
3217
3219
  className: "d-flex flex-column",
3218
- children: [/*#__PURE__*/jsxRuntime.jsx("span", {
3219
- className: "sr-only",
3220
+ children: [/*#__PURE__*/jsxRuntime.jsx(Body, {
3221
+ type: exports.Typography.BODY_DEFAULT,
3220
3222
  children: monthLabel
3221
3223
  }), /*#__PURE__*/jsxRuntime.jsx(SelectInput, {
3222
3224
  name: "month",
@@ -3238,12 +3240,23 @@ const DateInput = ({
3238
3240
  })]
3239
3241
  });
3240
3242
  };
3243
+ const isDayValid = (newDay, newMonth, newYear) => {
3244
+ const maxDay = new Date(newYear, newMonth + 1, 0).getDate();
3245
+ return newDay <= maxDay;
3246
+ };
3241
3247
  const handleInternalValue = (newDay = day, newMonth = month, newYear = year) => {
3242
- if (newDay == null || newMonth == null || newYear == null) {
3248
+ if (newDay == null || newDay === 0 || newMonth == null || newYear == null || newYear === 0) {
3249
+ broadcastNewValue(null);
3250
+ return;
3251
+ }
3252
+ if (!isDayValid(newDay, newMonth, newYear)) {
3243
3253
  broadcastNewValue(null);
3244
3254
  return;
3245
3255
  }
3246
3256
  const dateValue = new Date(newYear, newMonth, newDay);
3257
+ if (newYear < 100) {
3258
+ dateValue.setFullYear(newYear);
3259
+ }
3247
3260
  if (!isDateValid(dateValue)) {
3248
3261
  broadcastNewValue(null);
3249
3262
  return;
@@ -3257,11 +3270,11 @@ const DateInput = ({
3257
3270
  }
3258
3271
  };
3259
3272
  const handleDayChange = event => {
3260
- const {
3261
- checkedDay
3262
- } = checkDate(Number.parseInt(event.target.value, 10), month, year);
3263
- setDay(checkedDay);
3264
- handleInternalValue(checkedDay, month, year);
3273
+ const newDayString = event.target.value.replace(/\D/g, '');
3274
+ const newDayNumber = Number.parseInt(newDayString, 10);
3275
+ setDay(newDayNumber);
3276
+ setDisplayDay(newDayString);
3277
+ handleInternalValue(newDayNumber, month, year);
3265
3278
  };
3266
3279
  const handleMonthChange = selectedMonth => {
3267
3280
  if (selectedMonth === null) {
@@ -3269,30 +3282,19 @@ const DateInput = ({
3269
3282
  handleInternalValue(day, null, year);
3270
3283
  return;
3271
3284
  }
3272
- const {
3273
- checkedDay
3274
- } = checkDate(day, selectedMonth, year);
3275
3285
  setMonth(selectedMonth);
3276
- if (day && checkedDay !== day) {
3277
- setDay(checkedDay);
3278
- }
3279
- handleInternalValue(checkedDay, selectedMonth, year);
3286
+ handleInternalValue(day, selectedMonth, year);
3280
3287
  };
3281
3288
  const handleYearChange = event => {
3282
- const newValue = event.target.value;
3283
- const slicedYear = newValue.length > 4 ? newValue.slice(0, 4) : newValue;
3284
- if (slicedYear.toString().length === 4) {
3285
- // Correct day based on year and month.
3286
- const {
3287
- checkedDay
3288
- } = checkDate(day, month, Number.parseInt(newValue, 10));
3289
- if (day && checkedDay !== day) {
3290
- setDay(checkedDay);
3291
- }
3292
- setYear(Number.parseInt(slicedYear, 10));
3293
- handleInternalValue(checkedDay, month, Number.parseInt(slicedYear, 10));
3289
+ const newYearString = event.target.value.replace(/\D/g, '');
3290
+ const newYearNumber = Number.parseInt(newYearString, 10);
3291
+ if (newYearString.length >= 4 && newYearString.length <= 6) {
3292
+ setYear(newYearNumber);
3293
+ setDisplayYear(newYearString);
3294
+ handleInternalValue(day, month, newYearNumber);
3294
3295
  } else {
3295
- setYear(Number.parseInt(slicedYear, 10));
3296
+ setYear(null);
3297
+ setDisplayYear(newYearString);
3296
3298
  handleInternalValue(day, month, null);
3297
3299
  }
3298
3300
  };
@@ -3302,24 +3304,6 @@ const DateInput = ({
3302
3304
  onChange(newValue != null ? getDateAsString(newValue) : null);
3303
3305
  }
3304
3306
  };
3305
- const checkDate = (newDay = null, newMonth = 0, newYear = null) => {
3306
- let checkedDay = newDay;
3307
- const maxDay = new Date(newYear || 2000, newMonth != null ? newMonth + 1 : 1, 0).getDate();
3308
- if (!newDay) {
3309
- checkedDay = null;
3310
- }
3311
- if (newDay && newDay < 0) {
3312
- checkedDay = 1;
3313
- }
3314
- if (newDay && newMonth || newDay && newDay > 31) {
3315
- checkedDay = newDay > maxDay ? maxDay : newDay;
3316
- }
3317
- return {
3318
- checkedDay,
3319
- checkedMonth: newMonth,
3320
- checkedYear: newYear
3321
- };
3322
- };
3323
3307
  const monthYearOnly = mode === exports.DateMode.MONTH_YEAR;
3324
3308
  const monthWidth = classNames__default.default({
3325
3309
  'col-sm-8': monthYearOnly,
@@ -3335,8 +3319,8 @@ const DateInput = ({
3335
3319
  return /*#__PURE__*/jsxRuntime.jsx("div", {
3336
3320
  className: "col-sm-3",
3337
3321
  children: /*#__PURE__*/jsxRuntime.jsxs("label", {
3338
- children: [/*#__PURE__*/jsxRuntime.jsx("span", {
3339
- className: "sr-only",
3322
+ children: [/*#__PURE__*/jsxRuntime.jsx(Body, {
3323
+ type: exports.Typography.BODY_DEFAULT,
3340
3324
  children: dayLabel
3341
3325
  }), /*#__PURE__*/jsxRuntime.jsx("div", {
3342
3326
  className: `input-group input-group-${size}`,
@@ -3346,10 +3330,12 @@ const DateInput = ({
3346
3330
  pattern: "[0-9]*",
3347
3331
  name: "day",
3348
3332
  autoComplete: dayAutoComplete,
3349
- value: day || '',
3333
+ value: displayDay || '',
3350
3334
  placeholder: placeholders?.day,
3351
3335
  disabled: disabled,
3352
3336
  min: 1,
3337
+ max: 31,
3338
+ maxLength: 2,
3353
3339
  onChange: event => handleDayChange(event)
3354
3340
  })
3355
3341
  })]
@@ -3360,8 +3346,8 @@ const DateInput = ({
3360
3346
  return /*#__PURE__*/jsxRuntime.jsx("div", {
3361
3347
  className: "col-sm-4",
3362
3348
  children: /*#__PURE__*/jsxRuntime.jsxs("label", {
3363
- children: [/*#__PURE__*/jsxRuntime.jsx("span", {
3364
- className: "sr-only",
3349
+ children: [/*#__PURE__*/jsxRuntime.jsx(Body, {
3350
+ type: exports.Typography.BODY_DEFAULT,
3365
3351
  children: yearLabel
3366
3352
  }), /*#__PURE__*/jsxRuntime.jsx("div", {
3367
3353
  className: `input-group input-group-${size}`,
@@ -3372,9 +3358,11 @@ const DateInput = ({
3372
3358
  name: "year",
3373
3359
  autoComplete: yearAutoComplete,
3374
3360
  placeholder: placeholders?.year,
3375
- value: year || '',
3361
+ value: displayYear || '',
3376
3362
  disabled: disabled,
3377
- min: 1,
3363
+ min: 0,
3364
+ max: 9999,
3365
+ maxLength: 6,
3378
3366
  onChange: event => handleYearChange(event)
3379
3367
  })
3380
3368
  })]
@@ -4937,14 +4925,14 @@ const Field = ({
4937
4925
  id,
4938
4926
  label,
4939
4927
  message: propMessage,
4940
- type: propType,
4928
+ sentiment: propType,
4941
4929
  className,
4942
4930
  children,
4943
4931
  ...props
4944
4932
  }) => {
4945
- const type = props.error ? exports.Sentiment.NEGATIVE : propType;
4933
+ const sentiment = props.error ? exports.Sentiment.NEGATIVE : propType;
4946
4934
  const message = props.error || props.hint || propMessage;
4947
- const hasError = type === exports.Sentiment.NEGATIVE;
4935
+ const hasError = sentiment === exports.Sentiment.NEGATIVE;
4948
4936
  const labelId = React.useId();
4949
4937
  const fallbackInputId = React.useId();
4950
4938
  const inputId = id !== null ? id ?? fallbackInputId : undefined;
@@ -4959,17 +4947,17 @@ const Field = ({
4959
4947
  value: hasError,
4960
4948
  children: /*#__PURE__*/jsxRuntime.jsxs("div", {
4961
4949
  className: classNames__default.default('form-group d-block', {
4962
- 'has-success': type === exports.Sentiment.POSITIVE,
4963
- 'has-warning': type === exports.Sentiment.WARNING,
4950
+ 'has-success': sentiment === exports.Sentiment.POSITIVE,
4951
+ 'has-warning': sentiment === exports.Sentiment.WARNING,
4964
4952
  'has-error': hasError,
4965
- 'has-info': type === exports.Sentiment.NEUTRAL
4953
+ 'has-info': sentiment === exports.Sentiment.NEUTRAL
4966
4954
  }, className),
4967
4955
  children: [/*#__PURE__*/jsxRuntime.jsxs(Label, {
4968
4956
  id: labelId,
4969
4957
  htmlFor: inputId,
4970
4958
  children: [label, children]
4971
4959
  }), message && /*#__PURE__*/jsxRuntime.jsx(InlineAlert, {
4972
- type: type,
4960
+ type: sentiment,
4973
4961
  id: descriptionId,
4974
4962
  children: message
4975
4963
  })]