@ltht-react/table 2.0.162 → 2.0.164

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