Skip to content

Commit

Permalink
[Vis-Builder] Fix Droppable Areas not highlighting while dragging a F…
Browse files Browse the repository at this point in the history
…ield (opensearch-project#7527) (opensearch-project#7780)

* [Vis-Builder] Highlight Droppable Areas while dragging a Field



* Changeset file for PR opensearch-project#7527 created/updated

---------



(cherry picked from commit 901ac51)

Signed-off-by: Suchit Sahoo <suchsah@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 21, 2024
1 parent 3835fc2 commit 06da6ce
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 56 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7527.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Not highlighting Droppable Areas while dragging a field ([#7527](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7527))
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 @@ const DropboxComponent = ({
isValidDropTarget,
canDrop,
dropProps,
isDragging,
}: DropboxProps) => {
const prefersReducedMotion = usePrefersReducedMotion();
const [closing, setClosing] = useState<boolean | string>(false);
Expand All @@ -79,58 +80,74 @@ const DropboxComponent = ({
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}>
{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 dropBox__dropTarget ${
isDragging ? 'validField' : ''
} ${snapshot.isDraggingOver ? 'canDrop' : ''}`}
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>
))}
</div>
)}
</EuiDroppable>
<EuiDroppable droppableId={`AddPanel_${dropboxId}`}>
{(provided, snapshot) => (
<div ref={provided.innerRef} {...provided.droppableProps}>
{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()}
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 { useTypedDispatch, useTypedSelector } from '../../utils/state_management
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 @@ export const DataTab = () => {
return acc;
}, {});
});
const [isDragging, setIsDragging] = useState<boolean>(false);
const dispatch = useTypedDispatch();

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

const handleDragEnd = (dropResult: DropResult) => {
try {
setIsDragging(false); // Reseting the Dragging flag
const { source, destination, combine } = dropResult;

const destinationSchemaName = destination?.droppableId || combine?.droppableId;
Expand All @@ -76,6 +78,49 @@ export const DataTab = () => {

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

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

if (Object.values(FIELD_SELECTOR_ID).includes(sourceSchemaName as FIELD_SELECTOR_ID)) {
if (panelGroups.includes(updatedDestinationSchemaName)) {
const newDropResult = {
...dropResult,
destination: {
droppableId: updatedDestinationSchemaName,
index: 0,
} as DraggableLocation,
};
addFieldToConfiguration({
dropResult: newDropResult,
aggProps,
aggService,
activeSchemaFields,
dispatch,
schemas,
});
}
} else if (panelGroups.includes(sourceSchemaName)) {
if (panelGroups.includes(updatedDestinationSchemaName)) {
const newDropResult = {
...dropResult,
destination: {
droppableId: updatedDestinationSchemaName,
index: 0,
} as DraggableLocation,
};
moveFieldBetweenSchemas({
dropResult: newDropResult,
aggProps,
aggService,
activeSchemaFields,
dispatch,
schemas,
});
}
}
return;
}

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 @@ export const DataTab = () => {
};

return (
<EuiDragDropContext onDragEnd={handleDragEnd}>
<EuiDragDropContext onDragEnd={handleDragEnd} onDragStart={() => setIsDragging(true)}>
<div className="vbDataTab">
<FieldSelector />
<ConfigPanel
Expand All @@ -143,6 +188,7 @@ export const DataTab = () => {
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,
};
};

0 comments on commit 06da6ce

Please sign in to comment.