Skip to content

Commit

Permalink
Merge d716d74 into 95b693d
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomg committed Feb 20, 2018
2 parents 95b693d + d716d74 commit cfc564f
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 244 deletions.
1 change: 1 addition & 0 deletions src/components/Common/InputField.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const InputField = props => {
value={props.value}
onChange={props.onChange}
onKeyPress={props.onKeyPress}
onPaste={props.onPaste}
/>
<p className="description">{props.description}</p>
{ props.pristine ? null : <p style={errorStyle}>{error}</p> }
Expand Down
46 changes: 29 additions & 17 deletions src/components/Common/NumericInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,60 @@ export class NumericInput extends Component {
}
}

onPaste = e => {
if (isNaN(parseFloat(e.clipboardData.getData('text/plain'))))
e.preventDefault()
}

onKeyPress = e => {
if (!this.props.acceptFloat) {
if (isNaN(parseInt(e.key, 10))) e.preventDefault()
const firstCharacterIsMinus = e.key === '-' && this.state.value === ''
const acceptsNegativeValues = this.props.min < 0 || this.props.max < 0

if (!(firstCharacterIsMinus && acceptsNegativeValues) && isNaN(parseInt(e.key, 10))) e.preventDefault()
}
}

onChange = e => {
const { value } = e.target
let value = this.props.acceptFloat ? parseFloat(e.target.value) : parseInt(e.target.value, 10)
let isValid = true

if (this.props.min) {
isValid = isValid && value >= parseFloat(this.props.min)
}

if (isValid && this.props.max) {
isValid = isValid && value <= parseFloat(this.props.max)
}

if (isValid && this.props.acceptFloat) {
if (this.props.maxDecimals) {
if (this.props.acceptFloat) {
if (this.props.maxDecimals !== undefined) {
isValid = isValid && countDecimalPlaces(value) <= this.props.maxDecimals
}

if (this.props.minDecimals) {
if (this.props.minDecimals !== undefined) {
isValid = isValid && countDecimalPlaces(value) >= this.props.minDecimals
}
}

if (value === '') {
if (isValid && this.props.min !== undefined) {
isValid = isValid && value >= this.props.min
}

if (isValid && this.props.max !== undefined) {
isValid = isValid && value <= this.props.max
}

if (isNaN(value)) {
value = ''
isValid = true
}

const newValue = value === '' ? '' : parseFloat(value)
this.setState({
pristine: false,
valid: isValid ? VALID : INVALID,
value: newValue
}, () => this.props.onValueUpdate(newValue))
value
})

this.props.onValueUpdate(value)
}

render () {
return (
<InputField
disabled={this.props.disabled}
side={this.props.side}
type='number'
errorMessage={this.props.errorMessage}
Expand All @@ -70,6 +81,7 @@ export class NumericInput extends Component {
title={this.props.title}
onKeyPress={e => this.onKeyPress(e)}
onChange={e => this.onChange(e)}
onPaste={e => this.onPaste(e)}
description={this.props.description}
/>
)
Expand Down

0 comments on commit cfc564f

Please sign in to comment.