From ca6b48843cac3386b65226bec384393aba04f31b Mon Sep 17 00:00:00 2001 From: Lukas Martinelli Date: Sat, 31 Dec 2016 13:42:25 +0100 Subject: [PATCH] Support adding, editing and removing stops --- src/components/fields/ZoomSpecField.jsx | 61 +++++++++++++++++++++---- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/components/fields/ZoomSpecField.jsx b/src/components/fields/ZoomSpecField.jsx index 8deafa7..71f2ac9 100644 --- a/src/components/fields/ZoomSpecField.jsx +++ b/src/components/fields/ZoomSpecField.jsx @@ -18,12 +18,6 @@ function isZoomField(value) { return typeof value === 'object' && value.stops } -const specFieldProps = { - onChange: React.PropTypes.func.isRequired, - fieldName: React.PropTypes.string.isRequired, - fieldSpec: React.PropTypes.object.isRequired, -} - /** Supports displaying spec field for zoom function objects * https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property */ @@ -41,6 +35,45 @@ export default class ZoomSpecField extends React.Component { ]), } + addStop() { + const stops = this.props.value.stops.slice(0) + const lastStop = stops[stops.length - 1] + stops.push([lastStop[0] + 1, lastStop[1]]) + + const changedValue = { + ...this.props.value, + stops: stops, + } + + this.props.onChange(this.props.fieldName, changedValue) + } + + deleteStop(stopIdx) { + const stops = this.props.value.stops.slice(0) + stops.splice(stopIdx, 1) + + let changedValue = { + ...this.props.value, + stops: stops, + } + + if(stops.length === 1) { + changedValue = stops[0][1] + } + + this.props.onChange(this.props.fieldName, changedValue) + } + + changeStop(changeIdx, zoomLevel, value) { + const stops = this.props.value.stops.slice(0) + stops[changeIdx] = [zoomLevel, value] + const changedValue = { + ...this.props.value, + stops: stops, + } + this.props.onChange(this.props.fieldName, changedValue) + } + render() { let label = - @@ -75,7 +111,10 @@ export default class ZoomSpecField extends React.Component { marginRight: 0 }}> {label} - this.changeStop(idx, changedStop, value)} min={0} max={22} /> - this.changeStop(idx, zoomLevel, newValue)} style={{ width: '42%', marginLeft: margins[0],