@transferwise/components 0.0.0-experimental-5125346 → 0.0.0-experimental-2220d83

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.
@@ -0,0 +1,230 @@
1
+ import type { StoryObj } from '@storybook/react';
2
+ import { Flag } from '@wise/art';
3
+ import SelectOption, {
4
+ SelectOptionProps,
5
+ SelectOptionValue,
6
+ SelectOptiopsSection,
7
+ } from './SelectOption';
8
+ import { Bank, BankTransfer, Beach, Briefcase, Card, Plane } from '@transferwise/icons';
9
+ import { Field } from '../field/Field';
10
+ import { lorem10 } from '../test-utils';
11
+ import Badge from '../badge';
12
+ import Avatar from '../avatar';
13
+ import { Sentiment } from '../common';
14
+
15
+ export default {
16
+ title: 'Option/SelectOption',
17
+ };
18
+
19
+ type Story = StoryObj<typeof SelectOption>;
20
+
21
+ type CustomData = { data?: string };
22
+
23
+ function cardPaymentMethod(index?: number): SelectOptionValue<CustomData> {
24
+ return {
25
+ media: <Card />,
26
+ title: `Credit card ${index}`,
27
+ content: (
28
+ <>
29
+ <div>Transfer the money to Wise using your bank account.</div>
30
+ <div>0.32 GBP in fees, should arrive in seconds</div>
31
+ </>
32
+ ),
33
+ };
34
+ }
35
+
36
+ const recentPaymentMethods: SelectOptionValue<CustomData>[] = [
37
+ cardPaymentMethod(1),
38
+ cardPaymentMethod(2),
39
+ ];
40
+
41
+ const allOtherPaymentMethods: SelectOptionValue<CustomData>[] = [
42
+ {
43
+ media: <Flag code="gbp" />,
44
+ title: 'Wise GBP balance',
45
+ content: (
46
+ <>
47
+ <span>300 GBP available</span>
48
+ <span>0 GBP in fees, should arrive in seconds</span>
49
+ </>
50
+ ),
51
+ disabled: true,
52
+ value: { data: lorem10 },
53
+ },
54
+ cardPaymentMethod(),
55
+ {
56
+ media: <Card />,
57
+ title: 'Debit card',
58
+ content: (
59
+ <>
60
+ <span>Send from your Visa or Mastercard.</span>
61
+ <span>0.74 GBP in fees, should arrive in seconds</span>
62
+ </>
63
+ ),
64
+ },
65
+ {
66
+ media: <Bank />,
67
+ title: 'Swift Transfer',
68
+ content: (
69
+ <>
70
+ <span>Send money internationally. Your bank will charge you extra fees.</span>
71
+ <span>0.32 GBP in fees, should arrive by Thursday</span>
72
+ </>
73
+ ),
74
+ },
75
+ {
76
+ media: <BankTransfer />,
77
+ title: 'Bank Transfer',
78
+ content: (
79
+ <>
80
+ <span>Transfer the money to Wise using your bank account.</span>
81
+ <span>0.32 GBP in fees, should arrive in seconds</span>
82
+ </>
83
+ ),
84
+ },
85
+ {
86
+ media: <BankTransfer />,
87
+ title: 'Withdraw from your U.S. bank account (ACH)',
88
+ content: (
89
+ <>
90
+ <span>1.63 USD in total fees.</span>
91
+ <span>
92
+ Pay with ACH using the bank account you’ve connected to Wise. Should arrive in seconds.
93
+ </span>
94
+ </>
95
+ ),
96
+ },
97
+ cardPaymentMethod(1),
98
+ cardPaymentMethod(2),
99
+ cardPaymentMethod(3),
100
+ cardPaymentMethod(4),
101
+ cardPaymentMethod(5),
102
+ cardPaymentMethod(6),
103
+ ];
104
+
105
+ const paymentMethods: SelectOptiopsSection<CustomData>[] = [
106
+ {
107
+ title: 'Recently used',
108
+ options: recentPaymentMethods,
109
+ },
110
+ {
111
+ title: 'Payment methods',
112
+ options: allOtherPaymentMethods,
113
+ },
114
+ ];
115
+
116
+ const balances: SelectOptiopsSection[] = [
117
+ {
118
+ title: 'Balances',
119
+ options: [
120
+ {
121
+ media: <Flag code="gbp" />,
122
+ title: 'Wise GBP balance',
123
+ },
124
+ {
125
+ media: <Flag code="eur" />,
126
+ title: 'Wise EUR balance',
127
+ },
128
+ ],
129
+ },
130
+ {
131
+ title: 'Jars',
132
+ options: [
133
+ {
134
+ media: (
135
+ <Badge badge={<Flag code="usd" />} size="md">
136
+ <Avatar type="icon" size="md" backgroundColor="var(--color-bright-pink)">
137
+ <Beach size="24" />
138
+ </Avatar>
139
+ </Badge>
140
+ ),
141
+ title: 'Hawaii Holiday',
142
+ content: 'Wise USD jar',
143
+ },
144
+ {
145
+ media: (
146
+ <Badge badge={<Flag code="aed" />} size="md">
147
+ <Avatar type="icon" size="md" backgroundColor="var(--color-bright-yellow)">
148
+ <Briefcase size="24" />
149
+ </Avatar>
150
+ </Badge>
151
+ ),
152
+ title: 'Emirates Business Trip',
153
+ content: 'Wise AED jar',
154
+ },
155
+ {
156
+ media: (
157
+ <Badge badge={<Flag code="jpy" />} size="md">
158
+ <Avatar type="icon" size="md" backgroundColor="var(--color-bright-blue)">
159
+ <Plane size="24" />
160
+ </Avatar>
161
+ </Badge>
162
+ ),
163
+ title: 'Trip to Mars',
164
+ content: 'Wise Jpy jar',
165
+ },
166
+ ],
167
+ },
168
+ ];
169
+
170
+ export const Basic: Story = {
171
+ args: {
172
+ placeholder: { title: 'No balance selected' },
173
+ options: balances,
174
+ onChange: (value) => {
175
+ console.log('selected balance', value);
176
+ },
177
+ },
178
+ render: (args: SelectOptionProps) => {
179
+ return (
180
+ <div className="row">
181
+ <div className="col-md-8 col-md-offset-2">
182
+ <Field label="Balances">
183
+ <SelectOption {...args} />
184
+ </Field>
185
+ </div>
186
+ </div>
187
+ );
188
+ },
189
+ };
190
+
191
+ export const Error: Story = {
192
+ render: () => {
193
+ function onChange(value: SelectOptionValue<CustomData>): void {
194
+ console.log(value);
195
+ }
196
+ return (
197
+ <div className="row">
198
+ <div className="col-md-8 col-md-offset-2">
199
+ <Field
200
+ label={<>Payment method</>}
201
+ sentiment={Sentiment.NEGATIVE}
202
+ message="Just an example of validation message"
203
+ >
204
+ <SelectOption<CustomData>
205
+ aria-label="Choose one of payment methods"
206
+ placeholder={{ title: 'No method selected', actionLabel: 'Select' }}
207
+ options={paymentMethods}
208
+ onChange={onChange}
209
+ />
210
+ </Field>
211
+ </div>
212
+
213
+ <div className="col-md-8 col-md-offset-2">
214
+ <Field
215
+ label={<>Payment method</>}
216
+ sentiment={Sentiment.NEGATIVE}
217
+ message="Example of disabled select option"
218
+ >
219
+ <SelectOption<CustomData>
220
+ disabled
221
+ placeholder={{ title: 'No method selected' }}
222
+ options={paymentMethods}
223
+ onChange={onChange}
224
+ />
225
+ </Field>
226
+ </div>
227
+ </div>
228
+ );
229
+ },
230
+ };
@@ -0,0 +1,155 @@
1
+ import { useRef, useState } from 'react';
2
+ import { ActionButtonProps } from '../actionButton';
3
+ import Chevron from '../chevron';
4
+ import classNames from 'classnames';
5
+ import Option from '../common/Option';
6
+ import type { OptionProps } from '../common/Option/Option';
7
+ import { Breakpoint, Position } from '../common';
8
+ import Section from '../section';
9
+ import Header from '../header';
10
+ import { HeaderProps } from '../header/Header';
11
+ import NavigationOption from '../navigationOption';
12
+ import NavigationOptionsList from '../navigationOptionsList';
13
+ import { useInputAttributes } from '../inputs/contexts';
14
+ import messages from './SelectOption.messages';
15
+ import { useIntl } from 'react-intl';
16
+ import ResponsivePanel from '../common/responsivePanel';
17
+ import { useScreenSize } from '../common/hooks/useScreenSize';
18
+ import { Plus } from '@transferwise/icons';
19
+
20
+ export type SelectOptiopsSection<T = unknown> = {
21
+ title?: HeaderProps['title'];
22
+ options: SelectOptionValue<T>[];
23
+ };
24
+
25
+ export type SelectOptionValue<T = unknown> = Pick<
26
+ OptionProps,
27
+ 'media' | 'title' | 'content' | 'disabled'
28
+ > & {
29
+ value?: T;
30
+ };
31
+
32
+ export type SelectOptionPlaceholder = Pick<OptionProps, 'media' | 'title' | 'content'> & {
33
+ actionLabel?: ActionButtonProps['children'];
34
+ };
35
+
36
+ export type SelectOptionProps<T = unknown> = {
37
+ onChange: (selected: SelectOptionValue<T>) => void;
38
+ selected?: SelectOptionValue<T>;
39
+ options: SelectOptiopsSection<T>[];
40
+ placeholder: SelectOptionPlaceholder;
41
+ } & Omit<
42
+ OptionProps,
43
+ 'as' | 'title' | 'media' | 'content' | 'onClick' | 'onChange' | 'showMediaAtAllSizes' | 'decision'
44
+ >;
45
+
46
+ export default function SelectOption<T>({
47
+ selected: selectedValueProp = undefined,
48
+ options,
49
+ onChange,
50
+ placeholder,
51
+ disabled,
52
+ ...props
53
+ }: SelectOptionProps<T>) {
54
+ const intl = useIntl();
55
+ const rootRef = useRef(null);
56
+ const [selected, setSelected] = useState(selectedValueProp);
57
+ const [showOptions, setShowOptions] = useState(false);
58
+
59
+ const hasSelected = selected !== undefined;
60
+ const isLargeScreen = useScreenSize(Breakpoint.SMALL);
61
+
62
+ const inputAttributes = useInputAttributes();
63
+ const ariaLabelledBy = props['aria-labelledby'] ?? inputAttributes?.['aria-labelledby'];
64
+
65
+ function handleOnClick(showOptionsStatus: boolean) {
66
+ return () => {
67
+ setShowOptions(showOptionsStatus);
68
+ };
69
+ }
70
+
71
+ function handleOnChange(data: SelectOptionValue<T>) {
72
+ return () => {
73
+ setShowOptions(false);
74
+ setSelected(data);
75
+ onChange(data);
76
+ };
77
+ }
78
+
79
+ function getOptions(isLargeScreen = false) {
80
+ return (
81
+ <div className={classNames({ 'np-select-option-list': isLargeScreen })}>
82
+ {options.map((optionsSection) => (
83
+ // eslint-disable-next-line react/jsx-key
84
+ <Section
85
+ className={classNames('np-select-option-section', { 'p-x-2 p-y-1': isLargeScreen })}
86
+ >
87
+ {optionsSection.title && <Header title={optionsSection.title} />}
88
+ <NavigationOptionsList>
89
+ {optionsSection.options.map((option) => {
90
+ return (
91
+ // eslint-disable-next-line react/jsx-key
92
+ <NavigationOption
93
+ isContainerAligned={!isLargeScreen}
94
+ showMediaCircle
95
+ showMediaAtAllSizes
96
+ onClick={handleOnChange(option)}
97
+ {...option}
98
+ />
99
+ );
100
+ })}
101
+ </NavigationOptionsList>
102
+ </Section>
103
+ ))}
104
+ </div>
105
+ );
106
+ }
107
+
108
+ return (
109
+ <>
110
+ <Option
111
+ ref={rootRef}
112
+ showMediaAtAllSizes
113
+ disabled={disabled}
114
+ decision={false}
115
+ media={hasSelected ? selected.media : placeholder.media ?? <Plus size={24} />}
116
+ title={(hasSelected ? selected : placeholder).title}
117
+ content={(hasSelected ? selected : placeholder).content}
118
+ className={classNames(
119
+ 'np-select-option',
120
+ 'clickable',
121
+ hasSelected ? 'np-select-option-selected' : 'np-select-option-placeholder',
122
+ props.className,
123
+ )}
124
+ button={
125
+ <button
126
+ type="button"
127
+ disabled={disabled}
128
+ aria-haspopup="dialog"
129
+ aria-expanded={showOptions}
130
+ aria-labelledby={ariaLabelledBy}
131
+ className={hasSelected ? 'btn-unstyled' : 'np-action-btn'}
132
+ aria-label={hasSelected ? undefined : props['aria-label']}
133
+ onClick={handleOnClick(true)}
134
+ >
135
+ {hasSelected ? (
136
+ <Chevron />
137
+ ) : (
138
+ placeholder.actionLabel || intl.formatMessage(messages.actionLabel)
139
+ )}
140
+ </button>
141
+ }
142
+ />
143
+ <ResponsivePanel
144
+ anchorWidth
145
+ altAxis
146
+ anchorRef={rootRef}
147
+ open={showOptions}
148
+ position={Position.BOTTOM}
149
+ onClose={handleOnClick(false)}
150
+ >
151
+ {getOptions(isLargeScreen)}
152
+ </ResponsivePanel>
153
+ </>
154
+ );
155
+ }
@@ -0,0 +1,2 @@
1
+ export { default } from './SelectOption';
2
+ export type { SelectOptionProps, SelectOptionValue, SelectOptiopsSection } from './SelectOption';
package/src/ssr.spec.js CHANGED
@@ -226,6 +226,7 @@ describe('Server side rendering', () => {
226
226
  DateLookup: { value: new Date() },
227
227
  Link: { size: 16 },
228
228
  Tooltip: { children: <>yo</> },
229
+ SelectOption: { placeholder: { media: <img alt="img" /> } },
229
230
  };
230
231
 
231
232
  const { Provider } = exposedLibraryItems;