@transferwise/components 0.0.0-experimental-c0c64e3 → 0.0.0-experimental-f3d17a6

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.
@@ -3564,6 +3564,28 @@ DateHeader.defaultProps = {
3564
3564
  };
3565
3565
  var DateHeader$1 = DateHeader;
3566
3566
 
3567
+ function getFocusableTime(isSelected, isNow, isDisabled, timeSpan) {
3568
+ let selected = null;
3569
+ let now = null;
3570
+ let disabled = null;
3571
+ for (const time of timeSpan) {
3572
+ if (isSelected(time)) {
3573
+ selected = time;
3574
+ break;
3575
+ }
3576
+ if (isNow(time)) {
3577
+ now = time;
3578
+ }
3579
+ if (!isDisabled(time) && disabled === null) {
3580
+ disabled = time;
3581
+ }
3582
+ if (selected) {
3583
+ break;
3584
+ }
3585
+ }
3586
+ return selected || now || disabled;
3587
+ }
3588
+
3567
3589
  // NB! Using with UTC timestamp (YYYY-MM-DD) might lead to unexpected results, for example
3568
3590
  // getStartOfDay(new Date('1995-01-01')) in Los Angeles returns 31 Dec 1994, but in
3569
3591
  // in Singapore it is 1 Jan 1995.
@@ -3698,31 +3720,16 @@ class DayCalendarTable extends PureComponent {
3698
3720
  } = this.props;
3699
3721
  return !!(selectedDate && +new Date(viewYear, viewMonth, day) === +selectedDate);
3700
3722
  };
3701
- isToday = (day, viewMonth, viewYear) => {
3723
+ isToday = day => {
3724
+ const {
3725
+ viewMonth,
3726
+ viewYear
3727
+ } = this.props;
3702
3728
  return +getStartOfDay(new Date()) === +new Date(viewYear, viewMonth, day);
3703
3729
  };
3704
- getAutofocusDay = (weeks, viewMonth, viewYear) => {
3705
- let activeDay = null;
3706
- let today = null;
3707
- let lowestNonDisabledDay = null;
3708
- for (const week of weeks) {
3709
- for (const day of week) {
3710
- if (this.isActive(day)) {
3711
- activeDay = day;
3712
- break;
3713
- }
3714
- if (this.isToday(day, viewMonth, viewYear)) {
3715
- today = day;
3716
- }
3717
- if (day && !this.isDisabled(day) && lowestNonDisabledDay === null) {
3718
- lowestNonDisabledDay = day;
3719
- }
3720
- }
3721
- if (activeDay !== null) {
3722
- break;
3723
- }
3724
- }
3725
- return activeDay || today || lowestNonDisabledDay;
3730
+ getAutofocusDay = weeks => {
3731
+ const days = weeks.flatMap(week => week);
3732
+ return getFocusableTime(this.isActive, this.isToday, this.isDisabled, days);
3726
3733
  };
3727
3734
  render() {
3728
3735
  const {
@@ -3767,7 +3774,7 @@ class DayCalendarTable extends PureComponent {
3767
3774
  longTitle: formatDate(new Date(viewYear, viewMonth, day), locale),
3768
3775
  active: this.isActive(day),
3769
3776
  disabled: this.isDisabled(day),
3770
- today: this.isToday(day, viewMonth, viewYear),
3777
+ today: this.isToday(day),
3771
3778
  onClick: this.selectDay
3772
3779
  })
3773
3780
  }, dayIndex))
@@ -3887,33 +3894,26 @@ const MonthCalendarTable = ({
3887
3894
  item: month,
3888
3895
  type: "month",
3889
3896
  title: formatDate(new Date(viewYear, month), locale, MONTH_ONLY_FORMAT),
3890
- active: !!(selectedDate && month === selectedDate.getMonth() && viewYear === selectedDate.getFullYear()),
3897
+ active: isSelected(month),
3891
3898
  disabled: isDisabled(month),
3892
3899
  today: viewYear === new Date().getFullYear() && month === new Date().getMonth(),
3893
3900
  autofocus: autofocusMonth === month,
3894
3901
  onClick: onSelect
3895
3902
  });
3896
3903
  };
