Created
May 17, 2016 16:56
-
-
Save felipeleusin/850349363a3ea470b5db7f4d859d7058 to your computer and use it in GitHub Desktop.
Vanilla-Masker + Input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, PropTypes } from 'react' | |
import { findDOMNode } from 'react-dom' | |
import Input from 'react-toolbox/lib/input' | |
import masker from 'utils/vanilla-masker' | |
const maskOptions = { | |
precision: 2, | |
separator: ',', | |
delimiter: '.', | |
unit: 'R$', | |
} | |
const maskValue = (value) => { | |
return masker.toMoney((value * 0.01).toFixed(2), maskOptions) | |
} | |
export default class UnitValueInput extends Component { | |
static defaultProps = { | |
label: 'Valor Unitário' | |
} | |
static propTypes = { | |
value: PropTypes.number, | |
onChange: PropTypes.func.isRequired | |
} | |
state = { unitValue: this.props.value ? maskValue(this.props.value) : '' } | |
handleUnitValueChange = (value) => { | |
const numberValue = parseInt(value.replace(/[^\d]/g,'')) | |
this.setState({ unitValue: maskValue(numberValue) }) | |
this.props.onChange(numberValue) | |
} | |
componentDidMount() { | |
masker(findDOMNode(this._unitValue).querySelector('input')).maskMoney(maskOptions) | |
} | |
componentWillUnmount() { | |
masker(findDOMNode(this._unitValue).querySelector('input')).unMask() | |
} | |
focus() { | |
if (this._unitValue) { | |
this._unitValue.focus() | |
} | |
} | |
render() { | |
return ( | |
<Input | |
{...this.props} | |
type="text" | |
ref={(ref) => { this._unitValue = ref}} | |
value={this.state.unitValue} | |
onChange={this.handleUnitValueChange} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ao executar, o erro "VanillaMasker: There is no element to bind." é exibido