-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathbutton.jsx
338 lines (305 loc) · 8.87 KB
/
button.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* @flow */
/** @jsx node */
import type { FundingEligibilityType } from "@paypal/sdk-client/src";
import {
COUNTRY,
FUNDING,
ENV,
type LocaleType,
} from "@paypal/sdk-constants/src";
import { node, type ElementNode } from "@krakenjs/jsx-pragmatic/src";
import { LOGO_COLOR, LOGO_CLASS } from "@paypal/sdk-logos/src";
import {
noop,
preventClickFocus,
isBrowser,
isElement,
} from "@krakenjs/belter/src";
import type {
ContentType,
Wallet,
Experiment,
WalletInstrument,
} from "../../types";
import {
ATTRIBUTE,
CLASS,
BUTTON_COLOR,
BUTTON_NUMBER,
TEXT_COLOR,
BUTTON_FLOW,
} from "../../constants";
import { getFundingConfig } from "../../funding";
import type {
ButtonStyle,
Personalization,
OnShippingChange,
OnShippingAddressChange,
OnShippingOptionsChange,
} from "./props";
import { Spinner } from "./spinner";
import { MenuButton } from "./menu-button";
import { isBorderRadiusNumber, checkLabelEligibility } from "./util";
type IndividualButtonProps = {|
style: ButtonStyle,
fundingSource: $Values<typeof FUNDING>,
multiple: boolean,
buyerCountry: $Values<typeof COUNTRY>,
locale: LocaleType,
onClick?: Function,
env: $Values<typeof ENV>,
wallet?: ?Wallet,
fundingEligibility: FundingEligibilityType,
onShippingChange: ?OnShippingChange,
onShippingAddressChange: ?OnShippingAddressChange,
onShippingOptionsChange: ?OnShippingOptionsChange,
i: number,
nonce: string,
userIDToken: ?string,
customerId: ?string,
personalization: ?Personalization,
content: ?ContentType,
tagline: ?boolean,
commit: boolean,
experiment: Experiment,
flow: $Values<typeof BUTTON_FLOW>,
vault: boolean,
merchantFundingSource: ?$Values<typeof FUNDING>,
instrument: ?WalletInstrument,
showPayLabel: boolean,
|};
export function Button({
buyerCountry,
commit,
content,
customerId,
env,
experiment,
flow,
fundingEligibility,
fundingSource,
i,
instrument,
locale,
multiple,
nonce,
onClick = noop,
personalization,
showPayLabel,
style,
tagline,
userIDToken,
vault,
}: IndividualButtonProps): ElementNode {
const { layout, shape, borderRadius } = style;
const { isPaypalRebrandEnabled, defaultBlueButtonColor } = experiment;
const fundingConfig = getFundingConfig()[fundingSource];
if (!fundingConfig) {
throw new Error(`Can not find funding config for ${fundingSource}`);
}
const colors = fundingConfig.colors;
const secondaryColors = fundingConfig.secondaryColors || {};
let { color, period, label } = style;
// if no color option is passed in via style props
if (color === "" || typeof color === "undefined") {
// if a single button is rendered, we set color to first option in the fundingSource config
color = colors[0];
// if multiple buttons are being rendered (smart stack), we set default color as gold > first
if (multiple) {
color = "gold";
}
}
// validate the first button rendered has a valid color
// this check is needed to validate the first button in a smart stack gets the correct color
if (i === 0 && !colors.includes(color)) {
color = colors[0];
}
if (isPaypalRebrandEnabled) {
color =
(defaultBlueButtonColor === "gold"
? (color === "" ? "gold" : color) || "gold"
: defaultBlueButtonColor) || "gold";
}
// The secondary colors are used to render the smart stack (multiple buttons)
// they keep track of the mapping of the color style prop to the
if (multiple && i > 0) {
if (
secondaryColors[color] &&
colors.indexOf(secondaryColors[color] !== -1)
) {
color = secondaryColors[color];
} else if (colors.indexOf(secondaryColors[BUTTON_COLOR.DEFAULT]) !== -1) {
color = secondaryColors[BUTTON_COLOR.DEFAULT];
} else {
color = colors[0];
}
}
const { logoColors, textColors } = fundingConfig;
const logoColor =
logoColors[color] || logoColors[LOGO_COLOR.DEFAULT] || LOGO_COLOR.DEFAULT;
const textColor =
textColors[color] || textColors[TEXT_COLOR.DEFAULT] || TEXT_COLOR.DEFAULT;
const { Label, WalletLabel, Logo, showWalletMenu } = fundingConfig;
const clickHandler = (event, opts) => {
event.preventDefault();
event.stopPropagation();
onClick(event, { fundingSource, ...opts });
};
const keypressHandler = (event: KeyboardEvent, opts) => {
if (event.keyCode === 13 || event.keyCode === 32) {
clickHandler(event, opts);
}
};
const onButtonRender = (el) => {
if (isBrowser() && isElement(el)) {
preventClickFocus(el);
}
};
const eligibleLabel = checkLabelEligibility(label, buyerCountry);
function getAriaLabel(): string {
let labelText =
typeof fundingConfig.labelText === "function"
? fundingConfig.labelText({
buyerCountry,
content,
fundingEligibility,
label: eligibleLabel,
period,
})
: fundingConfig.labelText || fundingSource;
if (!showPayLabel && instrument?.vendor && instrument.label) {
labelText = instrument.secondaryInstruments
? `${instrument.secondaryInstruments[0].type} & ${instrument.vendor} ${instrument.label}`
: `${instrument.vendor} ${instrument.label}`;
}
return labelText;
}
const labelText = getAriaLabel();
const logoNode = (
<Logo
label={eligibleLabel}
locale={locale}
logoColor={logoColor}
fundingEligibility={fundingEligibility}
onClick={clickHandler}
onKeyPress={keypressHandler}
nonce={nonce}
experiment={experiment}
env={env}
/>
);
let labelNode = (
<Label
i={i}
logo={logoNode}
label={eligibleLabel}
nonce={nonce}
locale={locale}
logoColor={logoColor}
period={period}
layout={layout}
multiple={multiple}
fundingEligibility={fundingEligibility}
onClick={clickHandler}
onKeyPress={keypressHandler}
personalization={personalization}
tagline={tagline}
content={content}
experiment={experiment}
/>
);
let isWallet = false;
if (
WalletLabel &&
(!showPayLabel ||
flow === BUTTON_FLOW.PURCHASE ||
flow === BUTTON_FLOW.VAULT_WITHOUT_PURCHASE) &&
(instrument ||
(__WEB__ &&
(userIDToken || customerId) &&
(fundingSource === FUNDING.PAYPAL || fundingSource === FUNDING.VENMO)))
) {
labelNode = (
<WalletLabel
nonce={nonce}
logoColor={logoColor}
instrument={instrument}
locale={locale}
content={content}
commit={commit}
experiment={experiment}
vault={vault}
textColor={textColor}
fundingSource={fundingSource}
showPayLabel={showPayLabel}
/>
);
isWallet = true;
}
const shouldShowWalletMenu =
isWallet && instrument && showWalletMenu({ instrument, userIDToken });
const borderRadiusClass = isBorderRadiusNumber(borderRadius)
? CLASS.BORDER_RADIUS
: `${CLASS.SHAPE}-${shape}`;
return (
<div
class={[
CLASS.BUTTON_ROW,
`${CLASS.NUMBER}-${i}`,
`${CLASS.LAYOUT}-${layout}`,
`${CLASS.NUMBER}-${
multiple ? BUTTON_NUMBER.MULTIPLE : BUTTON_NUMBER.SINGLE
}`,
`${CLASS.ENV}-${env}`,
`${CLASS.COLOR}-${color}`,
`${CLASS.TEXT_COLOR}-${textColor}`,
`${LOGO_CLASS.LOGO_COLOR}-${logoColor}`,
`${isWallet ? CLASS.WALLET : ""}`,
`${shouldShowWalletMenu ? CLASS.WALLET_MENU : ""}`,
`${borderRadiusClass}`,
].join(" ")}
>
<div
role="link"
{...{
[ATTRIBUTE.BUTTON]: true,
[ATTRIBUTE.FUNDING_SOURCE]: fundingSource,
[ATTRIBUTE.PAYMENT_METHOD_ID]: instrument ? instrument.tokenID : null,
[ATTRIBUTE.INSTRUMENT_ID]: instrument
? instrument.instrumentID
: null,
[ATTRIBUTE.INSTRUMENT_TYPE]: instrument ? instrument.type : null,
[ATTRIBUTE.SECONDARY_INSTRUMENT_TYPE]:
instrument?.secondaryInstruments
? instrument.secondaryInstruments[0].type
: null,
}}
class={[
CLASS.BUTTON,
`${CLASS.NUMBER}-${i}`,
`${CLASS.LAYOUT}-${layout}`,
`${CLASS.NUMBER}-${
multiple ? BUTTON_NUMBER.MULTIPLE : BUTTON_NUMBER.SINGLE
}`,
`${CLASS.ENV}-${env}`,
`${CLASS.COLOR}-${color}`,
`${CLASS.TEXT_COLOR}-${textColor}`,
`${LOGO_CLASS.LOGO_COLOR}-${logoColor}`,
`${isWallet ? CLASS.WALLET : ""}`,
`${borderRadiusClass}`,
].join(" ")}
onClick={clickHandler}
onRender={onButtonRender}
onKeyPress={keypressHandler}
tabindex="0"
aria-label={labelText}
>
<div class={CLASS.BUTTON_LABEL}>{labelNode}</div>
<Spinner />
</div>
{shouldShowWalletMenu ? (
<MenuButton textColor={textColor} content={content} />
) : null}
</div>
);
}