Only submit number input value on blur; cancel on escape

pull/1885/head
Jared Scheib 2017-09-08 11:21:46 -07:00
parent 0f5a5d2ed7
commit b5b56cd9e4
1 changed files with 27 additions and 14 deletions

View File

@ -14,11 +14,13 @@ class FillQuery extends Component {
this.state = isNumberValue this.state = isNumberValue
? { ? {
selected: queryFills.find(fill => fill.type === NUMBER), selected: queryFills.find(fill => fill.type === NUMBER),
numberValue: props.value, currentNumberValue: props.value,
resetNumberValue: props.value,
} }
: { : {
selected: queryFills.find(fill => fill.type === props.value), selected: queryFills.find(fill => fill.type === props.value),
numberValue: '0', currentNumberValue: '0',
resetNumberValue: '0',
} }
} }
@ -35,25 +37,35 @@ class FillQuery extends Component {
} }
handleInputBlur = e => { handleInputBlur = e => {
if (!e.target.value) { const nextNumberValue = e.target.value
this.setState({numberValue: '0'}) ? e.target.value
} : this.state.resetNumberValue || '0'
const numberValue = e.target.value || '0' this.setState({
currentNumberValue: nextNumberValue,
resetNumberValue: nextNumberValue,
})
this.setState({numberValue}) this.props.onChooseFill(nextNumberValue)
this.props.onChooseFill(numberValue)
} }
handleInputChange = e => { handleInputChange = e => {
const numberValue = e.target.value const currentNumberValue = e.target.value
this.setState({numberValue}) this.setState({currentNumberValue})
}
handleKeyDown = e => {
if (e.key === 'Enter') {
this.numberInput.blur()
}
} }
handleKeyUp = e => { handleKeyUp = e => {
if (e.key === 'Enter' || e.key === 'Escape') { if (e.key === 'Escape') {
e.target.blur() this.setState({currentNumberValue: this.state.resetNumberValue}, () => {
this.numberInput.blur()
})
} }
} }
@ -72,7 +84,7 @@ class FillQuery extends Component {
render() { render() {
const {size, theme} = this.props const {size, theme} = this.props
const {selected, numberValue} = this.state const {selected, currentNumberValue} = this.state
return ( return (
<div className={`fill-query fill-query--${size}`}> <div className={`fill-query fill-query--${size}`}>
@ -84,8 +96,9 @@ class FillQuery extends Component {
theme theme
)} input-${size} fill-query--input`} )} input-${size} fill-query--input`}
placeholder="Custom Value" placeholder="Custom Value"
value={numberValue} value={currentNumberValue}
onKeyUp={this.handleKeyUp} onKeyUp={this.handleKeyUp}
onKeyDown={this.handleKeyDown}
onChange={this.handleInputChange} onChange={this.handleInputChange}
onBlur={this.handleInputBlur} onBlur={this.handleInputBlur}
/>} />}