Switch from field components to input components

This commit is contained in:
Lukas Martinelli 2016-12-26 11:48:52 +01:00
parent a3d586a75d
commit c159f7041f
6 changed files with 26 additions and 107 deletions

View file

@ -1,36 +0,0 @@
import React from 'react'
import input from '../../config/input.js'
class EnumField extends React.Component {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
name: React.PropTypes.string.isRequired,
value: React.PropTypes.string,
allowedValues: React.PropTypes.array.isRequired,
doc: React.PropTypes.string,
style: React.PropTypes.object,
}
onChange(e) {
return this.props.onChange(e.target.value)
}
render() {
const options = this.props.allowedValues.map(val => {
return <option key={val} value={val}>{val}</option>
})
return <select
style={{
...input.select,
...this.props.style
}}
value={this.props.value}
onChange={this.onChange.bind(this)}
>
{options}
</select>
}
}
export default EnumField

View file

@ -2,11 +2,11 @@ import React from 'react'
import color from 'color' import color from 'color'
import GlSpec from 'mapbox-gl-style-spec/reference/latest.min.js' import GlSpec from 'mapbox-gl-style-spec/reference/latest.min.js'
import NumberField from './NumberField'
import EnumField from './EnumField'
import BooleanField from './BooleanField'
import ColorField from './ColorField' import ColorField from './ColorField'
import StringField from './StringField' import NumberInput from '../inputs/NumberInput'
import CheckboxInput from '../inputs/CheckboxInput'
import StringInput from '../inputs/StringInput'
import SelectInput from '../inputs/SelectInput'
import input from '../../config/input.js' import input from '../../config/input.js'
@ -37,30 +37,29 @@ export default class SpecField extends React.Component {
render() { render() {
const commonProps = { const commonProps = {
doc: this.props.fieldSpec.doc,
style: this.props.style, style: this.props.style,
value: this.props.value, value: this.props.value,
name: this.props.fieldName, name: this.props.fieldName,
onChange: newValue => this.props.onChange(this.props.fieldName, newValue) onChange: newValue => this.props.onChange(this.props.fieldName, newValue)
} }
console.log(this.props.fieldName, this.props.fieldSpec.type)
switch(this.props.fieldSpec.type) { switch(this.props.fieldSpec.type) {
case 'number': return ( case 'number': return (
<NumberField <NumberInput
{...commonProps} {...commonProps}
default={this.props.fieldSpec.default} default={this.props.fieldSpec.default}
min={this.props.fieldSpec.minimum} min={this.props.fieldSpec.minimum}
max={this.props.fieldSpec.maximum} max={this.props.fieldSpec.maximum}
unit={this.props.fieldSpec.unit}
/> />
) )
case 'enum': return ( case 'enum': return (
<EnumField <SelectInput
{...commonProps} {...commonProps}
allowedValues={Object.keys(this.props.fieldSpec.values)} options={Object.keys(this.props.fieldSpec.values).map(v => [v, v])}
/> />
) )
case 'string': return ( case 'string': return (
<StringField <StringInput
{...commonProps} {...commonProps}
/> />
) )
@ -70,7 +69,7 @@ export default class SpecField extends React.Component {
/> />
) )
case 'boolean': return ( case 'boolean': return (
<BooleanField <CheckboxInput
{...commonProps} {...commonProps}
/> />
) )

View file

@ -1,34 +0,0 @@
import React from 'react'
import input from '../../config/input.js'
/*** Number fields with support for min, max and units and documentation*/
class StringField extends React.Component {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
name: React.PropTypes.string.isRequired,
value: React.PropTypes.string,
default: React.PropTypes.number,
doc: React.PropTypes.string,
style: React.PropTypes.object,
}
onChange(e) {
const value = e.target.value
return this.props.onChange(value === "" ? null: value)
}
render() {
return <input
style={{
...input.input,
...this.props.style
}}
name={this.props.name}
placeholder={this.props.default}
value={this.props.value ? this.props.value : ""}
onChange={this.onChange.bind(this)}
/>
}
}
export default StringField

View file

@ -2,11 +2,6 @@ import React from 'react'
import Color from 'color' import Color from 'color'
import Button from '../Button' import Button from '../Button'
import NumberField from './NumberField'
import EnumField from './EnumField'
import BooleanField from './BooleanField'
import ColorField from './ColorField'
import StringField from './StringField'
import SpecField from './SpecField' import SpecField from './SpecField'
import DocLabel from './DocLabel' import DocLabel from './DocLabel'
@ -45,7 +40,6 @@ export default class ZoomSpecField extends React.Component {
} }
render() { render() {
console.log(this.props.fieldSpec)
let label = <DocLabel let label = <DocLabel
label={labelFromFieldName(this.props.fieldName)} label={labelFromFieldName(this.props.fieldName)}
doc={this.props.fieldSpec.doc} doc={this.props.fieldSpec.doc}

View file

@ -1,16 +1,13 @@
import React from 'react' import React from 'react'
import input from '../../config/input.js'
import input from '../../config/input'
import colors from '../../config/colors' import colors from '../../config/colors'
import { margins } from '../../config/scales' import { margins } from '../../config/scales'
class BooleanField extends React.Component { class CheckboxInput extends React.Component {
static propTypes = { static propTypes = {
onChange: React.PropTypes.func.isRequired, value: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
value: React.PropTypes.bool,
doc: React.PropTypes.string,
style: React.PropTypes.object, style: React.PropTypes.object,
onChange: React.PropTypes.func,
} }
render() { render() {
@ -57,7 +54,6 @@ class BooleanField extends React.Component {
...styles.input, ...styles.input,
...this.props.style, ...this.props.style,
}} }}
value={this.props.value}
onChange={e => {this.props.onChange(!this.props.value)}} onChange={e => {this.props.onChange(!this.props.value)}}
checked={this.props.value} checked={this.props.value}
/> />
@ -72,4 +68,4 @@ class BooleanField extends React.Component {
} }
} }
export default BooleanField export default CheckboxInput

View file

@ -1,18 +1,14 @@
import React from 'react' import React from 'react'
import input from '../../config/input.js' import input from '../../config/input.js'
/*** Number fields with support for min, max and units and documentation*/ class NumberInput extends React.Component {
class NumberField extends React.Component {
static propTypes = { static propTypes = {
onChange: React.PropTypes.func.isRequired,
name: React.PropTypes.string.isRequired,
value: React.PropTypes.number, value: React.PropTypes.number,
style: React.PropTypes.object,
default: React.PropTypes.number, default: React.PropTypes.number,
unit: React.PropTypes.string,
min: React.PropTypes.number, min: React.PropTypes.number,
max: React.PropTypes.number, max: React.PropTypes.number,
doc: React.PropTypes.string, onChange: React.PropTypes.func,
style: React.PropTypes.object,
} }
onChange(e) { onChange(e) {
@ -21,7 +17,12 @@ class NumberField extends React.Component {
if(this.props.min && value < this.props.min) return if(this.props.min && value < this.props.min) return
if(this.props.max && value > this.props.max) return if(this.props.max && value > this.props.max) return
*/ */
this.props.onChange(value)
if(isNaN(value)) {
this.props.onChange(this.props.default)
} else {
this.props.onChange(value)
}
} }
render() { render() {
@ -35,11 +36,10 @@ class NumberField extends React.Component {
...input.input, ...input.input,
...this.props.style ...this.props.style
}} }}
type="number" type={"number"}
min={this.props.min} min={this.props.min}
max={this.props.max} max={this.props.max}
step={stepSize} step={stepSize}
name={this.props.name}
placeholder={this.props.default} placeholder={this.props.default}
value={this.props.value} value={this.props.value}
onChange={this.onChange.bind(this)} onChange={this.onChange.bind(this)}
@ -47,4 +47,4 @@ class NumberField extends React.Component {
} }
} }
export default NumberField export default NumberInput