@transferwise/components 46.30.2 → 46.32.0
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 +931 -523
- package/build/index.js.map +1 -1
- package/build/index.mjs +928 -523
- package/build/index.mjs.map +1 -1
- package/build/main.css +135 -0
- package/build/styles/carousel/Carousel.css +135 -0
- package/build/styles/main.css +135 -0
- package/build/types/carousel/Carousel.d.ts +26 -0
- package/build/types/carousel/Carousel.d.ts.map +1 -0
- package/build/types/carousel/index.d.ts +3 -0
- package/build/types/carousel/index.d.ts.map +1 -0
- package/build/types/common/card/Card.d.ts +2 -2
- package/build/types/common/card/Card.d.ts.map +1 -1
- package/build/types/dateInput/DateInput.d.ts +5 -4
- package/build/types/dateInput/DateInput.d.ts.map +1 -1
- package/build/types/dateLookup/DateLookup.d.ts +11 -4
- package/build/types/dateLookup/DateLookup.d.ts.map +1 -1
- package/build/types/field/Field.d.ts +12 -0
- package/build/types/field/Field.d.ts.map +1 -0
- package/build/types/index.d.ts +6 -0
- package/build/types/index.d.ts.map +1 -1
- package/build/types/inputs/Input.d.ts.map +1 -1
- package/build/types/inputs/SelectInput.d.ts +1 -1
- package/build/types/inputs/SelectInput.d.ts.map +1 -1
- package/build/types/inputs/TextArea.d.ts.map +1 -1
- package/build/types/inputs/_common.d.ts +2 -2
- package/build/types/inputs/_common.d.ts.map +1 -1
- package/build/types/inputs/contexts.d.ts +24 -0
- package/build/types/inputs/contexts.d.ts.map +1 -0
- package/build/types/label/Label.d.ts +9 -0
- package/build/types/label/Label.d.ts.map +1 -0
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts +1 -1
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts.map +1 -1
- package/build/types/promoCard/PromoCard.d.ts +16 -5
- package/build/types/promoCard/PromoCard.d.ts.map +1 -1
- package/build/types/radioGroup/RadioGroup.d.ts.map +1 -1
- package/build/types/switch/Switch.d.ts +6 -3
- package/build/types/switch/Switch.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/carousel/Carousel.css +135 -0
- package/src/carousel/Carousel.less +133 -0
- package/src/carousel/Carousel.spec.tsx +221 -0
- package/src/carousel/Carousel.story.tsx +63 -0
- package/src/carousel/Carousel.tsx +345 -0
- package/src/carousel/index.ts +3 -0
- package/src/common/card/Card.tsx +51 -43
- package/src/dateInput/DateInput.rtl.spec.tsx +17 -0
- package/src/dateInput/DateInput.tsx +28 -22
- package/src/dateLookup/DateLookup.keyboardEvents.spec.js +2 -2
- package/src/dateLookup/DateLookup.rtl.spec.tsx +21 -0
- package/src/dateLookup/DateLookup.state.spec.js +5 -5
- package/src/dateLookup/DateLookup.tests.story.tsx +4 -11
- package/src/dateLookup/DateLookup.tsx +24 -9
- package/src/dateLookup/DateLookup.view.spec.js +11 -11
- package/src/field/Field.spec.tsx +95 -0
- package/src/field/Field.story.tsx +59 -0
- package/src/field/Field.tsx +70 -0
- package/src/index.ts +6 -0
- package/src/inputs/Input.tsx +5 -3
- package/src/inputs/SelectInput.spec.tsx +10 -0
- package/src/inputs/SelectInput.tsx +9 -4
- package/src/inputs/TextArea.tsx +6 -3
- package/src/inputs/_ButtonInput.tsx +2 -2
- package/src/inputs/_common.ts +2 -2
- package/src/inputs/contexts.tsx +45 -0
- package/src/label/Label.spec.tsx +26 -0
- package/src/label/Label.story.tsx +37 -0
- package/src/label/Label.tsx +20 -0
- package/src/main.css +135 -0
- package/src/main.less +1 -0
- package/src/phoneNumberInput/PhoneNumberInput.story.tsx +16 -22
- package/src/phoneNumberInput/PhoneNumberInput.tsx +14 -2
- package/src/promoCard/PromoCard.story.tsx +2 -2
- package/src/promoCard/PromoCard.tsx +30 -9
- package/src/radioGroup/RadioGroup.rtl.spec.tsx +14 -0
- package/src/radioGroup/RadioGroup.story.tsx +26 -0
- package/src/radioGroup/RadioGroup.tsx +4 -1
- package/src/switch/Switch.spec.tsx +10 -0
- package/src/switch/Switch.tsx +22 -13
- package/src/utilities/logActionRequired.js +1 -1
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
|
+
import { userEvent } from '../test-utils';
|
|
3
|
+
|
|
4
|
+
import Display from '../display';
|
|
5
|
+
import Title from '../title';
|
|
6
|
+
|
|
7
|
+
import Carousel, { type CarouselCard } from './Carousel';
|
|
8
|
+
|
|
9
|
+
jest.mock('@wise/components-theming', () => ({
|
|
10
|
+
...jest.requireActual<Record<string, unknown>>('@wise/components-theming'),
|
|
11
|
+
useTheme: jest.fn().mockReturnValue({ theme: 'personal' }),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
const yourInvoiceOnClick = jest.fn();
|
|
15
|
+
|
|
16
|
+
const carouselCards: CarouselCard[] = [
|
|
17
|
+
{
|
|
18
|
+
id: 'YOUR_INVOICE',
|
|
19
|
+
type: 'anchor',
|
|
20
|
+
href: 'https://wise.com',
|
|
21
|
+
onClick: yourInvoiceOnClick,
|
|
22
|
+
content: <div>Add a link in accounting software like Xero or QuickBooks.</div>,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: 'YOUR_WEBSITE',
|
|
26
|
+
type: 'button',
|
|
27
|
+
onClick: () => {},
|
|
28
|
+
content: <Title type="title-screen">Wise.</Title>,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'SHOP_OR_STALL',
|
|
32
|
+
type: 'anchor',
|
|
33
|
+
href: 'https://wise.com',
|
|
34
|
+
onClick: () => {},
|
|
35
|
+
content: <Display type="display-small">Get Quick Pay</Display>,
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const onClick = jest.fn();
|
|
40
|
+
|
|
41
|
+
const renderCarousel = () => {
|
|
42
|
+
render(<Carousel header="Cool stuff" cards={carouselCards} onClick={onClick} />);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
describe('Carousel', () => {
|
|
46
|
+
const focusSpy = jest.fn();
|
|
47
|
+
const scrollBy = jest.fn();
|
|
48
|
+
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
// eslint-disable-next-line functional/immutable-data
|
|
51
|
+
window.HTMLElement.prototype.scrollIntoView = focusSpy;
|
|
52
|
+
// eslint-disable-next-line functional/immutable-data
|
|
53
|
+
window.HTMLElement.prototype.scrollBy = scrollBy;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
focusSpy.mockClear();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('shows expected content on page', async () => {
|
|
61
|
+
renderCarousel();
|
|
62
|
+
|
|
63
|
+
expect(
|
|
64
|
+
screen.getByText('Add a link in accounting software like Xero or QuickBooks.'),
|
|
65
|
+
).toBeInTheDocument();
|
|
66
|
+
|
|
67
|
+
expect(screen.getByText('Wise.')).toBeInTheDocument();
|
|
68
|
+
|
|
69
|
+
expect(screen.getByText('Get Quick Pay')).toBeInTheDocument();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('allows user to scroll carousel to the right when at start', () => {
|
|
73
|
+
mockPositions(948, 903);
|
|
74
|
+
|
|
75
|
+
renderCarousel();
|
|
76
|
+
|
|
77
|
+
const scrollLeftButton = screen.getByTestId('scroll-carousel-left');
|
|
78
|
+
const scrollRightButton = screen.getByTestId('scroll-carousel-right');
|
|
79
|
+
|
|
80
|
+
expect(scrollLeftButton).toBeDisabled();
|
|
81
|
+
expect(scrollRightButton).toBeEnabled();
|
|
82
|
+
|
|
83
|
+
userEvent.click(scrollRightButton);
|
|
84
|
+
|
|
85
|
+
expect(scrollBy).toHaveBeenCalledWith({ left: 300, behavior: 'smooth' });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('allows user to scroll carousel to the left or right when in middle', () => {
|
|
89
|
+
mockPositions(948, 903);
|
|
90
|
+
|
|
91
|
+
renderCarousel();
|
|
92
|
+
|
|
93
|
+
const carousel = screen.getByRole('list');
|
|
94
|
+
fireEvent.scroll(carousel, { target: { scrollLeft: 10 } });
|
|
95
|
+
|
|
96
|
+
const scrollLeftButton = screen.getByTestId('scroll-carousel-left');
|
|
97
|
+
const scrollRightButton = screen.getByTestId('scroll-carousel-right');
|
|
98
|
+
|
|
99
|
+
expect(scrollLeftButton).toBeEnabled();
|
|
100
|
+
expect(scrollRightButton).toBeEnabled();
|
|
101
|
+
|
|
102
|
+
userEvent.click(scrollLeftButton);
|
|
103
|
+
|
|
104
|
+
expect(scrollBy).toHaveBeenCalledWith({ left: -300, behavior: 'smooth' });
|
|
105
|
+
|
|
106
|
+
userEvent.click(scrollRightButton);
|
|
107
|
+
|
|
108
|
+
expect(scrollBy).toHaveBeenCalledWith({ left: 300, behavior: 'smooth' });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('allows user to scroll carousel to the left when at end', () => {
|
|
112
|
+
mockPositions(948, 903);
|
|
113
|
+
|
|
114
|
+
renderCarousel();
|
|
115
|
+
|
|
116
|
+
const carousel = screen.getByRole('list');
|
|
117
|
+
fireEvent.scroll(carousel, { target: { scrollLeft: 50 } });
|
|
118
|
+
|
|
119
|
+
const scrollLeftButton = screen.getByTestId('scroll-carousel-left');
|
|
120
|
+
const scrollRightButton = screen.getByTestId('scroll-carousel-right');
|
|
121
|
+
|
|
122
|
+
expect(scrollLeftButton).toBeEnabled();
|
|
123
|
+
expect(scrollRightButton).toBeDisabled();
|
|
124
|
+
|
|
125
|
+
userEvent.click(scrollLeftButton);
|
|
126
|
+
|
|
127
|
+
expect(scrollBy).toHaveBeenCalledWith({ left: -300, behavior: 'smooth' });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('allows user to go through carousel by indicator', async () => {
|
|
131
|
+
mockPositions(948, 903);
|
|
132
|
+
|
|
133
|
+
renderCarousel();
|
|
134
|
+
|
|
135
|
+
const yourInvoiceIndicator = screen.getByTestId('YOUR_INVOICE-indicator');
|
|
136
|
+
const yourWebsiteIndicator = screen.getByTestId('YOUR_WEBSITE-indicator');
|
|
137
|
+
const shopOrStallIndicator = screen.getByTestId('SHOP_OR_STALL-indicator');
|
|
138
|
+
const firstCard = getFirstCard();
|
|
139
|
+
const secondCard = getSecondCard();
|
|
140
|
+
const thirdCard = getThirdCard();
|
|
141
|
+
|
|
142
|
+
expect(focusSpy).toHaveBeenCalledTimes(0);
|
|
143
|
+
expect(firstCard).toHaveClass('carousel__card--focused');
|
|
144
|
+
|
|
145
|
+
userEvent.click(yourWebsiteIndicator);
|
|
146
|
+
|
|
147
|
+
expect(focusSpy).toHaveBeenCalledTimes(1);
|
|
148
|
+
expect(secondCard).toHaveClass('carousel__card--focused');
|
|
149
|
+
|
|
150
|
+
userEvent.click(shopOrStallIndicator);
|
|
151
|
+
|
|
152
|
+
expect(focusSpy).toHaveBeenCalledTimes(2);
|
|
153
|
+
expect(thirdCard).toHaveClass('carousel__card--focused');
|
|
154
|
+
|
|
155
|
+
userEvent.click(yourInvoiceIndicator);
|
|
156
|
+
|
|
157
|
+
expect(focusSpy).toHaveBeenCalledTimes(3);
|
|
158
|
+
expect(firstCard).toHaveClass('carousel__card--focused');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('is focused on the first element and lets user navigate through them', () => {
|
|
162
|
+
renderCarousel();
|
|
163
|
+
|
|
164
|
+
const firstCard = getFirstCard();
|
|
165
|
+
const secondCard = getSecondCard();
|
|
166
|
+
const thirdCard = getThirdCard();
|
|
167
|
+
|
|
168
|
+
expect(firstCard).toBeInTheDocument();
|
|
169
|
+
|
|
170
|
+
firstCard.focus();
|
|
171
|
+
expect(firstCard).toHaveFocus();
|
|
172
|
+
|
|
173
|
+
userEvent.keyboard('{arrowright}');
|
|
174
|
+
|
|
175
|
+
expect(secondCard).toHaveFocus();
|
|
176
|
+
|
|
177
|
+
userEvent.keyboard('{arrowright}');
|
|
178
|
+
|
|
179
|
+
expect(thirdCard).toHaveFocus();
|
|
180
|
+
|
|
181
|
+
userEvent.keyboard('{arrowleft}');
|
|
182
|
+
|
|
183
|
+
expect(secondCard).toHaveFocus();
|
|
184
|
+
|
|
185
|
+
userEvent.keyboard('{arrowleft}');
|
|
186
|
+
|
|
187
|
+
expect(firstCard).toHaveFocus();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('calls onClick when a card is clicked', () => {
|
|
191
|
+
renderCarousel();
|
|
192
|
+
|
|
193
|
+
userEvent.click(getFirstCard());
|
|
194
|
+
|
|
195
|
+
expect(onClick).toHaveBeenCalledTimes(1);
|
|
196
|
+
expect(yourInvoiceOnClick).toHaveBeenCalledTimes(1);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const mockPositions = (scrollWidth: number, offsetWidth: number) => {
|
|
201
|
+
// eslint-disable-next-line functional/immutable-data
|
|
202
|
+
Object.defineProperty(HTMLElement.prototype, 'scrollWidth', {
|
|
203
|
+
configurable: true,
|
|
204
|
+
value: scrollWidth,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// eslint-disable-next-line functional/immutable-data
|
|
208
|
+
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
209
|
+
configurable: true,
|
|
210
|
+
value: offsetWidth,
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const getFirstCard = () =>
|
|
215
|
+
screen.getByRole('link', {
|
|
216
|
+
name: /Add a link in accounting software like Xero or QuickBooks\./,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const getSecondCard = () => screen.getByRole('button', { name: /Wise\./ });
|
|
220
|
+
|
|
221
|
+
const getThirdCard = () => screen.getByRole('link', { name: /Get Quick Pay/ });
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { StoryFn } from '@storybook/react';
|
|
2
|
+
import { Illustration } from '@wise/art';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
|
|
5
|
+
import Display from '../display';
|
|
6
|
+
|
|
7
|
+
import Carousel, { type CarouselCard } from './Carousel';
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
component: Carousel,
|
|
11
|
+
title: 'Navigation/Carousel',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const carouselCards: CarouselCard[] = [
|
|
15
|
+
{
|
|
16
|
+
id: 'YOUR_INVOICE',
|
|
17
|
+
type: 'button',
|
|
18
|
+
onClick: () => {
|
|
19
|
+
console.log('Clicked on button');
|
|
20
|
+
},
|
|
21
|
+
content: (
|
|
22
|
+
<div className="d-flex p-a-1 p-t-0 flex-column align-items-center">
|
|
23
|
+
<div style={{ position: 'absolute' }}>
|
|
24
|
+
<Illustration disablePadding name="globe" size="small" alt="" />
|
|
25
|
+
</div>
|
|
26
|
+
<div style={{ position: 'absolute', zIndex: '2', bottom: '8px', left: '16px' }}>
|
|
27
|
+
<Display type="display-small">Here there, everywhere</Display>
|
|
28
|
+
</div>
|
|
29
|
+
</div>
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'YOUR_WEBSITE',
|
|
34
|
+
type: 'promo',
|
|
35
|
+
onClick: () => {
|
|
36
|
+
console.log('hi');
|
|
37
|
+
},
|
|
38
|
+
href: 'http://wise.com',
|
|
39
|
+
title: 'Wise',
|
|
40
|
+
description: 'The best way to move money internationally',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: 'SHOP_OR_STALL',
|
|
44
|
+
type: 'anchor',
|
|
45
|
+
href: 'https://wise.com',
|
|
46
|
+
onClick: () => {},
|
|
47
|
+
styles: {
|
|
48
|
+
backgroundImage:
|
|
49
|
+
'url(https://wise.com/public-resources/assets/team-page/tapestry/team_member_tapestry_01.jpg)',
|
|
50
|
+
backgroundSize: 'cover',
|
|
51
|
+
},
|
|
52
|
+
content: <Display type="display-small">Get Quick Pay</Display>,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const Template: StoryFn = (args) => {
|
|
57
|
+
return <Carousel header="Pretty nifty stuff" cards={carouselCards} {...args} />;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const CarouselDefault = {
|
|
61
|
+
render: Template,
|
|
62
|
+
args: {},
|
|
63
|
+
};
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { ChevronLeft, ChevronRight } from '@transferwise/icons';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import { type CSSProperties, type ReactNode, useEffect, useRef, useState } from 'react';
|
|
4
|
+
|
|
5
|
+
import ActionButton from '../actionButton';
|
|
6
|
+
import Title from '../title';
|
|
7
|
+
import type { PromoCardLinkProps } from '../promoCard/PromoCard';
|
|
8
|
+
import PromoCard from '../promoCard/PromoCard';
|
|
9
|
+
|
|
10
|
+
export type CarouselCardCommon = {
|
|
11
|
+
id: string;
|
|
12
|
+
href?: string;
|
|
13
|
+
onClick?: () => void;
|
|
14
|
+
className?: string;
|
|
15
|
+
styles?: CSSProperties;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type CarouselDefaultCard = CarouselCardCommon & {
|
|
19
|
+
type: 'anchor' | 'button';
|
|
20
|
+
content: ReactNode;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type CarouselPromoCard = CarouselCardCommon & {
|
|
24
|
+
type: 'promo';
|
|
25
|
+
} & Omit<PromoCardLinkProps, 'type'>;
|
|
26
|
+
|
|
27
|
+
export type CarouselCard = CarouselDefaultCard | CarouselPromoCard;
|
|
28
|
+
export interface CarouselProps {
|
|
29
|
+
header: string | ReactNode;
|
|
30
|
+
className?: string;
|
|
31
|
+
cards: CarouselCard[];
|
|
32
|
+
onClick?: (cardId: string) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type CardsReference = {
|
|
36
|
+
type: 'promo' | 'default';
|
|
37
|
+
cardElement: HTMLDivElement | HTMLAnchorElement;
|
|
38
|
+
anchorElement?: HTMLAnchorElement;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const LEFT_SCROLL_OFFSET = 8;
|
|
42
|
+
|
|
43
|
+
const Carousel: React.FC<CarouselProps> = ({ header, className, cards, onClick }) => {
|
|
44
|
+
const [scrollPosition, setScrollPosition] = useState(0);
|
|
45
|
+
const [previousScrollPosition, setPreviousScrollPosition] = useState(0);
|
|
46
|
+
const [scrollIsAtEnd, setScrollIsAtEnd] = useState(false);
|
|
47
|
+
const [visibleCardOnMobileView, setVisibleCardOnMobileView] = useState<string>('');
|
|
48
|
+
const carouselElementRef = useRef<HTMLDivElement>(null);
|
|
49
|
+
const carouselCardsRef = useRef<CardsReference[]>([]);
|
|
50
|
+
|
|
51
|
+
const isLeftActionButtonEnabled = scrollPosition > LEFT_SCROLL_OFFSET;
|
|
52
|
+
|
|
53
|
+
const areActionButtonsEnabled = isLeftActionButtonEnabled || !scrollIsAtEnd;
|
|
54
|
+
|
|
55
|
+
const [focusedCard, setFocusedCard] = useState(cards?.[0]?.id);
|
|
56
|
+
|
|
57
|
+
const updateScrollButtonsState = () => {
|
|
58
|
+
if (carouselElementRef.current) {
|
|
59
|
+
const { scrollWidth, offsetWidth } = carouselElementRef.current;
|
|
60
|
+
|
|
61
|
+
const scrollAtEnd = scrollWidth - offsetWidth <= scrollPosition + LEFT_SCROLL_OFFSET;
|
|
62
|
+
setScrollIsAtEnd(scrollAtEnd);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const scrollDirecton = scrollPosition > previousScrollPosition ? 'right' : 'left';
|
|
66
|
+
|
|
67
|
+
const cardsInFullViewIds: string[] = [];
|
|
68
|
+
carouselCardsRef.current.forEach((card) => {
|
|
69
|
+
if (isVisible(carouselElementRef.current as HTMLElement, card.cardElement as HTMLElement)) {
|
|
70
|
+
// eslint-disable-next-line functional/immutable-data
|
|
71
|
+
cardsInFullViewIds.push(card.cardElement.getAttribute('id') ?? '');
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (cardsInFullViewIds.length >= 1) {
|
|
76
|
+
const visibleCardIndex = scrollDirecton === 'right' ? cardsInFullViewIds.length - 1 : 0;
|
|
77
|
+
const visibleCardId = cardsInFullViewIds[visibleCardIndex];
|
|
78
|
+
setVisibleCardOnMobileView(visibleCardId);
|
|
79
|
+
setFocusedCard(visibleCardId);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setPreviousScrollPosition(scrollPosition);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const scrollCarousel = (direction: 'left' | 'right' = 'right') => {
|
|
86
|
+
if (carouselElementRef.current) {
|
|
87
|
+
const { scrollWidth } = carouselElementRef.current;
|
|
88
|
+
|
|
89
|
+
const cardWidth = scrollWidth / carouselCardsRef.current.length;
|
|
90
|
+
|
|
91
|
+
const res = Math.floor(cardWidth - cardWidth * 0.05);
|
|
92
|
+
|
|
93
|
+
carouselElementRef.current.scrollBy({
|
|
94
|
+
left: direction === 'right' ? res : -res,
|
|
95
|
+
behavior: 'smooth',
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const handleOnKeyDown = (
|
|
101
|
+
event: React.KeyboardEvent<HTMLAnchorElement | HTMLDivElement>,
|
|
102
|
+
index: number,
|
|
103
|
+
) => {
|
|
104
|
+
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
|
|
105
|
+
const nextIndex = event.key === 'ArrowRight' ? index + 1 : index - 1;
|
|
106
|
+
const nextCard = cards[nextIndex];
|
|
107
|
+
if (nextCard) {
|
|
108
|
+
const ref = carouselCardsRef.current[nextIndex];
|
|
109
|
+
if (ref.type === 'promo') {
|
|
110
|
+
ref.anchorElement?.focus();
|
|
111
|
+
} else {
|
|
112
|
+
ref.cardElement?.focus();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
scrollCardIntoView(carouselCardsRef.current[nextIndex].cardElement, nextCard);
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
121
|
+
event.currentTarget.click();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const scrollCardIntoView = (element: HTMLElement, card: CarouselCard) => {
|
|
126
|
+
element.scrollIntoView({
|
|
127
|
+
behavior: 'smooth',
|
|
128
|
+
block: 'nearest',
|
|
129
|
+
inline: 'center',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
setFocusedCard(card.id);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
updateScrollButtonsState();
|
|
137
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
138
|
+
}, [scrollPosition]);
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
window.addEventListener('resize', updateScrollButtonsState);
|
|
142
|
+
|
|
143
|
+
return () => {
|
|
144
|
+
window.removeEventListener('resize', updateScrollButtonsState);
|
|
145
|
+
};
|
|
146
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
147
|
+
}, []);
|
|
148
|
+
|
|
149
|
+
const addElementToCardsRefArray = (index: number, ref: Partial<CardsReference>) => {
|
|
150
|
+
if (ref) {
|
|
151
|
+
// eslint-disable-next-line functional/immutable-data
|
|
152
|
+
carouselCardsRef.current[index] = {
|
|
153
|
+
type: ref.type ?? carouselCardsRef.current?.[index]?.type,
|
|
154
|
+
cardElement: ref.cardElement ?? carouselCardsRef.current?.[index]?.cardElement,
|
|
155
|
+
anchorElement: ref.anchorElement ?? carouselCardsRef.current?.[index]?.anchorElement,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div className={classNames('carousel-wrapper', className)}>
|
|
162
|
+
<div className="d-flex justify-content-between carousel__header">
|
|
163
|
+
{typeof header === 'string' ? (
|
|
164
|
+
<Title as="span" type="title-body">
|
|
165
|
+
{header}
|
|
166
|
+
</Title>
|
|
167
|
+
) : (
|
|
168
|
+
header
|
|
169
|
+
)}
|
|
170
|
+
{areActionButtonsEnabled ? (
|
|
171
|
+
<div className="hidden-xs">
|
|
172
|
+
<ActionButton
|
|
173
|
+
className="carousel__scroll-button"
|
|
174
|
+
tabIndex={-1}
|
|
175
|
+
priority="secondary"
|
|
176
|
+
disabled={!isLeftActionButtonEnabled}
|
|
177
|
+
aria-hidden="true"
|
|
178
|
+
data-testid="scroll-carousel-left"
|
|
179
|
+
onClick={() => scrollCarousel('left')}
|
|
180
|
+
>
|
|
181
|
+
<ChevronLeft />
|
|
182
|
+
</ActionButton>
|
|
183
|
+
<ActionButton
|
|
184
|
+
tabIndex={-1}
|
|
185
|
+
className="carousel__scroll-button m-l-1"
|
|
186
|
+
priority="secondary"
|
|
187
|
+
aria-hidden="true"
|
|
188
|
+
data-testid="scroll-carousel-right"
|
|
189
|
+
disabled={scrollIsAtEnd}
|
|
190
|
+
onClick={() => scrollCarousel()}
|
|
191
|
+
>
|
|
192
|
+
<ChevronRight />
|
|
193
|
+
</ActionButton>
|
|
194
|
+
</div>
|
|
195
|
+
) : null}
|
|
196
|
+
</div>
|
|
197
|
+
<div
|
|
198
|
+
ref={carouselElementRef}
|
|
199
|
+
tabIndex={-1}
|
|
200
|
+
role="list"
|
|
201
|
+
className="carousel"
|
|
202
|
+
onScroll={(event) => {
|
|
203
|
+
const target = event.target as HTMLElement;
|
|
204
|
+
setScrollPosition(target.scrollLeft);
|
|
205
|
+
}}
|
|
206
|
+
>
|
|
207
|
+
{cards?.map((card, index) => {
|
|
208
|
+
const sharedProps = {
|
|
209
|
+
id: card.id,
|
|
210
|
+
className: classNames('carousel__card', {
|
|
211
|
+
'carousel__card--focused': card.id === focusedCard,
|
|
212
|
+
}),
|
|
213
|
+
onClick: () => {
|
|
214
|
+
card.onClick?.();
|
|
215
|
+
onClick?.(card.id);
|
|
216
|
+
},
|
|
217
|
+
onFocus: (event: React.FocusEvent<HTMLAnchorElement | HTMLDivElement>) => {
|
|
218
|
+
scrollCardIntoView(event.currentTarget, card);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const cardContent =
|
|
223
|
+
card.type !== 'promo' ? (
|
|
224
|
+
<div
|
|
225
|
+
id={`${card.id}-content`}
|
|
226
|
+
className={classNames('carousel__card-content', {
|
|
227
|
+
[card.className ?? '']: !!card.className,
|
|
228
|
+
})}
|
|
229
|
+
// eslint-disable-next-line react/forbid-dom-props
|
|
230
|
+
style={card.styles}
|
|
231
|
+
>
|
|
232
|
+
{card.content}
|
|
233
|
+
</div>
|
|
234
|
+
) : null;
|
|
235
|
+
|
|
236
|
+
if (card.type === 'button') {
|
|
237
|
+
return (
|
|
238
|
+
<div key={card.id} aria-labelledby={`${card.id}-content`} role="listitem">
|
|
239
|
+
<div
|
|
240
|
+
{...sharedProps}
|
|
241
|
+
ref={(el) => {
|
|
242
|
+
if (el) {
|
|
243
|
+
// eslint-disable-next-line functional/immutable-data
|
|
244
|
+
carouselCardsRef.current[index] = {
|
|
245
|
+
type: 'default',
|
|
246
|
+
cardElement: el,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}}
|
|
250
|
+
role="button"
|
|
251
|
+
tabIndex={0}
|
|
252
|
+
onKeyDown={(event) => handleOnKeyDown(event, index)}
|
|
253
|
+
>
|
|
254
|
+
{cardContent}
|
|
255
|
+
</div>
|
|
256
|
+
</div>
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (card.type === 'promo') {
|
|
261
|
+
return (
|
|
262
|
+
<div key={card.id} id={card.id} role="listitem" aria-labelledby={`${card.id}-anchor`}>
|
|
263
|
+
<PromoCard
|
|
264
|
+
{...{ ...card, type: undefined }}
|
|
265
|
+
{...{ ...sharedProps }}
|
|
266
|
+
ref={(el: HTMLDivElement | null) => {
|
|
267
|
+
if (el) {
|
|
268
|
+
addElementToCardsRefArray(index, {
|
|
269
|
+
type: 'promo',
|
|
270
|
+
cardElement: el,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}}
|
|
274
|
+
anchorRef={(el: HTMLAnchorElement) => {
|
|
275
|
+
if (el) {
|
|
276
|
+
addElementToCardsRefArray(index, {
|
|
277
|
+
type: 'promo',
|
|
278
|
+
anchorElement: el,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}}
|
|
282
|
+
anchorId={`${card.id}-anchor`}
|
|
283
|
+
onKeyDown={(event) => handleOnKeyDown(event, index)}
|
|
284
|
+
/>
|
|
285
|
+
</div>
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return (
|
|
290
|
+
<div key={card.id} aria-labelledby={`${card.id}-content`} role="listitem">
|
|
291
|
+
<a
|
|
292
|
+
{...sharedProps}
|
|
293
|
+
ref={(el) => {
|
|
294
|
+
if (el) {
|
|
295
|
+
// eslint-disable-next-line functional/immutable-data
|
|
296
|
+
carouselCardsRef.current[index] = {
|
|
297
|
+
type: 'default',
|
|
298
|
+
cardElement: el,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}}
|
|
302
|
+
href={card.href}
|
|
303
|
+
rel="noreferrer"
|
|
304
|
+
onKeyDown={(event) => handleOnKeyDown(event, index)}
|
|
305
|
+
>
|
|
306
|
+
{cardContent}
|
|
307
|
+
</a>
|
|
308
|
+
</div>
|
|
309
|
+
);
|
|
310
|
+
})}
|
|
311
|
+
</div>
|
|
312
|
+
<div className="visible-xs">
|
|
313
|
+
<div className="carousel__indicators">
|
|
314
|
+
{cards?.map((card, index) => (
|
|
315
|
+
<button
|
|
316
|
+
key={`${card.id}-indicator`}
|
|
317
|
+
data-testid={`${card.id}-indicator`}
|
|
318
|
+
tabIndex={-1}
|
|
319
|
+
aria-hidden
|
|
320
|
+
type="button"
|
|
321
|
+
className={classNames('carousel__indicator', {
|
|
322
|
+
'carousel__indicator--selected': card.id === visibleCardOnMobileView,
|
|
323
|
+
})}
|
|
324
|
+
onClick={() => {
|
|
325
|
+
scrollCardIntoView(carouselCardsRef.current[index].cardElement, card);
|
|
326
|
+
}}
|
|
327
|
+
/>
|
|
328
|
+
))}
|
|
329
|
+
</div>
|
|
330
|
+
</div>
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const isVisible = (container: HTMLElement, el: HTMLElement) => {
|
|
336
|
+
const cWidth = container.offsetWidth;
|
|
337
|
+
const cScrollOffset = container.scrollLeft;
|
|
338
|
+
|
|
339
|
+
const elemLeft = el.offsetLeft - container.offsetLeft;
|
|
340
|
+
const elemRight = elemLeft + el.offsetWidth;
|
|
341
|
+
|
|
342
|
+
return elemLeft >= cScrollOffset && elemRight <= cScrollOffset + cWidth;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
export default Carousel;
|