Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control the selection and sort #22

Merged
merged 31 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a200d8a
add setSelection to tableControl + add onOrderByChange and onSelectio…
severo Jan 8, 2025
bd09f5f
publish two components: HighTable and ControlledHighTable
severo Jan 9, 2025
eb5c85a
add missing export
severo Jan 9, 2025
960ae90
add tests
severo Jan 9, 2025
f177d37
replace selection in state by onSelectionChange for HighTable
severo Jan 10, 2025
6aa002d
format
severo Jan 16, 2025
db88dc9
apply React logic to HighTable selection
severo Jan 17, 2025
5d3b876
apply the same logic for orderBy / onOrderByChange
severo Jan 17, 2025
65e7911
remove orderBy and selection from the shared state
severo Jan 17, 2025
1654458
move state to ControlledHighTable
severo Jan 17, 2025
fcee341
remove intermediate ControlledHighTable
severo Jan 17, 2025
cc1bc00
add tests and fix bugs
severo Jan 17, 2025
7396902
remove useEffect and avoid calling onSelectionAndAnchorChange on moun…
severo Jan 20, 2025
6aea297
rename selectionAndAnchor to selection for simplicity
severo Jan 20, 2025
acb21f3
remove outdated comment
severo Jan 20, 2025
27252d2
add role to help accessibility and selectors in tests
severo Jan 20, 2025
95b67b5
add tests
severo Jan 20, 2025
50bc65b
add tests for mode 3
severo Jan 20, 2025
a4c501b
add tests for mode 4
severo Jan 20, 2025
063babe
add tests to show that selected prop cannot be defined/undefined afte…
severo Jan 20, 2025
312cec8
extract common code to useControlleableState custom hook
severo Jan 20, 2025
315e238
rename the hook useInputState, and add tests
severo Jan 20, 2025
4544727
update text in test
severo Jan 20, 2025
0392bf3
add README + default property to OrderBy
severo Jan 20, 2025
ae9da2a
fix css
severo Jan 21, 2025
2abb7d2
fix corner checkbox
severo Jan 21, 2025
73b3708
remove unneeded sort.ts file
severo Jan 22, 2025
2da4a38
replace fireEvent with userEvent where possible
severo Jan 22, 2025
137f365
fix OrderBy import/export
severo Jan 22, 2025
28a4256
hoist data and otherData
severo Jan 22, 2025
29ccfde
formatting
severo Jan 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ interface TableProps {
focus?: boolean // focus table on mount? (default true)
onDoubleClickCell?: (col: number, row: number) => void // double-click handler
onError?: (error: Error) => void // error handler
orderBy?: OrderBy; // order by column (if defined, the component order is controlled by the parent)
severo marked this conversation as resolved.
Show resolved Hide resolved
onOrderByChange?: (orderBy: OrderBy) => void; // orderBy change handler
selection?: Selection; // selection state (if defined, the component selection is controlled by the parent)
onSelectionChange?: (selection: Selection) => void; // selection change handler
}
```

Expand All @@ -95,6 +99,27 @@ interface DataFrame {
}
```

OrderBy is defined as:

```typescript
interface OrderBy {
column: string // column name
direction?: "ascending" // sort direction - only ascending is supported
}
```

Selection is defined as:

```typescript
interface Selection {
ranges: Array<{
start: number // inclusive lower limit, positive integer
end: number // exclusive upper limit, positive integer, strictly greater than start (no zero-length ranges).
}>; // the rows selection is an array of row index ranges (0-based). The values are indexes of the virtual table (sorted rows), and thus depend on the order.
anchor?: number // anchor row used as a reference for shift+click selection. It's a virtual table index (sorted), and thus depends on the order.
}
```

## Sortable DataFrame

If your data source supports sorting, set the sortable property to true in your DataFrame object. When sorting is enabled, the rows function will receive an additional orderBy parameter, which represents the column name to sort by.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "12.1.2",
"@testing-library/react": "16.1.0",
"@testing-library/user-event": "14.5.2",
"@types/node": "22.10.5",
"@types/react": "18.3.18",
"@types/react-dom": "18.3.1",
Expand Down
35 changes: 23 additions & 12 deletions src/HighTable.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,27 @@
.table th:first-child input {
display: none;
}
.selectable th:first-child:hover span, .selectable tr.selected th:first-child span {
/* for selected rows, replace row numbers with checkboxes and highlight the rows */
tr.selected th:first-child span {
display: none;
}
.selectable th:first-child:hover input, .selectable tr.selected th:first-child input {
tr.selected th:first-child input {
display: inline;
cursor: pointer;
}
.selectable tr.selected {
tr.selected {
background-color: #fbf7bf;
}
.selectable tr.selected th:first-child {
tr.selected th:first-child {
background-color: #f1edbb;
}
/* if selectable, show checkboxes on hover (and focus) */
.selectable th:first-child:hover span, .selectable th:first-child:focus span {
display: none;
}
.selectable th:first-child:hover input, .selectable th:first-child:focus input {
display: inline;
cursor: pointer;
}

/* cells */
.table th,
Expand Down Expand Up @@ -226,21 +234,24 @@
display: flex;
justify-content: center;
}
.selectable .table-corner {
background: #e4e4e6;
cursor: pointer;
}
/* replace corner with checkbox if selection is enabled (read-only or not) */
.table-corner input {
display: none;
}
.selectable .table-corner span {
.table-corner.show-corner-selection {
background: #e4e4e6;
}
.table-corner.show-corner-selection span {
display: none;
}
.selectable .table-corner input {
.table-corner.show-corner-selection input {
display: inline;
cursor: pointer;
text-align: center;
}
/* if selectable, show pointer cursor on checkbox */
.selectable .table-corner, .selectable .table-corner input {
cursor: pointer;
}

/* mock row numbers */
.mock-row-label {
Expand Down
Loading
Loading