Skip to content

Instantly share code, notes, and snippets.

@felipeleusin
Created May 17, 2016 16:56
Show Gist options
  • Save felipeleusin/850349363a3ea470b5db7f4d859d7058 to your computer and use it in GitHub Desktop.
Save felipeleusin/850349363a3ea470b5db7f4d859d7058 to your computer and use it in GitHub Desktop.
Vanilla-Masker + Input
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}
/>
)
}
}
@wallisonfelipe
Copy link

Ao executar, o erro "VanillaMasker: There is no element to bind." é exibido

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