Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fixed bug when switching to dataset layer view #2209

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 31 additions & 23 deletions src/components/src/side-panel/layer-panel/layer-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ const SortableStyledItem = styled.div<SortableStyledItemProps>`
}
`;

const INITIAL_LAYERS_TO_SHOW: Layer[] = [];

LayerListFactory.deps = [LayerPanelFactory];

function LayerListFactory(LayerPanel: ReturnType<typeof LayerPanelFactory>) {
// By wrapping layer panel using a sortable element we don't have to implement the drag and drop logic into the panel itself;
// Developers can provide any layer panel implementation and it will still be sortable
const SortableItem = ({layer, layerIndex, panelProps, layerActions}) => {
const SortableItem = ({layer, idx, panelProps, layerActions}) => {
const {attributes, listeners, setNodeRef, isDragging, transform, transition} = useSortable({
id: layer.id
});
Expand All @@ -97,7 +99,7 @@ function LayerListFactory(LayerPanel: ReturnType<typeof LayerPanelFactory>) {
{...panelProps}
{...layerActions}
key={layer.id}
idx={layerIndex}
idx={idx}
layer={layer}
listeners={listeners}
/>
Expand Down Expand Up @@ -132,12 +134,12 @@ function LayerListFactory(LayerPanel: ReturnType<typeof LayerPanelFactory>) {

const layersToShow = useMemo(() => {
return layerOrder.reduce((acc, layerId) => {
const layer = findById(layerId)(layers);
const layer = findById(layerId)(layers.filter(Boolean));
if (!layer) {
return acc;
}
return !layer.config.hidden ? [...acc, layer] : acc;
}, [] as Layer[]);
}, INITIAL_LAYERS_TO_SHOW);
}, [layers, layerOrder]);

const sidePanelDndItems = useMemo(() => {
Expand All @@ -158,34 +160,39 @@ function LayerListFactory(LayerPanel: ReturnType<typeof LayerPanelFactory>) {
[layerClasses]
);

const layerActions = {
layerColorUIChange: visStateActions.layerColorUIChange,
layerConfigChange: visStateActions.layerConfigChange,
layerVisualChannelConfigChange: visStateActions.layerVisualChannelConfigChange,
layerTypeChange: visStateActions.layerTypeChange,
layerVisConfigChange: visStateActions.layerVisConfigChange,
layerTextLabelChange: visStateActions.layerTextLabelChange,
removeLayer: visStateActions.removeLayer,
duplicateLayer: visStateActions.duplicateLayer,
layerSetIsValid: visStateActions.layerSetIsValid
};

const panelProps = {
datasets,
openModal,
layerTypeOptions
};
const layerActions = useMemo(
() => ({
layerColorUIChange: visStateActions.layerColorUIChange,
layerConfigChange: visStateActions.layerConfigChange,
layerVisualChannelConfigChange: visStateActions.layerVisualChannelConfigChange,
layerTypeChange: visStateActions.layerTypeChange,
layerVisConfigChange: visStateActions.layerVisConfigChange,
layerTextLabelChange: visStateActions.layerTextLabelChange,
removeLayer: visStateActions.removeLayer,
duplicateLayer: visStateActions.duplicateLayer,
layerSetIsValid: visStateActions.layerSetIsValid
}),
[visStateActions]
);

const panelProps = useMemo(
() => ({
datasets,
openModal,
layerTypeOptions
}),
[datasets, openModal, layerTypeOptions]
);

return isSortable ? (
<>
<SortableList containerId="sortablelist" sidePanelDndItems={sidePanelDndItems}>
{/* warning: containerId should be similar to the first key in dndItems defined in kepler-gl.js*/}

{layersToShow.map(layer => (
<SortableItem
key={layer.id}
layer={layer}
layerIndex={layers.findIndex(({id}) => id === layer.id)}
idx={layers.findIndex(l => l?.id === layer.id)}
panelProps={panelProps}
layerActions={layerActions}
/>
Expand All @@ -199,6 +206,7 @@ function LayerListFactory(LayerPanel: ReturnType<typeof LayerPanelFactory>) {
{...panelProps}
{...layerActions}
key={layer.id}
idx={layers.findIndex(l => l?.id === layer.id)}
layer={layer}
isDraggable={false}
/>
Expand Down
1 change: 1 addition & 0 deletions src/components/src/side-panel/layer-panel/layer-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type LayerPanelProps = {
requireData: any; //
}[];
isDraggable?: boolean;
idx: number;
openModal: ActionHandler<typeof toggleModal>;
layerColorUIChange: ActionHandler<typeof VisStateActions.layerColorUIChange>;
layerConfigChange: ActionHandler<typeof VisStateActions.layerConfigChange>;
Expand Down
8 changes: 7 additions & 1 deletion src/components/src/side-panel/panel-header-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ export default function PanelHeaderActionFactory(): React.FC<PanelHeaderActionPr
flush={flush}
>
{IconComponent ? (
<IconComponent data-tip data-for={`${tooltip}_${id}`} height="16px" onClick={onClick} />
<IconComponent
className="panel--header__action__component"
data-tip
data-for={`${tooltip}_${id}`}
height="16px"
onClick={onClick}
/>
) : null}
{tooltip ? (
<Tooltip id={`${tooltip}_${id}`} effect="solid" delayShow={500} type={tooltipType}>
Expand Down
33 changes: 33 additions & 0 deletions test/browser/components/side-panel/layer-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import React from 'react';
import test from 'tape';
import sinon from 'sinon';

import {StateWMultiH3Layers} from 'test/helpers/mock-state';

Expand Down Expand Up @@ -98,3 +99,35 @@ test('Components -> SidePanel -> LayerPanel -> LayerList -> render non-sortable

t.end();
});

test('Components -> SidePanel -> LayerPanel -> LayerList -> pass null entries as layers', t => {
let wrapper;
const layers = [...defaultProps.layers];
layers[0] = null;
const removeLayerSpy = sinon.spy();
const visStateActions = {...defaultProps.visStateActions};
visStateActions.removeLayer = removeLayerSpy;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<LayerList
{...defaultProps}
isSortable={false}
layers={layers}
visStateActions={visStateActions}
/>
</IntlWrapper>
);
}, 'LayerList should render');

t.equal(wrapper.find('LayerPanel').length, 1, 'should render 1 LayerPanel');

const removeLayer = wrapper.find(
'div.panel--header__action.layer__remove-layer svg.data-ex-icons-trash'
);

removeLayer.simulate('click');
t.equal(removeLayerSpy.called, true, 'Should have called remove layer when clicked');

t.end();
});