|
| 1 | +import React, { |
| 2 | + ComponentProps, |
| 3 | + FunctionComponent, |
| 4 | + useCallback, |
| 5 | + useEffect, |
| 6 | + useMemo, |
| 7 | + useState, |
| 8 | +} from 'react'; |
| 9 | +import { |
| 10 | + Cell, |
| 11 | + ColumnDef, |
| 12 | + ExpandedState, |
| 13 | + flexRender, |
| 14 | + getCoreRowModel, |
| 15 | + getExpandedRowModel, |
| 16 | + getSortedRowModel, |
| 17 | + OnChangeFn, |
| 18 | + RowData, |
| 19 | + RowSelectionState, |
| 20 | + SortingState, |
| 21 | + TableOptions, |
| 22 | + useReactTable, |
| 23 | +} from '@tanstack/react-table'; |
| 24 | +import { ScrollArea } from '@/common/components/molecules/ScrollArea/ScrollArea'; |
| 25 | +import { DefaultCell } from '@/lib/blocks/components/TableCell/DefaultCell'; |
| 26 | +import { |
| 27 | + Table, |
| 28 | + TableBody, |
| 29 | + TableCaption, |
| 30 | + TableCell, |
| 31 | + TableHead, |
| 32 | + TableHeader, |
| 33 | + TableRow, |
| 34 | +} from '@/common/components/atoms/Table'; |
| 35 | +import { ctw } from '@/common/utils/ctw/ctw'; |
| 36 | +import { CollapsibleContent as ShadCNCollapsibleContent } from '@/common/components/molecules/Collapsible/Collapsible.Content'; |
| 37 | +import { Collapsible } from '@/common/components/molecules/Collapsible/Collapsible'; |
| 38 | +import { useSort } from '@/common/hooks/useSort/useSort'; |
| 39 | +import { isInstanceOfFunction } from '@/common/utils/is-instance-of-function/is-instance-of-function'; |
| 40 | +import { checkIsBooleanishRecord } from '@/lib/zod/utils/checkers'; |
| 41 | +import { useSelect } from '@/common/hooks/useSelect/useSelect'; |
| 42 | +import { ChevronDown } from 'lucide-react'; |
| 43 | +import { FunctionComponentWithChildren } from '@/common/types'; |
| 44 | + |
| 45 | +export interface IDataTableProps<TData, TValue = any> { |
| 46 | + data: TData[]; |
| 47 | + sortByField?: string; |
| 48 | + columns: Array<ColumnDef<TData, TValue>>; |
| 49 | + caption?: ComponentProps<typeof TableCaption>['children']; |
| 50 | + |
| 51 | + CellContentWrapper?: FunctionComponentWithChildren<{ cell: Cell<TData, TValue> }>; |
| 52 | + CollapsibleContent?: FunctionComponent<{ row: TData }>; |
| 53 | + |
| 54 | + // Component props |
| 55 | + props?: { |
| 56 | + scroll?: Partial<ComponentProps<typeof ScrollArea>>; |
| 57 | + table?: ComponentProps<typeof Table>; |
| 58 | + header?: ComponentProps<typeof TableHeader>; |
| 59 | + head?: ComponentProps<typeof TableHead>; |
| 60 | + row?: ComponentProps<typeof TableRow>; |
| 61 | + body?: ComponentProps<typeof TableBody>; |
| 62 | + cell?: ComponentProps<typeof TableCell>; |
| 63 | + caption?: ComponentProps<typeof TableCaption>; |
| 64 | + }; |
| 65 | + |
| 66 | + // react-table options |
| 67 | + options?: Omit<TableOptions<TData>, 'getCoreRowModel' | 'data' | 'columns'>; |
| 68 | +} |
| 69 | + |
| 70 | +export const DataTable = <TData extends RowData, TValue = any>({ |
| 71 | + data, |
| 72 | + props, |
| 73 | + caption, |
| 74 | + columns, |
| 75 | + CellContentWrapper, |
| 76 | + options = {}, |
| 77 | + CollapsibleContent, |
| 78 | +}: IDataTableProps<TData, TValue>) => { |
| 79 | + const [expanded, setExpanded] = useState<ExpandedState>({}); |
| 80 | + |
| 81 | + const { enableSorting = false } = options; |
| 82 | + |
| 83 | + const { onSort, sortBy, sortDir } = useSort(); |
| 84 | + const [sorting, setSorting] = useState<SortingState>([ |
| 85 | + { |
| 86 | + id: sortBy || options?.initialState?.sorting?.[0]?.id || 'id', |
| 87 | + desc: sortDir === 'desc' || options?.initialState?.sorting?.[0]?.desc || false, |
| 88 | + }, |
| 89 | + ]); |
| 90 | + |
| 91 | + const onSortingChange: OnChangeFn<SortingState> = useCallback( |
| 92 | + sortingUpdaterOrValue => { |
| 93 | + setSorting(prevSortingState => { |
| 94 | + if (!isInstanceOfFunction(sortingUpdaterOrValue)) { |
| 95 | + onSort({ |
| 96 | + sortBy: sortingUpdaterOrValue[0]?.id || sortBy, |
| 97 | + sortDir: sortingUpdaterOrValue[0]?.desc ? 'desc' : 'asc', |
| 98 | + }); |
| 99 | + |
| 100 | + return sortingUpdaterOrValue; |
| 101 | + } |
| 102 | + |
| 103 | + const newSortingState = sortingUpdaterOrValue(prevSortingState); |
| 104 | + |
| 105 | + onSort({ |
| 106 | + sortBy: newSortingState[0]?.id || sortBy, |
| 107 | + sortDir: newSortingState[0]?.desc ? 'desc' : 'asc', |
| 108 | + }); |
| 109 | + |
| 110 | + return newSortingState; |
| 111 | + }); |
| 112 | + }, |
| 113 | + [onSort, sortBy], |
| 114 | + ); |
| 115 | + |
| 116 | + const { selected: ids, onSelect } = useSelect(); |
| 117 | + const [rowSelection, setRowSelection] = useState<RowSelectionState>( |
| 118 | + checkIsBooleanishRecord(ids) ? ids : {}, |
| 119 | + ); |
| 120 | + |
| 121 | + const onRowSelectionChange: OnChangeFn<RowSelectionState> = useCallback( |
| 122 | + selectionUpdaterOrValue => { |
| 123 | + setRowSelection(prevSelectionState => { |
| 124 | + if (!isInstanceOfFunction(selectionUpdaterOrValue)) { |
| 125 | + onSelect(selectionUpdaterOrValue); |
| 126 | + |
| 127 | + return selectionUpdaterOrValue; |
| 128 | + } |
| 129 | + |
| 130 | + const newSelectionState = selectionUpdaterOrValue(prevSelectionState); |
| 131 | + |
| 132 | + onSelect(newSelectionState); |
| 133 | + |
| 134 | + return newSelectionState; |
| 135 | + }); |
| 136 | + }, |
| 137 | + [onSelect], |
| 138 | + ); |
| 139 | + |
| 140 | + useEffect(() => { |
| 141 | + if (Object.keys(ids ?? {}).length > 0) return; |
| 142 | + |
| 143 | + setRowSelection({}); |
| 144 | + }, [ids]); |
| 145 | + |
| 146 | + const state = useMemo( |
| 147 | + () => ({ |
| 148 | + rowSelection, |
| 149 | + ...(enableSorting && { |
| 150 | + sorting, |
| 151 | + }), |
| 152 | + ...(CollapsibleContent && { |
| 153 | + expanded, |
| 154 | + }), |
| 155 | + }), |
| 156 | + [CollapsibleContent, enableSorting, expanded, rowSelection, sorting], |
| 157 | + ); |
| 158 | + |
| 159 | + const table = useReactTable<TData>({ |
| 160 | + state, |
| 161 | + ...options, |
| 162 | + data: data ?? [], |
| 163 | + columns: columns ?? [], |
| 164 | + onRowSelectionChange, |
| 165 | + enableRowSelection: true, |
| 166 | + getRowId: row => row.id, |
| 167 | + getCoreRowModel: getCoreRowModel(), |
| 168 | + defaultColumn: { |
| 169 | + cell: DefaultCell, |
| 170 | + }, |
| 171 | + ...(enableSorting && { |
| 172 | + enableSorting, |
| 173 | + onSortingChange, |
| 174 | + manualSorting: true, |
| 175 | + sortDescFirst: true, |
| 176 | + enableSortingRemoval: false, |
| 177 | + getSortedRowModel: getSortedRowModel(), |
| 178 | + }), |
| 179 | + ...(CollapsibleContent |
| 180 | + ? { |
| 181 | + onExpandedChange: setExpanded, |
| 182 | + getExpandedRowModel: getExpandedRowModel(), |
| 183 | + } |
| 184 | + : {}), |
| 185 | + }); |
| 186 | + |
| 187 | + return ( |
| 188 | + <div className="d-full relative overflow-auto rounded-md border bg-white shadow"> |
| 189 | + <ScrollArea orientation="both" {...props?.scroll}> |
| 190 | + <Table {...props?.table}> |
| 191 | + {caption && ( |
| 192 | + <TableCaption |
| 193 | + {...props?.caption} |
| 194 | + className={ctw('text-foreground', props?.caption?.className)} |
| 195 | + > |
| 196 | + {caption} |
| 197 | + </TableCaption> |
| 198 | + )} |
| 199 | + <TableHeader className="border-0" {...props?.header}> |
| 200 | + {table.getHeaderGroups()?.map(({ id, headers }) => ( |
| 201 | + <TableRow |
| 202 | + key={id} |
| 203 | + {...props?.row} |
| 204 | + className={ctw('border-b-none', props?.row?.className)} |
| 205 | + > |
| 206 | + {headers?.map((header, index) => { |
| 207 | + return ( |
| 208 | + <TableHead |
| 209 | + key={header.id} |
| 210 | + {...props?.head} |
| 211 | + className={ctw( |
| 212 | + 'sticky top-0 z-10 h-[34px] bg-white p-1 text-[14px] font-bold text-[#787981]', |
| 213 | + { |
| 214 | + '!pl-3.5': index === 0, |
| 215 | + }, |
| 216 | + props?.head?.className, |
| 217 | + )} |
| 218 | + > |
| 219 | + {header.column.id === 'select' && ( |
| 220 | + <span className={'pe-4'}> |
| 221 | + {flexRender(header.column.columnDef.header, header.getContext())} |
| 222 | + </span> |
| 223 | + )} |
| 224 | + {header.column.getCanSort() && header.column.id !== 'select' && ( |
| 225 | + <button |
| 226 | + className="flex h-9 flex-row items-center gap-x-2 px-3 text-left text-[#A3A3A3]" |
| 227 | + onClick={() => header.column.toggleSorting()} |
| 228 | + > |
| 229 | + <span> |
| 230 | + {flexRender(header.column.columnDef.header, header.getContext())} |
| 231 | + </span> |
| 232 | + <ChevronDown |
| 233 | + className={ctw('d-4', { |
| 234 | + 'rotate-180': header.column.getIsSorted() === 'asc', |
| 235 | + })} |
| 236 | + /> |
| 237 | + </button> |
| 238 | + )} |
| 239 | + {!header.column.getCanSort() && |
| 240 | + !header.isPlaceholder && |
| 241 | + flexRender(header.column.columnDef.header, header.getContext())} |
| 242 | + </TableHead> |
| 243 | + ); |
| 244 | + })} |
| 245 | + </TableRow> |
| 246 | + ))} |
| 247 | + </TableHeader> |
| 248 | + <TableBody {...props?.body}> |
| 249 | + {!!table.getRowModel().rows?.length && |
| 250 | + table.getRowModel().rows?.map(row => ( |
| 251 | + <React.Fragment key={row.id}> |
| 252 | + <TableRow |
| 253 | + key={row.id} |
| 254 | + {...props?.row} |
| 255 | + className={ctw( |
| 256 | + 'h-[76px] border-b-0 even:bg-[#F4F6FD]/50 hover:bg-[#F4F6FD]/90', |
| 257 | + props?.row?.className, |
| 258 | + )} |
| 259 | + > |
| 260 | + {row.getVisibleCells()?.map(cell => ( |
| 261 | + <TableCell |
| 262 | + key={cell.id} |
| 263 | + {...props?.cell} |
| 264 | + className={ctw('!py-px !pl-3.5', props?.cell?.className)} |
| 265 | + > |
| 266 | + {CellContentWrapper ? ( |
| 267 | + <CellContentWrapper cell={cell}> |
| 268 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 269 | + </CellContentWrapper> |
| 270 | + ) : ( |
| 271 | + flexRender(cell.column.columnDef.cell, cell.getContext()) |
| 272 | + )} |
| 273 | + </TableCell> |
| 274 | + ))} |
| 275 | + </TableRow> |
| 276 | + {CollapsibleContent && ( |
| 277 | + <Collapsible open={row.getIsExpanded()} asChild> |
| 278 | + <ShadCNCollapsibleContent asChild> |
| 279 | + <TableRow className={`max-h-[228px] border-y-[1px]`}> |
| 280 | + <TableCell colSpan={10} className={`p-8`}> |
| 281 | + <CollapsibleContent row={row.original} /> |
| 282 | + </TableCell> |
| 283 | + </TableRow> |
| 284 | + </ShadCNCollapsibleContent> |
| 285 | + </Collapsible> |
| 286 | + )} |
| 287 | + </React.Fragment> |
| 288 | + ))} |
| 289 | + {!table.getRowModel().rows?.length && ( |
| 290 | + <TableRow |
| 291 | + {...props?.row} |
| 292 | + className={ctw('hover:bg-unset h-6 border-none', props?.row?.className)} |
| 293 | + > |
| 294 | + <TableCell |
| 295 | + colSpan={columns?.length} |
| 296 | + {...props?.cell} |
| 297 | + className={ctw('!py-px !pl-3.5', props?.cell?.className)} |
| 298 | + > |
| 299 | + No results. |
| 300 | + </TableCell> |
| 301 | + </TableRow> |
| 302 | + )} |
| 303 | + </TableBody> |
| 304 | + </Table> |
| 305 | + </ScrollArea> |
| 306 | + </div> |
| 307 | + ); |
| 308 | +}; |
0 commit comments