maputnik/src/fields/spec.jsx

95 lines
2.6 KiB
React
Raw Normal View History

2016-09-12 19:44:28 +02:00
import React from 'react'
import Immutable from 'immutable'
import GlSpec from 'mapbox-gl-style-spec/reference/latest.min.js'
import NumberField from './number'
import EnumField from './enum'
2016-09-12 19:47:28 +02:00
import ColorField from './color'
2016-09-12 19:54:02 +02:00
import StringField from './string'
2016-09-12 19:44:28 +02:00
class SpecField extends React.Component {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
2016-09-12 19:44:28 +02:00
fieldName: React.PropTypes.string.isRequired,
fieldSpec: React.PropTypes.object.isRequired,
value: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.string,
React.PropTypes.number,
]).isRequired,
}
2016-09-12 20:29:53 +02:00
onValueChanged(property, value) {
return this.props.onChange(property, value)
}
2016-09-12 19:44:28 +02:00
render() {
switch(this.props.fieldSpec.type) {
case 'number': return (
<NumberField
onChange={this.onValueChanged.bind(this, this.props.fieldName)}
2016-09-12 19:44:28 +02:00
value={this.props.value}
name={this.props.fieldName}
default={this.props.fieldSpec.default}
min={this.props.fieldSpec.min}
max={this.props.fieldSpec.max}
unit={this.props.fieldSpec.unit}
doc={this.props.fieldSpec.doc}
/>
)
case 'enum': return (
<EnumField
onChange={this.onValueChanged.bind(this, this.props.fieldName)}
2016-09-12 19:44:28 +02:00
value={this.props.value}
name={this.props.fieldName}
allowedValues={this.props.fieldSpec.values}
doc={this.props.fieldSpec.doc}
/>
)
2016-09-12 19:54:02 +02:00
case 'string': return (
<StringField
onChange={this.onValueChanged.bind(this, this.props.fieldName)}
2016-09-12 19:54:02 +02:00
value={this.props.value}
name={this.props.fieldName}
doc={this.props.fieldSpec.doc}
/>
)
2016-09-12 19:47:28 +02:00
case 'color': return (
<ColorField
onChange={this.onValueChanged.bind(this, this.props.fieldName)}
2016-09-12 19:47:28 +02:00
value={this.props.value}
name={this.props.fieldName}
doc={this.props.fieldSpec.doc}
/>
)
2016-09-12 19:44:28 +02:00
default: return null
}
}
}
export class PropertyGroup extends React.Component {
static propTypes = {
onChange: React.PropTypes.func.isRequired,
2016-09-12 19:44:28 +02:00
properties: React.PropTypes.instanceOf(Immutable.Map).isRequired,
2016-09-15 18:51:39 +02:00
layerType: React.PropTypes.oneOf(['fill', 'background', 'line', 'symbol']).isRequired,
2016-09-12 19:44:28 +02:00
groupType: React.PropTypes.oneOf(['paint', 'layout']).isRequired,
}
render() {
const layerTypeSpec = GlSpec[this.props.groupType + "_" + this.props.layerType]
const specFields = Object.keys(layerTypeSpec).map(propName => {
const fieldSpec = layerTypeSpec[propName]
const propValue = this.props.properties.get(propName)
return <SpecField
onChange={this.props.onChange}
2016-09-12 19:44:28 +02:00
key={propName}
value={propValue}
fieldName={propName}
fieldSpec={fieldSpec}
/>
})
return <div>
{specFields}
</div>
}
}