@ltht-react/table 2.0.147 → 2.0.149

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,161 +1,161 @@
1
- import { FC, useEffect, useMemo, useRef, useState } from 'react'
2
- import {
3
- getCoreRowModel,
4
- useReactTable,
5
- getExpandedRowModel,
6
- getSortedRowModel,
7
- ExpandedState,
8
- SortingState,
9
- PaginationState,
10
- ColumnDef,
11
- SortingFn,
12
- } from '@tanstack/react-table'
13
- import { Axis } from '@ltht-react/types'
14
- import { ScrollableContainer } from './table-styled-components'
15
- import useScrollRef from './useScrollRef'
16
- import { handleDataUpdate, handleDataUpdateForManualPagination, handleScrollEvent } from './table-methods'
17
- import TableComponent, { TableNavigationButton, TableSpinner } from './table-component'
18
- import { CellProps } from './table-cell'
19
-
20
- const Table: FC<IProps> = ({
21
- tableData,
22
- staticColumns = 0,
23
- currentPage = 1,
24
- pageSize: pageSizeParam = 10,
25
- headerAxis = 'x',
26
- manualPagination = false,
27
- getCanNextPage = () => false,
28
- nextPage = () => null,
29
- isFetching = false,
30
- keepPreviousData = false,
31
- maxHeight,
32
- maxWidth,
33
- enableSorting = true,
34
- sortingFunctions = undefined,
35
- }) => {
36
- const scrollableDivElement = useRef(null)
37
- const scrollState = useScrollRef(scrollableDivElement)
38
-
39
- const [expanded, setExpanded] = useState<ExpandedState>({})
40
- const [sorting, setSorting] = useState<SortingState>([])
41
- const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
42
- pageIndex: currentPage - 1,
43
- pageSize: pageSizeParam,
44
- })
45
-
46
- const pagination = useMemo(() => ({ pageIndex, pageSize }), [pageIndex, pageSize])
47
- const [data, setData] = useState<DataEntity[]>([])
48
- const [columns, setColumns] = useState<ColumnDef<DataEntity>[]>([])
49
- const [pageCount, setPageCount] = useState<number>(-1)
50
-
51
- useEffect(() => {
52
- if (!manualPagination) {
53
- handleDataUpdate(tableData, pageIndex, pageSize, headerAxis, setColumns, setData, setPageCount)
54
- }
55
- }, [pageIndex, pageSize, headerAxis, tableData, manualPagination])
56
-
57
- useEffect(() => {
58
- if (manualPagination) {
59
- handleDataUpdateForManualPagination(tableData, headerAxis, keepPreviousData, setColumns, setData)
60
- }
61
- }, [headerAxis, tableData, manualPagination, keepPreviousData])
62
-
63
- const table = useReactTable({
64
- data,
65
- columns,
66
- ...(!manualPagination ? { pageCount } : {}),
67
- state: {
68
- expanded,
69
- sorting,
70
- ...(!manualPagination ? { pagination } : {}),
71
- },
72
- manualPagination: true,
73
- onExpandedChange: setExpanded,
74
- onSortingChange: setSorting,
75
- enableSorting,
76
- sortingFns: sortingFunctions,
77
- getSubRows: (row) => row.subRows,
78
- getCoreRowModel: getCoreRowModel(),
79
- getExpandedRowModel: getExpandedRowModel(),
80
- getSortedRowModel: getSortedRowModel(),
81
- ...(!manualPagination ? { onPaginationChange: setPagination } : {}),
82
- })
83
-
84
- useEffect(() => {
85
- if (!scrollState) {
86
- return
87
- }
88
-
89
- if (!manualPagination) {
90
- handleScrollEvent(table, headerAxis, scrollState)
91
- }
92
- }, [scrollState, table, headerAxis, manualPagination])
93
-
94
- const getNextPage = () => {
95
- if (manualPagination) {
96
- nextPage()
97
- } else {
98
- table.nextPage()
99
- }
100
- }
101
-
102
- return (
103
- <ScrollableContainer ref={scrollableDivElement} tableHeaderAxis={headerAxis} {...{ maxHeight, maxWidth }}>
104
- <TableComponent table={table} staticColumns={staticColumns} headerAxis={headerAxis} />
105
- {manualPagination ? (
106
- <TableSpinner position={headerAxis === 'x' ? 'bottom' : 'right'} hidden={!isFetching} />
107
- ) : null}
108
- <TableNavigationButton
109
- position={headerAxis === 'x' ? 'bottom' : 'right'}
110
- hidden={isFetching || (manualPagination ? !getCanNextPage() : !table.getCanNextPage())}
111
- clickHandler={getNextPage}
112
- />
113
- </ScrollableContainer>
114
- )
115
- }
116
-
117
- interface IProps extends ITableConfig, IPaginationProps, ITableDimensionProps {
118
- tableData: TableData
119
- }
120
-
121
- export interface ITableConfig {
122
- staticColumns?: 0 | 1 | 2
123
- headerAxis?: Axis
124
- enableSorting?: boolean
125
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
- sortingFunctions?: Record<string, SortingFn<any>> | undefined
127
- }
128
-
129
- export interface IPaginationProps {
130
- currentPage?: number
131
- pageSize?: number
132
- manualPagination?: boolean
133
- nextPage?: () => void
134
- getCanNextPage?: () => boolean
135
- isFetching?: boolean
136
- keepPreviousData?: boolean
137
- }
138
-
139
- export interface ITableDimensionProps {
140
- maxWidth?: string
141
- maxHeight?: string
142
- }
143
-
144
- type DataEntity = Record<string, CellProps | DataEntity[]> & {
145
- subRows?: DataEntity[]
146
- }
147
-
148
- interface Header {
149
- type: 'accessor' | 'group' | 'display'
150
- id: string
151
- cellProps: CellProps
152
- subHeaders?: Header[]
153
- }
154
-
155
- interface TableData {
156
- headers: Header[]
157
- rows: DataEntity[]
158
- }
159
-
160
- export default Table
161
- export { DataEntity, CellProps, Header, TableData }
1
+ import { FC, useEffect, useMemo, useRef, useState } from 'react'
2
+ import {
3
+ getCoreRowModel,
4
+ useReactTable,
5
+ getExpandedRowModel,
6
+ getSortedRowModel,
7
+ ExpandedState,
8
+ SortingState,
9
+ PaginationState,
10
+ ColumnDef,
11
+ SortingFn,
12
+ } from '@tanstack/react-table'
13
+ import { Axis } from '@ltht-react/types'
14
+ import { ScrollableContainer } from './table-styled-components'
15
+ import useScrollRef from './useScrollRef'
16
+ import { handleDataUpdate, handleDataUpdateForManualPagination, handleScrollEvent } from './table-methods'
17
+ import TableComponent, { TableNavigationButton, TableSpinner } from './table-component'
18
+ import { CellProps } from './table-cell'
19
+
20
+ const Table: FC<IProps> = ({
21
+ tableData,
22
+ staticColumns = 0,
23
+ currentPage = 1,
24
+ pageSize: pageSizeParam = 10,
25
+ headerAxis = 'x',
26
+ manualPagination = false,
27
+ getCanNextPage = () => false,
28
+ nextPage = () => null,
29
+ isFetching = false,
30
+ keepPreviousData = false,
31
+ maxHeight,
32
+ maxWidth,
33
+ enableSorting = true,
34
+ sortingFunctions = undefined,
35
+ }) => {
36
+ const scrollableDivElement = useRef(null)
37
+ const scrollState = useScrollRef(scrollableDivElement)
38
+
39
+ const [expanded, setExpanded] = useState<ExpandedState>({})
40
+ const [sorting, setSorting] = useState<SortingState>([])
41
+ const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
42
+ pageIndex: currentPage - 1,
43
+ pageSize: pageSizeParam,
44
+ })
45
+
46
+ const pagination = useMemo(() => ({ pageIndex, pageSize }), [pageIndex, pageSize])
47
+ const [data, setData] = useState<DataEntity[]>([])
48
+ const [columns, setColumns] = useState<ColumnDef<DataEntity>[]>([])
49
+ const [pageCount, setPageCount] = useState<number>(-1)
50
+
51
+ useEffect(() => {
52
+ if (!manualPagination) {
53
+ handleDataUpdate(tableData, pageIndex, pageSize, headerAxis, setColumns, setData, setPageCount)
54
+ }
55
+ }, [pageIndex, pageSize, headerAxis, tableData, manualPagination])
56
+
57
+ useEffect(() => {
58
+ if (manualPagination) {
59
+ handleDataUpdateForManualPagination(tableData, headerAxis, keepPreviousData, setColumns, setData)
60
+ }
61
+ }, [headerAxis, tableData, manualPagination, keepPreviousData])
62
+
63
+ const table = useReactTable({
64
+ data,
65
+ columns,
66
+ ...(!manualPagination ? { pageCount } : {}),
67
+ state: {
68
+ expanded,
69
+ sorting,
70
+ ...(!manualPagination ? { pagination } : {}),
71
+ },
72
+ manualPagination: true,
73
+ onExpandedChange: setExpanded,
74
+ onSortingChange: setSorting,
75
+ enableSorting,
76
+ sortingFns: sortingFunctions,
77
+ getSubRows: (row) => row.subRows,
78
+ getCoreRowModel: getCoreRowModel(),
79
+ getExpandedRowModel: getExpandedRowModel(),
80
+ getSortedRowModel: getSortedRowModel(),
81
+ ...(!manualPagination ? { onPaginationChange: setPagination } : {}),
82
+ })
83
+
84
+ useEffect(() => {
85
+ if (!scrollState) {
86
+ return
87
+ }
88
+
89
+ if (!manualPagination) {
90
+ handleScrollEvent(table, headerAxis, scrollState)
91
+ }
92
+ }, [scrollState, table, headerAxis, manualPagination])
93
+
94
+ const getNextPage = () => {
95
+ if (manualPagination) {
96
+ nextPage()
97
+ } else {
98
+ table.nextPage()
99
+ }
100
+ }
101
+
102
+ return (
103
+ <ScrollableContainer ref={scrollableDivElement} tableHeaderAxis={headerAxis} {...{ maxHeight, maxWidth }}>
104
+ <TableComponent table={table} staticColumns={staticColumns} headerAxis={headerAxis} />
105
+ {manualPagination ? (
106
+ <TableSpinner position={headerAxis === 'x' ? 'bottom' : 'right'} hidden={!isFetching} />
107
+ ) : null}
108
+ <TableNavigationButton
109
+ position={headerAxis === 'x' ? 'bottom' : 'right'}
110
+ hidden={isFetching || (manualPagination ? !getCanNextPage() : !table.getCanNextPage())}
111
+ clickHandler={getNextPage}
112
+ />
113
+ </ScrollableContainer>
114
+ )
115
+ }
116
+
117
+ interface IProps extends ITableConfig, IPaginationProps, ITableDimensionProps {
118
+ tableData: TableData
119
+ }
120
+
121
+ export interface ITableConfig {
122
+ staticColumns?: 0 | 1 | 2
123
+ headerAxis?: Axis
124
+ enableSorting?: boolean
125
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
+ sortingFunctions?: Record<string, SortingFn<any>> | undefined
127
+ }
128
+
129
+ export interface IPaginationProps {
130
+ currentPage?: number
131
+ pageSize?: number
132
+ manualPagination?: boolean
133
+ nextPage?: () => void
134
+ getCanNextPage?: () => boolean
135
+ isFetching?: boolean
136
+ keepPreviousData?: boolean
137
+ }
138
+
139
+ export interface ITableDimensionProps {
140
+ maxWidth?: string
141
+ maxHeight?: string
142
+ }
143
+
144
+ type DataEntity = Record<string, CellProps | DataEntity[]> & {
145
+ subRows?: DataEntity[]
146
+ }
147
+
148
+ interface Header {
149
+ type: 'accessor' | 'group' | 'display'
150
+ id: string
151
+ cellProps: CellProps
152
+ subHeaders?: Header[]
153
+ }
154
+
155
+ interface TableData {
156
+ headers: Header[]
157
+ rows: DataEntity[]
158
+ }
159
+
160
+ export default Table
161
+ export { DataEntity, CellProps, Header, TableData }
@@ -1,37 +1,37 @@
1
- import { useEffect, useState } from 'react'
2
-
3
- const useDimensionsRef = (elementRef: React.RefObject<HTMLElement>, parentElementRef: React.RefObject<HTMLElement>) => {
4
- const [dimensions, setDimensions] = useState({
5
- width: 0,
6
- height: 0,
7
- })
8
-
9
- useEffect(() => {
10
- const getDimensions = () => ({
11
- width: (elementRef && elementRef.current && elementRef.current.offsetWidth) || 0,
12
- height: (elementRef && elementRef.current && elementRef.current.offsetHeight) || 0,
13
- })
14
-
15
- const handleResize = () => {
16
- setDimensions(getDimensions())
17
- }
18
-
19
- if (elementRef.current) {
20
- setDimensions(getDimensions())
21
- }
22
-
23
- const parentElementResizeObserver = new ResizeObserver((_e: ResizeObserverEntry[]) => handleResize())
24
-
25
- if (parentElementRef?.current) {
26
- parentElementResizeObserver.observe(parentElementRef.current)
27
- }
28
-
29
- return () => {
30
- parentElementResizeObserver?.disconnect()
31
- }
32
- }, [elementRef, parentElementRef])
33
-
34
- return dimensions
35
- }
36
-
37
- export default useDimensionsRef
1
+ import { useEffect, useState } from 'react'
2
+
3
+ const useDimensionsRef = (elementRef: React.RefObject<HTMLElement>, parentElementRef: React.RefObject<HTMLElement>) => {
4
+ const [dimensions, setDimensions] = useState({
5
+ width: 0,
6
+ height: 0,
7
+ })
8
+
9
+ useEffect(() => {
10
+ const getDimensions = () => ({
11
+ width: (elementRef && elementRef.current && elementRef.current.offsetWidth) || 0,
12
+ height: (elementRef && elementRef.current && elementRef.current.offsetHeight) || 0,
13
+ })
14
+
15
+ const handleResize = () => {
16
+ setDimensions(getDimensions())
17
+ }
18
+
19
+ if (elementRef.current) {
20
+ setDimensions(getDimensions())
21
+ }
22
+
23
+ const parentElementResizeObserver = new ResizeObserver((_e: ResizeObserverEntry[]) => handleResize())
24
+
25
+ if (parentElementRef?.current) {
26
+ parentElementResizeObserver.observe(parentElementRef.current)
27
+ }
28
+
29
+ return () => {
30
+ parentElementResizeObserver?.disconnect()
31
+ }
32
+ }, [elementRef, parentElementRef])
33
+
34
+ return dimensions
35
+ }
36
+
37
+ export default useDimensionsRef
@@ -1,36 +1,36 @@
1
- import { useEffect, useState } from 'react'
2
-
3
- const useScrollRef = (elementRef: React.RefObject<HTMLElement>) => {
4
- const [scrollState, setScrollState] = useState<ScrollState>()
5
- const element = elementRef.current
6
-
7
- useEffect(() => {
8
- const getScrollState = (): ScrollState => ({
9
- scrollWidth: element?.scrollWidth ?? 0,
10
- scrollHeight: element?.scrollHeight ?? 0,
11
- currentXScroll: (element?.scrollLeft ?? 0) + (element?.clientWidth ?? 0),
12
- currentYScroll: (element?.scrollTop ?? 0) + (element?.clientHeight ?? 0),
13
- })
14
-
15
- const handleScroll = () => {
16
- setScrollState(getScrollState())
17
- }
18
-
19
- element?.addEventListener('scroll', handleScroll)
20
-
21
- return () => {
22
- element?.removeEventListener('scroll', handleScroll)
23
- }
24
- }, [element])
25
-
26
- return scrollState
27
- }
28
-
29
- export interface ScrollState {
30
- scrollWidth: number
31
- scrollHeight: number
32
- currentXScroll: number
33
- currentYScroll: number
34
- }
35
-
36
- export default useScrollRef
1
+ import { useEffect, useState } from 'react'
2
+
3
+ const useScrollRef = (elementRef: React.RefObject<HTMLElement>) => {
4
+ const [scrollState, setScrollState] = useState<ScrollState>()
5
+ const element = elementRef.current
6
+
7
+ useEffect(() => {
8
+ const getScrollState = (): ScrollState => ({
9
+ scrollWidth: element?.scrollWidth ?? 0,
10
+ scrollHeight: element?.scrollHeight ?? 0,
11
+ currentXScroll: (element?.scrollLeft ?? 0) + (element?.clientWidth ?? 0),
12
+ currentYScroll: (element?.scrollTop ?? 0) + (element?.clientHeight ?? 0),
13
+ })
14
+
15
+ const handleScroll = () => {
16
+ setScrollState(getScrollState())
17
+ }
18
+
19
+ element?.addEventListener('scroll', handleScroll)
20
+
21
+ return () => {
22
+ element?.removeEventListener('scroll', handleScroll)
23
+ }
24
+ }, [element])
25
+
26
+ return scrollState
27
+ }
28
+
29
+ export interface ScrollState {
30
+ scrollWidth: number
31
+ scrollHeight: number
32
+ currentXScroll: number
33
+ currentYScroll: number
34
+ }
35
+
36
+ export default useScrollRef
@@ -1,33 +1,33 @@
1
- import Table, { IPaginationProps, ITableDimensionProps, ITableConfig, TableData } from '../molecules/table'
2
-
3
- const GenericTable = <TColumn, TRow>({
4
- columnData,
5
- rowData,
6
- mapToTableData,
7
- headerAxis = 'x',
8
- pageSize = 10,
9
- currentPage = 1,
10
- keepPreviousData = true,
11
- ...props
12
- }: IProps<TColumn, TRow>) => {
13
- const tableData = mapToTableData(columnData, rowData)
14
-
15
- return (
16
- <Table
17
- tableData={tableData}
18
- headerAxis={headerAxis}
19
- pageSize={pageSize}
20
- currentPage={currentPage}
21
- keepPreviousData={keepPreviousData}
22
- {...props}
23
- />
24
- )
25
- }
26
-
27
- interface IProps<TColumn, TRow> extends ITableConfig, IPaginationProps, ITableDimensionProps {
28
- columnData: TColumn
29
- rowData: TRow
30
- mapToTableData: (colData: TColumn, rowData: TRow) => TableData
31
- }
32
-
33
- export default GenericTable
1
+ import Table, { IPaginationProps, ITableDimensionProps, ITableConfig, TableData } from '../molecules/table'
2
+
3
+ const GenericTable = <TColumn, TRow>({
4
+ columnData,
5
+ rowData,
6
+ mapToTableData,
7
+ headerAxis = 'x',
8
+ pageSize = 10,
9
+ currentPage = 1,
10
+ keepPreviousData = true,
11
+ ...props
12
+ }: IProps<TColumn, TRow>) => {
13
+ const tableData = mapToTableData(columnData, rowData)
14
+
15
+ return (
16
+ <Table
17
+ tableData={tableData}
18
+ headerAxis={headerAxis}
19
+ pageSize={pageSize}
20
+ currentPage={currentPage}
21
+ keepPreviousData={keepPreviousData}
22
+ {...props}
23
+ />
24
+ )
25
+ }
26
+
27
+ interface IProps<TColumn, TRow> extends ITableConfig, IPaginationProps, ITableDimensionProps {
28
+ columnData: TColumn
29
+ rowData: TRow
30
+ mapToTableData: (colData: TColumn, rowData: TRow) => TableData
31
+ }
32
+
33
+ export default GenericTable