You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -251,27 +259,32 @@ Now that we've covered the table header, let's move on to the body. We'll use
251
259
the <TypeLinklinks={docs.links}type={docs.exports.useTableRow} /> hook to render each row in the table.
252
260
Table rows can be focused and navigated to using the keyboard via the arrow keys. In addition, table rows
253
261
can optionally support selection via mouse, touch, or keyboard. Clicking, tapping, or pressing the <kbd>Space</kbd>
254
-
key anywhere in the row selects it.
262
+
key anywhere in the row selects it. Row actions are also supported via the provided `onAction` prop. See [below](#row-and-cell-actions) for details.
255
263
256
264
We'll use the <TypeLinklinks={selectionDocs.links}type={selectionDocs.exports.SelectionManager} /> object exposed
257
265
by the `state` to determine if a row is selected, and render a pink background if so. We'll also use the <TypeLinklinks={focusDocs.links}type={focusDocs.exports.useFocusRing} />
258
266
hook to render a focus ring when the user navigates to the row with the keyboard.
259
267
260
268
```tsx example export=true render=false
261
-
function TableRow({item, children, state}) {
269
+
function TableRow({item, children, state, onAction}) {
262
270
let ref =useRef();
263
271
let isSelected =state.selectionManager.isSelected(item.key);
264
-
let {rowProps} =useTableRow({node: item}, state, ref);
272
+
let {rowProps, isPressed} =useTableRow({
273
+
node: item,
274
+
onAction: onAction&& (() =>onAction(item.key))
275
+
}, state, ref);
265
276
let {isFocusVisible, focusProps} =useFocusRing();
266
277
267
278
return (
268
279
<tr
269
280
style={{
270
281
background: isSelected
271
282
?'blueviolet'
272
-
:item.index%2
273
-
?'var(--spectrum-alias-highlight-hover)'
274
-
:'none',
283
+
:isPressed
284
+
?'var(--spectrum-global-color-gray-400)'
285
+
:item.index%2
286
+
?'var(--spectrum-alias-highlight-hover)'
287
+
:'none',
275
288
color: isSelected?'white':null,
276
289
outline: isFocusVisible?'2px solid orange':'none'
277
290
}}
@@ -584,6 +597,26 @@ Note that you are responsible for the styling of disabled rows, however, the sel
By default, `useTable` uses the `"toggle"` selection behavior, which behaves like a checkbox group: clicking, tapping, or pressing the <kbd>Space</kbd> or <kbd>Enter</kbd> keys toggles selection for the focused row. Using the arrow keys moves focus but does not change selection. The `"toggle"` selection mode is often paired with a column of checkboxes in each row as an explicit affordance for selection.
603
+
604
+
When the `selectionBehavior` prop is set to `"replace"`, clicking a row with the mouse _replaces_ the selection with only that row. Using the arrow keys moves both focus and selection. To select multiple rows, modifier keys such as <kbd>Ctrl</kbd>, <kbd>Cmd</kbd>, and <kbd>Shift</kbd> can be used. To move focus without moving selection, the <kbd>Ctrl</kbd> key on Windows or the <kbd>Option</kbd> key on macOS can be held while pressing the arrow keys. Holding this modifier while pressing the <kbd>Space</kbd> key toggles selection for the focused row, which allows multiple selection of non-contiguous items. On touch screen devices, selection always behaves as toggle since modifier keys may not be available. This behavior emulates native platforms such as macOS and Windows, and is often used when checkboxes in each row are not desired.
`useTable` may be used in use cases where users can perform actions on rows or cells, such as navigating into items to open them or get more details. In the default `"toggle"` selection behavior, it is recommended to use a link in the first column for navigation.
613
+
614
+
In the `"replace"` selection behavior, the `onAction` prop can be used to enable row and cell actions, which differ depending on the interaction method. When provided to `useTableRow` or `useTableCell`, double clicking with a mouse or pressing the <kbd>Enter</kbd> key triggers `onAction`, while single click and the <kbd>Space</kbd> key are reserved for selection. On touch devices, `onAction` becomes the primary tap interaction, and a long press enters into selection mode, which temporarily swaps the selection behavior to `"toggle"` to perform selection (you may wish to display checkboxes when this happens). Deselecting all items exits selection mode and reverts the selection behavior back to `"replace"`. Double clicking matches the behavior of desktop platforms like macOS and Windows, and a separate selection mode matches mobile platforms like iOS and Android.
By default, TableView uses the checkbox selection style, which includes a column of checkboxes in each row for selection. When the `selectionStyle` prop is set to `"highlight"`, the checkboxes are hidden, and the selected rows are displayed with a highlighted background instead.
471
+
472
+
In addition to changing the appearance, the selection behavior also changes depending on the `selectionStyle` prop. In the default checkbox selection style, clicking, tapping, or pressing the <kbd>Space</kbd> or <kbd>Enter</kbd> keys toggles selection for the focused row. Using the arrow keys moves focus but does not change selection.
473
+
474
+
In the highlight selection style, however, clicking a row with the mouse _replaces_ the selection with only that row. Using the arrow keys moves both focus and selection. To select multiple rows, modifier keys such as <kbd>Ctrl</kbd>, <kbd>Cmd</kbd>, and <kbd>Shift</kbd> can be used. To move focus without moving selection, the <kbd>Ctrl</kbd> key on Windows or the <kbd>Option</kbd> key on macOS can be held while pressing the arrow keys. Holding this modifier while pressing the <kbd>Space</kbd> key toggles selection for the focused row, which allows multiple selection of non-contiguous items. On touch screen devices, selection always behaves as toggle since modifier keys may not be available. This behavior emulates native platforms such as macOS and Windows.
TableView may be used in use cases where users can perform actions on rows, such as navigating into items to open them or get more details. In the default checkbox selection style, it is recommended to use a [Link](Link.html) component in the first column for navigation.
483
+
484
+
In the highlight selection style, the `onAction` prop can be used to enable row actions, which differ depending on the interaction method. When provided, double clicking with a mouse or pressing the <kbd>Enter</kbd> key triggers `onAction`, while single click and the <kbd>Space</kbd> key are reserved for selection. On touch devices, `onAction` becomes the primary tap interaction, and a long press enters into selection mode, which displays checkboxes to perform selection. Deselecting all items exits selection mode and hides the checkboxes. Double clicking matches the behavior of desktop platforms like macOS and Windows, and a separate selection mode matches mobile platforms like iOS and Android.
0 commit comments