maputnik/src/components/fields/SpecField.jsx

82 lines
2.2 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 NumberField from './NumberField'
import EnumField from './EnumField'
import BooleanField from './BooleanField'
import ColorField from './ColorField'
import StringField from './StringField'
import input from '../../config/input.js'
import theme from '../../config/rebass.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 = {
doc: this.props.fieldSpec.doc,
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 (
<NumberField
{...commonProps}
2016-12-17 17:40:44 +01:00
default={this.props.fieldSpec.default}
min={this.props.fieldSpec.min}
max={this.props.fieldSpec.max}
unit={this.props.fieldSpec.unit}
/>
)
case 'enum': return (
<EnumField
{...commonProps}
allowedValues={Object.keys(this.props.fieldSpec.values)}
2016-12-17 17:40:44 +01:00
/>
)
case 'string': return (
<StringField
{...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 (
<BooleanField
{...commonProps}
2016-12-17 17:40:44 +01:00
/>
)
default: return null
}
}
2016-09-12 19:44:28 +02:00
}