Skip to content

Commit

Permalink
fix: Sort presets by sort field, then by name
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Oct 6, 2019
1 parent 3f999c1 commit d64312d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/FilterPanel/FilterPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const FilterPanel = ({
// stats['categoryId'] &&
// stats['categoryId'].string.values.has(preset.id)
// )
.sort(presetCompare)
.map(preset => ({
value: preset.id,
label: preset.name
Expand Down Expand Up @@ -312,3 +313,26 @@ const useStyles = makeStyles(theme => ({
minWidth: 40
}
}))

// Sort presets by sort property and then by name, then filter only point presets
function presetCompare(a, b) {
if (typeof a.sort !== 'undefined' && typeof b.sort !== 'undefined') {
// If sort value is the same, then sort by name
if (a.sort === b.sort) return compareStrings(a.name, b.name)
// Lower sort numbers come before higher numbers
else return a.sort - b.sort
} else if (typeof a.sort !== 'undefined') {
// If a has a sort field but b doesn't, a comes first
return -1
} else if (typeof b.sort !== 'undefined') {
// if b has a sort field but a doesn't, b comes first
return 1
} else {
// if neither have sort defined, compare by name
return compareStrings(a.name, b.name)
}
}

function compareStrings(a = '', b = '') {
return a.toLowerCase().localeCompare(b.toLowerCase())
}
1 change: 0 additions & 1 deletion src/FilterPanel/FilterPanel.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const presets: Preset[] = [

export const withPresets = () => {
const [filter, setFilter] = React.useState(null)
console.log(filter)
return (
<FilterPanel filter={filter} onChangeFilter={setFilter} presets={presets} />
)
Expand Down

0 comments on commit d64312d

Please sign in to comment.