-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathdroppable.jsx
184 lines (167 loc) · 4.96 KB
/
droppable.jsx
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
// @flow
import ReactDOM from 'react-dom';
import { useMemo, useCallback } from 'use-memo-one';
import React, { useRef, useContext, type Node } from 'react';
import { invariant } from '../../invariant';
import type { DraggableId } from '../../types';
import type { DefaultProps, Props, Provided } from './droppable-types';
import useDroppablePublisher from '../use-droppable-publisher';
import Placeholder from '../placeholder';
import AppContext, { type AppContextValue } from '../context/app-context';
import DroppableContext, {
type DroppableContextValue,
} from '../context/droppable-context';
// import useAnimateInOut from '../use-animate-in-out/use-animate-in-out';
import getMaxWindowScroll from '../window/get-max-window-scroll';
import useValidation from './use-validation';
import type {
StateSnapshot as DraggableStateSnapshot,
Provided as DraggableProvided,
} from '../draggable/draggable-types';
import AnimateInOut, {
type AnimateProvided,
} from '../animate-in-out/animate-in-out';
import { PrivateDraggable } from '../draggable/draggable-api';
function getBody(): HTMLElement {
invariant(document.body, 'document.body is not ready');
return document.body;
}
export const defaultProps: DefaultProps = {
mode: 'standard',
type: 'DEFAULT',
direction: 'vertical',
isDropDisabled: false,
isCombineEnabled: false,
ignoreContainerClipping: false,
renderClone: null,
getContainerForClone: getBody,
};
export default function Droppable(passedProps: Props) {
const props = { ...defaultProps, ...passedProps };
const appContext: ?AppContextValue = useContext<?AppContextValue>(AppContext);
invariant(appContext, 'Could not find app context');
const { contextId, isMovementAllowed } = appContext;
const droppableRef = useRef<?HTMLElement>(null);
const placeholderRef = useRef<?HTMLElement>(null);
const {
// own props
children,
droppableId,
type,
mode,
direction,
ignoreContainerClipping,
isDropDisabled,
isCombineEnabled,
// map props
snapshot,
useClone,
// dispatch props
updateViewportMaxScroll,
// clone (ownProps)
getContainerForClone,
} = props;
const getDroppableRef = useCallback(
(): ?HTMLElement => droppableRef.current,
[],
);
const setDroppableRef = useCallback((value: ?HTMLElement) => {
droppableRef.current = value;
}, []);
const getPlaceholderRef = useCallback(
(): ?HTMLElement => placeholderRef.current,
[],
);
const setPlaceholderRef = useCallback((value: ?HTMLElement) => {
placeholderRef.current = value;
}, []);
useValidation({
props,
getDroppableRef,
getPlaceholderRef,
});
const onPlaceholderTransitionEnd = useCallback(() => {
// A placeholder change can impact the window's max scroll
if (isMovementAllowed()) {
updateViewportMaxScroll({ maxScroll: getMaxWindowScroll() });
}
}, [isMovementAllowed, updateViewportMaxScroll]);
useDroppablePublisher({
droppableId,
type,
mode,
direction,
isDropDisabled,
isCombineEnabled,
ignoreContainerClipping,
getDroppableRef,
});
const placeholder: Node = (
<AnimateInOut
on={props.placeholder}
shouldAnimate={props.shouldAnimatePlaceholder}
>
{({ onClose, data, animate }: AnimateProvided) => (
<Placeholder
placeholder={(data: any)}
onClose={onClose}
innerRef={setPlaceholderRef}
animate={animate}
contextId={contextId}
onTransitionEnd={onPlaceholderTransitionEnd}
/>
)}
</AnimateInOut>
);
const provided: Provided = useMemo(
(): Provided => ({
innerRef: setDroppableRef,
placeholder,
droppableProps: {
'data-rbd-droppable-id': droppableId,
'data-rbd-droppable-context-id': contextId,
},
}),
[contextId, droppableId, placeholder, setDroppableRef],
);
const isUsingCloneFor: ?DraggableId = useClone
? useClone.dragging.draggableId
: null;
const droppableContext: ?DroppableContextValue = useMemo(
() => ({
droppableId,
type,
isUsingCloneFor,
}),
[droppableId, isUsingCloneFor, type],
);
function getClone(): ?Node {
if (!useClone) {
return null;
}
const { dragging, render } = useClone;
const node: Node = (
<PrivateDraggable
draggableId={dragging.draggableId}
index={dragging.source.index}
isClone
isEnabled
// not important as drag has already started
shouldRespectForcePress={false}
canDragInteractiveElements
>
{(
draggableProvided: DraggableProvided,
draggableSnapshot: DraggableStateSnapshot,
) => render(draggableProvided, draggableSnapshot, dragging)}
</PrivateDraggable>
);
return ReactDOM.createPortal(node, getContainerForClone());
}
return (
<DroppableContext.Provider value={droppableContext}>
{children(provided, snapshot)}
{getClone()}
</DroppableContext.Provider>
);
}