@transferwise/components 45.25.2 → 45.26.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.
Files changed (39) hide show
  1. package/build/index.esm.js +152 -75
  2. package/build/index.esm.js.map +1 -1
  3. package/build/index.js +152 -75
  4. package/build/index.js.map +1 -1
  5. package/build/main.css +1 -1
  6. package/build/styles/chips/Chip.css +1 -1
  7. package/build/styles/dateLookup/DateLookup.css +1 -1
  8. package/build/styles/main.css +1 -1
  9. package/build/types/dateLookup/DateLookup.d.ts.map +1 -1
  10. package/build/types/dateLookup/getFocusableTime/getFocusableTime.d.ts +9 -0
  11. package/build/types/dateLookup/getFocusableTime/getFocusableTime.d.ts.map +1 -0
  12. package/build/types/dateLookup/monthCalendar/table/MonthCalendarTable.d.ts +1 -1
  13. package/build/types/dateLookup/monthCalendar/table/MonthCalendarTable.d.ts.map +1 -1
  14. package/build/types/dateLookup/tableLink/TableLink.d.ts +14 -4
  15. package/build/types/dateLookup/tableLink/TableLink.d.ts.map +1 -1
  16. package/build/types/dateLookup/tableLink/index.d.ts +1 -1
  17. package/build/types/dateLookup/tableLink/index.d.ts.map +1 -1
  18. package/build/types/dateLookup/yearCalendar/table/YearCalendarTable.d.ts.map +1 -1
  19. package/package.json +2 -2
  20. package/src/chips/Chip.css +1 -1
  21. package/src/chips/Chip.less +9 -21
  22. package/src/dateLookup/DateLookup.css +1 -1
  23. package/src/dateLookup/DateLookup.js +19 -4
  24. package/src/dateLookup/DateLookup.keyboardEvents.spec.js +12 -0
  25. package/src/dateLookup/DateLookup.less +39 -49
  26. package/src/dateLookup/DateLookup.story.js +8 -7
  27. package/src/dateLookup/dayCalendar/table/DayCalendarTable.js +28 -3
  28. package/src/dateLookup/dayCalendar/table/DayCalendarTable.spec.js +25 -0
  29. package/src/dateLookup/getFocusableTime/getFocusable.spec.ts +40 -0
  30. package/src/dateLookup/getFocusableTime/getFocusableTime.tsx +14 -0
  31. package/src/dateLookup/monthCalendar/table/MonthCalendarTable.js +33 -20
  32. package/src/dateLookup/monthCalendar/table/MonthCalendarTable.spec.js +33 -0
  33. package/src/dateLookup/tableLink/TableLink.spec.js +6 -15
  34. package/src/dateLookup/tableLink/TableLink.tsx +79 -0
  35. package/src/dateLookup/yearCalendar/table/YearCalendarTable.js +33 -11
  36. package/src/dateLookup/yearCalendar/table/YearCalendarTable.spec.js +26 -0
  37. package/src/main.css +1 -1
  38. package/src/dateLookup/tableLink/TableLink.js +0 -70
  39. /package/src/dateLookup/tableLink/{index.js → index.ts} +0 -0
@@ -2,33 +2,40 @@ import { formatDate } from '@transferwise/formatting';
2
2
  import PropTypes from 'prop-types';
3
3
  import { useIntl } from 'react-intl';
4
4
 
5
+ import { getFocusableTime } from '../../getFocusableTime/getFocusableTime';
5
6
  import TableLink from '../../tableLink';
6
7
 
7
8
  const ROWS = 3;
8
9
  const COLS = 4;
9
- const MONTH_ONLY_FORMAY = { month: 'short' };
10
+ const MONTH_ONLY_FORMAT = { month: 'short' };
10
11
 
