-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ListViewItem.tsx
235 lines (226 loc) · 9.62 KB
/
ListViewItem.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
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {Checkbox} from '@react-spectrum/checkbox';
import ChevronLeftMedium from '@spectrum-icons/ui/ChevronLeftMedium';
import ChevronRightMedium from '@spectrum-icons/ui/ChevronRightMedium';
import {classNames, ClearSlots, SlotProvider} from '@react-spectrum/utils';
import {Content} from '@react-spectrum/view';
import {CSSTransition} from 'react-transition-group';
import type {DraggableItemResult} from '@react-aria/dnd';
import {FocusRing, useFocusRing} from '@react-aria/focus';
import {Grid} from '@react-spectrum/layout';
import ListGripper from '@spectrum-icons/ui/ListGripper';
import listStyles from './styles.css';
import {ListViewContext} from './ListView';
import {mergeProps} from '@react-aria/utils';
import React, {useContext, useRef} from 'react';
import {useButton} from '@react-aria/button';
import {useGridCell, useGridSelectionCheckbox} from '@react-aria/grid';
import {useHover, usePress} from '@react-aria/interactions';
import {useLocale} from '@react-aria/i18n';
import {useVisuallyHidden} from '@react-aria/visually-hidden';
export function ListViewItem(props) {
let {
item,
isEmphasized,
dragHooks,
hasActions
} = props;
let {state, dragState, isListDraggable, layout} = useContext(ListViewContext);
let {direction} = useLocale();
let rowRef = useRef<HTMLDivElement>();
let {
isFocusVisible: isFocusVisibleWithin,
focusProps: focusWithinProps
} = useFocusRing({within: true});
let {isFocusVisible, focusProps} = useFocusRing();
let allowsInteraction = state.selectionManager.selectionMode !== 'none' || hasActions;
let isDisabled = !allowsInteraction || state.disabledKeys.has(item.key);
let isDraggable = dragState?.isDraggable(item.key) && !isDisabled;
let {hoverProps, isHovered} = useHover({isDisabled});
let {pressProps, isPressed} = usePress({isDisabled});
// We only make use of useGridCell here to allow for keyboard navigation to the focusable children of the row.
// The actual grid cell of the ListView is inert since we don't want to ever focus it to decrease screenreader
// verbosity, so we pretend the row node is the cell for interaction purposes. useGridRow is never used since
// it would conflict with useGridCell if applied to the same node.
let {gridCellProps} = useGridCell({
node: item,
focusMode: 'cell',
isVirtualized: true,
shouldSelectOnPressUp: isListDraggable
}, state, rowRef);
delete gridCellProps['aria-colindex'];
let draggableItem: DraggableItemResult;
if (isListDraggable) {
// eslint-disable-next-line react-hooks/rules-of-hooks
draggableItem = dragHooks.useDraggableItem({key: item.key}, dragState);
}
let {checkboxProps} = useGridSelectionCheckbox({...props, key: item.key}, state);
let dragButtonRef = React.useRef();
let {buttonProps} = useButton({
...draggableItem?.dragButtonProps,
elementType: 'div'
}, dragButtonRef);
let chevron = direction === 'ltr'
? (
<ChevronRightMedium
aria-hidden="true"
UNSAFE_className={
classNames(
listStyles,
'react-spectrum-ListViewItem-parentIndicator',
{'react-spectrum-ListViewItem-parentIndicator--hasChildItems': item.props.hasChildItems}
)
} />
)
: (
<ChevronLeftMedium
aria-hidden="true"
UNSAFE_className={
classNames(
listStyles,
'react-spectrum-ListViewItem-parentIndicator',
{'react-spectrum-ListViewItem-parentIndicator--hasChildItems': item.props.hasChildItems}
)
} />
);
let showCheckbox = state.selectionManager.selectionMode !== 'none' && state.selectionManager.selectionBehavior === 'toggle';
let isSelected = state.selectionManager.isSelected(item.key);
let showDragHandle = isDraggable && isFocusVisibleWithin;
let {visuallyHiddenProps} = useVisuallyHidden();
let rowProps = {
role: 'row',
'aria-label': item.textValue,
'aria-selected': state.selectionManager.selectionMode !== 'none' ? isSelected : undefined,
'aria-rowindex': item.index + 1
};
const mergedProps = mergeProps(
gridCellProps,
rowProps,
pressProps,
isDraggable && draggableItem?.dragProps,
hoverProps,
focusWithinProps,
focusProps
);
let isFirstRow = item.prevKey == null;
let isLastRow = item.nextKey == null;
// Figure out if the ListView content is equal or greater in height to the container. If so, we'll need to round the bottom
// border corners of the last row when selected and we can get rid of the bottom border if it isn't selected to avoid border overlap
// with bottom border
let isFlushWithContainerBottom = false;
if (isLastRow) {
if (layout.getContentSize()?.height >= layout.virtualizer?.getVisibleRect().height) {
isFlushWithContainerBottom = true;
}
}
return (
<div
{...mergedProps}
className={
classNames(
listStyles,
'react-spectrum-ListView-row',
{
'focus-ring': isFocusVisible
}
)
}
ref={rowRef}>
<div
// TODO: refactor the css here now that we are focusing the row?
className={
classNames(
listStyles,
'react-spectrum-ListViewItem',
{
'is-active': isPressed,
'is-focused': isFocusVisibleWithin,
'focus-ring': isFocusVisible,
'is-hovered': isHovered,
'is-selected': isSelected,
'is-next-selected': state.selectionManager.isSelected(item.nextKey),
'react-spectrum-ListViewItem--highlightSelection': state.selectionManager.selectionBehavior === 'replace' && (isSelected || state.selectionManager.isSelected(item.nextKey)),
'react-spectrum-ListViewItem--draggable': isDraggable,
'react-spectrum-ListViewItem--firstRow': isFirstRow,
'react-spectrum-ListViewItem--lastRow': isLastRow,
'react-spectrum-ListViewItem--isFlushBottom': isFlushWithContainerBottom
}
)
}
role="gridcell"
aria-colindex={1}>
<Grid UNSAFE_className={listStyles['react-spectrum-ListViewItem-grid']}>
{isListDraggable &&
<div className={listStyles['react-spectrum-ListViewItem-draghandle-container']}>
{isDraggable &&
<FocusRing focusRingClass={classNames(listStyles, 'focus-ring')}>
<div
{...buttonProps as React.HTMLAttributes<HTMLElement>}
className={
classNames(
listStyles,
'react-spectrum-ListViewItem-draghandle-button'
)
}
style={!showDragHandle ? {...visuallyHiddenProps.style} : {}}
ref={dragButtonRef}
draggable="true">
<ListGripper />
</div>
</FocusRing>
}
</div>
}
<CSSTransition
in={showCheckbox}
unmountOnExit
classNames={{
enter: listStyles['react-spectrum-ListViewItem-checkbox--enter'],
enterActive: listStyles['react-spectrum-ListViewItem-checkbox--enterActive'],
exit: listStyles['react-spectrum-ListViewItem-checkbox--exit'],
exitActive: listStyles['react-spectrum-ListViewItem-checkbox--exitActive']
}}
timeout={160} >
<div className={listStyles['react-spectrum-ListViewItem-checkboxWrapper']}>
<Checkbox
{...checkboxProps}
UNSAFE_className={listStyles['react-spectrum-ListViewItem-checkbox']}
isEmphasized={isEmphasized} />
</div>
</CSSTransition>
<SlotProvider
slots={{
content: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-content']},
text: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-content']},
description: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-description']},
icon: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-icon'], size: 'M'},
image: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-image']},
link: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-content'], isQuiet: true},
actionButton: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-actions'], isQuiet: true},
actionGroup: {
UNSAFE_className: listStyles['react-spectrum-ListViewItem-actions'],
isQuiet: true,
density: 'compact'
},
actionMenu: {UNSAFE_className: listStyles['react-spectrum-ListViewItem-actionmenu'], isQuiet: true}
}}>
{typeof item.rendered === 'string' ? <Content>{item.rendered}</Content> : item.rendered}
<ClearSlots>
{chevron}
</ClearSlots>
</SlotProvider>
</Grid>
</div>
</div>
);
}