cleave react demo
by dorenstein
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-with-addons.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave-react.min.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave-phone.i18n.js"></script>
<div id="content"></div>
CSS
* {
font-family: sans-serif;
}
.raw-values {
margin-top: 20px;
border-top: 1px dotted #AAA;
}
input {
display: block;
outline: none;
-webkit-appearance: none;
border: 1px solid #e6e6e6;
border-radius: 5px;
background-color: #fff;
line-height: 30px;
vertical-align: middle;
height: 33px;
width: 170px;
font-size: 14px;
padding: 0 8px;
margin-bottom: 10px;
}
.input-numeral {
text-align: right;
}
Babel + JSX
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
creditCardType: '',
creditCardRawValue: '',
phoneRawValue: '',
dateRawValue: '',
numeralRawValue: '',
customRawValue: ''
};
this.onCreditCardChange = this.onCreditCardChange.bind(this);
this.onCreditCardTypeChanged = this.onCreditCardTypeChanged.bind(this);
this.onPhoneChange = this.onPhoneChange.bind(this);
this.onDateChange = this.onDateChange.bind(this);
this.onNumeralChange = this.onNumeralChange.bind(this);
this.onCustomChange = this.onCustomChange.bind(this);
}
onCreditCardChange(event) {
this.setState({creditCardRawValue: event.target.rawValue});
}
onPhoneChange(event) {
this.setState({phoneRawValue: event.target.rawValue});
}
onDateChange(event) {
this.setState({dateRawValue: event.target.rawValue});
}
onNumeralChange(event) {
this.setState({numeralRawValue: event.target.rawValue});
}
onCustomChange(event) {
this.setState({customRawValue: event.target.rawValue});
}
onCreditCardTypeChanged(type){
this.setState({creditCardType: type});
}
render() {
return (
<div>
<Cleave placeholder="Enter credit card number" options={{creditCard: true, onCreditCardTypeChanged: this.onCreditCardTypeChanged}}
onChange={this.onCreditCardChange}/>
<Cleave placeholder="Enter phone number" options={{phone: true, phoneRegionCode: 'AU'}}
onChange={this.onPhoneChange}/>
<Cleave placeholder="Enter date" options={{date: true, datePattern: ['Y', 'm', 'd']}}
onChange={this.onDateChange}/>
<Cleave className="input-numeral"...