diff --git a/example/ManifestWrapper.js b/example/ManifestWrapper.js index 030b259..98e3436 100644 --- a/example/ManifestWrapper.js +++ b/example/ManifestWrapper.js @@ -9,7 +9,8 @@ const definition = [{ id: 'date', label: 'Date', sortable: true, - cellComponent: CellEpochDate + cellComponent: CellEpochDate, + sort: 1 }, { id: 'firstName', label: 'First Name', @@ -17,7 +18,8 @@ const definition = [{ }, { id: 'lastName', label: 'Last Name', - sortable: true + sortable: true, + sort: 2 }, { id: 'age', label: 'Age', diff --git a/src/containers/Headers.js b/src/containers/Headers.js index 23ab1df..4996fe0 100644 --- a/src/containers/Headers.js +++ b/src/containers/Headers.js @@ -1,6 +1,6 @@ import { bindActionCreators } from 'redux' import { connect } from 'react-redux' -import { compose, withHandlers } from 'recompose' +import { compose, withHandlers, lifecycle } from 'recompose' import * as actions from '../actions' import Headers from '../components/Headers' @@ -21,18 +21,47 @@ const mapDispatchToProps = dispatch => bindActionCreators({ const hasClass = (className, classString) => classString.match(className) !== null -const updateFilterSort = (id, isAsc, sorts) => [{id, isAsc}] +const updateFilterSort = (id, isAsc) => [{id, isAsc}] const handlers = { updateSort: props => event => { const isAsc = !hasClass('sorted-asc', event.target.className) const id = event.target.getAttribute('data-id') - props.refreshData(props.name, {...props.filter, sorts: updateFilterSort(id, isAsc, props.filter.sorts)}) + props.refreshData(props.name, {...props.filter, sorts: updateFilterSort(id, isAsc)}) + } +} + +const sort = props => { + // multi column sort support not implemented + // lowest sort integer column sorts asc + let sortRank + let sortId + + const filterSort = def => { + if (!sortRank && def.sort) { + sortRank = def.sort + sortId = def.id + } + if (def.sort && def.sort < sortRank) { + sortId = def.id + } + } + + props.definition.filter(filterSort) + if (sortId) { + props.refreshData(props.name, {...props.filter, sorts: updateFilterSort(sortId, true)}) + } +} + +const lifecycleHandlers = { + componentWillMount () { + sort(this.props) } } const enhance = compose( connect(mapStateToProps, mapDispatchToProps), + lifecycle(lifecycleHandlers), withHandlers(handlers) )