#32Sortable Table
Overview
Build a sortable table where users can click on column headers to sort data in ascending or descending order.
Requirements
- Render a table with the provided user data.
- All columns except
idshould be rendered and sortable. - Clicking a column header sorts the rows by that column.
- Clicking the same column again toggles sort direction.
- Display a sort icon (
BsSortDown/BsSortDownAlt) in the active sorted column. - Icon should update when sort direction changes.
- Only the active column shows a sort icon.
- Each header button should have an
aria-labelthat updates when clicked, e.g., from"Sort by name in descending order"to"Sort by name in ascending order". - Alternate row background colors for better readability (
even:bg-neutral-200).
Notes
- The
joinedcolumn is a date string; convert toDatewhen sorting. - The
activecolumn is a boolean;falseshould appear beforetruein ascending order. - Use state hooks to manage the sorted data, the currently sorted column, and the sort direction.
- Ensure that icons and
aria-labelreflect the current sort state and intended direction. - The icon reflects the current sort state in the UI, while the aria-label indicates the sort direction that will be applied when the button is clicked.
Tests
- render the title
- renders the table headers correctly
- renders the correct number of rows
- sorts the table by a column when the header is clicked
- toggles sort direction when clicking the same column twice
- has alternating background colors for rows
- buttons have accessible aria-labels
- updates aria-label when clicking a column header
- updates icon when clicking a column header
- shows the sort icon only on the clicked column