@transferwise/components 0.0.0-experimental-8f9699a → 0.0.0-experimental-a25e4d8

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 +75 -152
  2. package/build/index.esm.js.map +1 -1
  3. package/build/index.js +75 -152
  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/monthCalendar/table/MonthCalendarTable.d.ts +1 -1
  11. package/build/types/dateLookup/monthCalendar/table/MonthCalendarTable.d.ts.map +1 -1
  12. package/build/types/dateLookup/tableLink/TableLink.d.ts +4 -14
  13. package/build/types/dateLookup/tableLink/TableLink.d.ts.map +1 -1
  14. package/build/types/dateLookup/tableLink/index.d.ts +1 -1
  15. package/build/types/dateLookup/tableLink/index.d.ts.map +1 -1
  16. package/build/types/dateLookup/yearCalendar/table/YearCalendarTable.d.ts.map +1 -1
  17. package/package.json +2 -2
  18. package/src/chips/Chip.css +1 -1
  19. package/src/chips/Chip.less +9 -21
  20. package/src/dateLookup/DateLookup.css +1 -1
  21. package/src/dateLookup/DateLookup.js +4 -19
  22. package/src/dateLookup/DateLookup.keyboardEvents.spec.js +0 -12
  23. package/src/dateLookup/DateLookup.less +49 -39
  24. package/src/dateLookup/DateLookup.story.js +7 -8
  25. package/src/dateLookup/dayCalendar/table/DayCalendarTable.js +3 -28
  26. package/src/dateLookup/dayCalendar/table/DayCalendarTable.spec.js +0 -25
  27. package/src/dateLookup/monthCalendar/table/MonthCalendarTable.js +20 -33
  28. package/src/dateLookup/monthCalendar/table/MonthCalendarTable.spec.js +0 -33
  29. package/src/dateLookup/tableLink/TableLink.js +70 -0
  30. package/src/dateLookup/tableLink/TableLink.spec.js +15 -6
  31. package/src/dateLookup/yearCalendar/table/YearCalendarTable.js +11 -33
  32. package/src/dateLookup/yearCalendar/table/YearCalendarTable.spec.js +0 -26
  33. package/src/main.css +1 -1
  34. package/build/types/dateLookup/getFocusableTime/getFocusableTime.d.ts +0 -9
  35. package/build/types/dateLookup/getFocusableTime/getFocusableTime.d.ts.map +0 -1
  36. package/src/dateLookup/getFocusableTime/getFocusable.spec.ts +0 -40
  37. package/src/dateLookup/getFocusableTime/getFocusableTime.tsx +0 -14
  38. package/src/dateLookup/tableLink/TableLink.tsx +0 -79
  39. /package/src/dateLookup/tableLink/{index.ts → index.js} +0 -0
@@ -0,0 +1,70 @@
1
+ import PropTypes from 'prop-types';
2
+ import { PureComponent } from 'react';
3
+ import { injectIntl } from 'react-intl';
4
+
5
+ import messages from '../DateLookup.messages';
6
+
7
+ class TableLink extends PureComponent {
8
+ onClick = (event) => {
9
+ event.preventDefault();
10
+ if (!this.props.disabled) {
11
+ this.props.onClick(this.props.item);
12
+ }
13
+ };
14
+
15
+ calculateAriaLabel = (longTitle, title, active, type, formatMessage) => {
16
+ if (active) {
17
+ return `${longTitle || title}, ${formatMessage(messages.selected)} ${formatMessage(
18
+ messages[type],
19
+ )}`;
20
+ }
21
+ return longTitle || title;
22
+ };
23
+
24
+ render() {
25
+ const {
26
+ item,
27
+ type,
28
+ title,
29
+ longTitle,
30
+ active,
31
+ disabled,
32
+ today,
33
+ intl: { formatMessage },
34
+ } = this.props;
35
+ return (
36
+ <>
37
+ <button
38
+ type="button"
39
+ className={`tw-date-lookup-${type}-option ${active ? 'active' : ''} ${
40
+ today ? 'today' : ''
41
+ } np-text-body-default-bold`}
42
+ disabled={disabled}
43
+ aria-label={this.calculateAriaLabel(longTitle, title, active, type, formatMessage)}
44
+ aria-pressed={active}
45
+ onClick={this.onClick}
46
+ >
47
+ {title || item}
48
+ </button>
49
+ </>
50
+ );
51
+ }
52
+ }
53
+
54
+ TableLink.propTypes = {
55
+ item: PropTypes.number.isRequired, // day (1-31), month (0-11) or year (2018 etc)
56
+ type: PropTypes.oneOf(['day', 'month', 'year']).isRequired,
57
+ title: PropTypes.string,
58
+ longTitle: PropTypes.string,
59
+ active: PropTypes.bool.isRequired,
60
+ disabled: PropTypes.bool.isRequired,
61
+ today: PropTypes.bool.isRequired,
62
+ onClick: PropTypes.func.isRequired,
63
+ };
64
+
65
+ TableLink.defaultProps = {
66
+ title: null,
67
+ longTitle: null,
68
+ };
69
+
70
+ export default injectIntl(TableLink);
@@ -1,9 +1,15 @@
1
1
  import { shallow } from 'enzyme';
2
- import { useIntl } from 'react-intl';
3
2
 
4
3
  import TableLink from '.';
5
4
 
6
- jest.mock('react-intl');
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
+ }));
7
13
 
8
14
  jest.mock('../DateLookup.messages', () => ({
9
15
  selected: {
@@ -23,15 +29,18 @@ jest.mock('../DateLookup.messages', () => ({
23
29
  defaultMessage: 'year',
24
30
  },
25
31
  }));
32
+ jest.mock('react-intl', () => ({
33
+ injectIntl: (Component) =>
34
+ function (props) {
35
+ return <Component {...props} intl={{ formatMessage }} />;
36
+ },
37
+ }));
26
38
 
27
39
  describe('TableLink', () => {
28
40
  let component;
29
41
  let props;
30
42
 
31
43
  beforeEach(() => {
32
- useIntl.mockReturnValue({
33
- formatMessage: (message) => message.defaultMessage,
34
- });
35
44
  props = {
36
45
  item: 12,
37
46
  type: 'day',
@@ -42,7 +51,7 @@ describe('TableLink', () => {
42
51
  title: '12',
43
52
  longTitle: '12/12/1212',
44
53
  };
45
- component = shallow(<TableLink {...props} />);
54
+ component = shallow(<TableLink {...props} />).dive();
46
55
  });
47
56
 
48
57
  it('shows item value', () => {
@@ -2,7 +2,6 @@ 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';
6
5
  import TableLink from '../../tableLink';
7
6
 
8
7
  const ROWS = 5;
@@ -12,38 +11,17 @@ const YEAR_ONLY_FORMAT = { year: 'numeric' };
12
11
  const YearCalendarTable = ({ selectedDate, min, max, viewYear, placeholder, onSelect }) => {
13
12
  const { locale } = useIntl();
14
13
  const startYear = viewYear - (viewYear % 20);
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
-
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
+ );
47
25
  return (
48
26
  <table className="table table-condensed table-bordered tw-date-lookup-calendar m-b-0">
49
27
  <thead className="sr-only">
@@ -90,32 +90,6 @@ 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
-
119
93
  const getTableLinkAt = (i) => component.find('[type="year"]').at(i);
120
94
  const getTableLink = () => component.find('[type="year"]');
121
95
  });