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

[Vis-Builder] Fix Droppable Areas not highlighting while dragging a Field #7527

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ConfigPanelProps {
aggProps: AggProps;
activeSchemaFields: SchemaDisplayStates;
setActiveSchemaFields: React.Dispatch<React.SetStateAction<SchemaDisplayStates>>;
isDragging: boolean;
}

export function ConfigPanel({
Expand All @@ -39,14 +40,16 @@ export function ConfigPanel({
aggProps,
activeSchemaFields,
setActiveSchemaFields,
isDragging,
}: ConfigPanelProps) {
if (!schemas) return null;

const mainPanel = mapSchemaToAggPanel(
schemas,
aggProps,
activeSchemaFields,
setActiveSchemaFields
setActiveSchemaFields,
isDragging
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export enum FIELD_SELECTOR_ID {
NUMERICAL = 'numericalFields',
META = 'metaFields',
}
export const ADD_PANEL_PREFIX = 'AddPanel_';
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
isValidDropTarget,
canDrop,
dropProps,
isDragging,
}: DropboxProps) => {
const prefersReducedMotion = usePrefersReducedMotion();
const [closing, setClosing] = useState<boolean | string>(false);
Expand All @@ -79,58 +80,74 @@
droppableId={dropboxId}
isCombineEnabled={true}
>
{fields.map(({ id, label }, index) => (
<EuiDraggable
className={`dropBox__draggable ${id === closing && 'closing'}`}
key={id}
draggableId={id}
index={index}
>
<EuiPanel
key={index}
paddingSize="s"
className="dropBox__field"
data-test-subj={`dropBoxField-${dropboxId}-${index}`}
>
<EuiText size="s" className="dropBox__field_text" onClick={() => onEditField(id)}>
<a role="button" tabIndex={0}>
{label}
</a>
</EuiText>
<EuiSmallButtonIcon
color="subdued"
iconType="cross"
aria-label="clear-field"
iconSize="s"
onClick={() => animateDelete(id)}
data-test-subj="dropBoxRemoveBtn"
/>
</EuiPanel>
</EuiDraggable>
))}
{(provided, snapshot) => (
<div ref={provided.innerRef} {...provided.droppableProps}>

Check warning on line 84 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L84

Added line #L84 was not covered by tests
{fields.map(({ id, label }, index) => (
<EuiDraggable

Check warning on line 86 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L86

Added line #L86 was not covered by tests
className={`dropBox__draggable ${id === closing && 'closing'}`}
key={id}
draggableId={id}
index={index}
>
<EuiPanel
key={index}
paddingSize="s"
className={`dropBox__field dropBox__dropTarget ${
isDragging ? 'validField' : ''
} ${snapshot.isDraggingOver ? 'canDrop' : ''}`}
data-test-subj={`dropBoxField-${dropboxId}-${index}`}
>
<EuiText
size="s"
className="dropBox__field_text"
onClick={() => onEditField(id)}

Check warning on line 103 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L103

Added line #L103 was not covered by tests
>
<a role="button" tabIndex={0}>
{label}
</a>
</EuiText>
<EuiSmallButtonIcon
color="subdued"
iconType="cross"
aria-label="clear-field"
iconSize="s"
onClick={() => animateDelete(id)}

Check warning on line 114 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L114

Added line #L114 was not covered by tests
data-test-subj="dropBoxRemoveBtn"
/>
</EuiPanel>
</EuiDraggable>
))}
</div>
)}
</EuiDroppable>
<EuiDroppable droppableId={`AddPanel_${dropboxId}`}>
{(provided, snapshot) => (
<div ref={provided.innerRef} {...provided.droppableProps}>

Check warning on line 125 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L125

Added line #L125 was not covered by tests
{fields.length < limit && (
<EuiPanel
data-test-subj={`dropBoxAddField-${dropboxId}`}
className={`dropBox__field dropBox__dropTarget ${
isDragging ? 'validField' : ''
} ${snapshot.isDraggingOver ? 'canDrop' : ''}`}
{...(isValidDropTarget && dropProps)}
>
<EuiText size="s">
{i18n.translate('visBuilder.dropbox.addField.title', {
defaultMessage: 'Click or drop to add',
})}
</EuiText>
<EuiSmallButtonIcon
iconType="plusInCircle"
aria-label="clear-field"
iconSize="s"
onClick={() => onAddField()}

Check warning on line 143 in src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/dropbox.tsx#L143

Added line #L143 was not covered by tests
data-test-subj="dropBoxAddBtn"
/>
</EuiPanel>
)}
</div>
)}
</EuiDroppable>
{fields.length < limit && (
<EuiPanel
data-test-subj={`dropBoxAddField-${dropboxId}`}
className={`dropBox__field dropBox__dropTarget ${
isValidDropTarget ? 'validField' : ''
} ${canDrop ? 'canDrop' : ''}`}
{...(isValidDropTarget && dropProps)}
>
<EuiText size="s">
{i18n.translate('visBuilder.dropbox.addField.title', {
defaultMessage: 'Click or drop to add',
})}
</EuiText>
<EuiSmallButtonIcon
iconType="plusInCircle"
aria-label="clear-field"
iconSize="s"
onClick={() => onAddField()}
data-test-subj="dropBoxAddBtn"
/>
</EuiPanel>
)}
</div>
</EuiCompressedFormRow>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import React, { useEffect, useState } from 'react';
import { DropResult, EuiDragDropContext } from '@elastic/eui';
import { DraggableLocation, DropResult, EuiDragDropContext } from '@elastic/eui';
import { FieldSelector } from './field_selector';

import './index.scss';
Expand All @@ -15,7 +15,7 @@
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { VisBuilderServices } from '../../../types';
import { DropboxDisplay } from './dropbox';
import { FIELD_SELECTOR_ID } from './constants';
import { ADD_PANEL_PREFIX, FIELD_SELECTOR_ID } from './constants';
import { addFieldToConfiguration } from './drag_drop/add_field_to_configuration';
import { replaceFieldInConfiguration } from './drag_drop/replace_field_in_configuration';
import { reorderFieldsWithinSchema } from './drag_drop/reorder_fields_within_schema';
Expand Down Expand Up @@ -49,6 +49,7 @@
return acc;
}, {});
});
const [isDragging, setIsDragging] = useState<boolean>(false);

