maputnik/src/components/fields/PropertyGroup.jsx

59 lines
1.7 KiB
React
Raw Normal View History

import React from 'react'
2016-12-25 19:00:21 +01:00
import GlSpec from 'mapbox-gl-style-spec/reference/latest.js'
2016-12-20 11:44:22 +01:00
import ZoomSpecField from './ZoomSpecField'
import colors from '../../config/colors'
import { margins } from '../../config/scales'
2016-12-20 11:44:22 +01:00
/** Extract field spec by {@fieldName} from the {@layerType} in the
* style specification from either the paint or layout group */
function getFieldSpec(layerType, fieldName) {
const groupName = getGroupName(layerType, fieldName)
const group = GlSpec[groupName + '_' + layerType]
return group[fieldName]
}
function getGroupName(layerType, fieldName) {
const paint = GlSpec['paint_' + layerType] || {}
if (fieldName in paint) {
return 'paint'
} else {
return 'layout'
}
}
export default class PropertyGroup extends React.Component {
static propTypes = {
layer: React.PropTypes.object.isRequired,
2016-12-20 16:08:49 +01:00
groupFields: React.PropTypes.array.isRequired,
onChange: React.PropTypes.func.isRequired,
}
onPropertyChange(property, newValue) {
const group = getGroupName(this.props.layer.type, property)
this.props.onChange(group , property, newValue)
}
render() {
const fields = this.props.groupFields.map(fieldName => {
const fieldSpec = getFieldSpec(this.props.layer.type, fieldName)
const paint = this.props.layer.paint || {}
const layout = this.props.layer.layout || {}
const fieldValue = fieldName in paint ? paint[fieldName] : layout[fieldName]
return <ZoomSpecField
onChange={this.onPropertyChange.bind(this)}
key={fieldName}
fieldName={fieldName}
value={fieldValue}
fieldSpec={fieldSpec}
/>
2016-12-20 16:08:49 +01:00
})
2016-12-30 20:01:14 +01:00
return <div>
{fields}
</div>
}
}