Skip to content

Commit 370f443

Browse files
committed
Fix pivot header groups
Fixes TanStack#376
1 parent 67f8c9a commit 370f443

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/methods.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default Base =>
5555
columnsWithExpander = [expanderColumn, ...columnsWithExpander]
5656
}
5757

58-
const makeDecoratedColumn = column => {
58+
const makeDecoratedColumn = (column, parentColumn) => {
5959
let dcol
6060
if (column.expander) {
6161
dcol = {
@@ -70,44 +70,51 @@ export default Base =>
7070
}
7171
}
7272

73+
// Ensure minWidth is not greater than maxWidth if set
74+
if (dcol.maxWidth < dcol.minWidth) {
75+
dcol.minWidth = dcol.maxWidth
76+
}
77+
78+
if (parentColumn) {
79+
dcol.parentColumn = parentColumn
80+
}
81+
82+
// First check for string accessor
7383
if (typeof dcol.accessor === 'string') {
7484
dcol.id = dcol.id || dcol.accessor
7585
const accessorString = dcol.accessor
7686
dcol.accessor = row => _.get(row, accessorString)
7787
return dcol
7888
}
7989

90+
// Fall back to functional accessor (but require an ID)
8091
if (dcol.accessor && !dcol.id) {
8192
console.warn(dcol)
8293
throw new Error(
8394
'A column id is required if using a non-string accessor for column above.'
8495
)
8596
}
8697

98+
// Fall back to an undefined accessor
8799
if (!dcol.accessor) {
88100
dcol.accessor = d => undefined
89101
}
90102

91-
// Ensure minWidth is not greater than maxWidth if set
92-
if (dcol.maxWidth < dcol.minWidth) {
93-
dcol.minWidth = dcol.maxWidth
94-
}
95-
96103
return dcol
97104
}
98105

99106
// Decorate the columns
100-
const decorateAndAddToAll = col => {
101-
const decoratedColumn = makeDecoratedColumn(col)
107+
const decorateAndAddToAll = (column, parentColumn) => {
108+
const decoratedColumn = makeDecoratedColumn(column, parentColumn)
102109
allDecoratedColumns.push(decoratedColumn)
103110
return decoratedColumn
104111
}
105-
let allDecoratedColumns = []
112+
const allDecoratedColumns = []
106113
const decoratedColumns = columnsWithExpander.map((column, i) => {
107114
if (column.columns) {
108115
return {
109116
...column,
110-
columns: column.columns.map(decorateAndAddToAll),
117+
columns: column.columns.map(d => decorateAndAddToAll(d, column)),
111118
}
112119
} else {
113120
return decorateAndAddToAll(column)
@@ -156,8 +163,17 @@ export default Base =>
156163
}
157164
})
158165

166+
let PivotParentColumn = pivotColumns.reduce(
167+
(prev, current) =>
168+
prev && prev === current.parentColumn && current.parentColumn,
169+
pivotColumns[0].parentColumn
170+
)
171+
172+
let PivotGroupHeader = hasHeaderGroups && PivotParentColumn.Header
173+
PivotGroupHeader = PivotGroupHeader || (() => <strong>Pivoted</strong>)
174+
159175
let pivotColumnGroup = {
160-
header: () => <strong>Group</strong>,
176+
Header: PivotGroupHeader,
161177
columns: pivotColumns.map(col => ({
162178
...this.props.pivotDefaults,
163179
...col,

0 commit comments

Comments
 (0)