This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
forked from frontend-collective/react-sortable-tree
-
Notifications
You must be signed in to change notification settings - Fork 54
/
node-renderer-default.tsx
231 lines (216 loc) · 6.66 KB
/
node-renderer-default.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
import React from 'react'
import { ConnectDragPreview, ConnectDragSource } from 'react-dnd'
import { classnames } from './utils/classnames'
import { isDescendant } from './utils/tree-data-utils'
import './node-renderer-default.css'
import { NodeData, TreeItem } from '.'
const defaultProps = {
isSearchMatch: false,
isSearchFocus: false,
canDrag: false,
toggleChildrenVisibility: undefined,
buttons: [],
className: '',
style: {},
parentNode: undefined,
draggedNode: undefined,
canDrop: false,
title: undefined,
subtitle: undefined,
rowDirection: 'ltr',
}
export interface NodeRendererProps {
node: TreeItem
path: number[]
treeIndex: number
isSearchMatch: boolean
isSearchFocus: boolean
canDrag: boolean
scaffoldBlockPxWidth: number
toggleChildrenVisibility?(data: NodeData): void | undefined
buttons?: JSX.Element[] | undefined
className?: string | undefined
style?: React.CSSProperties | undefined
title?: ((data: NodeData) => JSX.Element | JSX.Element) | undefined
subtitle?: ((data: NodeData) => JSX.Element | JSX.Element) | undefined
icons?: JSX.Element[] | undefined
lowerSiblingCounts: number[]
swapDepth?: number | undefined
swapFrom?: number | undefined
swapLength?: number | undefined
listIndex: number
treeId: string
rowDirection?: 'ltr' | 'rtl' | string | undefined
connectDragPreview: ConnectDragPreview
connectDragSource: ConnectDragSource
parentNode?: TreeItem | undefined
startDrag: ({ path }: { path: number[] }) => void
endDrag: (dropResult: unknown) => void
isDragging: boolean
didDrop: boolean
draggedNode?: TreeItem | undefined
isOver: boolean
canDrop?: boolean | undefined
}
const NodeRendererDefault: React.FC<NodeRendererProps> = (props) => {
props = { ...defaultProps, ...props }
const {
scaffoldBlockPxWidth,
toggleChildrenVisibility,
connectDragPreview,
connectDragSource,
isDragging,
canDrop,
canDrag,
node,
title,
subtitle,
draggedNode,
path,
treeIndex,
isSearchMatch,
isSearchFocus,
buttons,
className,
style,
didDrop,
treeId: _treeId,
isOver: _isOver, // Not needed, but preserved for other renderers
parentNode: _parentNode, // Needed for dndManager
rowDirection,
...otherProps
} = props
const nodeTitle = title || node.title
const nodeSubtitle = subtitle || node.subtitle
const rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : undefined
let handle
if (canDrag) {
handle =
typeof node.children === 'function' && node.expanded ? (
<div className="rst__loadingHandle">
<div className="rst__loadingCircle">
{Array.from({ length: 12 }).map((_, index) => (
<div
key={index}
className={classnames(
'rst__loadingCirclePoint',
rowDirectionClass ?? ''
)}
/>
))}
</div>
</div>
) : (
connectDragSource(<div className="rst__moveHandle" />, {
dropEffect: 'copy',
})
)
}
const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node)
const isLandingPadActive = !didDrop && isDragging
let buttonStyle = { left: -0.5 * scaffoldBlockPxWidth, right: 0 }
if (rowDirection === 'rtl') {
buttonStyle = { right: -0.5 * scaffoldBlockPxWidth, left: 0 }
}
return (
<div style={{ height: '100%' }} {...otherProps}>
{toggleChildrenVisibility &&
node.children &&
(node.children.length > 0 || typeof node.children === 'function') && (
<div>
<button
type="button"
aria-label={node.expanded ? 'Collapse' : 'Expand'}
className={classnames(
node.expanded ? 'rst__collapseButton' : 'rst__expandButton',
rowDirectionClass ?? ''
)}
style={buttonStyle}
onClick={() =>
toggleChildrenVisibility({
node,
path,
treeIndex,
})
}
/>
{node.expanded && !isDragging && (
<div
style={{ width: scaffoldBlockPxWidth }}
className={classnames(
'rst__lineChildren',
rowDirectionClass ?? ''
)}
/>
)}
</div>
)}
<div className={classnames('rst__rowWrapper', rowDirectionClass ?? '')}>
{/* Set the row preview to be used during drag and drop */}
{connectDragPreview(
<div
className={classnames(
'rst__row',
isLandingPadActive ? 'rst__rowLandingPad' : '',
isLandingPadActive && !canDrop ? 'rst__rowCancelPad' : '',
isSearchMatch ? 'rst__rowSearchMatch' : '',
isSearchFocus ? 'rst__rowSearchFocus' : '',
rowDirectionClass ?? '',
className ?? ''
)}
style={{
opacity: isDraggedDescendant ? 0.5 : 1,
...style,
}}>
{handle}
<div
className={classnames(
'rst__rowContents',
canDrag ? '' : 'rst__rowContentsDragDisabled',
rowDirectionClass ?? ''
)}>
<div
className={classnames(
'rst__rowLabel',
rowDirectionClass ?? ''
)}>
<span
className={classnames(
'rst__rowTitle',
node.subtitle ? 'rst__rowTitleWithSubtitle' : ''
)}>
{typeof nodeTitle === 'function'
? nodeTitle({
node,
path,
treeIndex,
})
: nodeTitle}
</span>
{nodeSubtitle && (
<span className="rst__rowSubtitle">
{typeof nodeSubtitle === 'function'
? nodeSubtitle({
node,
path,
treeIndex,
})
: nodeSubtitle}
</span>
)}
</div>
<div className="rst__rowToolbar">
{buttons?.map((btn, index) => (
<div key={index} className="rst__toolbarButton">
{btn}
</div>
))}
</div>
</div>
</div>
)}
</div>
</div>
)
}
export default NodeRendererDefault