Skip to content

feat(core/sortable-table): add click-to-sort table columns with runtime#5292

Draft
marcoscaceres wants to merge 4 commits into
mainfrom
feat/3483-sortable-tables
Draft

feat(core/sortable-table): add click-to-sort table columns with runtime#5292
marcoscaceres wants to merge 4 commits into
mainfrom
feat/3483-sortable-tables

Conversation

@marcoscaceres
Copy link
Copy Markdown
Contributor

Closes #3483

Adds a class="sortable" opt-in for tables. Each <th> gets a sort button (⇕/▲/▼) with aria-sort attributes. Clicking cycles through ascending, descending, and original order. Numeric columns are detected automatically and sorted by value, not lexicographically. The sort runtime is injected as a <script> so it persists in exported/saved specs.

Tables with class="sortable" gain sort buttons in each <th>. Click cycles
through ascending, descending, and back to the original row order.

Improvements over sidvishnoi's PR #3812:
- Three-state toggle (ascending → descending → reset to original order)
- Proper aria-sort attribute on <th> (WCAG 1.3.1)
- Numeric sort when all cells in a column parse as numbers
- Correct script id (respec-sortable-table, not a copy-paste of dfn-panel)
- Clicking a new column resets sort state on all other columns
- 15 Jasmine tests covering bootstrap, aria, text sort, numeric sort, and reset

Based on sidvishnoi's work in #3812.
Closes #3483
- Use ASC/DESC constants instead of string literals (a11y safety)
- Replace nested ternary with NEXT_DIR lookup
- Unify sort direction toggle via dir multiplier
- Decide column sort type (numeric vs text) once per sort, not per pair
- Stamp original row indices before both sort and reset paths
- Replace append(...rows) spread with loop (avoids argument-count limit)
- Remove redundant getThLabel function (inlined)
- Fix test timing: flush iframe microtasks after makeRSDoc so the runtime's
  document.respec.ready.then() callback completes before button assertions
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in sortable table feature to ReSpec’s core pipeline by injecting CSS plus a small runtime script when table.sortable is present, enabling click-to-sort on header columns with aria-sort updates and numeric-aware sorting.

Changes:

  • Introduces core/sortable-table plugin to inject sortable-table CSS and a persistent runtime script.
  • Adds the runtime implementation for cycling sort states (ascending/descending/reset) and numeric vs text sorting.
  • Adds a dedicated spec test suite for bootstrap behavior, button/ARIA behavior, and sorting outcomes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/spec/core/sortable-table-spec.js Adds coverage for runtime/script injection, button creation, aria-sort, and text/numeric sorting behavior.
src/styles/sortable-table.css.js Adds styling for sortable header buttons and aria-sort visual states.
src/core/sortable-table.runtime.js Implements the exported-spec runtime for click-to-sort behavior and DOM updates.
src/core/sortable-table.js Implements the ReSpec plugin that injects the CSS and inlined runtime script only when needed.
profiles/w3c.js Enables the module in the W3C profile plugin list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/sortable-table.runtime.js
Comment thread src/core/sortable-table.runtime.js
Comment thread src/core/sortable-table.runtime.js
Comment thread src/core/sortable-table.runtime.js
parseFloat('10px') returns 10, causing mixed columns to sort
numerically. Number('10px') returns NaN, correctly falling back
to lexicographic sort. Also add radix 10 to parseInt calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make table rows sortable

2 participants