feat(explore): ColumnSelectControl with drag-and-drop (#13210)

* initial dnd

* shift group by items

* lint

* fix shift options

* wip

* wip

* fix shift action

* support scalar dimentions

* control rename to DndColumnSelectControl

* remove unused files

* added feature flag

* ff to False by default

* fix ut

* lint

* improve code smell

* added indicator

* replace value when column is scalcar

* scalar to isArray

* var rename

* minor fix

* update dependence

* minor fix
This commit is contained in:
Yongjie Zhao
2021-02-26 16:48:23 +08:00
committed by GitHub
parent f9fc85453e
commit 1e3130d588
18 changed files with 933 additions and 406 deletions

View File

@@ -0,0 +1,119 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, { useState } from 'react';
import { useDrop } from 'react-dnd';
import { isEmpty } from 'lodash';
import { t, useTheme } from '@superset-ui/core';
import { BaseControlConfig, ColumnMeta } from '@superset-ui/chart-controls';
import ControlHeader from 'src/explore/components/ControlHeader';
import {
AddControlLabel,
DndLabelsContainer,
HeaderContainer,
} from 'src/explore/components/OptionControls';
import {
DatasourcePanelDndItem,
DatasourcePanelDndType,
} from 'src/explore/components/DatasourcePanel/types';
import Icon from 'src/components/Icon';
import OptionWrapper from './components/OptionWrapper';
import { OptionSelector } from './utils';
interface LabelProps extends BaseControlConfig {
name: string;
value: string[] | string | null;
onChange: (value: string[] | string | null) => void;
options: { string: ColumnMeta };
}
export default function DndColumnSelectLabel(props: LabelProps) {
const theme = useTheme();
const { value, options } = props;
const optionSelector = new OptionSelector(options, value);
const [groupByOptions, setGroupByOptions] = useState<ColumnMeta[]>(
optionSelector.groupByOptions,
);
const [{ isOver, canDrop }, datasourcePanelDrop] = useDrop({
accept: DatasourcePanelDndType.COLUMN,
drop: (item: DatasourcePanelDndItem) => {
if (!optionSelector.isArray && !isEmpty(optionSelector.groupByOptions)) {
optionSelector.replace(0, item.metricOrColumnName);
} else {
optionSelector.add(item.metricOrColumnName);
}
setGroupByOptions(optionSelector.groupByOptions);
props.onChange(optionSelector.getValues());
},
canDrop: (item: DatasourcePanelDndItem) =>
!optionSelector.has(item.metricOrColumnName),
collect: monitor => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
type: monitor.getItemType(),
}),
});
function onClickClose(index: number) {
optionSelector.del(index);
setGroupByOptions(optionSelector.groupByOptions);
props.onChange(optionSelector.getValues());
}
function onShiftOptions(dragIndex: number, hoverIndex: number) {
optionSelector.swap(dragIndex, hoverIndex);
setGroupByOptions(optionSelector.groupByOptions);
props.onChange(optionSelector.getValues());
}
function renderPlaceHolder() {
return (
<AddControlLabel cancelHover>
<Icon name="plus-small" color={theme.colors.grayscale.light1} />
{t('Drop Columns')}
</AddControlLabel>
);
}
function renderOptions() {
return groupByOptions.map((column, idx) => (
<OptionWrapper
key={idx}
index={idx}
column={column}
clickClose={onClickClose}
onShiftOptions={onShiftOptions}
/>
));
}
return (
<div ref={datasourcePanelDrop}>
<HeaderContainer>
<ControlHeader {...props} />
</HeaderContainer>
<DndLabelsContainer canDrop={canDrop} isOver={isOver}>
{isEmpty(groupByOptions) ? renderPlaceHolder() : renderOptions()}
</DndLabelsContainer>
</div>
);
}

View File

@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { useTheme } from '@superset-ui/core';
import { ColumnOption } from '@superset-ui/chart-controls';
import Icon from 'src/components/Icon';
import {
CaretContainer,
CloseContainer,
OptionControlContainer,
Label,
} from 'src/explore/components/OptionControls';
import { OptionProps } from '../types';
export default function Option(props: OptionProps) {
const theme = useTheme();
return (
<OptionControlContainer data-test="option-label">
<CloseContainer
role="button"
data-test="remove-control-button"
onClick={() => props.clickClose(props.index)}
>
<Icon name="x-small" color={theme.colors.grayscale.light1} />
</CloseContainer>
<Label data-test="control-label">
<ColumnOption column={props.column} showType />
</Label>
{props.withCaret && (
<CaretContainer>
<Icon name="caret-right" color={theme.colors.grayscale.light1} />
</CaretContainer>
)}
</OptionControlContainer>
);
}

View File

@@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, { useRef } from 'react';
import {
useDrag,
useDrop,
DropTargetMonitor,
DragSourceMonitor,
} from 'react-dnd';
import { DragContainer } from 'src/explore/components/OptionControls';
import Option from './Option';
import { OptionProps, GroupByItemInterface, GroupByItemType } from '../types';
export default function OptionWrapper(props: OptionProps) {
const { index, onShiftOptions } = props;
const ref = useRef<HTMLDivElement>(null);
const item: GroupByItemInterface = {
dragIndex: index,
type: GroupByItemType,
};
const [, drag] = useDrag({
item,
collect: (monitor: DragSourceMonitor) => ({
isDragging: monitor.isDragging(),
}),
});
const [, drop] = useDrop({
accept: GroupByItemType,
hover: (item: GroupByItemInterface, monitor: DropTargetMonitor) => {
if (!ref.current) {
return;
}
const { dragIndex } = item;
const hoverIndex = index;
// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}
// Determine rectangle on screen
const hoverBoundingRect = ref.current?.getBoundingClientRect();
// Get vertical middle
const hoverMiddleY =
(hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
// Determine mouse position
const clientOffset = monitor.getClientOffset();
// Get pixels to the top
const hoverClientY = clientOffset?.y
? clientOffset?.y - hoverBoundingRect.top
: 0;
// Only perform the move when the mouse has crossed half of the items height
// When dragging downwards, only move when the cursor is below 50%
// When dragging upwards, only move when the cursor is above 50%
// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
}
// Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
}
// Time to actually perform the action
onShiftOptions(dragIndex, hoverIndex);
// eslint-disable-next-line no-param-reassign
item.dragIndex = hoverIndex;
},
});
drag(drop(ref));
return (
<DragContainer ref={ref}>
<Option {...props} />
</DragContainer>
);
}

View File

@@ -0,0 +1,19 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export { default } from './DndColumnSelectLabel';

View File

@@ -0,0 +1,34 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ColumnMeta } from '@superset-ui/chart-controls';
export interface OptionProps {
column: ColumnMeta;
index: number;
clickClose: (index: number) => void;
onShiftOptions: (dragIndex: number, hoverIndex: number) => void;
withCaret?: boolean;
}
export const GroupByItemType = 'groupByItem';
export interface GroupByItemInterface {
type: typeof GroupByItemType;
dragIndex: number;
}

View File

@@ -0,0 +1,19 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export * from './optionSelector';

View File

@@ -0,0 +1,86 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ColumnMeta } from '@superset-ui/chart-controls';
export class OptionSelector {
groupByOptions: ColumnMeta[];
options: { string: ColumnMeta };
isArray: boolean;
constructor(
options: { string: ColumnMeta },
values: string[] | string | null,
) {
this.options = options;
let groupByValues: string[];
if (Array.isArray(values)) {
groupByValues = values;
this.isArray = true;
} else {
groupByValues = values ? [values] : [];
this.isArray = false;
}
this.groupByOptions = groupByValues
.map(value => {
if (value in options) {
return options[value];
}
return null;
})
.filter(Boolean);
}
add(value: string) {
if (value in this.options) {
this.groupByOptions.push(this.options[value]);
}
}
del(idx: number) {
this.groupByOptions.splice(idx, 1);
}
replace(idx: number, value: string) {
if (this.groupByOptions[idx]) {
this.groupByOptions[idx] = this.options[value];
}
}
swap(a: number, b: number) {
[this.groupByOptions[a], this.groupByOptions[b]] = [
this.groupByOptions[b],
this.groupByOptions[a],
];
}
has(groupBy: string): boolean {
return !!this.getValues()?.includes(groupBy);
}
getValues(): string[] | string | null {
if (!this.isArray) {
return this.groupByOptions.length > 0
? this.groupByOptions[0].column_name
: null;
}
return this.groupByOptions.map(option => option.column_name);
}
}