James McCool commited on
Commit
e74f850
Β·
1 Parent(s): 9f63aed

Enhance DataTable sorting functionality to support three-state sorting (asc, desc, none) and update sort indicator logic for better user experience.

Browse files
Files changed (1) hide show
  1. src/components/DataTable.jsx +13 -5
src/components/DataTable.jsx CHANGED
@@ -29,9 +29,15 @@ const DataTable = () => {
29
  const sortData = (key) => {
30
  let direction = 'asc';
31
 
32
- // If clicking the same column, toggle direction
33
- if (sortConfig.key === key && sortConfig.direction === 'asc') {
34
- direction = 'desc';
 
 
 
 
 
 
35
  }
36
 
37
  setSortConfig({ key, direction });
@@ -39,7 +45,7 @@ const DataTable = () => {
39
 
40
  // Get sorted data
41
  const getSortedData = () => {
42
- if (!sortConfig.key) return data;
43
 
44
  return [...data].sort((a, b) => {
45
  let aValue = a[sortConfig.key];
@@ -64,7 +70,9 @@ const DataTable = () => {
64
  // Get sort indicator for column headers
65
  const getSortIndicator = (key) => {
66
  if (sortConfig.key !== key) return '↕️';
67
- return sortConfig.direction === 'asc' ? '↑' : '↓';
 
 
68
  };
69
 
70
  const fetchFromMongoDB = async () => {
 
29
  const sortData = (key) => {
30
  let direction = 'asc';
31
 
32
+ // Three-state sorting: asc β†’ desc β†’ none
33
+ if (sortConfig.key === key) {
34
+ if (sortConfig.direction === 'asc') {
35
+ direction = 'desc';
36
+ } else if (sortConfig.direction === 'desc') {
37
+ // Reset to no sorting
38
+ setSortConfig({ key: null, direction: 'none' });
39
+ return;
40
+ }
41
  }
42
 
43
  setSortConfig({ key, direction });
 
45
 
46
  // Get sorted data
47
  const getSortedData = () => {
48
+ if (!sortConfig.key || sortConfig.direction === 'none') return data;
49
 
50
  return [...data].sort((a, b) => {
51
  let aValue = a[sortConfig.key];
 
70
  // Get sort indicator for column headers
71
  const getSortIndicator = (key) => {
72
  if (sortConfig.key !== key) return '↕️';
73
+ if (sortConfig.direction === 'asc') return '↑';
74
+ if (sortConfig.direction === 'desc') return '↓';
75
+ return '↕️';
76
  };
77
 
78
  const fetchFromMongoDB = async () => {