mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 18:05:11 +03:00
Stack 3/4 · previous: #50687 · next: #50689 ## Problem `DataTableInfinite` could only open one row at a time. Multi-select meant clicking a separate checkbox column, with no support for ranges or modifier keys. ## Solution This PR only adds shared primitives. Behavior stays the same until a consumer passes `onSelectRow` (wired up in the next PR). - `selectTableRow` (`rowSelection.utils.ts`): a pure reducer for plain, Cmd/Ctrl-toggle, Shift-range, and additive-range selection over the current display order, with a fixed anchor. - `useTableRowSelection`: holds the selection state and resets it when its `scope` (project/filters) changes. - `DataTableProvider` gets an optional `onSelectRow`. When it's set, `DataTableInfinite` rows use it and reflect `row.getIsSelected()`. Rows also get `aria-selected`, Space activates them like Enter, and shift-click no longer selects text. ## Review instructions 1. Read `rowSelection.utils.test.ts`: it covers the selection rules. 2. Open Unified Logs. Row clicks should behave the same as on master. ## Checklist - [ ] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added richer data-table row selection, including multi-select, range selection, toggling, and keyboard support. - Added visual and accessibility feedback for selected rows, including pointer cursor and `aria-selected`. - Preserved selections across paging and live updates while resetting them when filters or projects change. - Added support for external row-selection callbacks. - **Tests** - Added comprehensive coverage for selection behavior and state persistence. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import type {
|
|
ColumnDef,
|
|
ColumnFiltersState,
|
|
PaginationState,
|
|
RowSelectionState,
|
|
SortingState,
|
|
Table,
|
|
VisibilityState,
|
|
} from '@tanstack/react-table'
|
|
import { createContext, ReactNode, useContext, useMemo } from 'react'
|
|
|
|
import { DataTableFilterField } from '../DataTable.types'
|
|
import { RowSelectionModifiers } from '../rowSelection.utils'
|
|
import { ResponseError } from '@/types'
|
|
|
|
// REMINDER: read about how to move controlled state out of the useReactTable hook
|
|
// https://github.com/TanStack/table/discussions/4005#discussioncomment-7303569
|
|
|
|
interface DataTableStateContextType<TSearchParams = unknown> {
|
|
columnFilters: ColumnFiltersState
|
|
sorting: SortingState
|
|
rowSelection: RowSelectionState
|
|
columnOrder: string[]
|
|
columnVisibility: VisibilityState
|
|
pagination: PaginationState
|
|
enableColumnOrdering: boolean
|
|
searchParameters: TSearchParams
|
|
openRowId: string | undefined
|
|
setOpenRowId: (id: string | undefined) => void
|
|
onSelectRow?: (id: string, modifiers?: RowSelectionModifiers) => void
|
|
}
|
|
|
|
interface DataTableBaseContextType<TData = unknown, TValue = unknown> {
|
|
table: Table<TData>
|
|
error: ResponseError | null
|
|
filterFields: DataTableFilterField<TData>[]
|
|
columns: ColumnDef<TData, TValue>[]
|
|
isFetching: boolean
|
|
isError: boolean
|
|
isLoading: boolean
|
|
isLoadingCounts: boolean
|
|
getFacetedUniqueValues?: (table: Table<TData>, columnId: string) => Map<string, number>
|
|
getFacetedMinMaxValues?: (table: Table<TData>, columnId: string) => undefined | [number, number]
|
|
}
|
|
|
|
interface DataTableContextType<TData = unknown, TValue = unknown, TSearchParams = unknown>
|
|
extends DataTableStateContextType<TSearchParams>, DataTableBaseContextType<TData, TValue> {}
|
|
|
|
export const DataTableContext = createContext<DataTableContextType<any, any, any> | null>(null)
|
|
|
|
export function DataTableProvider<TData, TValue, TSearchParams = unknown>({
|
|
children,
|
|
...props
|
|
}: Partial<DataTableStateContextType<TSearchParams>> &
|
|
DataTableBaseContextType<TData, TValue> & {
|
|
children: ReactNode
|
|
}) {
|
|
const value = useMemo(
|
|
() => ({
|
|
...props,
|
|
columnFilters: props.columnFilters ?? [],
|
|
sorting: props.sorting ?? [],
|
|
rowSelection: props.rowSelection ?? {},
|
|
columnOrder: props.columnOrder ?? [],
|
|
columnVisibility: props.columnVisibility ?? {},
|
|
pagination: props.pagination ?? { pageIndex: 0, pageSize: 10 },
|
|
enableColumnOrdering: props.enableColumnOrdering ?? false,
|
|
searchParameters: props.searchParameters ?? ({} as any),
|
|
openRowId: props.openRowId,
|
|
setOpenRowId: props.setOpenRowId ?? (() => {}),
|
|
}),
|
|
[props]
|
|
)
|
|
|
|
return <DataTableContext.Provider value={value}>{children}</DataTableContext.Provider>
|
|
}
|
|
|
|
export function useDataTable<TData, TValue, TSearchParams = unknown>() {
|
|
const context = useContext(DataTableContext)
|
|
|
|
if (!context) {
|
|
throw new Error('useDataTable must be used within a DataTableProvider')
|
|
}
|
|
|
|
return context as DataTableContextType<TData, TValue, TSearchParams>
|
|
}
|