11
- const MonthCalendarTable = ({
12
- selectedDate: selected,
13
- min,
14
- max,
15
- viewYear,
16
- placeholder,
17
- onSelect,
18
- }) => {
12
+ const MonthCalendarTable = ({ selectedDate, min, max, viewYear, placeholder, onSelect }) => {
19
13
  const { locale } = useIntl();
14
+ const getLink = (month) => {
15
+ return (
16
+ <TableLink
17
+ item={month}
18
+ type="month"
19
+ title={formatDate(new Date(viewYear, month), locale, MONTH_ONLY_FORMAT)}
20
+ active={isActive(month)}
21
+ disabled={isDisabled(month)}
22
+ today={viewYear === new Date().getFullYear() && month === new Date().getMonth()}
23
+ autofocus={autofocusMonth === month}
24
+ onClick={onSelect}
25
+ />
26
+ );
27
+ };
28
+
29
+ const isActive = (month) => {
30
+ return (
31
+ selectedDate && month === selectedDate.getMonth() && viewYear === selectedDate.getFullYear()
32
+ );
33
+ };
34
+
35
+ const isThisMonth = (month) => {
36
+ return viewYear === new Date().getFullYear() && month === new Date().getMonth();
37
+ };
20
38
 
21
- const getLink = (month) => (
22
- <TableLink
23
- item={month}
24
- type="month"
25
- title={formatDate(new Date(viewYear, month), locale, MONTH_ONLY_FORMAY)}
26
- active={!!(selected && month === selected.getMonth() && viewYear === selected.getFullYear())}
27
- disabled={isDisabled(month)}
28
- today={viewYear === new Date().getFullYear() && month === new Date().getMonth()}
29
- onClick={onSelect}
30
- />
31
- );
32
39
  const isDisabled = (month) => {
33
40
  const date = new Date(viewYear, month);
34
41
  return !!(
@@ -36,6 +43,12 @@ const MonthCalendarTable = ({
36
43
  (max && date > new Date(max.getFullYear(), max.getMonth()))
37
44
  );
38
45
  };
46
+
47
+ const autofocusMonth = (() => {
48
+ const months = [...new Array(ROWS * COLS)].map((_, index) => index);
49
+ return getFocusableTime({ isActive, isNow: isThisMonth, isDisabled, timeSpan: months });
50
+ })();
51
+
39
52
  return (
40
53
  <table className="table table-condensed table-bordered tw-date-lookup-calendar np-text-body-default-bold m-b-0">
41
54
  <thead className="sr-only">
@@ -82,6 +82,39 @@ describe('MonthCalendarTable', () => {
82
82
  expect(component.find('.sr-only').text()).toBe('Enter date..');
83
83
  });
84
84
 
85
+ it('sets autofocus to true when June is the selected month', () => {
86
+ component.setProps({ selectedDate: new Date(2018, 5, 1) });
87
+ expect(getTableLinkAt(5).prop('autofocus')).toBe(true);
88
+ });
89
+
90
+ it('does not set autofocus to true when June is the selected month but not the right year', () => {
91
+ component.setProps({ selectedDate: new Date(2017, 5, 1) });
92
+ expect(getTableLinkAt(5).prop('autofocus')).toBe(false);
93
+ });
94
+
95
+ it('sets autofocus to true when selected date is null and current month is visible', () => {
96
+ const today = new Date();
97
+ component.setProps({
98
+ selectedDate: null,
99
+ viewYear: today.getFullYear(),
100
+ viewMonth: today.getMonth(),
101
+ });
102
+ expect(getTableLink().find({ today: true }).prop('autofocus')).toBe(true);
103
+ });
104
+
105
+ it('sets autofocus to true on the second month when the first month is disabled', () => {
106
+ component.setProps({
107
+ selectedDate: null,
108
+ viewYear: 2022,
109
+ min: new Date(2022, 1, 28),
110
+ viewMonth: 0,
111
+ isDisabled: (month) => month === 0,
112
+ });
113
+
114
+ expect(getTableLinkAt(0).prop('autofocus')).toBe(false);
115
+ expect(getTableLinkAt(1).prop('autofocus')).toBe(true);
116
+ });
117
+
85
118
  const getTableLinkAt = (i) => component.find('[title="MonthName"]').at(i);
86
119
  const getTableLink = () => component.find('[title="MonthName"]');
87
120
  });
@@ -1,15 +1,9 @@
1
1
  import { shallow } from 'enzyme';
2
+ import { useIntl } from 'react-intl';
2
3
 
3
4
  import TableLink from '.';
4
5
 
5
- const formatMessage = (id) => id.defaultMessage;
6
- jest.mock('react-intl', () => ({
7
- injectIntl: (Component) =>
8
- function (props) {
9
- return <Component {...props} intl={{ formatMessage }} />;
10
- },
11
- defineMessages: (translations) => translations,
12
- }));
6
+ jest.mock('react-intl');
13
7
 
14
8
  jest.mock('../DateLookup.messages', () => ({
15
9
  selected: {
@@ -29,18 +23,15 @@ jest.mock('../DateLookup.messages', () => ({
29
23
  defaultMessage: 'year',
30
24
  },
31
25
  }));
32
- jest.mock('react-intl', () => ({
33
- injectIntl: (Component) =>
34
- function (props) {
35
- return <Component {...props} intl={{ formatMessage }} />;
36
- },
37
- }));
38
26
 
39
27
  describe('TableLink', () => {
40
28
  let component;
41
29
  let props;
42
30
 
43
31
  beforeEach(() => {
32
+ useIntl.mockReturnValue({
33
+ formatMessage: (message) => message.defaultMessage,
34
+ });
44
35
  props = {
45
36
  item: 12,
46
37
  type: 'day',
@@ -51,7 +42,7 @@ describe('TableLink', () => {
51
42
  title: '12',
52
43
  longTitle: '12/12/1212',
53
44
  };
54
- component = shallow(<TableLink {...props} />).dive();
45
+ component = shallow(<TableLink {...props} />);
55
46
  });
56
47
 
57
48
  it('shows item value', () => {
@@ -0,0 +1,79 @@
1
+ import classNames from 'classnames';
2
+ import { useEffect, useRef } from 'react';
3
+ import { useIntl } from 'react-intl';
4
+
5
+ import messages from '../DateLookup.messages';
6
+
7
+ interface TableLinkProps {
8
+ item: number;
9
+ type: 'day' | 'month' | 'year';
10
+ title?: string;
11
+ longTitle?: string;
12
+ active: boolean;
13
+ disabled: boolean;
14
+ today: boolean;
15
+ autofocus?: boolean;
16
+ onClick: (item: number) => void;
17
+ }
18
+
19
+ const TableLink = ({
20
+ item,
21
+ type,
22
+ title,
23
+ longTitle,
24
+ active,
25
+ disabled,
26
+ today,
27
+ autofocus,
28
+ onClick,
29
+ }: TableLinkProps) => {
30
+ const buttonRef = useRef<HTMLButtonElement>(null);
31
+ const intl = useIntl();
32
+
33
+ useEffect(() => {
34
+ if (autofocus) {
35
+ setTimeout(() => {
36
+ buttonRef.current?.focus();
37
+ }, 0);
38
+ }
39
+ }, [autofocus]);
40
+
41
+ const onCalendarClick = (event: React.MouseEvent<HTMLButtonElement>) => {
42
+ event.preventDefault();
43
+ if (!disabled) {
44
+ onClick(item);
45
+ }
46
+ };
47
+
48
+ const calculateAriaLabel = () => {
49
+ if (active) {
50
+ return `${longTitle || title || ''}, ${intl.formatMessage(
51
+ messages.selected,
52
+ )} ${intl.formatMessage(messages[type])}`;
53
+ }
54
+ return longTitle || title;
55
+ };
56
+
57
+ return (
58
+ <>
59
+ <button
60
+ ref={buttonRef}
61
+ type="button"
62
+ className={classNames(
63
+ `tw-date-lookup-${type}-option np-text-body-default-bold`,
64
+ { active: !!active },
65
+ { today: !!today },
66
+ )}
67
+ disabled={disabled}
68
+ tabIndex={autofocus ? 0 : -1}
69
+ aria-label={calculateAriaLabel()}
70
+ aria-pressed={active}
71
+ onClick={onCalendarClick}
72
+ >
73
+ {title || item}
74
+ </button>
75
+ </>
76
+ );
77
+ };
78
+
79
+ export default TableLink;
@@ -2,6 +2,7 @@ import { formatDate } from '@transferwise/formatting';
2
2
  import PropTypes from 'prop-types';
3
3
  import { useIntl } from 'react-intl';
4
4
 
5
+ import { getFocusableTime } from '../../getFocusableTime/getFocusableTime';
5
6
  import TableLink from '../../tableLink';
6
7
 
7
8
  const ROWS = 5;
@@ -11,17 +12,38 @@ const YEAR_ONLY_FORMAT = { year: 'numeric' };
11
12
  const YearCalendarTable = ({ selectedDate, min, max, viewYear, placeholder, onSelect }) => {
12
13
  const { locale } = useIntl();
13
14
  const startYear = viewYear - (viewYear % 20);
14
- const getLink = (year) => (
15
- <TableLink
16
- item={year}
17
- type="year"
18
- title={formatDate(new Date(year, 0), locale, YEAR_ONLY_FORMAT)}
19
- active={!!(selectedDate && year === selectedDate.getFullYear())}
20
- disabled={!!((min && year < min.getFullYear()) || (max && year > max.getFullYear()))}
21
- today={year === new Date().getFullYear()}
22
- onClick={onSelect}
23
- />
24
- );
15
+ const getLink = (year) => {
16
+ return (
17
+ <TableLink
18
+ item={year}
19
+ type="year"
20
+ title={formatDate(new Date(year, 0), locale, YEAR_ONLY_FORMAT)}
21
+ active={isActive(year)}
22
+ disabled={isDisabled(year)}
23
+ today={isThisYear(year)}
24
+ autofocus={autofocusYear === year}
25
+ onClick={onSelect}
26
+ />
27
+ );
28
+ };
29
+
30
+ const isActive = (year) => {
31
+ return !!(selectedDate && year === selectedDate.getFullYear());
32
+ };
33
+
34
+ const isThisYear = (year) => {
35
+ return year === new Date().getFullYear();
36
+ };
37
+
38
+ const isDisabled = (year) => {
39
+ return !!((min && year < min.getFullYear()) || (max && year > max.getFullYear()));
40
+ };
41
+
42
+ const autofocusYear = (() => {
43
+ const years = [...new Array(ROWS * COLS)].map((_, index) => startYear + index);
44
+ return getFocusableTime({ isActive, isNow: isThisYear, isDisabled, timeSpan: years });
45
+ })();
46
+
25
47
  return (
26
48
  <table className="table table-condensed table-bordered tw-date-lookup-calendar m-b-0">
27
49
  <thead className="sr-only">
@@ -90,6 +90,32 @@ describe('YearCalendarTable', () => {
90
90
  expect(component.find('.sr-only').text()).toBe('Enter date..');
91
91
  });
92
92
 
93
+ it('sets autofocus to true when 2002 is the selected year', () => {
94
+ component.setProps({ selectedDate: new Date(2002, 5, 1) });
95
+ expect(getTableLinkAt(2).prop('autofocus')).toBe(true);
96
+ });
97
+
98
+ it('sets autofocus to true when selected year is null and current year is visible', () => {
99
+ const today = new Date();
100
+ component.setProps({
101
+ selectedDate: null,
102
+ viewYear: today.getFullYear(),
103
+ });
104
+ expect(getTableLink().find({ today: true }).prop('autofocus')).toBe(true);
105
+ });
106
+
107
+ it('sets autofocus to true on the second year when the first year is disabled', () => {
108
+ component.setProps({
109
+ selectedDate: null,
110
+ viewYear: 2000,
111
+ min: new Date(2001, 1, 1),
112
+ isDisabled: (month) => month === 0,
113
+ });
114
+
115
+ expect(getTableLinkAt(0).prop('autofocus')).toBe(false);
116
+ expect(getTableLinkAt(1).prop('autofocus')).toBe(true);
117
+ });
118
+
93
119
  const getTableLinkAt = (i) => component.find('[type="year"]').at(i);
94
120
  const getTableLink = () => component.find('[type="year"]');
95
121
  });