From 957d805ab8bc94824be3fff26c62b69f4160d32a Mon Sep 17 00:00:00 2001 From: lukasmartinelli Date: Mon, 12 Sep 2016 19:44:28 +0200 Subject: [PATCH] Manage to display dynamic fields --- src/fields/enum.jsx | 8 +++-- src/fields/number.jsx | 25 +++++++++++++++ src/fields/spec.jsx | 67 +++++++++++++++++++++++++++++++++++++++ src/layers/background.jsx | 9 +++--- src/layers/fill.jsx | 10 ++---- src/layers/line.jsx | 33 +++---------------- src/layers/symbol.jsx | 5 ++- 7 files changed, 114 insertions(+), 43 deletions(-) create mode 100644 src/fields/number.jsx create mode 100644 src/fields/spec.jsx diff --git a/src/fields/enum.jsx b/src/fields/enum.jsx index 1b6a22b..fcd165c 100644 --- a/src/fields/enum.jsx +++ b/src/fields/enum.jsx @@ -1,18 +1,20 @@ import React from 'react' import { Select, Input } from 'rebass' -export default class EnumField extends React.Component { +class EnumField extends React.Component { static propTypes = { name: React.PropTypes.string.isRequired, - values: React.PropTypes.array.isRequired, value: React.PropTypes.string.isRequired, + allowedValues: React.PropTypes.array.isRequired, doc: React.PropTypes.string, } render() { - const options = this.props.values.map(val => { + const options = this.props.allowedValues.map(val => { return {children: val, value: val} }) return + } +} + +export default NumberField diff --git a/src/fields/spec.jsx b/src/fields/spec.jsx new file mode 100644 index 0000000..d596cdd --- /dev/null +++ b/src/fields/spec.jsx @@ -0,0 +1,67 @@ +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' + +class SpecField extends React.Component { + static propTypes = { + fieldName: React.PropTypes.string.isRequired, + fieldSpec: React.PropTypes.object.isRequired, + value: React.PropTypes.oneOfType([ + React.PropTypes.object, + React.PropTypes.string, + React.PropTypes.number, + ]).isRequired, + } + + render() { + switch(this.props.fieldSpec.type) { + case 'number': return ( + + ) + case 'enum': return ( + + ) + default: return null + } + } +} + +export class PropertyGroup extends React.Component { + static propTypes = { + properties: React.PropTypes.instanceOf(Immutable.Map).isRequired, + layerType: React.PropTypes.oneOf(['fill', 'background', 'line']).isRequired, + 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 + }) + return
+ {specFields} +
+ } +} diff --git a/src/layers/background.jsx b/src/layers/background.jsx index 42f1bf7..a744008 100644 --- a/src/layers/background.jsx +++ b/src/layers/background.jsx @@ -1,10 +1,12 @@ import React from 'react' +import Immutable from 'immutable' import { Input } from 'rebass' +import { PropertyGroup } from '../fields/spec' import PureRenderMixin from 'react-addons-pure-render-mixin'; export default class BackgroundLayer extends React.Component { static propTypes = { - layer: React.PropTypes.object.isRequired, + layer: React.PropTypes.instanceOf(Immutable.Map).isRequired, onPaintChanged: React.PropTypes.func.isRequired } @@ -22,10 +24,9 @@ export default class BackgroundLayer extends React.Component { } render() { - const paint = this.props.layer.get('paint') return
- - + +
} } diff --git a/src/layers/fill.jsx b/src/layers/fill.jsx index 1a9623d..cb75453 100644 --- a/src/layers/fill.jsx +++ b/src/layers/fill.jsx @@ -1,6 +1,7 @@ import React from 'react' import Immutable from 'immutable' import { Checkbox, Input } from 'rebass' +import { PropertyGroup } from '../fields/spec' import PureRenderMixin from 'react-addons-pure-render-mixin'; export default class FillLayer extends React.Component { @@ -24,14 +25,9 @@ export default class FillLayer extends React.Component { } render() { - const paint = this.props.layer.get('paint') return
- - - - - - + +
} } diff --git a/src/layers/line.jsx b/src/layers/line.jsx index da4838a..5be37cc 100644 --- a/src/layers/line.jsx +++ b/src/layers/line.jsx @@ -1,7 +1,6 @@ import React from 'react' import Immutable from 'immutable' -import spec from 'mapbox-gl-style-spec/reference/latest.min.js' -import EnumField from '../fields/enum.jsx' +import { PropertyGroup } from '../fields/spec' export default class LineLayer extends React.Component { static propTypes = { @@ -9,31 +8,9 @@ export default class LineLayer extends React.Component { } render() { - const enumFieldFromType = (key, field) => { - if(field.type === 'enum') { - return - } - } - - const layoutLineFields = () => { - const type = spec["layout_line"] - return Object.keys(type).map(key => { - return enumFieldFromType(key, type[key]) - }) - } - - const paintLineFields = () => { - const type = spec["paint_line"] - return Object.keys(type).map(key => { - return enumFieldFromType(key, type[key]) - }) - } - - return
{layoutLineFields()}{paintLineFields()}
+ return
+ + +
} } diff --git a/src/layers/symbol.jsx b/src/layers/symbol.jsx index 69e2024..d081309 100644 --- a/src/layers/symbol.jsx +++ b/src/layers/symbol.jsx @@ -2,6 +2,9 @@ import React from 'react' export default class SymbolLayer extends React.Component { render() { - return
+ return
+ + +
} }