3904
+ const isSelected = month => {
3905
+ return selectedDate && month === selectedDate.getMonth() && viewYear === selectedDate.getFullYear();
3906
+ };
3907
+ const isThisMonth = month => {
3908
+ return viewYear === new Date().getFullYear() && month === new Date().getMonth();
3909
+ };
3897
3910
  const isDisabled = month => {
3898
3911
  const date = new Date(viewYear, month);
3899
3912
  return !!(min && date < new Date(min.getFullYear(), min.getMonth()) || max && date > new Date(max.getFullYear(), max.getMonth()));
3900
3913
  };
3901
- const months = [...new Array(ROWS$1 * COLS$1)].map((_, index) => index);
3902
3914
  const autofocusMonth = (() => {
3903
- let activeMonth = null;
3904
- let lowestNonDisabledMonth = null;
3905
- for (const month of months) {
3906
- if (selectedDate && month === selectedDate.getMonth()) {
3907
- activeMonth = month;
3908
- }
3909
- if (!isDisabled(month) && lowestNonDisabledMonth === null) {
3910
- lowestNonDisabledMonth = month;
3911
- }
3912
- if (activeMonth !== null) {
3913
- break;
3914
- }
3915
- }
3916
- return activeMonth || lowestNonDisabledMonth;
3915
+ const months = [...new Array(ROWS$1 * COLS$1)].map((_, index) => index);
3916
+ return getFocusableTime(isSelected, isThisMonth, isDisabled, months);
3917
3917
  })();
3918
3918
  return /*#__PURE__*/jsxs("table", {
3919
3919
  className: "table table-condensed table-bordered tw-date-lookup-calendar np-text-body-default-bold m-b-0",
@@ -4045,25 +4045,18 @@ const YearCalendarTable = ({
4045
4045
  onClick: onSelect
4046
4046
  });
4047
4047
  };
4048
+ const isSelected = year => {
4049
+ return selectedDate && year === selectedDate.getFullYear();
4050
+ };
4051
+ const isThisYear = year => {
4052
+ return year === new Date().getFullYear();
4053
+ };
4048
4054
  const isDisabled = year => {
4049
4055
  return !!(min && year < min.getFullYear() || max && year > max.getFullYear());
4050
4056
  };
4051
- const years = [...new Array(ROWS * COLS)].map((_, index) => startYear + index);
4052
4057
  const autofocusYear = (() => {
4053
- let activeYear = null;
4054
- let lowestNonDisabledYear = null;
4055
- for (const year of years) {
4056
- if (selectedDate && year === selectedDate.getFullYear()) {
4057
- activeYear = year;
4058
- }
4059
- if (!isDisabled(year) && lowestNonDisabledYear === null) {
4060
- lowestNonDisabledYear = year;
4061
- }
4062
- if (activeYear !== null) {
4063
- break;
4064
- }
4065
- }
4066
- return activeYear || lowestNonDisabledYear;
4058
+ const years = [...new Array(ROWS * COLS)].map((_, index) => startYear + index);
4059
+ return getFocusableTime(isSelected, isThisYear, isDisabled, years);
4067
4060
  })();
4068
4061
  return /*#__PURE__*/jsxs("table", {
4069
4062
  className: "table table-condensed table-bordered tw-date-lookup-calendar m-b-0",
@@ -4234,6 +4227,15 @@ class DateLookup extends PureComponent {
4234
4227
  onFocus();
4235
4228
  }
4236
4229
  };
4230
+ discard = () => {
4231
+ const {
4232
+ originalDate
4233
+ } = this.state;
4234
+ if (originalDate !== null) {
4235
+ this.props.onChange(originalDate);
4236
+ }
4237
+ this.close();
4238
+ };
4237
4239
  close = () => {
4238
4240
  const {
4239
4241
  onBlur
@@ -4284,7 +4286,7 @@ class DateLookup extends PureComponent {
4284
4286
  event.preventDefault();
4285
4287
  break;
4286
4288
  case KeyCodes.ESCAPE:
4287
- this.props.onChange(originalDate);
4289
+ originalDate && this.props.onChange(originalDate);
4288
4290
  this.close();
4289
4291
  event.preventDefault();
4290
4292
  break;
@@ -4435,7 +4437,7 @@ class DateLookup extends PureComponent {
4435
4437
  open: open,
4436
4438
  className: "tw-date-lookup-menu",
4437
4439
  position: Position.BOTTOM,
4438
- onClose: this.close,
4440
+ onClose: this.discard,
4439
4441
  children: this.getCalendar()
4440
4442
  })]
4441
4443
  });