Skip to content

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

fix: fixes to channel by value (#3216)

Igor Dykhta24529655d636
diff --git a/src/components/src/common/color-legend.tsx b/src/components/src/common/color-legend.tsx
index 2dcedc729..7a479d148 100644
--- a/src/components/src/common/color-legend.tsx
+++ b/src/components/src/common/color-legend.tsx
@@ -168,7 +168,7 @@ export function LegendRowFactory(
[color, onUpdateLabel]
);
const onReset = useCallback(() => onResetLabel && onResetLabel(color), [color, onResetLabel]);
- const value = displayLabel ? label.toString() : '';
+ const value = displayLabel ? String(label ?? '') : '';
return (
<StyledLegendRow>
<LegendColorDisplay color={color} />
diff --git a/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx b/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
index 521861e6f..bb839332d 100644
--- a/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
+++ b/src/components/src/side-panel/layer-panel/color-breaks-panel.tsx
@@ -12,7 +12,7 @@ import {
colorMapToColorBreaks,
isNumericColorBreaks as notOrdinalColorBreaks
} from '@kepler.gl/utils';
-import React, {useCallback, useMemo} from 'react';
+import React, {useCallback, useMemo, useEffect} from 'react';
import styled from 'styled-components';
import ColumnStatsChartFactory from '../../common/column-stats-chart';
import {Edit} from '../../common/icons';
@@ -155,6 +155,17 @@ function ColorBreaksPanelFactory(
[customPalette.colorMap, isEditingCustomBreaks, colorBreaks]
);
+ // Update layers on editing custom breaks
+ useEffect(() => {
+ const {type} = customPalette || {};
+ if (
+ isEditingCustomBreaks &&
+ (type === SCALE_TYPES.customOrdinal || type === SCALE_TYPES.custom)
+ ) {
+ onScaleChange(type, customPalette);
+ }
+ }, [isEditingCustomBreaks, customPalette, onScaleChange]);
+
const onClickEditCustomBreaks = useCallback(() => {
setColorUI({
colorRangeConfig: {
@@ -231,8 +242,8 @@ function ColorBreaksPanelFactory(
currentBreaks={currentBreaks}
onEdit={isCustomBreaks ? onClickEditCustomBreaks : null}
/>
- ) : customPalette.colorMap &&
- customPalette.type === 'customOrdinal' &&
+ ) : (isCustomBreaks || customPalette.type === SCALE_TYPES.customOrdinal) &&
+ customPalette.colorMap &&
customPalette.name?.endsWith(colorField.name) ? (
<CategoricalColorDisplay
colorMap={customPalette.colorMap}
diff --git a/src/components/src/side-panel/layer-panel/color-scale-selector.tsx b/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
index 881905ca6..2b2cd19b0 100644
--- a/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
+++ b/src/components/src/side-panel/layer-panel/color-scale-selector.tsx
@@ -148,6 +148,28 @@ function ColorScaleSelectorFactory(
);
const [tippyInstance, setTippyInstance] = useState<TippyInstance>();
const isEditingColorBreaks = colorUIConfig?.colorRangeConfig?.customBreaks;
+
+ // Stores the previous selection for live preview: when choosing Custom/Custom Ordinal, we apply a temporary palette.
+ // Cancel restores {scale, range} from this ref; Confirm keeps the change and clears the ref.
+ // If the user switches between different custom scale types (e.g., from "Custom" to "Custom Ordinal") or is already in a custom scale state,
+ // this ref is updated to always store the most recent non-custom selection. Only the latest non-custom selection is restorable on cancel.
+ const prevSelectionRef = React.useRef<{scale: string; range: ColorRange} | null>(null);
+
+ // when custom color scale - but Confirm is not clicked yet
+ const pendingOption = useMemo(
+ () =>
+ isEditingColorBreaks
+ ? (dropdownSelectProps.options || []).find(
+ o => getOptionValue(o) === colorUIConfig?.customPalette?.type
+ ) || null
+ : null,
+ [
+ isEditingColorBreaks,
+ dropdownSelectProps.options,
+ getOptionValue,
+ colorUIConfig?.customPalette?.type
+ ]
+ );
const colorScale = useMemo(
() =>
getLayerColorScale({
@@ -234,14 +256,16 @@ function ColorScaleSelectorFactory(
const onSelectScale = useCallback(
val => {
// highlight selected option
- if (!val || isEditingColorBreaks) return;
+ if (!val) return;
+
const selectedScale = getOptionValue(val);
- if (selectedScale === SCALE_TYPES.custom) {
+ if (selectedScale === SCALE_TYPES.custom || selectedScale === SCALE_TYPES.customOrdinal) {
const customPalette = initCustomPaletteByCustomScale({
scale: selectedScale,
field,
range,
- colorBreaks
+ colorBreaks,
+ ...(selectedScale === SCALE_TYPES.customOrdinal ? {ordinalDomain} : {})
});
setColorUI({
showColorChart: true,
@@ -250,28 +274,55 @@ function ColorScaleSelectorFactory(
},
customPalette
});
+ // store previous selection for cancel, then preview custom on the map
+ if (!prevSelectionRef.current) {
+ prevSelectionRef.current = {scale: scaleType, range};
+ }
onSelect(selectedScale, customPalette);
- } else if (hasColorMap(range) && selectedScale !== SCALE_TYPES.customOrdinal) {
+ } else if (hasColorMap(range)) {
// not custom
// remove colorMap
// eslint-disable-next-line no-unused-vars
const {colorMap: _, ...newRange} = range;
+ // reset colorUI before changing the scale
+ setColorUI({
+ showColorChart: false,
+ colorRangeConfig: {
+ customBreaks: false
+ }
+ });
onSelect(selectedScale, newRange);
} else {
+ // reset colorUI before changing the scale
+ setColorUI({
+ showColorChart: false,
+ colorRangeConfig: {
+ customBreaks: false
+ }
+ });
onSelect(selectedScale);
}
},
- [isEditingColorBreaks, field, setColorUI, onSelect, range, getOptionValue, colorBreaks]
+ [field, setColorUI, onSelect, range, getOptionValue, colorBreaks, ordinalDomain, scaleType]
);
const onApply = useCallback(() => {
- onSelect(scaleType, colorUIConfig.customPalette);
+ // change scale type only if confirmed
+ const nextScaleType = colorUIConfig?.customPalette?.type || scaleType;
+ onSelect(nextScaleType, colorUIConfig.customPalette);
hideTippy(tippyInstance);
+ prevSelectionRef.current = null;
}, [onSelect, colorUIConfig.customPalette, tippyInstance, scaleType]);
const onCancel = useCallback(() => {
+ // restore previous selection if any
+ if (prevSelectionRef.current) {
+ const {scale: prevScale, range: prevRange} = prevSelectionRef.current;
+ onSelect(prevScale, prevRange);
+ }
hideTippy(tippyInstance);
- }, [tippyInstance]);
+ prevSelectionRef.current = null;
+ }, [tippyInstance, onSelect]);
const isCustomBreaks =
scaleType === SCALE_TYPES.custom || scaleType === SCALE_TYPES.customOrdinal;
@@ -317,6 +368,9 @@ function ColorScaleSelectorFactory(
customListComponent={ColorScaleSelectDropdown}
searchable={false}
showOptionsWhenEmpty
+ selectedItems={
+ pendingOption ? [pendingOption] : dropdownSelectProps.selectedItems
+ }
/>
)}
</DropdownWrapper>
@@ -327,7 +381,7 @@ function ColorScaleSelectorFactory(
<DropdownSelect
{...dropdownSelectProps}
displayOption={displayOption}
- value={dropdownSelectProps.selectedItems[0]}
+ value={pendingOption || dropdownSelectProps.selectedItems[0]}
/>
</div>
</LazyTippy>
diff --git a/src/components/src/side-panel/layer-panel/custom-palette.tsx b/src/components/src/side-panel/layer-panel/custom-palette.tsx
index c9c486ee4..b8ebcd965 100644
--- a/src/components/src/side-panel/layer-panel/custom-palette.tsx
+++ b/src/components/src/side-panel/layer-panel/custom-palette.tsx
@@ -377,8 +377,11 @@ export const EditableColorRange: React.FC<EditableColorRangeProps> = ({
editColorMap,
editable
}) => {
- const noMinBound = !Number.isFinite(item.inputs[0]) && index === 0;
- const noMaxBound = !Number.isFinite(item.inputs[1]) && isLast;
+ const hasInputs = Array.isArray(item?.inputs);
+ const leftInput = hasInputs ? item.inputs[0] : undefined;
+ const rightInput = hasInputs ? item.inputs[1] : undefined;
+ const noMinBound = !Number.isFinite(leftInput) && index === 0;
+ const noMaxBound = !Number.isFinite(rightInput) && isLast;
const onChangeLeft = useCallback(
val => {
if (editable && editColorMap) editColorMap(parseFloat(val), index - 1);
@@ -395,7 +398,7 @@ export const EditableColorRange: React.FC<EditableColorRangeProps> = ({
return (
<StyledRangeInput>
<ColorPaletteInput
- value={noMinBound ? 'Less' : item.inputs[0].toString()}
+ value={noMinBound ? 'Less' : String(leftInput ?? '')}
id={`color-palette-input-${index}-left`}
width="50px"
textAlign="end"
@@ -404,7 +407,7 @@ export const EditableColorRange: React.FC<EditableColorRangeProps> = ({
/>
<Dash />
<ColorPaletteInput
- value={noMaxBound ? 'More' : item.inputs[1].toString()}
+ value={noMaxBound ? 'More' : String(rightInput ?? '')}
id={`color-palette-input-${index}-right`}
width="50px"
textAlign="end"
@@ -468,7 +471,7 @@ export const CustomPaletteInput: React.FC<CustomPaletteInputProps> = ({
/>
</StyledColorHexInput>
) : null}
- {isNumericColorBreaks(colorBreaks) ? (
+ {colorBreaks && index < colorBreaks.length && isNumericColorBreaks(colorBreaks) ? (
<EditableColorRange
item={colorBreaks[index]}
isLast={index === colorBreaks.length - 1}
@@ -719,6 +722,9 @@ export const CategoricalSelector: React.FC<CategoricalSelectorProps> = ({
listAnchor: 'list__item__anchor'
}}
options={allValues}
+ // add safe string casting for the Typeahead, so fuzzy search never receives non-strings, preventing the toLowerCase crash
+ displayOption={o => String(o ?? '')}
+ filterOption={(input, o) => String(o ?? '').includes(String(input ?? ''))}
placeholder={'Search'}
onOptionSelected={onOptionSelected}
customListComponent={ModifiedDropdownList}
diff --git a/test/browser/components/side-panel/channel-by-value-selctor-test.js b/test/browser/components/side-panel/channel-by-value-selctor-test.js
index de1b7bd71..00b266cba 100644
--- a/test/browser/components/side-panel/channel-by-value-selctor-test.js
+++ b/test/browser/components/side-panel/channel-by-value-selctor-test.js
@@ -373,7 +373,11 @@ test('Components -> ChannelByValueSelector -> ColorScaleSelector -> ColorBreakDi
t.equal(confirmButton.text(), 'Confirm', 'should render confirm button');
confirmButton.simulate('click');
- t.ok(updateLayerVisualChannelConfig.calledTwice, 'should call updateLayerVisualChannelConfig');
+ // Confirm should result in at least a second update call
+ t.ok(
+ updateLayerVisualChannelConfig.args && updateLayerVisualChannelConfig.args.length >= 2,
+ 'should call updateLayerVisualChannelConfig'
+ );
const expectedArgs3 = [
{colorScale: 'custom'},
@@ -432,13 +436,13 @@ test('Components -> ChannelByValueSelector -> ColorScaleSelector -> ColorBreakDi
quantileOption.simulate('click');
const expectedArgs5 = [
- {colorScale: 'quantile'},
+ {colorScale: 'quantize'},
'color',
{
colorRange: {
- name: 'color.customPalette.custom.uid',
- type: 'custom',
- category: 'Custom',
+ name: 'Uber Viz Sequential',
+ type: 'sequential',
+ category: 'Uber',
colors: ['#00939C', '#6BB5B9', '#AAD7D9', '#E6FAFA']
}
}
@@ -450,7 +454,7 @@ test('Components -> ChannelByValueSelector -> ColorScaleSelector -> ColorBreakDi
'should pass custom scale and custom colorRange'
);
- t.deepEqual(setColorUI.args.length, 3, 'should not call setColorUI');
+ t.deepEqual(setColorUI.args.length, 4, 'should not call setColorUI');
t.end();
});