Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Oct 2, 2020
1 parent 1786760 commit 21d284c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@
.lnsDimensionContainer__header {
padding: $euiSizeS;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import './dimension_container.scss';

import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import {
EuiFlyoutHeader,
EuiFlyoutFooter,
Expand All @@ -21,21 +21,30 @@ import { i18n } from '@kbn/i18n';
export function DimensionContainer({
isOpen,
groupLabel,
close,
handleClose,
panel,
}: {
isOpen: boolean;
close: () => void;
handleClose: () => void;
panel: React.ReactElement;
groupLabel: string;
}) {
const [focusTrapIsEnabled, setFocusTrapIsEnabled] = useState(false);

const closeFlyout = () => {
close();
handleClose();
setFocusTrapIsEnabled(false);
};

useEffect(() => {
if (isOpen) {
// without setTimeout here the flyout pushes content when animating
setTimeout(() => {
setFocusTrapIsEnabled(true);
}, 255);
}
}, [isOpen]);

return isOpen ? (
<EuiFocusTrap disabled={!focusTrapIsEnabled} clickOutsideDisables={true}>
<EuiOutsideClickDetector onOutsideClick={closeFlyout} isDisabled={!isOpen}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import { DragContext, DragDrop, ChildDragDropProvider } from '../../../drag_drop
import { LayerSettings } from './layer_settings';
import { trackUiEvent } from '../../../lens_ui_telemetry';
import { generateId } from '../../../id_generator';
import { ConfigPanelWrapperProps, DimensionState } from './types';
import { ConfigPanelWrapperProps, ActiveDimensionState } from './types';
import { DimensionContainer } from './dimension_container';

const initialDimensionState = {
isOpen: false,
const initialActiveDimensionState = {
isNew: false,
};

Expand Down Expand Up @@ -69,13 +68,15 @@ export function LayerPanel(
}
) {
const dragDropContext = useContext(DragContext);
const [dimensionState, setDimensionState] = useState<DimensionState>(initialDimensionState);
const [activeDimension, setActiveDimension] = useState<ActiveDimensionState>(
initialActiveDimensionState
);

const { framePublicAPI, layerId, isOnlyLayer, onRemoveLayer, dataTestSubj } = props;
const datasourcePublicAPI = framePublicAPI.datasourceLayers[layerId];

useEffect(() => {
setDimensionState(initialDimensionState);
setActiveDimension(initialActiveDimensionState);
}, [props.activeVisualizationId]);

if (
Expand Down Expand Up @@ -114,7 +115,7 @@ export function LayerPanel(

const { groups } = activeVisualization.getConfiguration(layerVisualizationConfigProps);
const isEmptyLayer = !groups.some((d) => d.accessors.length > 0);
const { activeId, activeGroup } = dimensionState;
const { activeId, activeGroup } = activeDimension;
return (
<ChildDragDropProvider {...dragDropContext}>
<EuiPanel data-test-subj={dataTestSubj} className="lnsLayerPanel" paddingSize="s">
Expand Down Expand Up @@ -209,7 +210,7 @@ export function LayerPanel(
: 'add'
}
data-test-subj={group.dataTestSubj}
draggable={!dimensionState.isOpen}
draggable={!activeId}
value={{ columnId: accessor, groupId: group.groupId, layerId }}
isValueEqual={isSameConfiguration}
label={group.groupLabel}
Expand Down Expand Up @@ -253,11 +254,10 @@ export function LayerPanel(
filterOperations: group.filterOperations,
suggestedPriority: group.suggestedPriority,
onClick: () => {
if (dimensionState.isOpen) {
setDimensionState(initialDimensionState);
if (activeId) {
setActiveDimension(initialActiveDimensionState);
} else {
setDimensionState({
isOpen: true,
setActiveDimension({
isNew: false,
activeGroup: group,
activeId: accessor,
Expand Down Expand Up @@ -355,11 +355,10 @@ export function LayerPanel(
}}
data-test-subj="lns-empty-dimension"
onClick={() => {
if (dimensionState.isOpen) {
setDimensionState(initialDimensionState);
if (activeId) {
setActiveDimension(initialActiveDimensionState);
} else {
setDimensionState({
isOpen: true,
setActiveDimension({
isNew: true,
activeGroup: group,
activeId: newId,
Expand All @@ -380,9 +379,9 @@ export function LayerPanel(
);
})}
<DimensionContainer
isOpen={dimensionState.isOpen}
isOpen={!!activeId}
groupLabel={activeGroup?.groupLabel || ''}
close={() => setDimensionState(initialDimensionState)}
handleClose={() => setActiveDimension(initialActiveDimensionState)}
panel={
<>
{activeGroup && activeId && (
Expand All @@ -405,8 +404,8 @@ export function LayerPanel(
prevState: props.visualizationState,
})
);
setDimensionState({
...dimensionState,
setActiveDimension({
...activeDimension,
isNew: false,
});
},
Expand All @@ -415,7 +414,7 @@ export function LayerPanel(
)}
{activeGroup &&
activeId &&
!dimensionState.isNew &&
!activeDimension.isNew &&
activeVisualization.renderDimensionEditor &&
activeGroup?.enableDimensionEditor && (
<div className="lnsLayerPanel__styleEditor">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export interface ConfigPanelWrapperProps {
core: DatasourceDimensionEditorProps['core'];
}

export interface DimensionState {
isOpen: boolean;
export interface ActiveDimensionState {
isNew: boolean;
activeId?: string;
activeGroup?: VisualizationDimensionGroupConfig;
Expand Down

0 comments on commit 21d284c

Please sign in to comment.