@ltht-react/table 2.0.151 → 2.0.152

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,147 +1,146 @@
1
- import Icon from '@ltht-react/icon'
2
- import { flexRender, Header as ReactTableHeader, Table } from '@tanstack/react-table'
3
- import React, { useMemo, useRef } from 'react'
4
- import { calculateStaticColumnOffset } from './table-methods'
5
- import {
6
- StyledNextPageButtonContainer,
7
- StyledSpinnerContainer,
8
- StyledTable,
9
- StyledTableRow,
10
- StyledTableData,
11
- StyledTableHeader,
12
- StyledTHead,
13
- } from './table-styled-components'
14
- import useDimensionsRef from './useDimensionRef'
15
- import { CellProps } from './table-cell'
16
- import { ITableConfig } from './table'
17
-
18
- const TableComponent = <T,>({ table, staticColumns = 0, headerAxis, isFlex }: ITableHeadProps<T>): JSX.Element => {
19
- const firstColumn = useRef(null)
20
- const secondColumn = useRef(null)
21
- const tableElement = useRef(null)
22
- const { width: firstColumnWidth } = useDimensionsRef(firstColumn, tableElement)
23
- const { width: secondColumnWidth } = useDimensionsRef(secondColumn, tableElement)
24
-
25
- const usingExpanderColumn = table.getHeaderGroups().some((x) => x.headers.some((h) => h.column.id === 'expander'))
26
- const totalStaticColumns = useMemo(
27
- () => (usingExpanderColumn ? staticColumns + 1 : staticColumns),
28
- [usingExpanderColumn, staticColumns]
29
- )
30
-
31
- const getHeaderColumn = <TData, TValue>(header: ReactTableHeader<TData, TValue>, headerIndex: number) => {
32
- switch (headerIndex) {
33
- case 0:
34
- return getHeaderElement(
35
- header,
36
- calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth),
37
- firstColumn
38
- )
39
- case 1:
40
- return getHeaderElement(
41
- header,
42
- calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth),
43
- secondColumn
44
- )
45
-
46
- default:
47
- return getHeaderElement(
48
- header,
49
- calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth)
50
- )
51
- }
52
- }
53
-
54
- const getHeaderElement = <TData, TValue>(
55
- header: ReactTableHeader<TData, TValue>,
56
- stickyWidth?: number,
57
- elementRef?: React.MutableRefObject<null>
58
- ) => (
59
- <StyledTableHeader
60
- stickyWidth={stickyWidth}
61
- key={header.id}
62
- colSpan={header.colSpan}
63
- ref={elementRef}
64
- role="columnheader"
65
- {...(header.column.id !== 'expander'
66
- ? {
67
- style: {
68
- cursor: header.column.getCanSort() ? 'pointer' : '',
69
- },
70
- onClick: header.column.getToggleSortingHandler(),
71
- }
72
- : {})}
73
- >
74
- {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
75
- </StyledTableHeader>
76
- )
77
-
78
- return (
79
- <StyledTable ref={tableElement} isFlex={isFlex}>
80
- <StyledTHead>
81
- {table.getHeaderGroups().map((headerGroup) => (
82
- <tr key={headerGroup.id} role="row">
83
- {headerGroup.headers.map((header, headerIndex) => getHeaderColumn(header, headerIndex))}
84
- </tr>
85
- ))}
86
- </StyledTHead>
87
- <tbody>
88
- {table.getRowModel().rows.map((row) => (
89
- <StyledTableRow tableHeaderAxis={headerAxis} key={row.id} role="row">
90
- {row.getVisibleCells().map((cell, cellIdx) => (
91
- <StyledTableData
92
- tableHeaderAxis={headerAxis}
93
- stickyWidth={calculateStaticColumnOffset(
94
- cellIdx,
95
- totalStaticColumns,
96
- firstColumnWidth,
97
- secondColumnWidth
98
- )}
99
- key={cell.id}
100
- role="cell"
101
- style={(cell.getValue() as CellProps)?.parentStyle}
102
- >
103
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
104
- </StyledTableData>
105
- ))}
106
- </StyledTableRow>
107
- ))}
108
- </tbody>
109
- </StyledTable>
110
- )
111
- }
112
-
113
- const TableNavigationButton = ({ position, hidden, clickHandler }: ITableNavButtonProps) => (
114
- <StyledNextPageButtonContainer
115
- role="button"
116
- elementPosition={position}
117
- onClick={() => clickHandler()}
118
- hidden={hidden}
119
- >
120
- <Icon type="chevron" direction={position === 'bottom' ? 'down' : 'right'} size="medium" />
121
- </StyledNextPageButtonContainer>
122
- )
123
-
124
- const TableSpinner = ({ position, hidden }: ITableSpinnerProps) => (
125
- <StyledSpinnerContainer elementPosition={position} hidden={hidden}>
126
- <Icon type="spinner" size="medium" title="Loading..." />
127
- </StyledSpinnerContainer>
128
- )
129
-
130
- interface ITableNavButtonProps {
131
- position: 'bottom' | 'right'
132
- hidden: boolean
133
- clickHandler: () => void
134
- }
135
-
136
- interface ITableSpinnerProps {
137
- position: 'bottom' | 'right'
138
- hidden: boolean
139
- }
140
-
141
- interface ITableHeadProps<T> extends ITableConfig {
142
- table: Table<T>
143
- isFlex?: boolean
144
- }
145
-
146
- export default TableComponent
147
- export { TableNavigationButton, TableSpinner }
1
+ import Icon from '@ltht-react/icon'
2
+ import { flexRender, Header as ReactTableHeader, Table } from '@tanstack/react-table'
3
+ import React, { useMemo, useRef } from 'react'
4
+ import { calculateStaticColumnOffset } from './table-methods'
5
+ import {
6
+ StyledNextPageButtonContainer,
7
+ StyledSpinnerContainer,
8
+ StyledTable,
9
+ StyledTableRow,
10
+ StyledTableData,
11
+ StyledTableHeader,
12
+ StyledTHead,
13
+ } from './table-styled-components'
14
+ import useDimensionsRef from './useDimensionRef'
15
+ import { CellProps } from './table-cell'
16
+ import { ITableConfig } from './table'
17
+
18
+ const TableComponent = <T,>({ table, staticColumns = 0, headerAxis }: ITableHeadProps<T>): JSX.Element => {
19
+ const firstColumn = useRef(null)
20
+ const secondColumn = useRef(null)
21
+ const tableElement = useRef(null)
22
+ const { width: firstColumnWidth } = useDimensionsRef(firstColumn, tableElement)
23
+ const { width: secondColumnWidth } = useDimensionsRef(secondColumn, tableElement)
24
+
25
+ const usingExpanderColumn = table.getHeaderGroups().some((x) => x.headers.some((h) => h.column.id === 'expander'))
26
+ const totalStaticColumns = useMemo(
27
+ () => (usingExpanderColumn ? staticColumns + 1 : staticColumns),
28
+ [usingExpanderColumn, staticColumns]
29
+ )
30
+
31
+ const getHeaderColumn = <TData, TValue>(header: ReactTableHeader<TData, TValue>, headerIndex: number) => {
32
+ switch (headerIndex) {
33
+ case 0:
34
+ return getHeaderElement(
35
+ header,
36
+ calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth),
37
+ firstColumn
38
+ )
39
+ case 1:
40
+ return getHeaderElement(
41
+ header,
42
+ calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth),
43
+ secondColumn
44
+ )
45
+
46
+ default:
47
+ return getHeaderElement(
48
+ header,
49
+ calculateStaticColumnOffset(headerIndex, totalStaticColumns, firstColumnWidth, secondColumnWidth)
50
+ )
51
+ }
52
+ }
53
+
54
+ const getHeaderElement = <TData, TValue>(
55
+ header: ReactTableHeader<TData, TValue>,
56
+ stickyWidth?: number,
57
+ elementRef?: React.MutableRefObject<null>
58
+ ) => (
59
+ <StyledTableHeader
60
+ stickyWidth={stickyWidth}
61
+ key={header.id}
62
+ colSpan={header.colSpan}
63
+ ref={elementRef}
64
+ role="columnheader"
65
+ {...(header.column.id !== 'expander'
66
+ ? {
67
+ style: {
68
+ cursor: header.column.getCanSort() ? 'pointer' : '',
69
+ },
70
+ onClick: header.column.getToggleSortingHandler(),
71
+ }
72
+ : {})}
73
+ >
74
+ {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
75
+ </StyledTableHeader>
76
+ )
77
+
78
+ return (
79
+ <StyledTable ref={tableElement}>
80
+ <StyledTHead>
81
+ {table.getHeaderGroups().map((headerGroup) => (
82
+ <tr key={headerGroup.id} role="row">
83
+ {headerGroup.headers.map((header, headerIndex) => getHeaderColumn(header, headerIndex))}
84
+ </tr>
85
+ ))}
86
+ </StyledTHead>
87
+ <tbody>
88
+ {table.getRowModel().rows.map((row) => (
89
+ <StyledTableRow tableHeaderAxis={headerAxis} key={row.id} role="row">
90
+ {row.getVisibleCells().map((cell, cellIdx) => (
91
+ <StyledTableData
92
+ tableHeaderAxis={headerAxis}
93
+ stickyWidth={calculateStaticColumnOffset(
94
+ cellIdx,
95
+ totalStaticColumns,
96
+ firstColumnWidth,
97
+ secondColumnWidth
98
+ )}
99
+ key={cell.id}
100
+ role="cell"
101
+ style={(cell.getValue() as CellProps)?.parentStyle}
102
+ >
103
+ {flexRender(cell.column.columnDef.cell, cell.getContext())}
104
+ </StyledTableData>
105
+ ))}
106
+ </StyledTableRow>
107
+ ))}
108
+ </tbody>
109
+ </StyledTable>
110
+ )
111
+ }
112
+
113
+ const TableNavigationButton = ({ position, hidden, clickHandler }: ITableNavButtonProps) => (
114
+ <StyledNextPageButtonContainer
115
+ role="button"
116
+ elementPosition={position}
117
+ onClick={() => clickHandler()}
118
+ hidden={hidden}
119
+ >
120
+ <Icon type="chevron" direction={position === 'bottom' ? 'down' : 'right'} size="medium" />
121
+ </StyledNextPageButtonContainer>
122
+ )
123
+
124
+ const TableSpinner = ({ position, hidden }: ITableSpinnerProps) => (
125
+ <StyledSpinnerContainer elementPosition={position} hidden={hidden}>
126
+ <Icon type="spinner" size="medium" title="Loading..." />
127
+ </StyledSpinnerContainer>
128
+ )
129
+
130
+ interface ITableNavButtonProps {
131
+ position: 'bottom' | 'right'
132
+ hidden: boolean
133
+ clickHandler: () => void
134
+ }
135
+
136
+ interface ITableSpinnerProps {
137
+ position: 'bottom' | 'right'
138
+ hidden: boolean
139
+ }
140
+
141
+ interface ITableHeadProps<T> extends ITableConfig {
142
+ table: Table<T>
143
+ }
144
+
145
+ export default TableComponent
146
+ export { TableNavigationButton, TableSpinner }