Refactor sort order

pull/1679/head
Andrew Watkins 2017-06-29 11:34:25 -07:00
parent 646aba5f89
commit fcb872a4c8
2 changed files with 7 additions and 17 deletions

View File

@ -22,7 +22,7 @@ export default class Dygraph extends Component {
}, },
sortType: '', sortType: '',
filterText: '', filterText: '',
legendOrder: 'asc', isAscending: true,
} }
this.getTimeSeries = ::this.getTimeSeries this.getTimeSeries = ::this.getTimeSeries
@ -45,17 +45,7 @@ export default class Dygraph extends Component {
} }
handleSortLegend(sortType) { handleSortLegend(sortType) {
if (this.state.legendOrder === 'asc') { this.setState({sortType, isAscending: !this.state.isAscending})
return this.setState({
sortType,
legendOrder: 'desc',
})
}
this.setState({
sortType,
legendOrder: 'asc',
})
} }
handleLegendInputChange(e) { handleLegendInputChange(e) {
@ -280,7 +270,7 @@ export default class Dygraph extends Component {
} }
render() { render() {
const {legend, filterText, legendOrder, sortType, isHidden} = this.state const {legend, filterText, isAscending, sortType, isHidden} = this.state
return ( return (
<div className="dygraph-child"> <div className="dygraph-child">
@ -289,7 +279,7 @@ export default class Dygraph extends Component {
onSort={this.handleSortLegend} onSort={this.handleSortLegend}
onInputChange={this.handleLegendInputChange} onInputChange={this.handleLegendInputChange}
filterText={filterText} filterText={filterText}
sortOrder={legendOrder} isAscending={isAscending}
sortType={sortType} sortType={sortType}
legendRef={el => this.legendRef = el} legendRef={el => this.legendRef = el}
isHidden={isHidden} isHidden={isHidden}

View File

@ -8,14 +8,14 @@ const DygraphLegend = ({
legendRef, legendRef,
filterText, filterText,
onInputChange, onInputChange,
sortOrder, isAscending,
sortType, sortType,
}) => { }) => {
const sorted = _.sortBy( const sorted = _.sortBy(
series, series,
({y, label}) => (sortType === 'numeric' ? y : label) ({y, label}) => (sortType === 'numeric' ? y : label)
) )
const ordered = sortOrder === 'desc' ? sorted.reverse() : sorted const ordered = isAscending ? sorted : sorted.reverse()
const filtered = ordered.filter(s => s.label.match(filterText)) const filtered = ordered.filter(s => s.label.match(filterText))
const hidden = isHidden ? 'hidden' : '' const hidden = isHidden ? 'hidden' : ''
@ -87,7 +87,7 @@ DygraphLegend.propTypes = {
onSort: func.isRequired, onSort: func.isRequired,
onInputChange: func.isRequired, onInputChange: func.isRequired,
filterText: string.isRequired, filterText: string.isRequired,
sortOrder: string.isRequired, isAscending: bool.isRequired,
sortType: string.isRequired, sortType: string.isRequired,
isHidden: bool.isRequired, isHidden: bool.isRequired,
legendRef: func.isRequired, legendRef: func.isRequired,