Skip to content

Instantly share code, notes, and snippets.

@HaNdTriX
Last active November 23, 2021 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HaNdTriX/ab9dd2f6d2c3045f55c7b3f541b03941 to your computer and use it in GitHub Desktop.
Save HaNdTriX/ab9dd2f6d2c3045f55c7b3f541b03941 to your computer and use it in GitHub Desktop.
Use Stripe and Material-UI
import React, { useImperativeHandle } from "react";
import PropTypes from "prop-types";
import TextField from "@material-ui/core/TextField";
import { fade, useTheme } from "@material-ui/core/styles";
function StripeInput(props) {
const {
component: Component,
inputRef,
/* eslint-disable no-unused-vars */
"aria-invalid": ariaInvalid,
"aria-describedby": ariaDescribeBy,
defaultValue,
required,
onKeyDown,
onKeyUp,
readOnly,
autoComplete,
autoFocus,
type,
name,
rows,
options,
/* eslint-enable no-unused-vars */
...other
} = props;
const theme = useTheme();
const [mountNode, setMountNode] = React.useState(null);
useImperativeHandle(
inputRef,
() => ({
focus: () => mountNode.focus(),
}),
[mountNode]
);
return (
<Component
onReady={setMountNode}
options={{
...options,
style: {
base: {
color: theme.palette.text.primary,
fontSize: `${theme.typography.fontSize}px`,
fontFamily: theme.typography.fontFamily,
"::placeholder": {
color: fade(theme.palette.text.primary, 0.42),
},
},
invalid: {
color: theme.palette.text.primary,
},
},
}}
{...other}
/>
);
}
StripeInput.propTypes = {
component: PropTypes.node.isRequired,
inputRef: PropTypes.shape({ current: PropTypes.any }),
"aria-invalid": PropTypes.bool,
"aria-describedby": PropTypes.string,
defaultValue: PropTypes.string,
required: PropTypes.bool,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
readOnly: PropTypes.bool,
autoComplete: PropTypes.bool,
autoFocus: PropTypes.bool,
type: PropTypes.string,
name: PropTypes.string,
rows: PropTypes.number,
options: PropTypes.object,
};
export default function StripeTextField(props) {
const {
InputLabelProps,
stripeElement,
InputProps = {},
inputProps,
...other
} = props;
return (
<TextField
fullWidth
InputLabelProps={{
...InputLabelProps,
shrink: true,
}}
InputProps={{
...InputProps,
inputProps: {
...inputProps,
...InputProps.inputProps,
component: stripeElement,
},
inputComponent: StripeInput,
}}
{...other}
/>
);
}
StripeTextField.propTypes = {
...TextField.propTypes,
stripeElement: PropTypes.node.isRequired,
};
@HaNdTriX
Copy link
Author

HaNdTriX commented Aug 26, 2020

Usage

import React from 'react'
import { CardElement } from "@stripe/react-stripe-js";
import StripeTextField from "./StripeTextField"

function MyComponent () {
  return (
    <StripeTextField label="Card" stripeElement={CardElement} />
  )
}

More Info: mui/material-ui#16037

@BenSeward
Copy link

I really like this solution, thanks!

@KabyleBOT
Copy link

I am trying to add options but it remains undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment