Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat(): support aggr and dnd for non-position channels

kyushod9bd16ba2554
diff --git a/packages/graphic-walker/src/fields/aestheticFields.tsx b/packages/graphic-walker/src/fields/aestheticFields.tsx
index 5ea31284..abfd54ba 100644
--- a/packages/graphic-walker/src/fields/aestheticFields.tsx
+++ b/packages/graphic-walker/src/fields/aestheticFields.tsx
@@ -14,7 +14,7 @@ const AestheticFields: React.FC = props => {
<Droppable droppableId={dkey.id} direction="horizontal">
{(provided, snapshot) => (
// <OBFieldContainer dkey={dkey} provided={provided} />
- <SingleEncodeEditor dkey={dkey} provided={provided} />
+ <SingleEncodeEditor dkey={dkey} provided={provided} snapshot={snapshot} />
)}
</Droppable>
</AestheticFieldContainer>)
diff --git a/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx b/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
index 2a310d07..f7fa0f7a 100644
--- a/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
+++ b/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
@@ -1,43 +1,76 @@
-import React from "react";
+import React, { useMemo } from "react";
import { IDraggableStateKey } from "../../interfaces";
import { observer } from "mobx-react-lite";
import { useGlobalStore } from "../../store";
import DropdownSelect from "./singleEncodeDropDown";
import { DroppableProvided } from "react-beautiful-dnd";
-import { TrashIcon } from "@heroicons/react/24/outline";
+import { ChevronUpDownIcon, TrashIcon } from "@heroicons/react/24/outline";
import { useTranslation } from "react-i18next";
+import { COUNT_FIELD_ID } from "../../constants";
+import DropdownContext from "../../components/dropdownContext";
+import { AGGREGATOR_LIST } from "../fieldsContext";
+import { Draggable, DroppableStateSnapshot } from "@kanaries/react-beautiful-dnd";
interface SingleEncodeEditorProps {
dkey: IDraggableStateKey;
provided: DroppableProvided;
+ snapshot: DroppableStateSnapshot;
}
const SingleEncodeEditor: React.FC<SingleEncodeEditorProps> = (props) => {
- const { dkey, provided } = props;
+ const { dkey, provided, snapshot } = props;
const { vizStore } = useGlobalStore();
- const { draggableFieldState } = vizStore;
+ const { draggableFieldState, visualConfig } = vizStore;
const channelItem = draggableFieldState[dkey.id][0];
- const allFieldOptions = draggableFieldState.dimensions.concat(draggableFieldState.measures).map((f) => ({
- value: f.fid,
- label: f.name,
- }));
const { t } = useTranslation();
+
+ const aggregationOptions = useMemo(() => {
+ return AGGREGATOR_LIST.map((op) => ({
+ value: op,
+ label: t(`constant.aggregator.${op}`),
+ }));
+ }, []);
+
return (
- <div className="p-1 flex items-center relative" {...provided.droppableProps} ref={provided.innerRef}>
+ <div className="p-1 select-none relative" {...provided.droppableProps} ref={provided.innerRef}>
+ <div className={`p-1.5 bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 flex item-center justify-center grow text-gray-500 dark:text-gray-400 ${snapshot.draggingFromThisWith || snapshot.isDraggingOver || !channelItem ? 'opacity-100' : 'opacity-0'} relative z-0`}>
+ {t('actions.drop_field')}
+ </div>
{channelItem && (
- <div
- onClick={() => {
- vizStore.removeField(dkey.id, 0);
+ <Draggable key={channelItem.dragId} draggableId={channelItem.dragId} index={0}>
+ {(provided, snapshot) => {
+ return (
+ <div ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps} className="flex items-stretch absolute z-10 top-0 left-0 right-0 bottom-0 m-1">
+ <div
+ onClick={() => {
+ vizStore.removeField(dkey.id, 0);
+ }}
+ className="grow-0 shrink-0 px-1.5 flex items-center justify-center bg-red-50 dark:bg-red-900 border border-red-200 dark:border-red-700 cursor-pointer"
+ >
+ <TrashIcon className="w-4" />
+ </div>
+ <div className="flex-1 flex items-center border border-gray-200 dark:border-gray-700 border-l-0 px-2 space-x-2">
+ <span className="flex-1 truncate">
+ {channelItem.name}
+ </span>
+ {channelItem.analyticType === "measure" && channelItem.fid !== COUNT_FIELD_ID && visualConfig.defaultAggregated && (
+ <DropdownContext
+ options={aggregationOptions}
+ onSelect={(value) => {
+ vizStore.setFieldAggregator(dkey.id, 0, value);
+ }}
+ >
+ <span className="bg-transparent text-gray-700 dark:text-gray-200 float-right focus:outline-none focus:border-gray-500 dark:focus:border-gray-400 flex items-center ml-2">
+ {channelItem.aggName || ""}
+ <ChevronUpDownIcon className="w-3" />
+ </span>
+ </DropdownContext>
+ )}
+ </div>
+ </div>
+ );
}}
- className="p-1.5 bg-red-50 dark:bg-red-900 shrink-0 grow-0 border border-red-200 dark:border-red-700 cursor-pointer"
- >
- <TrashIcon className="w-4" />
- </div>
- )}
- {channelItem && (
- <DropdownSelect className="shrink grow static" buttonClassName="rounded-none" options={allFieldOptions} selectedKey={channelItem.fid} onSelect={(value) => vizStore.replaceField(dkey.id, 0, value)} />
+ </Draggable>
)}
- {!channelItem && <div className="p-1.5 bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 flex item-center justify-center grow text-gray-500 dark:text-gray-400">
- {t('actions.drop_field')}</div>}
</div>
);
};
diff --git a/packages/graphic-walker/src/fields/obComponents/obPill.tsx b/packages/graphic-walker/src/fields/obComponents/obPill.tsx
index 37f47a1e..eee5d3ec 100644
--- a/packages/graphic-walker/src/fields/obComponents/obPill.tsx
+++ b/packages/graphic-walker/src/fields/obComponents/obPill.tsx
@@ -44,7 +44,7 @@ const OBPill: React.FC<PillProps> = (props) => {
vizStore.setFieldAggregator(dkey.id, fIndex, value);
}}
>
- <span className="bg-transparent text-gray-200 dark:text-gray-700 float-right focus:outline-none focus:border-gray-500 dark:focus:border-gray-400 flex items-center ml-2">
+ <span className="bg-transparent text-gray-700 float-right focus:outline-none focus:border-gray-500 dark:focus:border-gray-400 flex items-center ml-2">
{field.aggName || ""}
<ChevronUpDownIcon className="w-3" />
</span>