How to implement 3-state sorting? #326
-
Is your feature request related to a problem? Please describe. They both remove sorting when clicked 3 times. Describe the solution you'd like |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @nitedani, thanks for the issue! Here's my train of thoughts: in a real-life application the concept of "unsorted data" doesn't make much sense. This, in my opinion, should reflect in either using or not using sorting, from a UX/UI perspective. That's why I went to using 2-state sorting instead of 3-state. I'm open to discussing this, of course, but I wanted to clarify that this was a deliberate choice. |
Beta Was this translation helpful? Give feedback.
-
Even if it is not supported as part of the library, it can be implemented relatively easily in user-land by wrapping the setter. When the sort status would change from const useSortOrder = () => {
const [order, setOrder] = useState<DataTableSortStatus>({ columnAccessor: '', direction: 'asc'});
const specialSetOrder = useCallback((state: DataTableSortStatus) => {
setOrder(a => (a.columnAccessor === state.columnAccessor && a.direction === 'desc' && state.direction === 'asc')
// Set "columnAccessor" to a value that is unlikely to be used.
? { columnAccessor: '', direction: 'asc' }
: state
);
}, [setOrder]);
return [order, specialSetOrder] as const;
} Which would then be used in a way similar to the const [order, setOrder] = useSortOrder();
return <DataTable
records={[/*…*/]}
columns={[/*…*/]}
sortStatus={order}
onSortStatusChange={setOrder}
/> The option is there if you need it; the usefulness might however be a tad questionable. I suppose it could be useful if the default sorting order is based on a column that is hidden as part of a media query… It could also be relevant for multi-level sorting, though that would require a bigger rework. |
Beta Was this translation helpful? Give feedback.
Even if it is not supported as part of the library, it can be implemented relatively easily in user-land by wrapping the setter. When the sort status would change from
'desc'
to'asc'
you'd instead set a different (non-existent?) column instead. This logic could be placed in a hook like the following: