Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default sort support #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions example/ManifestWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const definition = [{
id: 'date',
label: 'Date',
sortable: true,
cellComponent: CellEpochDate
cellComponent: CellEpochDate,
sort: 1
}, {
id: 'firstName',
label: 'First Name',
sortable: true
}, {
id: 'lastName',
label: 'Last Name',
sortable: true
sortable: true,
sort: 2
}, {
id: 'age',
label: 'Age',
Expand Down
35 changes: 32 additions & 3 deletions src/containers/Headers.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user sort here instead:

const sort = (a, b) => {
if (a.sort < b.sort) return a
if (a.sort > b.sort) return b
return 0
}
const sorted = [...props.definition]
sorted.sort(sort)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take another look into this, but I initially did a sort similar to this and it resorted the columns as opposed to the data within the columns :/

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be triggering a new refresh for this, this logic should instead just update the initial state

withHandlers(handlers)
)

Expand Down