diff --git a/packages/react-bootstrap-table2/src/contexts/data-context.js b/packages/react-bootstrap-table2/src/contexts/data-context.js index 9a08976..5e34a84 100644 --- a/packages/react-bootstrap-table2/src/contexts/data-context.js +++ b/packages/react-bootstrap-table2/src/contexts/data-context.js @@ -16,11 +16,18 @@ export default () => { this.setState(() => ({ data: nextProps.data })); } + getData = (filterProps, sortProps) => { + if (sortProps) return sortProps.data; + else if (filterProps) return filterProps.data; + return this.props.data; + } + render() { return ( { this.props.children } diff --git a/packages/react-bootstrap-table2/src/contexts/index.js b/packages/react-bootstrap-table2/src/contexts/index.js index f01d7d3..37562f4 100644 --- a/packages/react-bootstrap-table2/src/contexts/index.js +++ b/packages/react-bootstrap-table2/src/contexts/index.js @@ -19,12 +19,20 @@ const withContext = (Base) => { constructor(props) { super(props); DataContext = createDataContext(props.data); - SelectionContext = createSelectionContext(dataOperator); - SortContext = createSortContext(dataOperator, this.isRemoteSort, this.handleSortChange); + + if (props.columns.filter(col => col.sort).length > 0) { + SortContext = createSortContext(dataOperator, this.isRemoteSort, this.handleSortChange); + } + + if (props.selectRow) { + SelectionContext = createSelectionContext(dataOperator); + } + if (props.cellEdit && props.cellEdit.createContext) { CellEditContext = props.cellEdit.createContext( _, dataOperator, this.isRemoteCellEdit, this.handleCellChange); } + if (props.filter) { FilterContext = props.filter.createContext( _, this.isRemoteFiltering, this.handleRemoteFilterChange); @@ -37,38 +45,72 @@ const withContext = (Base) => { } } - renderBase(baseProps) { - return (rootProps, cellEditProps, filterProps) => ( + renderBase() { + return ( + rootProps, + cellEditProps, + filterProps, + sortProps, + selectionProps + ) => ( + + ); + } + + renderWithSelectionCtx(base, baseProps) { + return ( + rootProps, + cellEditProps, + filterProps, + sortProps + ) => ( + + + { + selectionProps => base( + rootProps, + cellEditProps, + filterProps, + sortProps, + selectionProps + ) + } + + + ); + } + + renderWithSortCtx(base, baseProps) { + return ( + rootProps, + cellEditProps, + filterProps + ) => ( this.sortContext = n } defaultSorted={ this.props.defaultSorted } defaultSortDirection={ this.props.defaultSortDirection } - data={ filterProps ? filterProps.data : rootProps.data } + data={ rootProps.getData(filterProps) } > { - sortProps => ( - - - { - selectionProps => ( - - ) - } - - + sortProps => base( + rootProps, + cellEditProps, + filterProps, + sortProps ) } @@ -76,28 +118,35 @@ const withContext = (Base) => { ); } - renderWithFilter(base, baseProps) { - return (rootProps, cellEditprops) => ( + renderWithFilterCtx(base, baseProps) { + return ( + rootProps, + cellEditprops + ) => ( this.filterContext = n } - data={ rootProps.data } + data={ rootProps.getData() } > { - filterProps => base(rootProps, cellEditprops, filterProps) + filterProps => base( + rootProps, + cellEditprops, + filterProps + ) } ); } - renderWithCellEdit(base, baseProps) { + renderWithCellEditCtx(base, baseProps) { return rootProps => ( { @@ -112,14 +161,22 @@ const withContext = (Base) => { const { keyField, columns } = this.props; const baseProps = { keyField, columns }; - let base = this.renderBase(baseProps); + let base = this.renderBase(); + + if (SelectionContext) { + base = this.renderWithSelectionCtx(base, baseProps); + } + + if (SortContext) { + base = this.renderWithSortCtx(base, baseProps); + } if (FilterContext) { - base = this.renderWithFilter(base, baseProps); + base = this.renderWithFilterCtx(base, baseProps); } if (CellEditContext) { - base = this.renderWithCellEdit(base, baseProps); + base = this.renderWithCellEditCtx(base, baseProps); } return ( diff --git a/packages/react-bootstrap-table2/src/store/selection.js b/packages/react-bootstrap-table2/src/store/selection.js index 246c5f5..6e66121 100644 --- a/packages/react-bootstrap-table2/src/store/selection.js +++ b/packages/react-bootstrap-table2/src/store/selection.js @@ -1,7 +1,7 @@ import _ from '../utils'; import { getRowByRowId } from './rows'; -export const isSelectedAll = (data, selected) => data.length === selected.length; +export const isSelectedAll = (data, selected = []) => data.length === selected.length; export const isAnySelectedRow = (selected, skips = []) => { if (skips.length === 0) {