-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
control_group_component.tsx
178 lines (163 loc) · 5.85 KB
/
control_group_component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import '../control_group.scss';
import {
arrayMove,
SortableContext,
rectSortingStrategy,
sortableKeyboardCoordinates,
} from '@dnd-kit/sortable';
import {
closestCenter,
DndContext,
DragEndEvent,
DragOverlay,
KeyboardSensor,
PointerSensor,
useSensor,
useSensors,
LayoutMeasuringStrategy,
} from '@dnd-kit/core';
import classNames from 'classnames';
import React, { useMemo, useState } from 'react';
import { TypedUseSelectorHook, useSelector } from 'react-redux';
import { EuiButtonIcon, EuiFlexGroup, EuiFlexItem, EuiPanel } from '@elastic/eui';
import { ViewMode } from '@kbn/embeddable-plugin/public';
import { ControlGroupReduxState } from '../types';
import { ControlGroupStrings } from '../control_group_strings';
import { ControlClone, SortableControl } from './control_group_sortable_item';
import { useControlGroupContainer } from '../embeddable/control_group_container';
const contextSelect = useSelector as TypedUseSelectorHook<ControlGroupReduxState>;
export const ControlGroup = () => {
const controlGroup = useControlGroupContainer();
// current state
const panels = contextSelect((state) => state.explicitInput.panels);
const viewMode = contextSelect((state) => state.explicitInput.viewMode);
const controlStyle = contextSelect((state) => state.explicitInput.controlStyle);
const showAddButton = contextSelect((state) => state.componentState.showAddButton);
const isEditable = viewMode === ViewMode.EDIT;
const idsInOrder = useMemo(
() =>
Object.values(panels)
.sort((a, b) => (a.order > b.order ? 1 : -1))
.reduce((acc, panel) => {
acc.push(panel.explicitInput.id);
return acc;
}, [] as string[]),
[panels]
);
const [draggingId, setDraggingId] = useState<string | null>(null);
const draggingIndex = useMemo(
() => (draggingId ? idsInOrder.indexOf(draggingId) : -1),
[idsInOrder, draggingId]
);
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
);
const onDragEnd = ({ over }: DragEndEvent) => {
if (over) {
const overIndex = idsInOrder.indexOf(over.id);
if (draggingIndex !== overIndex) {
const newIndex = overIndex;
controlGroup.dispatch.setControlOrders({
ids: arrayMove([...idsInOrder], draggingIndex, newIndex),
});
}
}
(document.activeElement as HTMLElement)?.blur();
setDraggingId(null);
};
const emptyState = !(idsInOrder && idsInOrder.length > 0);
// Empty, non-editable view is null
if (!isEditable && emptyState) {
return null;
}
let panelBg: 'transparent' | 'plain' | 'success' = 'transparent';
if (emptyState) panelBg = 'plain';
if (draggingId) panelBg = 'success';
return (
<>
{idsInOrder.length > 0 || showAddButton ? (
<EuiPanel
borderRadius="m"
color={panelBg}
paddingSize={emptyState ? 's' : 'none'}
data-test-subj="controls-group-wrapper"
className={classNames('controlsWrapper', {
'controlsWrapper--empty': emptyState,
'controlsWrapper--twoLine': controlStyle === 'twoLine',
})}
>
<EuiFlexGroup
wrap={false}
gutterSize="m"
direction="row"
responsive={false}
alignItems="center"
data-test-subj="controls-group"
>
<EuiFlexItem>
<DndContext
onDragStart={({ active }) => setDraggingId(active.id)}
onDragEnd={onDragEnd}
onDragCancel={() => setDraggingId(null)}
sensors={sensors}
collisionDetection={closestCenter}
layoutMeasuring={{
strategy: LayoutMeasuringStrategy.Always,
}}
>
<SortableContext items={idsInOrder} strategy={rectSortingStrategy}>
<EuiFlexGroup
className={classNames('controlGroup', {
'controlGroup-isDragging': draggingId,
})}
alignItems="center"
gutterSize="s"
wrap={true}
>
{idsInOrder.map(
(controlId, index) =>
panels[controlId] && (
<SortableControl
isEditable={isEditable}
dragInfo={{ index, draggingIndex }}
embeddableId={controlId}
embeddableType={panels[controlId].type}
key={controlId}
/>
)
)}
</EuiFlexGroup>
</SortableContext>
<DragOverlay>
{draggingId ? <ControlClone draggingId={draggingId} /> : null}
</DragOverlay>
</DndContext>
</EuiFlexItem>
{showAddButton && (
<EuiFlexItem grow={false}>
<EuiButtonIcon
size="s"
iconSize="m"
display="base"
iconType={'plusInCircle'}
aria-label={ControlGroupStrings.management.getAddControlTitle()}
onClick={() => controlGroup.openAddDataControlFlyout()}
/>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiPanel>
) : (
<></>
)}
</>
);
};