forked from hello-pangea/dnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.tsx
239 lines (205 loc) · 5.74 KB
/
task.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, {
Component,
ReactElement,
MouseEvent,
TouchEvent,
KeyboardEvent,
} from 'react';
import styled from '@emotion/styled';
import { colors } from '@atlaskit/theme';
import { Draggable } from '@hello-pangea/dnd';
import type {
DraggableProvided,
DraggableStateSnapshot,
} from '@hello-pangea/dnd';
import { grid, borderRadius } from '../constants';
import type { Id, Task as TaskType } from '../types';
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const primaryButton = 0;
interface Props {
task: TaskType;
index: number;
isSelected: boolean;
isGhosting: boolean;
selectionCount: number;
toggleSelection: (taskId: Id) => void;
toggleSelectionInGroup: (taskId: Id) => void;
multiSelectTo: (taskId: Id) => void;
}
interface GetBackgroundColorArgs {
isSelected: boolean;
isDragging: boolean;
isGhosting: boolean;
}
const getBackgroundColor = ({
isSelected,
isGhosting,
}: GetBackgroundColorArgs): string => {
if (isGhosting) {
return colors.N10;
}
if (isSelected) {
return colors.B50;
}
return colors.N10;
};
interface ContainerProps {
isDragging: boolean;
isSelected: boolean;
isGhosting: boolean;
}
const getColor = ({ isSelected, isGhosting }: ContainerProps): string => {
if (isGhosting) {
return 'darkgrey';
}
if (isSelected) {
return colors.B200;
}
return colors.N900;
};
const Container = styled.div<ContainerProps>`
background-color: ${(props) => getBackgroundColor(props)};
color: ${(props) => getColor(props)};
padding: ${grid}px;
margin-bottom: ${grid}px;
border-radius: ${borderRadius}px;
font-size: 18px;
border: 3px solid ${colors.N90};
${(props) =>
props.isDragging ? `box-shadow: 2px 2px 1px ${colors.N90};` : ''} ${(
props,
) => (props.isGhosting ? 'opacity: 0.8;' : '')}
/* needed for SelectionCount */
position: relative;
/* avoid default outline which looks lame with the position: absolute; */
&:focus {
outline: none;
border-color: ${colors.G200};
}
`;
const Content = styled.div``;
const size = 30;
const SelectionCount = styled.div`
right: -${grid}px;
top: -${grid}px;
color: ${colors.N0};
background: ${colors.N200};
border-radius: 50%;
height: ${size}px;
width: ${size}px;
line-height: ${size}px;
position: absolute;
text-align: center;
font-size: 0.8rem;
`;
const keyCodes = {
enter: 13,
escape: 27,
arrowDown: 40,
arrowUp: 38,
tab: 9,
};
export default class Task extends Component<Props> {
onKeyDown = (
event: KeyboardEvent<HTMLDivElement>,
provided: DraggableProvided,
snapshot: DraggableStateSnapshot,
): void => {
if (event.defaultPrevented) {
return;
}
if (snapshot.isDragging) {
return;
}
if (event.keyCode !== keyCodes.enter) {
return;
}
// we are using the event for selection
event.preventDefault();
this.performAction(event);
};
// Using onClick as it will be correctly
// preventing if there was a drag
onClick = (event: MouseEvent<HTMLDivElement>): void => {
if (event.defaultPrevented) {
return;
}
if (event.button !== primaryButton) {
return;
}
// marking the event as used
event.preventDefault();
this.performAction(event);
};
onTouchEnd = (event: TouchEvent<HTMLDivElement>): void => {
if (event.defaultPrevented) {
return;
}
// marking the event as used
// we would also need to add some extra logic to prevent the click
// if this element was an anchor
event.preventDefault();
this.props.toggleSelectionInGroup(this.props.task.id);
};
// Determines if the platform specific toggle selection in group key was used
wasToggleInSelectionGroupKeyUsed = (
event: MouseEvent | KeyboardEvent,
): boolean => {
const isUsingWindows = navigator.platform.indexOf('Win') >= 0;
return isUsingWindows ? event.ctrlKey : event.metaKey;
};
// Determines if the multiSelect key was used
wasMultiSelectKeyUsed = (event: MouseEvent | KeyboardEvent): boolean =>
event.shiftKey;
performAction = (event: MouseEvent | KeyboardEvent): void => {
const { task, toggleSelection, toggleSelectionInGroup, multiSelectTo } =
this.props;
if (this.wasToggleInSelectionGroupKeyUsed(event)) {
toggleSelectionInGroup(task.id);
return;
}
if (this.wasMultiSelectKeyUsed(event)) {
multiSelectTo(task.id);
return;
}
toggleSelection(task.id);
};
render(): ReactElement {
const task: TaskType = this.props.task;
const index: number = this.props.index;
const isSelected: boolean = this.props.isSelected;
const selectionCount: number = this.props.selectionCount;
const isGhosting: boolean = this.props.isGhosting;
return (
<Draggable draggableId={task.id} index={index}>
{(
provided: DraggableProvided,
snapshot: DraggableStateSnapshot,
): ReactElement => {
const shouldShowSelection: boolean =
snapshot.isDragging && selectionCount > 1;
return (
<Container
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
onClick={this.onClick}
onTouchEnd={this.onTouchEnd}
onKeyDown={(event: KeyboardEvent<HTMLDivElement>) =>
this.onKeyDown(event, provided, snapshot)
}
isDragging={snapshot.isDragging}
isSelected={isSelected}
isGhosting={isGhosting}
>
<Content>{task.content}</Content>
{shouldShowSelection ? (
<SelectionCount>{selectionCount}</SelectionCount>
) : null}
</Container>
);
}}
</Draggable>
);
}
}