Make up defaults from function field if we can't find one from the spec.

This commit is contained in:
orangemug 2019-10-25 22:54:08 +01:00
parent ca202d7701
commit 4cbcf14588

View file

@ -14,6 +14,25 @@ function isDataField(value) {
return typeof value === 'object' && value.stops && typeof value.property !== 'undefined' return typeof value === 'object' && value.stops && typeof value.property !== 'undefined'
} }
/**
* If we don't have a default value just make one up
*/
function findDefaultFromSpec (spec) {
if (spec.default) {
return spec.default;
}
const defaults = {
'color': '#000000',
'string': '',
'boolean': false,
'number': 0,
'array': [],
}
return defaults[spec.type] || '';
}
/** Supports displaying spec field for zoom function objects /** Supports displaying spec field for zoom function objects
* https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property * https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property
*/ */
@ -82,8 +101,8 @@ export default class FunctionSpecProperty extends React.Component {
makeZoomFunction = () => { makeZoomFunction = () => {
const zoomFunc = { const zoomFunc = {
stops: [ stops: [
[6, this.props.value], [6, this.props.value || findDefaultFromSpec(this.props.fieldSpec)],
[10, this.props.value] [10, this.props.value || findDefaultFromSpec(this.props.fieldSpec)]
] ]
} }
this.props.onChange(this.props.fieldName, zoomFunc) this.props.onChange(this.props.fieldName, zoomFunc)
@ -96,8 +115,8 @@ export default class FunctionSpecProperty extends React.Component {
property: "", property: "",
type: functionType, type: functionType,
stops: [ stops: [
[{zoom: 6, value: stopValue}, this.props.value || stopValue], [{zoom: 6, value: stopValue}, this.props.value || findDefaultFromSpec(this.props.fieldSpec)],
[{zoom: 10, value: stopValue}, this.props.value || stopValue] [{zoom: 10, value: stopValue}, this.props.value || findDefaultFromSpec(this.props.fieldSpec)]
] ]
} }
this.props.onChange(this.props.fieldName, dataFunc) this.props.onChange(this.props.fieldName, dataFunc)