mirror of
https://github.com/gosticks/react-table.git
synced 2026-12-04 11:41:45 +00:00
Enhance pivots (#224)
* Prevent transitions while resizing for a smoother effect. * Disable text selection when resizing columns * Use PivotValueComponent instead of pivotRender * Add changelog file
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
## 6.0.0
|
||||
|
||||
* Pivot Columns can now be sorted and filtered individually.
|
||||
* More control over Pivot Column rendering.
|
||||
* Prevent transitions while column resizing for a smoother resize effect.
|
||||
* Disable text selection while resizing columns.
|
||||
|
||||
##### Breaking API Changes
|
||||
* Removed `pivotRender` column option. You can now control how the value is displayed by overriding the `PivotValueComponent`.
|
||||
* Changed how special `expander: true` column renders pivot columns.
|
||||
See [Pivoting Options Story](https://react-table.js.org/?selectedKind=2.%20Demos&selectedStory=Pivoting%20Options&full=0&down=1&left=1&panelRight=0&downPanel=kadirahq%2Fstorybook-addon-actions%2Factions-panel) for a reference on how to customize pivot column rendering.
|
||||
@@ -182,7 +182,6 @@ These are all of the available props (and their default values) for the main `<R
|
||||
|
||||
// Pivoting
|
||||
pivotBy: undefined,
|
||||
pivotColumnWidth: 200,
|
||||
pivotValKey: '_pivotVal',
|
||||
pivotIDKey: '_pivotID',
|
||||
subRowsKey: '_subRows',
|
||||
@@ -268,7 +267,8 @@ These are all of the available props (and their default values) for the main `<R
|
||||
|
||||
// Global Pivot Column Defaults
|
||||
pivotDefaults: {
|
||||
// render: will be overriden in methods.js to display ExpanderComponent
|
||||
filterRender: undefined
|
||||
// render: will be overriden in methods.js to display ExpanderComponent and PivotValueComponent
|
||||
},
|
||||
|
||||
// Text
|
||||
@@ -578,7 +578,7 @@ const columns = [{
|
||||
}]
|
||||
```
|
||||
|
||||
Pivoted columns can be sorted just like regular columns, but not independently of each other. For instance, if you click to sort the pivot column in ascending order, it will sort by each pivot recursively in ascending order together.
|
||||
Pivoted columns can be sorted just like regular columns including holding down the `<shift>` button to multi-sort.
|
||||
|
||||
## Sub Tables and Sub Components
|
||||
By adding a `SubComponent` props, you can easily add an expansion level to all root-level rows:
|
||||
@@ -742,6 +742,7 @@ Object.assign(ReactTableDefaults, {
|
||||
TdComponent: component,
|
||||
TfootComponent: component,
|
||||
ExpanderComponent: component,
|
||||
PivotValueComponent: component,
|
||||
PaginationComponent: component,
|
||||
PreviousComponent: undefined,
|
||||
NextComponent: undefined,
|
||||
|
||||
+10
-9
@@ -44,7 +44,6 @@ export default {
|
||||
|
||||
// Pivoting
|
||||
pivotBy: undefined,
|
||||
pivotColumnWidth: 200,
|
||||
pivotValKey: '_pivotVal',
|
||||
pivotIDKey: '_pivotID',
|
||||
subRowsKey: '_subRows',
|
||||
@@ -129,7 +128,8 @@ export default {
|
||||
|
||||
// Global Pivot Column Defaults
|
||||
pivotDefaults: {
|
||||
// render: will be overriden in methods.js to display ExpanderComponent
|
||||
filterRender: undefined
|
||||
// render: will be overriden in methods.js to display ExpanderComponent and PivotValueComponent
|
||||
},
|
||||
|
||||
// Text
|
||||
@@ -162,13 +162,14 @@ export default {
|
||||
},
|
||||
TdComponent: _.makeTemplateComponent('rt-td'),
|
||||
TfootComponent: _.makeTemplateComponent('rt-tfoot'),
|
||||
ExpanderComponent: ({isExpanded}) => {
|
||||
return (
|
||||
<div className={classnames('rt-expander', isExpanded && '-open')}>
|
||||
•
|
||||
</div>
|
||||
)
|
||||
},
|
||||
ExpanderComponent: ({isExpanded}) => (
|
||||
<div className={classnames('rt-expander', isExpanded && '-open')}>
|
||||
•
|
||||
</div>
|
||||
),
|
||||
PivotValueComponent: ({subRows, value}) => (
|
||||
<span>{value} {subRows && `(${subRows.length})`}</span>
|
||||
),
|
||||
PaginationComponent: Pagination,
|
||||
PreviousComponent: undefined,
|
||||
NextComponent: undefined,
|
||||
|
||||
+167
-250
@@ -38,7 +38,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
expandedRows: {},
|
||||
filtering: props.defaultFiltering,
|
||||
resizing: props.defaultResizing,
|
||||
currentlyResizing: undefined,
|
||||
currentlyResizing: false,
|
||||
skipNextSort: false
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
pages,
|
||||
// Pivoting State
|
||||
pivotValKey,
|
||||
pivotBy,
|
||||
subRowsKey,
|
||||
expandedRows,
|
||||
onExpandRow,
|
||||
@@ -110,7 +111,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
headerGroups,
|
||||
hasHeaderGroups,
|
||||
// Sorted Data
|
||||
sortedData
|
||||
sortedData,
|
||||
currentlyResizing
|
||||
} = resolvedState
|
||||
|
||||
// Pagination
|
||||
@@ -120,7 +122,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
const minRows = this.getMinRows()
|
||||
const padRows = _.range(Math.max(minRows - pageRows.length, 0))
|
||||
|
||||
const hasColumnFooter = allVisibleColumns.some(d => d.footer)
|
||||
const hasColumnFooter = allVisibleColumns.some(d => d.footer || (d.pivotColumns && d.pivotColumns.some(e => e.footer)))
|
||||
|
||||
const recurseRowsViewIndex = (rows, path = [], index = -1) => {
|
||||
return [
|
||||
@@ -191,18 +193,23 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
}
|
||||
|
||||
const makeHeaderGroup = (column, i) => {
|
||||
const resizedValue = col => (resizing.find(x => x.id === col.id) || {}).value
|
||||
|
||||
const flex = _.sum(column.columns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return d.width || resized.value ? 0 : d.minWidth
|
||||
const widthFunction = col => col.width || resizedValue(col) ? 0 : col.minWidth
|
||||
return d.pivotColumns ? _.sum(d.pivotColumns.map(widthFunction)) : widthFunction(d)
|
||||
}))
|
||||
|
||||
const width = _.sum(column.columns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.minWidth)
|
||||
const widthFunction = col => _.getFirstDefined(resizedValue(col), col.width, col.minWidth)
|
||||
return d.pivotColumns ? _.sum(d.pivotColumns.map(widthFunction)) : widthFunction(d)
|
||||
}))
|
||||
|
||||
const maxWidth = _.sum(column.columns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.maxWidth)
|
||||
const widthFunction = col => _.getFirstDefined(resizedValue(col), col.width, col.maxWidth)
|
||||
return d.pivotColumns ? _.sum(d.pivotColumns.map(widthFunction)) : widthFunction(d)
|
||||
}))
|
||||
|
||||
const theadGroupThProps = _.splitProps(getTheadGroupThProps(finalState, undefined, column, this))
|
||||
const columnHeaderProps = _.splitProps(column.getHeaderProps(finalState, undefined, column, this))
|
||||
|
||||
@@ -231,7 +238,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
|
||||
return (
|
||||
<ThComponent
|
||||
key={i}
|
||||
key={i + '-' + column.id}
|
||||
className={classnames(
|
||||
classes
|
||||
)}
|
||||
@@ -302,67 +309,25 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
<ResizerComponent
|
||||
onMouseDown={e => this.resizeColumnStart(column, e, false)}
|
||||
onTouchStart={e => this.resizeColumnStart(column, e, true)}
|
||||
|
||||
{...resizerProps}
|
||||
/>
|
||||
) : null
|
||||
|
||||
if (column.expander) {
|
||||
if (column.pivotColumns) {
|
||||
const pivotSort = sorting.find(d => d.id === column.pivotColumns[0].id)
|
||||
return (
|
||||
<ThComponent
|
||||
key={i}
|
||||
className={classnames(
|
||||
'rt-pivot-header',
|
||||
'rt-resizable-header',
|
||||
column.sortable && '-cursor-pointer',
|
||||
classes,
|
||||
pivotSort ? (pivotSort.desc ? '-sort-desc' : '-sort-asc') : ''
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
toggleSort={(e) => {
|
||||
column.sortable && this.sortColumn(column.pivotColumns, e.shiftKey)
|
||||
}}
|
||||
{...rest}
|
||||
>
|
||||
<div className='rt-resizable-header-content'>
|
||||
{column.pivotColumns.map((pivotColumn, i) => {
|
||||
return (
|
||||
<span key={pivotColumn.id}>
|
||||
{_.normalizeComponent(pivotColumn.header, {
|
||||
data: sortedData,
|
||||
column: column
|
||||
})}
|
||||
{i < column.pivotColumns.length - 1 && (
|
||||
_.normalizeComponent(column.render, {
|
||||
data: sortedData,
|
||||
column: column
|
||||
})
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
{resizer}
|
||||
</ThComponent>
|
||||
)
|
||||
}
|
||||
if (column.pivotColumns) {
|
||||
return column.pivotColumns.map(makeHeader)
|
||||
}
|
||||
|
||||
return (
|
||||
<ThComponent
|
||||
key={i}
|
||||
key={i + '-' + column.id}
|
||||
className={classnames(
|
||||
classes,
|
||||
'rt-resizable-header',
|
||||
sort ? (sort.desc ? '-sort-desc' : '-sort-asc') : '',
|
||||
column.sortable && '-cursor-pointer',
|
||||
!show && '-hidden',
|
||||
pivotBy && pivotBy.slice(0, -1).includes(column.id) && 'rt-header-pivot'
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
@@ -433,67 +398,15 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
...columnHeaderProps.rest
|
||||
}
|
||||
|
||||
if (column.expander) {
|
||||
if (column.pivotColumns) {
|
||||
const pivotCols = []
|
||||
for (let i = 0; i < column.pivotColumns.length; i++) {
|
||||
const col = column.pivotColumns[i]
|
||||
const filter = filtering.find(filter => filter.id === column.id && filter.pivotId === col.id)
|
||||
pivotCols.push(
|
||||
<span key={col.id}
|
||||
style={{flex: 1}}>
|
||||
{!col.hideFilter ? (
|
||||
_.normalizeComponent(col.filterRender,
|
||||
{
|
||||
col,
|
||||
filter,
|
||||
onFilterChange: (value) => (this.filterColumn(column, value, col))
|
||||
},
|
||||
defaultProps.column.filterRender
|
||||
)
|
||||
) : null}
|
||||
</span>
|
||||
)
|
||||
if (i < column.pivotColumns.length - 1) {
|
||||
pivotCols.push(
|
||||
_.normalizeComponent(column.filterRender,
|
||||
{
|
||||
column,
|
||||
filter,
|
||||
key: col.id + '-' + i
|
||||
},
|
||||
defaultProps.column.filterRender
|
||||
))
|
||||
}
|
||||
}
|
||||
return (
|
||||
<ThComponent
|
||||
key={i}
|
||||
className={classnames(
|
||||
'rt-pivot-header',
|
||||
column.sortable && '-cursor-pointer',
|
||||
classes
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`,
|
||||
display: 'flex'
|
||||
}}
|
||||
{...rest}
|
||||
>
|
||||
{pivotCols}
|
||||
</ThComponent>
|
||||
)
|
||||
}
|
||||
if (!column.filterRender && column.pivotColumns) {
|
||||
return column.pivotColumns.map(makeFilter)
|
||||
}
|
||||
|
||||
const filter = filtering.find(filter => filter.id === column.id)
|
||||
|
||||
return (
|
||||
<ThComponent
|
||||
key={i}
|
||||
key={i + '-' + column.id}
|
||||
className={classnames(
|
||||
classes
|
||||
)}
|
||||
@@ -587,8 +500,20 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
extraProps['onClick'] = onTdClick
|
||||
|
||||
if (column.pivotColumns) {
|
||||
const pivotFlex = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return d.width || resized.value ? 0 : d.minWidth
|
||||
}))
|
||||
const pivotWidth = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.minWidth)
|
||||
}))
|
||||
const pivotMaxWidth = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.maxWidth)
|
||||
}))
|
||||
|
||||
// Return the pivot expander cell
|
||||
const PivotCell = column.pivotRender
|
||||
return (
|
||||
<TdComponent
|
||||
key={i2}
|
||||
@@ -599,35 +524,25 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
style={{
|
||||
...styles,
|
||||
paddingLeft: rowInfo.nestingPath.length === 1 ? undefined : `${30 * (rowInfo.nestingPath.length - 1)}px`,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
flex: `${pivotFlex} 0 auto`,
|
||||
width: `${pivotWidth}px`,
|
||||
maxWidth: `${pivotMaxWidth}px`
|
||||
}}
|
||||
{...tdProps.rest}
|
||||
onClick={onTdClick}
|
||||
>
|
||||
{rowInfo.subRows ? (
|
||||
<span>
|
||||
{_.normalizeComponent(column.render, {
|
||||
...rowInfo,
|
||||
value: rowInfo.rowValues[column.id],
|
||||
isExpanded
|
||||
}, rowInfo.rowValues[column.id])}
|
||||
{column && column.pivotRender ? (
|
||||
<PivotCell
|
||||
{...rowInfo}
|
||||
value={rowInfo.rowValues[pivotValKey]}
|
||||
/>
|
||||
) : <span>{row[pivotValKey]} ({rowInfo.subRows.length})</span>}
|
||||
</span>
|
||||
_.normalizeComponent(column.render, {
|
||||
...rowInfo,
|
||||
value: row[pivotValKey],
|
||||
isExpanded
|
||||
}, rowInfo.rowValues[column.id])
|
||||
) : SubComponent ? (
|
||||
<span>
|
||||
{_.normalizeComponent(column.render, {
|
||||
...rowInfo,
|
||||
value: rowInfo.rowValues[column.id],
|
||||
isExpanded
|
||||
}, rowInfo.rowValues[column.id])}
|
||||
</span>
|
||||
_.normalizeComponent(column.render, {
|
||||
...rowInfo,
|
||||
value: rowInfo.rowValues[column.id],
|
||||
isExpanded
|
||||
}, rowInfo.rowValues[column.id])
|
||||
) : null}
|
||||
</TdComponent>
|
||||
)
|
||||
@@ -637,7 +552,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
// Return regular cell
|
||||
return (
|
||||
<TdComponent
|
||||
key={i2}
|
||||
key={i2 + '-' + column.id}
|
||||
className={classnames(
|
||||
classes,
|
||||
!show && 'hidden'
|
||||
@@ -681,54 +596,73 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
<TrComponent
|
||||
className={classnames(
|
||||
'-padRow',
|
||||
(pageRows.length + i) % 2 ? '-even' : '-odd',
|
||||
trProps.className,
|
||||
)}
|
||||
style={trProps.style || {}}
|
||||
>
|
||||
{allVisibleColumns.map((column, i2) => {
|
||||
const resized = resizing.find(x => x.id === column.id) || {}
|
||||
const show = typeof column.show === 'function' ? column.show() : column.show
|
||||
const width = _.getFirstDefined(resized.value, column.width, column.minWidth)
|
||||
const maxWidth = _.getFirstDefined(resized.value, column.width, column.maxWidth)
|
||||
const tdProps = _.splitProps(getTdProps(finalState, undefined, column, this))
|
||||
const columnProps = _.splitProps(column.getProps(finalState, undefined, column, this))
|
||||
|
||||
const classes = [
|
||||
tdProps.className,
|
||||
column.className,
|
||||
columnProps.className
|
||||
]
|
||||
|
||||
const styles = {
|
||||
...tdProps.style,
|
||||
...column.style,
|
||||
...columnProps.style
|
||||
}
|
||||
|
||||
return (
|
||||
<TdComponent
|
||||
key={i2}
|
||||
className={classnames(
|
||||
classes,
|
||||
!show && 'hidden'
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
{...tdProps.rest}
|
||||
>
|
||||
|
||||
</TdComponent>
|
||||
)
|
||||
})}
|
||||
{allVisibleColumns.map(makePadColumn)}
|
||||
</TrComponent>
|
||||
</TrGroupComponent>
|
||||
)
|
||||
}
|
||||
|
||||
const makePadColumn = (column, i) => {
|
||||
const resized = resizing.find(x => x.id === column.id) || {}
|
||||
const show = typeof column.show === 'function' ? column.show() : column.show
|
||||
let width = _.getFirstDefined(resized.value, column.width, column.minWidth)
|
||||
let flex = width
|
||||
let maxWidth = _.getFirstDefined(resized.value, column.width, column.maxWidth)
|
||||
const tdProps = _.splitProps(getTdProps(finalState, undefined, column, this))
|
||||
const columnProps = _.splitProps(column.getProps(finalState, undefined, column, this))
|
||||
|
||||
const classes = [
|
||||
tdProps.className,
|
||||
column.className,
|
||||
columnProps.className
|
||||
]
|
||||
|
||||
const styles = {
|
||||
...tdProps.style,
|
||||
...column.style,
|
||||
...columnProps.style
|
||||
}
|
||||
|
||||
if (column.pivotColumns) {
|
||||
flex = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return d.width || resized.value ? 0 : d.minWidth
|
||||
}))
|
||||
width = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.minWidth)
|
||||
}))
|
||||
maxWidth = _.sum(column.pivotColumns.map(d => {
|
||||
const resized = resizing.find(x => x.id === d.id) || {}
|
||||
return _.getFirstDefined(resized.value, d.width, d.maxWidth)
|
||||
}))
|
||||
}
|
||||
|
||||
return (
|
||||
<TdComponent
|
||||
key={i + '-' + column.id}
|
||||
className={classnames(
|
||||
classes,
|
||||
!show && 'hidden'
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${flex} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
{...tdProps.rest}
|
||||
>
|
||||
|
||||
</TdComponent>
|
||||
)
|
||||
}
|
||||
|
||||
const makeColumnFooters = () => {
|
||||
const tFootProps = getTfootProps(finalState, undefined, undefined, this)
|
||||
const tFootTrProps = _.splitProps(getTfootTrProps(finalState, undefined, undefined, this))
|
||||
@@ -748,84 +682,64 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
style={tFootTrProps.style}
|
||||
{...tFootTrProps.rest}
|
||||
>
|
||||
{allVisibleColumns.map((column, i2) => {
|
||||
const resized = resizing.find(x => x.id === column.id) || {}
|
||||
const show = typeof column.show === 'function' ? column.show() : column.show
|
||||
const width = _.getFirstDefined(resized.value, column.width, column.minWidth)
|
||||
const maxWidth = _.getFirstDefined(resized.value, column.width, column.maxWidth)
|
||||
const tFootTdProps = _.splitProps(getTfootTdProps(finalState, undefined, undefined, this))
|
||||
const columnProps = _.splitProps(column.getProps(finalState, undefined, column, this))
|
||||
const columnFooterProps = _.splitProps(column.getFooterProps(finalState, undefined, column, this))
|
||||
|
||||
const classes = [
|
||||
tFootTdProps.className,
|
||||
column.className,
|
||||
columnProps.className,
|
||||
columnFooterProps.className
|
||||
]
|
||||
|
||||
const styles = {
|
||||
...tFootTdProps.style,
|
||||
...column.style,
|
||||
...columnProps.style,
|
||||
...columnFooterProps.style
|
||||
}
|
||||
|
||||
if (column.expander) {
|
||||
if (column.pivotColumns) {
|
||||
return (
|
||||
<TdComponent
|
||||
key={i2}
|
||||
className={classnames(
|
||||
'rt-pivot',
|
||||
classes
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
{...columnProps.rest}
|
||||
{...tFootTdProps.rest}
|
||||
{...columnFooterProps.rest}
|
||||
>
|
||||
{_.normalizeComponent(column.footer)}
|
||||
</TdComponent>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Return regular cell
|
||||
return (
|
||||
<TdComponent
|
||||
key={i2}
|
||||
className={classnames(
|
||||
classes,
|
||||
!show && 'hidden'
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
{...columnProps.rest}
|
||||
{...tFootTdProps.rest}
|
||||
{...columnFooterProps.rest}
|
||||
>
|
||||
{_.normalizeComponent(column.footer, {
|
||||
data: sortedData,
|
||||
column: column
|
||||
})}
|
||||
</TdComponent>
|
||||
)
|
||||
})}
|
||||
{allVisibleColumns.map(makeColumnFooter)}
|
||||
</TrComponent>
|
||||
</TfootComponent>
|
||||
)
|
||||
}
|
||||
|
||||
const makeColumnFooter = (column, i) => {
|
||||
const resized = resizing.find(x => x.id === column.id) || {}
|
||||
const show = typeof column.show === 'function' ? column.show() : column.show
|
||||
const width = _.getFirstDefined(resized.value, column.width, column.minWidth)
|
||||
const maxWidth = _.getFirstDefined(resized.value, column.width, column.maxWidth)
|
||||
const tFootTdProps = _.splitProps(getTfootTdProps(finalState, undefined, undefined, this))
|
||||
const columnProps = _.splitProps(column.getProps(finalState, undefined, column, this))
|
||||
const columnFooterProps = _.splitProps(column.getFooterProps(finalState, undefined, column, this))
|
||||
|
||||
const classes = [
|
||||
tFootTdProps.className,
|
||||
column.className,
|
||||
columnProps.className,
|
||||
columnFooterProps.className
|
||||
]
|
||||
|
||||
const styles = {
|
||||
...tFootTdProps.style,
|
||||
...column.style,
|
||||
...columnProps.style,
|
||||
...columnFooterProps.style
|
||||
}
|
||||
|
||||
if (!column.footer && column.pivotColumns) {
|
||||
return column.pivotColumns.map(makeColumnFooter)
|
||||
}
|
||||
|
||||
return (
|
||||
<TdComponent
|
||||
key={i + '-' + column.id}
|
||||
className={classnames(
|
||||
classes,
|
||||
!show && 'hidden'
|
||||
)}
|
||||
style={{
|
||||
...styles,
|
||||
flex: `${width} 0 auto`,
|
||||
width: `${width}px`,
|
||||
maxWidth: `${maxWidth}px`
|
||||
}}
|
||||
{...columnProps.rest}
|
||||
{...tFootTdProps.rest}
|
||||
{...columnFooterProps.rest}
|
||||
>
|
||||
{_.normalizeComponent(column.footer, {
|
||||
data: sortedData,
|
||||
column: column
|
||||
})}
|
||||
</TdComponent>
|
||||
)
|
||||
}
|
||||
|
||||
const rootProps = _.splitProps(getProps(finalState, undefined, undefined, this))
|
||||
const tableProps = _.splitProps(getTableProps(finalState, undefined, undefined, this))
|
||||
const tBodyProps = _.splitProps(getTbodyProps(finalState, undefined, undefined, this))
|
||||
@@ -848,7 +762,10 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
|
||||
{...rootProps.rest}
|
||||
>
|
||||
<TableComponent
|
||||
className={classnames(tableProps.className)}
|
||||
className={classnames(
|
||||
tableProps.className,
|
||||
currentlyResizing ? 'rt-resizing' : ''
|
||||
)}
|
||||
style={tableProps.style}
|
||||
{...tableProps.rest}
|
||||
>
|
||||
|
||||
@@ -65,6 +65,31 @@ $expandSize = 7px
|
||||
overflow: hidden
|
||||
text-overflow: ellipsis
|
||||
|
||||
.rt-header-pivot
|
||||
border-right-color: #f7f7f7
|
||||
|
||||
.rt-header-pivot:after, .rt-header-pivot:before
|
||||
left: 100%
|
||||
top: 50%
|
||||
border: solid transparent
|
||||
content: " "
|
||||
height: 0
|
||||
width: 0
|
||||
position: absolute
|
||||
pointer-events: none
|
||||
|
||||
.rt-header-pivot:after
|
||||
border-color: rgba(255, 255, 255, 0)
|
||||
border-left-color: #FFF
|
||||
border-width: 8px
|
||||
margin-top: -8px
|
||||
|
||||
.rt-header-pivot:before
|
||||
border-color: rgba(102, 102, 102, 0)
|
||||
border-left-color: #f7f7f7
|
||||
border-width: 10px
|
||||
margin-top: -10px
|
||||
|
||||
.rt-tbody
|
||||
display: flex
|
||||
flex-direction: column
|
||||
@@ -287,3 +312,10 @@ $expandSize = 7px
|
||||
border-color: #999 transparent transparent
|
||||
border-style: solid
|
||||
border-width: 5px 5px 2.5px
|
||||
|
||||
.rt-resizing
|
||||
.rt-th
|
||||
.rt-td
|
||||
transition: none!important
|
||||
cursor: col-resize
|
||||
user-select none
|
||||
+22
-18
@@ -1,3 +1,4 @@
|
||||
import React from 'react'
|
||||
import _ from './utils'
|
||||
|
||||
export default Base => class extends Base {
|
||||
@@ -63,8 +64,12 @@ export default Base => class extends Base {
|
||||
if (pivotBy.length && column.expander) {
|
||||
dcol = {
|
||||
...this.props.column,
|
||||
render: this.props.ExpanderComponent,
|
||||
filterRender: this.props.ExpanderComponent,
|
||||
render: (props) => (
|
||||
<div>
|
||||
<this.props.ExpanderComponent {...props} />
|
||||
<this.props.PivotValueComponent {...props} />
|
||||
</div>
|
||||
),
|
||||
...this.props.pivotDefaults,
|
||||
...column
|
||||
}
|
||||
@@ -294,11 +299,16 @@ export default Base => class extends Base {
|
||||
(row) => {
|
||||
let column
|
||||
|
||||
if (nextFilter.pivotId) {
|
||||
const parentColumn = allVisibleColumns.find(x => x.id === nextFilter.id)
|
||||
column = parentColumn.pivotColumns.find(x => x.id === nextFilter.pivotId)
|
||||
} else {
|
||||
column = allVisibleColumns.find(x => x.id === nextFilter.id)
|
||||
column = allVisibleColumns.find(x => x.id === nextFilter.id || (x.pivotColumns && x.pivotColumns.some(y => y.id === nextFilter.id)))
|
||||
|
||||
// Could possibly be in pivotColumns
|
||||
if (column.id !== nextFilter.id && column.pivotColumns) {
|
||||
column = column.pivotColumns.find(x => x.id === nextFilter.id)
|
||||
}
|
||||
|
||||
// Don't filter hidden columns
|
||||
if (!column) {
|
||||
return
|
||||
}
|
||||
|
||||
const filterMethod = column.filterMethod || defaultFilterMethod
|
||||
@@ -494,12 +504,12 @@ export default Base => class extends Base {
|
||||
})
|
||||
}
|
||||
|
||||
filterColumn (column, value, pivotColumn) {
|
||||
filterColumn (column, value) {
|
||||
const {filtering} = this.getResolvedState()
|
||||
const {onFilteringChange} = this.props
|
||||
|
||||
if (onFilteringChange) {
|
||||
return onFilteringChange(column, value, pivotColumn)
|
||||
return onFilteringChange(column, value)
|
||||
}
|
||||
|
||||
// Remove old filter first if it exists
|
||||
@@ -507,19 +517,12 @@ export default Base => class extends Base {
|
||||
if (x.id !== column.id) {
|
||||
return true
|
||||
}
|
||||
if (x.pivotId) {
|
||||
if (pivotColumn) {
|
||||
return x.pivotId !== pivotColumn.id
|
||||
}
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
if (value !== '') {
|
||||
newFiltering.push({
|
||||
id: column.id,
|
||||
value: value,
|
||||
pivotId: pivotColumn ? pivotColumn.id : undefined
|
||||
value: value
|
||||
})
|
||||
}
|
||||
|
||||
@@ -585,7 +588,8 @@ export default Base => class extends Base {
|
||||
// event resize will not sort the column.
|
||||
if (!isTouch) {
|
||||
this.setStateWithData({
|
||||
skipNextSort: true
|
||||
skipNextSort: true,
|
||||
currentlyResizing: false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+92
-82
@@ -43,14 +43,11 @@ export default () => {
|
||||
hideFilter: true
|
||||
}]
|
||||
}, {
|
||||
header: () => <strong>First ⇢ Last Name Pivot</strong>,
|
||||
header: () => <strong>Overriden Pivot Column Header Group</strong>,
|
||||
expander: true,
|
||||
minWidth: 200,
|
||||
render: ({isExpanded, ...rest}) => (
|
||||
isExpanded ? <span> ➘ </span> : <span> ➙ </span>
|
||||
),
|
||||
filterRender: ({key}) => <span key={key}> ⥱ </span>,
|
||||
footer: () => <div style={{textAlign: 'center'}}><strong>Pivot Footer</strong></div>
|
||||
pivotRender: ({value}) => <span style={{color: 'darkred'}}>{value}</span>,
|
||||
footer: () => <div style={{textAlign: 'center'}}><strong>Overriden Pivot Column Footer</strong></div>
|
||||
}]
|
||||
|
||||
return (
|
||||
@@ -62,7 +59,15 @@ export default () => {
|
||||
defaultPageSize={10}
|
||||
className='-striped -highlight'
|
||||
pivotBy={['firstName', 'lastName']}
|
||||
defaultSorting={[{id: 'firstName', desc: false}, {id: 'lastName', desc: true}]}
|
||||
collapseOnSortingChange={false}
|
||||
showFilters={true}
|
||||
ExpanderComponent={({isExpanded, ...rest}) => (
|
||||
isExpanded ? <span> ➘ </span> : <span> ➙ </span>
|
||||
)}
|
||||
PivotValueComponent={ ({subRows, value}) => (
|
||||
<span><span style={{color: 'darkred'}}>{value}</span> {subRows && `(${subRows.length} Last Names)`}</span>
|
||||
)}
|
||||
SubComponent={(row) => {
|
||||
return (
|
||||
<div style={{padding: '20px'}}>
|
||||
@@ -71,7 +76,7 @@ export default () => {
|
||||
<br />
|
||||
<ReactTable
|
||||
data={data}
|
||||
columns={columns.filter(x=>!x.expander)}
|
||||
columns={columns.filter(x => !x.expander)}
|
||||
defaultPageSize={3}
|
||||
showPagination={false}
|
||||
SubComponent={(row) => {
|
||||
@@ -99,85 +104,90 @@ export default () => {
|
||||
|
||||
function getCode () {
|
||||
return `
|
||||
const columns = [{
|
||||
header: 'Name',
|
||||
columns: [{
|
||||
header: 'First Name',
|
||||
accessor: 'firstName'
|
||||
const columns = [{
|
||||
header: 'Name',
|
||||
columns: [{
|
||||
header: 'First Name',
|
||||
accessor: 'firstName'
|
||||
}, {
|
||||
header: 'Last Name',
|
||||
id: 'lastName',
|
||||
accessor: d => d.lastName
|
||||
}]
|
||||
}, {
|
||||
header: 'Last Name',
|
||||
id: 'lastName',
|
||||
accessor: d => d.lastName
|
||||
}]
|
||||
}, {
|
||||
header: 'Info',
|
||||
columns: [{
|
||||
header: 'Age',
|
||||
accessor: 'age',
|
||||
aggregate: vals => {
|
||||
return _.round(_.mean(vals))
|
||||
},
|
||||
render: row => {
|
||||
return <span>{row.aggregated ? \`\${row.value} (avg)\` : row.value}</span>
|
||||
}
|
||||
header: 'Info',
|
||||
columns: [{
|
||||
header: 'Age',
|
||||
accessor: 'age',
|
||||
aggregate: vals => {
|
||||
return _.round(_.mean(vals))
|
||||
},
|
||||
render: row => {
|
||||
return <span>{row.aggregated ? \`\${row.value} (avg)\` : row.value}</span>
|
||||
}
|
||||
}, {
|
||||
header: 'Visits',
|
||||
accessor: 'visits',
|
||||
aggregate: vals => _.sum(vals),
|
||||
hideFilter: true
|
||||
}]
|
||||
}, {
|
||||
header: 'Visits',
|
||||
accessor: 'visits',
|
||||
aggregate: vals => _.sum(vals),
|
||||
hideFilter: true
|
||||
header: () => <strong>Overriden Pivot Column Header Group</strong>,
|
||||
expander: true,
|
||||
minWidth: 200,
|
||||
pivotRender: ({value}) => <span style={{color: 'darkred'}}>{value}</span>,
|
||||
footer: () => <div style={{textAlign: 'center'}}><strong>Overriden Pivot Column Footer</strong></div>
|
||||
}]
|
||||
}, {
|
||||
header: () => <strong>First ⇢ Last Name Pivot</strong>,
|
||||
expander: true,
|
||||
minWidth: 200,
|
||||
render: ({isExpanded, ...rest}) => (
|
||||
isExpanded ? <span> ➘ </span> : <span> ➙ </span>
|
||||
),
|
||||
filterRender: ({key}) => <span key={key}> ⥱ </span>,
|
||||
footer: () => <div style={{textAlign: 'center'}}><strong>Pivot Footer</strong></div>
|
||||
}]
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className='table-wrap'>
|
||||
<ReactTable
|
||||
data={data}
|
||||
columns={columns}
|
||||
defaultPageSize={10}
|
||||
className='-striped -highlight'
|
||||
pivotBy={['firstName', 'lastName']}
|
||||
showFilters={true}
|
||||
SubComponent={(row) => {
|
||||
return (
|
||||
<div style={{padding: '20px'}}>
|
||||
<em>You can put any component you want here, even another React Table!</em>
|
||||
<br />
|
||||
<br />
|
||||
<ReactTable
|
||||
data={data}
|
||||
columns={columns.filter(x=>!x.expander)}
|
||||
defaultPageSize={3}
|
||||
showPagination={false}
|
||||
SubComponent={(row) => {
|
||||
return (
|
||||
<div style={{padding: '20px'}}>
|
||||
<em>It even has access to the row data: </em>
|
||||
<CodeHighlight>{() => JSON.stringify(row, null, 2)}</CodeHighlight>
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
return (
|
||||
<div>
|
||||
<div className='table-wrap'>
|
||||
<ReactTable
|
||||
data={data}
|
||||
columns={columns}
|
||||
defaultPageSize={10}
|
||||
className='-striped -highlight'
|
||||
pivotBy={['firstName', 'lastName']}
|
||||
defaultSorting={[{id: 'firstName', desc: false}, {id: 'lastName', desc: true}]}
|
||||
collapseOnSortingChange={false}
|
||||
showFilters={true}
|
||||
ExpanderComponent={({isExpanded, ...rest}) => (
|
||||
isExpanded ? <span> ➘ </span> : <span> ➙ </span>
|
||||
)}
|
||||
PivotValueComponent={ ({subRows, value}) => (
|
||||
<span><span style={{color: 'darkred'}}>{value}</span> {subRows && \`(\${subRows.length} Last Names)\`}</span>
|
||||
)}
|
||||
SubComponent={(row) => {
|
||||
return (
|
||||
<div style={{padding: '20px'}}>
|
||||
<em>You can put any component you want here, even another React Table!</em>
|
||||
<br />
|
||||
<br />
|
||||
<ReactTable
|
||||
data={data}
|
||||
columns={columns.filter(x => !x.expander)}
|
||||
defaultPageSize={3}
|
||||
showPagination={false}
|
||||
SubComponent={(row) => {
|
||||
return (
|
||||
<div style={{padding: '20px'}}>
|
||||
<em>It even has access to the row data: </em>
|
||||
<CodeHighlight>{() => JSON.stringify(row, null, 2)}</CodeHighlight>
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div style={{textAlign: 'center'}}>
|
||||
<br />
|
||||
<em>Tip: Hold shift when sorting to multi-sort!</em>
|
||||
</div>
|
||||
<CodeHighlight>{() => getCode()}</CodeHighlight>
|
||||
</div>
|
||||
<div style={{textAlign: 'center'}}>
|
||||
<br />
|
||||
<em>Tip: Hold shift when sorting to multi-sort!</em>
|
||||
</div>
|
||||
<CodeHighlight>{() => getCode()}</CodeHighlight>
|
||||
</div>
|
||||
)
|
||||
)
|
||||
`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user