diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3bf9304 --- /dev/null +++ b/CHANGELOG.md @@ -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. \ No newline at end of file diff --git a/README.md b/README.md index d78a86a..6c89f7d 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,6 @@ These are all of the available props (and their default values) for the main `` 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, diff --git a/src/defaultProps.js b/src/defaultProps.js index 7cf413c..b9a8f6a 100644 --- a/src/defaultProps.js +++ b/src/defaultProps.js @@ -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 ( -
- • -
- ) - }, + ExpanderComponent: ({isExpanded}) => ( +
+ • +
+ ), + PivotValueComponent: ({subRows, value}) => ( + {value} {subRows && `(${subRows.length})`} + ), PaginationComponent: Pagination, PreviousComponent: undefined, NextComponent: undefined, diff --git a/src/index.js b/src/index.js index 9143ce5..e7061e2 100644 --- a/src/index.js +++ b/src/index.js @@ -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 ( 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 ( - { - column.sortable && this.sortColumn(column.pivotColumns, e.shiftKey) - }} - {...rest} - > -
- {column.pivotColumns.map((pivotColumn, i) => { - return ( - - {_.normalizeComponent(pivotColumn.header, { - data: sortedData, - column: column - })} - {i < column.pivotColumns.length - 1 && ( - _.normalizeComponent(column.render, { - data: sortedData, - column: column - }) - )} - - ) - })} -
- {resizer} -
- ) - } + if (column.pivotColumns) { + return column.pivotColumns.map(makeHeader) } return ( filter.id === column.id && filter.pivotId === col.id) - pivotCols.push( - - {!col.hideFilter ? ( - _.normalizeComponent(col.filterRender, - { - col, - filter, - onFilterChange: (value) => (this.filterColumn(column, value, col)) - }, - defaultProps.column.filterRender - ) - ) : null} - - ) - if (i < column.pivotColumns.length - 1) { - pivotCols.push( - _.normalizeComponent(column.filterRender, - { - column, - filter, - key: col.id + '-' + i - }, - defaultProps.column.filterRender - )) - } - } - return ( - - {pivotCols} - - ) - } + if (!column.filterRender && column.pivotColumns) { + return column.pivotColumns.map(makeFilter) } const filter = filtering.find(filter => filter.id === column.id) return ( { + 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 ( {rowInfo.subRows ? ( - - {_.normalizeComponent(column.render, { - ...rowInfo, - value: rowInfo.rowValues[column.id], - isExpanded - }, rowInfo.rowValues[column.id])} - {column && column.pivotRender ? ( - - ) : {row[pivotValKey]} ({rowInfo.subRows.length})} - + _.normalizeComponent(column.render, { + ...rowInfo, + value: row[pivotValKey], + isExpanded + }, rowInfo.rowValues[column.id]) ) : SubComponent ? ( - - {_.normalizeComponent(column.render, { - ...rowInfo, - value: rowInfo.rowValues[column.id], - isExpanded - }, rowInfo.rowValues[column.id])} - + _.normalizeComponent(column.render, { + ...rowInfo, + value: rowInfo.rowValues[column.id], + isExpanded + }, rowInfo.rowValues[column.id]) ) : null} ) @@ -637,7 +552,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) { // Return regular cell return ( - {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 ( - -   - - ) - })} + {allVisibleColumns.map(makePadColumn)} ) } + 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 ( + +   + + ) + } + 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 ( - - {_.normalizeComponent(column.footer)} - - ) - } - } - - // Return regular cell - return ( - - {_.normalizeComponent(column.footer, { - data: sortedData, - column: column - })} - - ) - })} + {allVisibleColumns.map(makeColumnFooter)} ) } + 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 ( + + {_.normalizeComponent(column.footer, { + data: sortedData, + column: column + })} + + ) + } + 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} > diff --git a/src/index.styl b/src/index.styl index 6986b73..7bbb745 100644 --- a/src/index.styl +++ b/src/index.styl @@ -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 \ No newline at end of file diff --git a/src/methods.js b/src/methods.js index 96f7bb8..e60c7ea 100644 --- a/src/methods.js +++ b/src/methods.js @@ -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) => ( +
+ + +
+ ), ...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 }) } } diff --git a/stories/PivotingOptions.js b/stories/PivotingOptions.js index 97ebb36..83cae96 100644 --- a/stories/PivotingOptions.js +++ b/stories/PivotingOptions.js @@ -43,14 +43,11 @@ export default () => { hideFilter: true }] }, { - header: () => First ⇢ Last Name Pivot, + header: () => Overriden Pivot Column Header Group, expander: true, minWidth: 200, - render: ({isExpanded, ...rest}) => ( - isExpanded ? ➘ : ➙ - ), - filterRender: ({key}) => ⥱ , - footer: () =>
Pivot Footer
+ pivotRender: ({value}) => {value}, + footer: () =>
Overriden Pivot Column Footer
}] 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 ? ➘ : ➙ + )} + PivotValueComponent={ ({subRows, value}) => ( + {value} {subRows && `(${subRows.length} Last Names)`} + )} SubComponent={(row) => { return (
@@ -71,7 +76,7 @@ export default () => {
!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 {row.aggregated ? \`\${row.value} (avg)\` : row.value} - } + header: 'Info', + columns: [{ + header: 'Age', + accessor: 'age', + aggregate: vals => { + return _.round(_.mean(vals)) + }, + render: row => { + return {row.aggregated ? \`\${row.value} (avg)\` : row.value} + } + }, { + header: 'Visits', + accessor: 'visits', + aggregate: vals => _.sum(vals), + hideFilter: true + }] }, { - header: 'Visits', - accessor: 'visits', - aggregate: vals => _.sum(vals), - hideFilter: true + header: () => Overriden Pivot Column Header Group, + expander: true, + minWidth: 200, + pivotRender: ({value}) => {value}, + footer: () =>
Overriden Pivot Column Footer
}] -}, { - header: () => First ⇢ Last Name Pivot, - expander: true, - minWidth: 200, - render: ({isExpanded, ...rest}) => ( - isExpanded ? ➘ : ➙ - ), - filterRender: ({key}) => ⥱ , - footer: () =>
Pivot Footer
-}] -return ( -
-
- { - return ( -
- You can put any component you want here, even another React Table! -
-
- !x.expander)} - defaultPageSize={3} - showPagination={false} - SubComponent={(row) => { - return ( -
- It even has access to the row data: - {() => JSON.stringify(row, null, 2)} -
- ) - }} - /> -
- ) - }} - /> + return ( +
+
+ ( + isExpanded ? ➘ : ➙ + )} + PivotValueComponent={ ({subRows, value}) => ( + {value} {subRows && \`(\${subRows.length} Last Names)\`} + )} + SubComponent={(row) => { + return ( +
+ You can put any component you want here, even another React Table! +
+
+ !x.expander)} + defaultPageSize={3} + showPagination={false} + SubComponent={(row) => { + return ( +
+ It even has access to the row data: + {() => JSON.stringify(row, null, 2)} +
+ ) + }} + /> +
+ ) + }} + /> +
+
+
+ Tip: Hold shift when sorting to multi-sort! +
+ {() => getCode()}
-
-
- Tip: Hold shift when sorting to multi-sort! -
- {() => getCode()} -
-) + ) ` }