@navinc/base-react-components
Version:
Nav's Pattern Library
82 lines • 4.62 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 from 'styled-components';
import { loadScript, global } from '@navinc/utils';
import noop from '@navinc/utils/noop.js';
import Copy from './copy';
import { Input, Label, FieldWrapper, Field, Errors, Err } from './form-elements/shared.js';
import * as theme from './theme.js';
export const stripeDefaults = {
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.neutral400,
fontFamily: 'Averta',
fontSize: '16px',
lineHeight: '1.8',
},
invalid: {
color: theme.error,
},
},
},
};
export const CCInputWrapper = styled(Input).attrs(() => ({ as: 'div' })) ``;
const CC_INPUT_CLASS_NAME = 'js-stripe_card';
export const CCInput = (_a) => {
var { className = '', label, hasSpaceForErrors, isInvalid, value, required, type, errors = [], lede = '', onChange = noop, onBlur = noop, onFocus = noop, createPaymentMethodRef = noop, createTokenRef = noop } = _a, props = __rest(_a, ["className", "label", "hasSpaceForErrors", "isInvalid", "value", "required", "type", "errors", "lede", "onChange", "onBlur", "onFocus", "createPaymentMethodRef", "createTokenRef"]);
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 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, Object.assign({ className: className }, { children: [lede && _jsx(Copy, Object.assign({ bold: true }, { children: lede }), void 0), _jsxs(Field, Object.assign({ value: value, isVisited: true, isInvalid: isInvalid, required: required, type: type }, { children: [_jsx(CCInputWrapper, Object.assign({ className: `js-private ${CC_INPUT_CLASS_NAME}` }, props), void 0), _jsx(Label, Object.assign({ required: required, value: value }, { children: label }), void 0)] }), void 0), _jsx(Errors, Object.assign({ hasSpaceForErrors: hasSpaceForErrors }, { children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) }), void 0)] }), void 0));
};
export default CCInput;
//# sourceMappingURL=cc-input.js.map