react-virtualized
Version:
React components for efficiently rendering large, scrollable lists and tabular data
68 lines (58 loc) • 2.27 kB
JavaScript
export default function createMultiSort(sortCallback) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
defaultSortBy = _ref.defaultSortBy,
_ref$defaultSortDirec = _ref.defaultSortDirection,
defaultSortDirection = _ref$defaultSortDirec === void 0 ? {} : _ref$defaultSortDirec;
if (!sortCallback) {
throw Error("Required parameter \"sortCallback\" not specified");
}
var sortBy = defaultSortBy || [];
var sortDirection = {};
sortBy.forEach(function (dataKey) {
sortDirection[dataKey] = defaultSortDirection[dataKey] !== undefined ? defaultSortDirection[dataKey] : 'ASC';
});
function sort(_ref2) {
var defaultSortDirection = _ref2.defaultSortDirection,
event = _ref2.event,
dataKey = _ref2.sortBy;
if (event.shiftKey) {
// Shift + click appends a column to existing criteria
if (sortDirection[dataKey] !== undefined) {
sortDirection[dataKey] = sortDirection[dataKey] === 'ASC' ? 'DESC' : 'ASC';
} else {
sortDirection[dataKey] = defaultSortDirection;
sortBy.push(dataKey);
}
} else if (event.ctrlKey || event.metaKey) {
// Control + click removes column from sort (if pressent)
var index = sortBy.indexOf(dataKey);
if (index >= 0) {
sortBy.splice(index, 1);
delete sortDirection[dataKey];
}
} else {
// Clear sortBy array of all non-selected keys
sortBy.length = 0;
sortBy.push(dataKey); // Clear sortDirection object of all non-selected keys
var sortDirectionKeys = Object.keys(sortDirection);
sortDirectionKeys.forEach(function (key) {
if (key !== dataKey) delete sortDirection[key];
}); // If key is already selected, reverse sort direction.
// Else, set sort direction to default direction.
if (sortDirection[dataKey] !== undefined) {
sortDirection[dataKey] = sortDirection[dataKey] === 'ASC' ? 'DESC' : 'ASC';
} else {
sortDirection[dataKey] = defaultSortDirection;
}
} // Notify application code
sortCallback({
sortBy: sortBy,
sortDirection: sortDirection
});
}
return {
sort: sort,
sortBy: sortBy,
sortDirection: sortDirection
};
}