|
1 | | -import {useEffect, useId, useRef, type ReactElement, type ReactNode, type RefObject} from 'react' |
| 1 | +import {Children, useEffect, useId, useRef, type ReactElement, type ReactNode, type RefObject} from 'react' |
2 | 2 | import type {Column} from './column' |
3 | 3 | import {useTable} from './useTable' |
4 | 4 | import type {SortDirection} from './sorting' |
@@ -237,6 +237,14 @@ function DataTableImplementation<Data extends UniqueRow>({ |
237 | 237 | const {headers, bodies, actions, selection, gridTemplateColumns} = table |
238 | 238 | const hasBulkActions = Boolean(rowSelection && renderBulkActions) |
239 | 239 | const bulkActionsActive = hasBulkActions && selection.selectedCount > 0 |
| 240 | + const renderedBulkActions = bulkActionsActive |
| 241 | + ? renderBulkActions?.({ |
| 242 | + selectedRows: new Set(selection.selectedRows), |
| 243 | + selectedCount: selection.selectedCount, |
| 244 | + clearSelection: actions.clearSelection, |
| 245 | + }) |
| 246 | + : null |
| 247 | + const bulkActionsAvailable = Children.toArray(renderedBulkActions).length > 0 |
240 | 248 |
|
241 | 249 | const renderRow = (row: (typeof bodies)[number]['rows'][number]) => ( |
242 | 250 | <TableRow key={`${typeof row.selectionId}:${row.selectionId}`}> |
@@ -269,35 +277,56 @@ function DataTableImplementation<Data extends UniqueRow>({ |
269 | 277 | const selectionAnnouncement = rowSelection |
270 | 278 | ? getSelectionAnnouncement({ |
271 | 279 | selectedCount: selection.selectedCount, |
272 | | - bulkActionsAvailable: hasBulkActions, |
| 280 | + bulkActionsAvailable, |
273 | 281 | }) |
274 | 282 | : null |
275 | 283 | const selectedCountForAnnouncement = rowSelection ? selection.selectedCount : null |
276 | 284 | const previousSelectedCount = useRef(selectedCountForAnnouncement) |
| 285 | + const previousSelectionAnnouncement = useRef<string | null>(null) |
| 286 | + const pendingSelectionAnnouncement = useRef<ReturnType<typeof announce> | null>(null) |
277 | 287 | useEffect(() => { |
278 | 288 | const previous = previousSelectedCount.current |
279 | 289 | previousSelectedCount.current = selectedCountForAnnouncement |
280 | | - if ( |
281 | | - previous === null || |
282 | | - selectedCountForAnnouncement === null || |
283 | | - previous === selectedCountForAnnouncement || |
284 | | - selectionAnnouncement === null |
285 | | - ) { |
| 290 | + // eslint-disable-next-line react-you-might-not-need-an-effect/no-event-handler |
| 291 | + if (selectedCountForAnnouncement === null) { |
| 292 | + pendingSelectionAnnouncement.current?.cancel() |
| 293 | + pendingSelectionAnnouncement.current = null |
| 294 | + previousSelectionAnnouncement.current = null |
| 295 | + return |
| 296 | + } |
| 297 | + // eslint-disable-next-line react-you-might-not-need-an-effect/no-event-handler |
| 298 | + if (previous === null || previous === selectedCountForAnnouncement || selectionAnnouncement === null) { |
286 | 299 | return |
287 | 300 | } |
288 | 301 |
|
289 | 302 | const table = tableRef.current |
290 | 303 | if (!table || !isVisible(table)) { |
| 304 | + pendingSelectionAnnouncement.current?.cancel() |
| 305 | + pendingSelectionAnnouncement.current = null |
| 306 | + previousSelectionAnnouncement.current = null |
291 | 307 | return |
292 | 308 | } |
293 | 309 |
|
294 | | - const announcement = announce(selectionAnnouncement, { |
| 310 | + // eslint-disable-next-line react-you-might-not-need-an-effect/no-event-handler |
| 311 | + if (previousSelectionAnnouncement.current === selectionAnnouncement) { |
| 312 | + return |
| 313 | + } |
| 314 | + |
| 315 | + pendingSelectionAnnouncement.current?.cancel() |
| 316 | + previousSelectionAnnouncement.current = selectionAnnouncement |
| 317 | + |
| 318 | + pendingSelectionAnnouncement.current = announce(selectionAnnouncement, { |
295 | 319 | politeness: 'polite', |
296 | 320 | delayMs: 150, |
297 | 321 | from: table, |
298 | 322 | }) |
299 | | - return () => announcement.cancel() |
300 | 323 | }, [selectedCountForAnnouncement, selectionAnnouncement]) |
| 324 | + useEffect( |
| 325 | + () => () => { |
| 326 | + pendingSelectionAnnouncement.current?.cancel() |
| 327 | + }, |
| 328 | + [], |
| 329 | + ) |
301 | 330 |
|
302 | 331 | return ( |
303 | 332 | <Table |
@@ -405,13 +434,7 @@ function DataTableImplementation<Data extends UniqueRow>({ |
405 | 434 | } |
406 | 435 | }} |
407 | 436 | > |
408 | | - {bulkActionsActive |
409 | | - ? renderBulkActions?.({ |
410 | | - selectedRows: new Set(selection.selectedRows), |
411 | | - selectedCount: selection.selectedCount, |
412 | | - clearSelection: actions.clearSelection, |
413 | | - }) |
414 | | - : null} |
| 437 | + {renderedBulkActions} |
415 | 438 | </TableBulkActions> |
416 | 439 | </TableSelectionAnnouncementContext.Provider> |
417 | 440 | </VisuallyHidden> |
|
0 commit comments