import React from 'react'; import { Braintree, HostedField } from '../src/index'; class Demo extends React.PureComponent { constructor(props) { super(props); this.numberField = React.createRef(); this.braintree = React.createRef(); [ 'onError', 'getToken', 'onCardTypeChange', 'onAuthorizationSuccess', ].forEach(prop => (this[prop] = this[prop].bind(this))); } state = {} onError(error) { this.setState({ error: error.message || String(error) }); } getToken() { this.tokenize().then( token => this.setState({ token, error: null }), ).catch(error => this.onError(error)); } onCardTypeChange({ cards }) { if (1 === cards.length) { const [card] = cards; this.setState({ card: card.type }); if (card.code && card.code.name) { this.cvvField.setPlaceholder(card.code.name); } else { this.cvvField.setPlaceholder('CVV'); } } else { this.setState({ card: '' }); this.cvvField.setPlaceholder('CVV'); } } state = { numberFocused: false, } componentDidMount() { this.setState({ authorization: 'sandbox_g42y39zw_348pk9cgf3bgyw2b' }); } renderResult(title, obj) { if (!obj) { return null; } return (
{title}:
{JSON.stringify(obj, null, 4)}
); } onAuthorizationSuccess() { this.numberField.current.focus(); } render() { return (

Braintree Hosted Fields Demo

{this.renderResult('Error', this.state.error)} {this.renderResult('Token', this.state.token)} (this.tokenize = t)} onCardTypeChange={this.onCardTypeChange} styles={{ input: { 'font-size': '14px', 'font-family': 'helvetica, tahoma, calibri, sans-serif', color: '#7d6b6b', }, ':focus': { color: 'black', }, }} >
Number: this.setState({ numberFocused: false })} onFocus={() => this.setState({ numberFocused: true })} className={this.state.numberFocused ? 'focused' : ''} prefill="4111 1111 1111 1111" ref={this.numberField} />

Card type: {this.state.card}

Name: Date: Month: Year: CVV: { this.cvvField = cvvField; }} /> Zip:
); } } export default Demo;