@equinor/fusion
Advanced tools
Comparing version
@@ -9,5 +9,5 @@ export declare type PropertyAccessorFunction<T> = (item: T) => string | null; | ||
direction: "desc" | "asc" | null; | ||
set: (sortBy: PropertyAccessorFunction<T> | keyof T | null, direction: "desc" | "asc" | null) => void; | ||
set: (newSortBy: PropertyAccessorFunction<T> | keyof T | null, newDirection: "desc" | "asc" | null) => void; | ||
reset: () => void; | ||
}; | ||
export default _default; |
@@ -19,3 +19,3 @@ import { useState, useEffect } from "react"; | ||
const comparer = new Intl.Collator().compare; | ||
return data.sort((a, b) => { | ||
return data.slice().sort((a, b) => { | ||
const aValue = getValue(a, sortBy); | ||
@@ -35,2 +35,11 @@ const bValue = getValue(b, sortBy); | ||
}; | ||
const cycleSortDirection = (direction) => { | ||
if (!direction) { | ||
return "asc"; | ||
} | ||
else if (direction === "asc") { | ||
return "desc"; | ||
} | ||
return null; | ||
}; | ||
export default (data, defaultSortBy = null, defaultDirection = null) => { | ||
@@ -47,5 +56,9 @@ const [sortBy, setSortBy] = useState(defaultSortBy); | ||
}; | ||
const set = (sortBy, direction) => { | ||
setSortBy(sortBy); | ||
setDirection(direction); | ||
const set = (newSortBy, newDirection) => { | ||
newDirection = | ||
newDirection || sortBy === newSortBy || !newSortBy | ||
? cycleSortDirection(direction) | ||
: cycleSortDirection(null); | ||
setSortBy(newSortBy); | ||
setDirection(newDirection); | ||
}; | ||
@@ -52,0 +65,0 @@ return { |
{ | ||
"name": "@equinor/fusion", | ||
"version": "0.1.78", | ||
"version": "0.1.79", | ||
"description": "Everything a Fusion app needs to communicate with the core", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
149836
0.26%3554
0.37%