Check warning on line 52 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L52

Added line #L52 was not covered by tests
const dispatch = useTypedDispatch();

useEffect(() => {
Expand All @@ -64,6 +65,7 @@

const handleDragEnd = (dropResult: DropResult) => {
try {
setIsDragging(false); // Reseting the Dragging flag

Check warning on line 68 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L68

Added line #L68 was not covered by tests
const { source, destination, combine } = dropResult;

const destinationSchemaName = destination?.droppableId || combine?.droppableId;
Expand All @@ -76,6 +78,49 @@

const panelGroups = Array.from(schemas.all.map((schema) => schema.name));

if (destinationSchemaName.startsWith(ADD_PANEL_PREFIX)) {
const updatedDestinationSchemaName = destinationSchemaName.split(ADD_PANEL_PREFIX)[1];

Check warning on line 82 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L82

Added line #L82 was not covered by tests

if (Object.values(FIELD_SELECTOR_ID).includes(sourceSchemaName as FIELD_SELECTOR_ID)) {
if (panelGroups.includes(updatedDestinationSchemaName)) {
const newDropResult = {

Check warning on line 86 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L86

Added line #L86 was not covered by tests
...dropResult,
destination: {
droppableId: updatedDestinationSchemaName,
index: 0,
} as DraggableLocation,
};
addFieldToConfiguration({

Check warning on line 93 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L93

Added line #L93 was not covered by tests
dropResult: newDropResult,
aggProps,
aggService,
activeSchemaFields,
dispatch,
schemas,
});
}
} else if (panelGroups.includes(sourceSchemaName)) {
if (panelGroups.includes(updatedDestinationSchemaName)) {
const newDropResult = {

Check warning on line 104 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L104

Added line #L104 was not covered by tests
...dropResult,
destination: {
droppableId: updatedDestinationSchemaName,
index: 0,
} as DraggableLocation,
};
moveFieldBetweenSchemas({

Check warning on line 111 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L111

Added line #L111 was not covered by tests
dropResult: newDropResult,
aggProps,
aggService,
activeSchemaFields,
dispatch,
schemas,
});
}
}
return;

Check warning on line 121 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L121

Added line #L121 was not covered by tests
}

if (Object.values(FIELD_SELECTOR_ID).includes(sourceSchemaName as FIELD_SELECTOR_ID)) {
if (panelGroups.includes(destinationSchemaName) && !combine) {
addFieldToConfiguration({
Expand Down Expand Up @@ -134,7 +179,7 @@
};

return (
<EuiDragDropContext onDragEnd={handleDragEnd}>
<EuiDragDropContext onDragEnd={handleDragEnd} onDragStart={() => setIsDragging(true)}>

Check warning on line 182 in src/plugins/vis_builder/public/application/components/data_tab/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/application/components/data_tab/index.tsx#L182

Added line #L182 was not covered by tests
<div className="vbDataTab">
<FieldSelector />
<ConfigPanel
Expand All @@ -143,6 +188,7 @@
aggProps={aggProps}
activeSchemaFields={activeSchemaFields}
setActiveSchemaFields={setActiveSchemaFields}
isDragging={isDragging}
/>
</div>
</EuiDragDropContext>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const mapSchemaToAggPanel = (
schemas: Schemas,
aggProps: AggProps,
activeSchemaFields: SchemaDisplayStates,
setActiveSchemaFields: React.Dispatch<React.SetStateAction<SchemaDisplayStates>>
setActiveSchemaFields: React.Dispatch<React.SetStateAction<SchemaDisplayStates>>,
isDragging: boolean
) => {
const panelComponents = schemas.all.map((schema) => {
return (
Expand All @@ -25,6 +26,7 @@ export const mapSchemaToAggPanel = (
aggProps={aggProps}
activeSchemaFields={activeSchemaFields}
setActiveSchemaFields={setActiveSchemaFields}
isDragging={isDragging}
/>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface UseDropboxProps extends Pick<DropboxProps, 'id' | 'label'> {
aggProps: AggProps;
activeSchemaFields: SchemaDisplayStates;
setActiveSchemaFields: React.Dispatch<React.SetStateAction<SchemaDisplayStates>>;
isDragging: boolean;
}

export const useDropbox = (props: UseDropboxProps): DropboxProps => {
Expand All @@ -36,6 +37,7 @@ export const useDropbox = (props: UseDropboxProps): DropboxProps => {
aggProps,
activeSchemaFields,
setActiveSchemaFields,
isDragging,
} = props;
const [validAggTypes, setValidAggTypes] = useState<string[]>([]);
const { aggConfigs, indexPattern, aggs, timeRange } = aggProps;
Expand Down Expand Up @@ -209,5 +211,6 @@ export const useDropbox = (props: UseDropboxProps): DropboxProps => {
dragData,
isValidDropTarget: canDrop,
dropProps,
isDragging,
};
};
Loading