@navinc/base-react-components
Version:
Nav's Pattern Library
82 lines • 4.5 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect } from 'react';
import styled, { useTheme } from 'styled-components';
import { loadScript, global, noop } from '@navinc/utils';
import { Copy } from './copy.js';
import { Input, Label, FieldWrapper, Field, Errors, Err } from './form-elements/shared.js';
export const getStripeDefaults = (theme) => ({
stripe: {
fonts: [
{
family: 'Averta',
src: `local("Averta"),
local("averta"),
url('https://dxkdvuv3hanyu.cloudfront.net/webfonts/Averta-Regular.eot?#iefix') format('embedded-opentype'),
url('https://dxkdvuv3hanyu.cloudfront.net/webfonts/Averta-Regular.woff2') format('woff2'),
url('https://dxkdvuv3hanyu.cloudfront.net/webfonts/Averta-Regular.woff') format('woff'),
url('https://dxkdvuv3hanyu.cloudfront.net/webfonts/Averta-Regular.ttf') format('truetype')`,
weight: 400,
style: 'normal',
},
],
},
elements: {
classes: {
base: 'c-input__input-field c-input__input-field--stripe',
empty: 'is-empty',
focus: 'is-focused',
complete: 'is-focused',
invalid: 'is-focused',
},
hideIcon: false,
style: {
base: {
color: theme.navNeutralDark,
fontFamily: 'Averta',
fontSize: '16px',
lineHeight: '1.8',
},
invalid: {
color: theme.navStatusNegative,
},
},
},
});
export const CCInputWrapper = styled(Input).attrs(() => ({ as: 'div' })).withConfig({ displayName: "brc-sc-CCInputWrapper", componentId: "brc-sc-1y4e3kz" }) ``;
const CC_INPUT_CLASS_NAME = 'js-stripe_card';
export const CCInput = (_a) => {
var { className = '', label, hasSpaceForErrors, required, errors = [], lede = '', onChange = noop, createPaymentMethodRef = noop, createTokenRef = noop } = _a, props = __rest(_a, ["className", "label", "hasSpaceForErrors", "required", "errors", "lede", "onChange", "createPaymentMethodRef", "createTokenRef"]);
const theme = useTheme();
useEffect(() => {
Promise.resolve(global.Stripe || loadScript('https://js.stripe.com/v3/').then(() => global.Stripe))
.then((stripe) => { var _a, _b, _c; return stripe((_c = (_b = (_a = global === null || global === void 0 ? void 0 : global.process) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b.CLIENT_BILLING_PUBLIC_KEY) !== null && _c !== void 0 ? _c : ''); })
.then((stripe) => {
const stripeDefaults = getStripeDefaults(theme);
// TODO: fix so `as any` isn't needed
const elements = stripe.elements(stripeDefaults.stripe);
const cc = elements.create('card', stripeDefaults.elements);
cc.mount(`.${CC_INPUT_CLASS_NAME}`);
cc.addEventListener('change', (changeEvent) => {
onChange(changeEvent);
});
createTokenRef((billingInfo) => stripe.createToken(cc, billingInfo));
createPaymentMethodRef((billingInfo) => stripe.createPaymentMethod('card', cc, billingInfo));
return stripe;
})
.catch((err) => console.error('Stripe failed to initialize! ', err));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (_jsxs(FieldWrapper, { className: className, children: [lede && _jsx(Copy, { bold: true, children: lede }), _jsxs(Field, { isVisited: true, children: [_jsx(CCInputWrapper, Object.assign({ "data-heap-redact-text": "true", className: `js-private ${CC_INPUT_CLASS_NAME}` }, props)), _jsx(Label, { required: required, children: label })] }), _jsx(Errors, { hasSpaceForErrors: hasSpaceForErrors, children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) })] }));
};
//# sourceMappingURL=cc-input.js.map