maputnik/src/components/fields/SpecField.jsx

89 lines
2.4 KiB
React
Raw Normal View History

2016-09-12 19:44:28 +02:00
import React from 'react'
2016-12-17 17:40:44 +01:00
import color from 'color'
2016-09-12 19:44:28 +02:00
import GlSpec from 'mapbox-gl-style-spec/reference/latest.min.js'
2016-12-20 11:44:22 +01:00
import ColorField from './ColorField'
import NumberInput from '../inputs/NumberInput'
import CheckboxInput from '../inputs/CheckboxInput'
import StringInput from '../inputs/StringInput'
import SelectInput from '../inputs/SelectInput'
2016-12-28 16:48:49 +01:00
import MultiButtonInput from '../inputs/MultiButtonInput'
import capitalize from 'lodash.capitalize'
2016-12-20 11:44:22 +01:00
import input from '../../config/input.js'
2016-09-12 19:44:28 +02:00
2016-12-17 20:36:43 +01:00
function labelFromFieldName(fieldName) {
let label = fieldName.split('-').slice(1).join(' ')
if(label.length > 0) {
label = label.charAt(0).toUpperCase() + label.slice(1);
}
return label
}
2016-12-17 17:40:44 +01:00
/** Display any field from the Mapbox GL style spec and
* choose the correct field component based on the @{fieldSpec}
* to display @{value}. */
2016-12-20 11:44:22 +01:00
export default class SpecField extends React.Component {
2016-12-17 17:40:44 +01:00
static propTypes = {
2016-12-20 11:44:22 +01:00
onChange: React.PropTypes.func.isRequired,
fieldName: React.PropTypes.string.isRequired,
fieldSpec: React.PropTypes.object.isRequired,
2016-12-17 17:40:44 +01:00
value: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
2016-12-19 16:21:22 +01:00
/** Override the style of the field */
style: React.PropTypes.object,
2016-12-17 17:40:44 +01:00
}
render() {
const commonProps = {
style: this.props.style,
value: this.props.value,
name: this.props.fieldName,
onChange: newValue => this.props.onChange(this.props.fieldName, newValue)
}
2016-12-17 17:40:44 +01:00
switch(this.props.fieldSpec.type) {
case 'number': return (
<NumberInput
{...commonProps}
2016-12-17 17:40:44 +01:00
default={this.props.fieldSpec.default}
min={this.props.fieldSpec.minimum}
max={this.props.fieldSpec.maximum}
2016-12-17 17:40:44 +01:00
/>
)
2016-12-28 16:48:49 +01:00
case 'enum':
const options = Object.keys(this.props.fieldSpec.values).map(v => [v, capitalize(v)])
if(options.length < 3) {
return <MultiButtonInput
{...commonProps}
options={options}
/>
} else {
return <SelectInput
{...commonProps}
options={options}
/>
}
2016-12-17 17:40:44 +01:00
case 'string': return (
<StringInput
{...commonProps}
2016-12-17 17:40:44 +01:00
/>
)
case 'color': return (
<ColorField
{...commonProps}
2016-12-18 20:08:20 +01:00
/>
)
case 'boolean': return (
<CheckboxInput
{...commonProps}
2016-12-17 17:40:44 +01:00
/>
)
default: return null
}
}
2016-09-12 19:44:28 +02:00
}