@ltht-react/table 2.0.151 → 2.0.153

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,163 +1,168 @@
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
- isFlex = false,
34
- enableSorting = true,
35
- sortingFunctions = undefined,
36
- }) => {
37
- const scrollableDivElement = useRef(null)
38
- const scrollState = useScrollRef(scrollableDivElement)
39
-
40
- const [expanded, setExpanded] = useState<ExpandedState>({})
41
- const [sorting, setSorting] = useState<SortingState>([])
42
- const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
43
- pageIndex: currentPage - 1,
44
- pageSize: pageSizeParam,
45
- })
46
-
47
- const pagination = useMemo(() => ({ pageIndex, pageSize }), [pageIndex, pageSize])
48
- const [data, setData] = useState<DataEntity[]>([])
49
- const [columns, setColumns] = useState<ColumnDef<DataEntity>[]>([])
50
- const [pageCount, setPageCount] = useState<number>(-1)
51
-
52
- useEffect(() => {
53
- if (!manualPagination) {
54
- handleDataUpdate(tableData, pageIndex, pageSize, headerAxis, setColumns, setData, setPageCount)
55
- }
56
- }, [pageIndex, pageSize, headerAxis, tableData, manualPagination])
57
-
58
- useEffect(() => {
59
- if (manualPagination) {
60
- handleDataUpdateForManualPagination(tableData, headerAxis, keepPreviousData, setColumns, setData)
61
- }
62
- }, [headerAxis, tableData, manualPagination, keepPreviousData])
63
-
64
- const table = useReactTable({
65
- data,
66
- columns,
67
- ...(!manualPagination ? { pageCount } : {}),
68
- state: {
69
- expanded,
70
- sorting,
71
- ...(!manualPagination ? { pagination } : {}),
72
- },
73
- manualPagination: true,
74
- onExpandedChange: setExpanded,
75
- onSortingChange: setSorting,
76
- enableSorting,
77
- sortingFns: sortingFunctions,
78
- getSubRows: (row) => row.subRows,
79
- getCoreRowModel: getCoreRowModel(),
80
- getExpandedRowModel: getExpandedRowModel(),
81
- getSortedRowModel: getSortedRowModel(),
82
- ...(!manualPagination ? { onPaginationChange: setPagination } : {}),
83
- })
84
-
85
- useEffect(() => {
86
- if (!scrollState) {
87
- return
88
- }
89
-
90
- if (!manualPagination) {
91
- handleScrollEvent(table, headerAxis, scrollState)
92
- }
93
- }, [scrollState, table, headerAxis, manualPagination])
94
-
95
- const getNextPage = () => {
96
- if (manualPagination) {
97
- nextPage()
98
- } else {
99
- table.nextPage()
100
- }
101
- }
102
-
103
- return (
104
- <ScrollableContainer ref={scrollableDivElement} tableHeaderAxis={headerAxis} {...{ maxHeight, maxWidth }}>
105
- <TableComponent table={table} staticColumns={staticColumns} headerAxis={headerAxis} isFlex={isFlex} />
106
- {manualPagination ? (
107
- <TableSpinner position={headerAxis === 'x' ? 'bottom' : 'right'} hidden={!isFetching} />
108
- ) : null}
109
- <TableNavigationButton
110
- position={headerAxis === 'x' ? 'bottom' : 'right'}
111
- hidden={isFetching || (manualPagination ? !getCanNextPage() : !table.getCanNextPage())}
112
- clickHandler={getNextPage}
113
- />
114
- </ScrollableContainer>
115
- )
116
- }
117
-
118
- interface IProps extends ITableConfig, IPaginationProps, ITableDimensionProps {
119
- tableData: TableData
120
- }
121
-
122
- export interface ITableConfig {
123
- staticColumns?: 0 | 1 | 2
124
- headerAxis?: Axis
125
- enableSorting?: boolean
126
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
- sortingFunctions?: Record<string, SortingFn<any>> | undefined
128
- }
129
-
130
- export interface IPaginationProps {
131
- currentPage?: number
132
- pageSize?: number
133
- manualPagination?: boolean
134
- nextPage?: () => void
135
- getCanNextPage?: () => boolean
136
- isFetching?: boolean
137
- keepPreviousData?: boolean
138
- }
139
-
140
- export interface ITableDimensionProps {
141
- maxWidth?: string
142
- maxHeight?: string
143
- isFlex?: boolean
144
- }
145
-
146
- type DataEntity = Record<string, CellProps | DataEntity[]> & {
147
- subRows?: DataEntity[]
148
- }
149
-
150
- interface Header {
151
- type: 'accessor' | 'group' | 'display'
152
- id: string
153
- cellProps: CellProps
154
- subHeaders?: Header[]
155
- }
156
-
157
- interface TableData {
158
- headers: Header[]
159
- rows: DataEntity[]
160
- }
161
-
162
- export default Table
163
- 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
+ isFlex = false,
34
+ enableSorting = true,
35
+ sortingFunctions = undefined,
36
+ }) => {
37
+ const scrollableDivElement = useRef(null)
38
+ const scrollState = useScrollRef(scrollableDivElement)
39
+
40
+ const [expanded, setExpanded] = useState<ExpandedState>({})
41
+ const [sorting, setSorting] = useState<SortingState>([])
42
+ const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
43
+ pageIndex: currentPage - 1,
44
+ pageSize: pageSizeParam,
45
+ })
46
+
47
+ const pagination = useMemo(() => ({ pageIndex, pageSize }), [pageIndex, pageSize])
48
+ const [data, setData] = useState<DataEntity[]>([])
49
+ const [columns, setColumns] = useState<ColumnDef<DataEntity>[]>([])
50
+ const [pageCount, setPageCount] = useState<number>(-1)
51
+
52
+ useEffect(() => {
53
+ if (!manualPagination) {
54
+ handleDataUpdate(tableData, pageIndex, pageSize, headerAxis, setColumns, setData, setPageCount)
55
+ }
56
+ }, [pageIndex, pageSize, headerAxis, tableData, manualPagination])
57
+
58
+ useEffect(() => {
59
+ if (manualPagination) {
60
+ handleDataUpdateForManualPagination(tableData, headerAxis, keepPreviousData, setColumns, setData)
61
+ }
62
+ }, [headerAxis, tableData, manualPagination, keepPreviousData])
63
+
64
+ const table = useReactTable({
65
+ data,
66
+ columns,
67
+ ...(!manualPagination ? { pageCount } : {}),
68
+ state: {
69
+ expanded,
70
+ sorting,
71
+ ...(!manualPagination ? { pagination } : {}),
72
+ },
73
+ manualPagination: true,
74
+ onExpandedChange: setExpanded,
75
+ onSortingChange: setSorting,
76
+ enableSorting,
77
+ sortingFns: sortingFunctions,
78
+ getSubRows: (row) => row.subRows,
79
+ getCoreRowModel: getCoreRowModel(),
80
+ getExpandedRowModel: getExpandedRowModel(),
81
+ getSortedRowModel: getSortedRowModel(),
82
+ ...(!manualPagination ? { onPaginationChange: setPagination } : {}),
83
+ })
84
+
85
+ useEffect(() => {
86
+ if (!scrollState) {
87
+ return
88
+ }
89
+
90
+ if (!manualPagination) {
91
+ handleScrollEvent(table, headerAxis, scrollState)
92
+ }
93
+ }, [scrollState, table, headerAxis, manualPagination])
94
+
95
+ const getNextPage = () => {
96
+ if (manualPagination) {
97
+ nextPage()
98
+ } else {
99
+ table.nextPage()
100
+ }
101
+ }
102
+
103
+ return (
104
+ <ScrollableContainer
105
+ ref={scrollableDivElement}
106
+ tableHeaderAxis={headerAxis}
107
+ {...{ maxHeight, maxWidth }}
108
+ isFlex={isFlex}
109
+ >
110
+ <TableComponent table={table} staticColumns={staticColumns} headerAxis={headerAxis} />
111
+ {manualPagination ? (
112
+ <TableSpinner position={headerAxis === 'x' ? 'bottom' : 'right'} hidden={!isFetching} />
113
+ ) : null}
114
+ <TableNavigationButton
115
+ position={headerAxis === 'x' ? 'bottom' : 'right'}
116
+ hidden={isFetching || (manualPagination ? !getCanNextPage() : !table.getCanNextPage())}
117
+ clickHandler={getNextPage}
118
+ />
119
+ </ScrollableContainer>
120
+ )
121
+ }
122
+
123
+ interface IProps extends ITableConfig, IPaginationProps, ITableDimensionProps {
124
+ tableData: TableData
125
+ }
126
+
127
+ export interface ITableConfig {
128
+ staticColumns?: 0 | 1 | 2
129
+ headerAxis?: Axis
130
+ enableSorting?: boolean
131
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
132
+ sortingFunctions?: Record<string, SortingFn<any>> | undefined
133
+ }
134
+
135
+ export interface IPaginationProps {
136
+ currentPage?: number
137
+ pageSize?: number
138
+ manualPagination?: boolean
139
+ nextPage?: () => void
140
+ getCanNextPage?: () => boolean
141
+ isFetching?: boolean
142
+ keepPreviousData?: boolean
143
+ }
144
+
145
+ export interface ITableDimensionProps {
146
+ maxWidth?: string
147
+ maxHeight?: string
148
+ isFlex?: boolean
149
+ }
150
+
151
+ type DataEntity = Record<string, CellProps | DataEntity[]> & {
152
+ subRows?: DataEntity[]
153
+ }
154
+
155
+ interface Header {
156
+ type: 'accessor' | 'group' | 'display'
157
+ id: string
158
+ cellProps: CellProps
159
+ subHeaders?: Header[]
160
+ }
161
+
162
+ interface TableData {
163
+ headers: Header[]
164
+ rows: DataEntity[]
165
+ }
166
+
167
+ export default Table
168
+ 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