@spaced-out/ui-design-system
Version:
Sense UI components library
78 lines (76 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useSortableEntries = useSortableEntries;
var React = _interopRequireWildcard(require("react"));
var _get = _interopRequireDefault(require("lodash/get"));
var _sortBy = _interopRequireDefault(require("lodash/sortBy"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function useSortableEntries(entries, idName, _ref) {
let {
defaultSortKey = 'id',
defaultSortDirection = 'original',
onSort,
enableInternalSorting = true
} = _ref;
const [sortKey, setSortKey] = React.useState(defaultSortKey);
const [sortDirection, setSortDirection] = React.useState(defaultSortDirection);
const getNextDirection = direction => {
switch (direction) {
case 'original':
return 'desc';
case 'asc':
return 'original';
case 'desc':
return 'asc';
default:
return 'original';
}
};
const advanceSortDirection = dir => {
const nextDirection = getNextDirection(dir);
// @ts-ignore - TS6133 - 'dir' is declared but its value is never read.
setSortDirection(dir => nextDirection);
return nextDirection;
};
const handleSortClick = React.useCallback(nextSortKey => {
let nextSortDirection;
if (nextSortKey === sortKey) {
nextSortDirection = advanceSortDirection(sortDirection);
} else {
setSortKey(nextSortKey);
nextSortDirection = 'desc';
}
// @ts-ignore - TS2345 - Argument of type 'string' is not assignable to parameter of type 'SetStateAction<SortDirection>'.
setSortDirection(nextSortDirection);
// @ts-ignore - TS2345 - Argument of type 'string' is not assignable to parameter of type 'SortDirection'.
onSort?.(nextSortKey, nextSortDirection);
}, [sortKey, sortDirection, entries]);
const sortedEntries = React.useMemo(() => {
if (!enableInternalSorting || sortDirection === 'original') {
return entries;
}
const caseInsensitiveSortFunction = entry => {
if (typeof entry[sortKey] === 'string') {
// @ts-ignore - TS2339 - Property 'toLowerCase' does not exist on type 'T[keyof T]'.
return entry[sortKey].toLowerCase();
}
return entry[sortKey];
};
const sortedAsc = (0, _sortBy.default)(entries, caseInsensitiveSortFunction);
return sortDirection === 'asc' ? sortedAsc : sortedAsc.reverse();
}, [sortDirection, sortKey, entries, enableInternalSorting]);
const sortedKeys = React.useMemo(() => sortedEntries.map(ent => (0, _get.default)(ent, idName)), [sortedEntries]);
return {
sortedEntries,
// @ts-ignore - TS2322 - Type 'T[keyof T][]' is not assignable to type 'keyof T[]'.
sortedKeys,
sortDirection,
// @ts-ignore - TS2322 - Type 'string | number | symbol' is not assignable to type 'string | undefined'.
sortKey,
// @ts-ignore - TS2322 - Type '(nextSortKey: string) => void' is not assignable to type '(sortKey: keyof T) => unknown'.
handleSortClick
};
}