Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Feat] allow custom value in layer slider (#1631)

Leraa20db9714f9d
diff --git a/src/components/side-panel/layer-panel/vis-config-slider.js b/src/components/side-panel/layer-panel/vis-config-slider.js
index 4ad7b9e48..a6af55ad8 100644
--- a/src/components/side-panel/layer-panel/vis-config-slider.js
+++ b/src/components/side-panel/layer-panel/vis-config-slider.js
@@ -18,12 +18,122 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React from 'react';
+import React, {useState, useCallback, useEffect, useRef} from 'react';
import PropTypes from 'prop-types';
-import {PanelLabel, SidePanelSection} from 'components/common/styled-components';
+import styled from 'styled-components';
+import {PanelLabel, SidePanelSection} from 'components/common/styled-components';
import RangeSliderFactory from 'components/common/range-slider';
import {FormattedMessage} from 'localization';
+import KeyEvent from 'constants/keyevent';
+import {Checkbox} from 'components';
+import {clamp} from 'utils/data-utils';
+import {isInRange} from 'utils/filter-utils';
+
+const InputWrapper = styled.div`
+ display: flex;
+ line-height: 12px;
+ margin-bottom: 12px;
+`;
+
+const CustomInputWrapper = styled.div`
+ display: flex;
+`;
+
+const CustomInputLabel = styled.label`
+ color: ${props => props.theme.textColor};
+ font-weight: 500;
+ letter-spacing: 0.2px;
+ font-size: ${props => props.theme.layerConfigGroupLabelLabelFontSize};
+ padding-right: 15px;
+
+ &:last-child {
+ position: absolute;
+ right: 0;
+ padding: 0;
+ }
+`;
+
+const RangeInput = styled.input`
+ ${props => props.theme.input};
+ font-size: ${props => props.theme.sliderInputFontSize};
+ width: 44px;
+ overflow: auto;
+ height: 20px;
+ margin-top: 5px;
+`;
+
+const LazyInput = ({value, onChange, name}) => {
+ const [stateValue, setValue] = useState(value);
+ const inputRef = useRef(null);
+ useEffect(() => {
+ setValue(value);
+ }, [value]);
+
+ const onKeyDown = useCallback(
+ e => {
+ switch (e.keyCode) {
+ case KeyEvent.DOM_VK_ENTER:
+ case KeyEvent.DOM_VK_RETURN:
+ onChange(stateValue);
+ if (inputRef !== null) {
+ // @ts-ignore
+ inputRef?.current.blur();
+ }
+ break;
+ default:
+ break;
+ }
+ },
+ [onChange, stateValue]
+ );
+
+ const _onChange = useCallback(e => setValue(e.target.value), [setValue]);
+ const onBlur = useCallback(() => onChange(name, stateValue), [onChange, name, stateValue]);
+
+ return (
+ <RangeInput
+ type="number"
+ ref={inputRef}
+ value={stateValue}
+ onChange={_onChange}
+ onBlur={onBlur}
+ onKeyDown={onKeyDown}
+ id={name}
+ />
+ );
+};
+
+const CustomInput = ({isRanged, value, onChangeCustomInput}) => {
+ const onChangeInput = useCallback(
+ (name, v) => {
+ if (isRanged) onChangeCustomInput(name === 'value0' ? [v, value[1]] : [value[0], v]);
+ else onChangeCustomInput(v);
+ },
+ [isRanged, value, onChangeCustomInput]
+ );
+
+ return (
+ <CustomInputWrapper>
+ {isRanged ? (
+ <InputWrapper>
+ <CustomInputLabel>
+ min
+ <LazyInput name="value0" value={value[0]} onChange={onChangeInput} />
+ </CustomInputLabel>
+ <CustomInputLabel>
+ max
+ <LazyInput name="value1" value={value[1]} onChange={onChangeInput} />
+ </CustomInputLabel>
+ </InputWrapper>
+ ) : (
+ <InputWrapper>
+ <LazyInput name="value" value={value} onChange={onChangeInput} />
+ </InputWrapper>
+ )}
+ </CustomInputWrapper>
+ );
+};
const propTypes = {
layer: PropTypes.object.isRequired,
@@ -47,34 +157,68 @@ export default function VisConfigSliderFactory(RangeSlider) {
range,
step,
isRanged,
+ allowCustomValue,
disabled,
onChange,
inputTheme
- }) => (
- <SidePanelSection disabled={Boolean(disabled)}>
- {label ? (
- <PanelLabel>
- {typeof label === 'string' ? (
- <FormattedMessage id={label} />
- ) : typeof label === 'function' ? (
- <FormattedMessage id={label(config)} />
- ) : (
- <FormattedMessage id={`property.${property}`} />
- )}
- </PanelLabel>
- ) : null}
- <RangeSlider
- range={range}
- value0={isRanged ? config.visConfig[property][0] : range[0]}
- value1={isRanged ? config.visConfig[property][1] : config.visConfig[property]}
- step={step}
- isRanged={Boolean(isRanged)}
- onChange={value => onChange({[property]: isRanged ? value : value[1]})}
- inputTheme={inputTheme}
- showInput
- />
- </SidePanelSection>
- );
+ }) => {
+ const value = config.visConfig[property];
+ const [custom, setCustom] = useState(false || !isInRange(value, range));
+
+ const onChangeCheckbox = useCallback(() => {
+ if (custom) {
+ // we are swithcing from custom to not custom
+ // adjust value to range
+ const adjustedValue = isRanged
+ ? [clamp(range, value[0]), clamp(range, value[1])]
+ : clamp(range, value);
+ onChange({[property]: adjustedValue});
+ }
+ setCustom(!custom);
+ }, [onChange, property, isRanged, value, range, custom, setCustom]);
+
+ return (
+ <SidePanelSection disabled={Boolean(disabled)}>
+ {label ? (
+ <PanelLabel>
+ {typeof label === 'string' ? (
+ <FormattedMessage id={label} />
+ ) : typeof label === 'function' ? (
+ <FormattedMessage id={label(config)} />
+ ) : (
+ <FormattedMessage id={`property.${property}`} />
+ )}
+ </PanelLabel>
+ ) : null}
+
+ {allowCustomValue ? (
+ <InputWrapper>
+ <CustomInputLabel>custom input</CustomInputLabel>
+ <Checkbox id={`property.${property}`} checked={custom} onChange={onChangeCheckbox} />
+ </InputWrapper>
+ ) : null}
+
+ {!custom ? (
+ <RangeSlider
+ range={range}
+ value0={isRanged ? value[0] : range[0]}
+ value1={isRanged ? value[1] : value}
+ step={step}
+ isRanged={Boolean(isRanged)}
+ onChange={v => onChange({[property]: isRanged ? v : v[1]})}
+ inputTheme={inputTheme}
+ showInput
+ />
+ ) : (
+ <CustomInput
+ isRanged={isRanged}
+ value={value}
+ onChangeCustomInput={v => onChange({[property]: v})}
+ />
+ )}
+ </SidePanelSection>
+ );
+ };
VisConfigSlider.propTypes = propTypes;
diff --git a/src/layers/layer-factory.js b/src/layers/layer-factory.js
index 564cb8211..6211dffb3 100644
--- a/src/layers/layer-factory.js
+++ b/src/layers/layer-factory.js
@@ -86,7 +86,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 100],
step: 0.1,
group: PROPERTY_GROUPS.stroke,
- property: 'thickness'
+ property: 'thickness',
+ allowCustomValue: true
},
strokeWidthRange: {
type: 'number',
@@ -96,7 +97,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 200],
step: 0.1,
group: PROPERTY_GROUPS.stroke,
- property: 'sizeRange'
+ property: 'sizeRange',
+ allowCustomValue: true
},
trailLength: {
type: 'number',
@@ -106,7 +108,8 @@ export const LAYER_VIS_CONFIGS = {
range: [1, 1000],
step: 1,
group: PROPERTY_GROUPS.stroke,
- property: 'trailLength'
+ property: 'trailLength',
+ allowCustomValue: true
},
// radius is actually radiusScale in deck.gl
radius: {
@@ -117,7 +120,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 100],
step: 0.1,
group: PROPERTY_GROUPS.radius,
- property: 'radius'
+ property: 'radius',
+ allowCustomValue: true
},
fixedRadius: {
defaultValue: false,
@@ -135,7 +139,8 @@ export const LAYER_VIS_CONFIGS = {
step: 0.1,
label: 'layerVisConfigs.radiusRange',
group: PROPERTY_GROUPS.radius,
- property: 'radiusRange'
+ property: 'radiusRange',
+ allowCustomValue: true
},
clusterRadius: {
type: 'number',
@@ -145,7 +150,8 @@ export const LAYER_VIS_CONFIGS = {
range: [1, 500],
step: 0.1,
group: PROPERTY_GROUPS.radius,
- property: 'clusterRadius'
+ property: 'clusterRadius',
+ allowCustomValue: true
},
clusterRadiusRange: {
type: 'number',
@@ -155,7 +161,8 @@ export const LAYER_VIS_CONFIGS = {
range: [1, 150],
step: 0.1,
group: PROPERTY_GROUPS.radius,
- property: 'radiusRange'
+ property: 'radiusRange',
+ allowCustomValue: true
},
opacity: {
type: 'number',
@@ -165,7 +172,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 1],
step: 0.01,
group: PROPERTY_GROUPS.color,
- property: 'opacity'
+ property: 'opacity',
+ allowCustomValue: false
},
coverage: {
type: 'number',
@@ -175,7 +183,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 1],
step: 0.01,
group: PROPERTY_GROUPS.cell,
- property: 'coverage'
+ property: 'coverage',
+ allowCustomValue: false
},
// used in point layer
outline: {
@@ -249,7 +258,8 @@ export const LAYER_VIS_CONFIGS = {
property: 'percentile',
// percentile filter only makes sense with linear aggregation
- condition: config => config.colorScale !== 'ordinal'
+ condition: config => config.colorScale !== 'ordinal',
+ allowCustomValue: false
},
elevationPercentile: {
type: 'number',
@@ -264,7 +274,8 @@ export const LAYER_VIS_CONFIGS = {
group: PROPERTY_GROUPS.height,
property: 'elevationPercentile',
// percentile filter only makes sense with linear aggregation
- condition: config => config.visConfig.enable3d && (config.colorField || config.sizeField)
+ condition: config => config.visConfig.enable3d && (config.colorField || config.sizeField),
+ allowCustomValue: false
},
resolution: {
type: 'number',
@@ -274,7 +285,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 13],
step: 1,
group: PROPERTY_GROUPS.cell,
- property: 'resolution'
+ property: 'resolution',
+ allowCustomValue: true
},
sizeScale: {
type: 'number',
@@ -284,7 +296,8 @@ export const LAYER_VIS_CONFIGS = {
range: [1, 1000],
step: 1,
group: PROPERTY_GROUPS.stroke,
- property: 'sizeScale'
+ property: 'sizeScale',
+ allowCustomValue: true
},
angle: {
type: 'number',
@@ -294,7 +307,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 360],
group: PROPERTY_GROUPS.angle,
step: 1,
- property: 'angle'
+ property: 'angle',
+ allowCustomValue: true
},
worldUnitSize: {
type: 'number',
@@ -304,7 +318,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 500],
step: 0.0001,
group: PROPERTY_GROUPS.cell,
- property: 'worldUnitSize'
+ property: 'worldUnitSize',
+ allowCustomValue: true
},
elevationScale: {
type: 'number',
@@ -314,7 +329,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 100],
step: 0.1,
group: PROPERTY_GROUPS.height,
- property: 'elevationScale'
+ property: 'elevationScale',
+ allowCustomValue: true
},
enableElevationZoomFactor: {
type: 'boolean',
@@ -332,7 +348,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 1000],
step: 0.01,
group: PROPERTY_GROUPS.height,
- property: 'sizeRange'
+ property: 'sizeRange',
+ allowCustomValue: true
},
heightRange: {
type: 'number',
@@ -342,7 +359,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 1000],
step: 0.01,
group: PROPERTY_GROUPS.height,
- property: 'heightRange'
+ property: 'heightRange',
+ allowCustomValue: true
},
coverageRange: {
type: 'number',
@@ -352,7 +370,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 1],
step: 0.01,
group: PROPERTY_GROUPS.radius,
- property: 'coverageRange'
+ property: 'coverageRange',
+ allowCustomValue: false
},
// hi precision is deprecated by deck.gl
'hi-precision': {
@@ -409,7 +428,8 @@ export const LAYER_VIS_CONFIGS = {
step: 0.01,
group: PROPERTY_GROUPS.cell,
property: 'weight',
- condition: config => config.weightField
+ condition: config => config.weightField,
+ allowCustomValue: true
},
heatmapRadius: {
type: 'number',
@@ -419,7 +439,8 @@ export const LAYER_VIS_CONFIGS = {
range: [0, 100],
step: 0.1,
group: PROPERTY_GROUPS.cell,
- property: 'radius'
+ property: 'radius',
+ allowCustomValue: true
}
};