Clean up component

Help from @121watts
pull/1885/head
Alex P 2017-08-17 20:36:05 -07:00
parent 9b8e8752c9
commit 70314681e2
2 changed files with 45 additions and 54 deletions

View File

@ -1,7 +1,11 @@
import React, {Component, PropTypes} from 'react' import React, {Component, PropTypes} from 'react'
import Dropdown from 'src/shared/components/Dropdown' import Dropdown from 'src/shared/components/Dropdown'
import {QUERY_FILL_OPTIONS} from 'src/shared/constants/queryFillOptions' import {
QUERY_FILL_OPTIONS,
NUMBER,
NULL,
} from 'src/shared/constants/queryFillOptions'
/* /*
NOTE NOTE
@ -10,14 +14,11 @@ import {QUERY_FILL_OPTIONS} from 'src/shared/constants/queryFillOptions'
- Either the selected item from the dropdown or the value of the input - Either the selected item from the dropdown or the value of the input
- A boolean to type the result, true = number, false = string - A boolean to type the result, true = number, false = string
*/ */
class FillQuery extends Component { class FillQuery extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = {
selected: QUERY_FILL_OPTIONS[0], selected: this.props.fillType,
useCustomNumber: false,
customNumber: null,
inputValue: '0', inputValue: '0',
} }
} }
@ -25,33 +26,27 @@ class FillQuery extends Component {
static defaultProps = { static defaultProps = {
size: 'xs', size: 'xs',
theme: 'blue', theme: 'blue',
fillType: NULL,
} }
handleDropdown = item => { handleDropdown = item => {
if (item.text === 'number') { if (item.text === 'number') {
this.setState({selected: item.text, useCustomNumber: true}, () => { this.setState({selected: item.text}, () => {
this.props.onSelection(this.state.inputValue, true) this.props.onSelection(this.state.inputValue, true)
}) })
} else { } else {
this.setState({selected: item.text, useCustomNumber: false}, () => { this.setState({selected: item.text}, () => {
this.props.onSelection(item.text, false) this.props.onSelection(item.text, false)
}) })
} }
} }
handleInputBlur = e => { handleInputBlur = e => {
const {useCustomNumber, inputValue} = this.state const inputValue = e.target.value || '0'
// Prevent user from submitting an empty string as their value
// Use 0 by default this.setState({inputValue})
if (e.target.value === '') {
const zeroVal = '0'
this.setState({inputValue: zeroVal}, () => {
this.props.onSelection(zeroVal, true)
})
} else if (useCustomNumber) {
this.props.onSelection(inputValue, true) this.props.onSelection(inputValue, true)
} }
}
handleInputChange = e => { handleInputChange = e => {
this.setState({inputValue: e.target.value}) this.setState({inputValue: e.target.value})
@ -63,26 +58,24 @@ class FillQuery extends Component {
} }
} }
getColor = theme => {
switch (theme) {
case 'BLUE':
return 'plutonium'
case 'GREEN':
return 'malachite'
case 'PURPLE':
return 'astronaut'
default:
return 'plutonium'
}
}
render() { render() {
const {size, theme} = this.props const {size, theme} = this.props
const {selected, useCustomNumber, inputValue} = this.state const {selected, inputValue} = this.state
const items = QUERY_FILL_OPTIONS.map(text => ({text})) const items = QUERY_FILL_OPTIONS.map(text => ({text}))
let inputTheme = ''
let dropdownTheme = ''
if (theme === 'blue') {
inputTheme = 'form-plutonium'
}
if (theme === 'green') {
inputTheme = 'form-malachite'
dropdownTheme = 'dropdown-malachite'
}
if (theme === 'purple') {
inputTheme = 'form-astronaut'
dropdownTheme = 'dropdown-astronaut'
}
return ( return (
<div className={`fill-query fill-query--${size}`}> <div className={`fill-query fill-query--${size}`}>
<label>Fill missing with</label> <label>Fill missing with</label>
@ -92,21 +85,22 @@ class FillQuery extends Component {
className="fill-query--dropdown" className="fill-query--dropdown"
buttonSize={`btn-${size}`} buttonSize={`btn-${size}`}
buttonColor="btn-default" buttonColor="btn-default"
menuClass={dropdownTheme} menuClass={`dropdown-${this.getColor(theme)}`}
onChoose={this.handleDropdown} onChoose={this.handleDropdown}
/> />
{useCustomNumber {selected === NUMBER &&
? <input <input
type="number" type="number"
className={`form-control monotype ${inputTheme} input-${size} fill-query--input`} className={`form-control monotype form-${this.getColor(
theme
)} input-${size} fill-query--input`}
placeholder="Custom Value" placeholder="Custom Value"
autoFocus={true} autoFocus={true}
value={inputValue} value={inputValue}
onKeyUp={this.handleKeyUp} onKeyUp={this.handleKeyUp}
onChange={this.handleInputChange} onChange={this.handleInputChange}
onBlur={this.handleInputBlur} onBlur={this.handleInputBlur}
/> />}
: null}
</div> </div>
) )
} }
@ -116,6 +110,7 @@ const {func, string} = PropTypes
FillQuery.propTypes = { FillQuery.propTypes = {
onSelection: func.isRequired, onSelection: func.isRequired,
fillType: string,
size: string, size: string,
theme: string, theme: string,
} }

View File

@ -1,7 +1,3 @@
export const QUERY_FILL_OPTIONS = [ export const NULL = 'null'
'null', export const NUMBER = 'number'
'linear', export const QUERY_FILL_OPTIONS = [NULL, 'linear', 'none', 'previous', NUMBER]
'none',
'previous',
'number',
]