-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
Update: I removed In edit: oops, I broke the tests, I'll update on Monday |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's generally heading in the right direction with SelectionAndAnchor
stuff.
I'm a little suspicious about exposing the state
and dispatch
as props which are meant to be InternalActions
but actually leak out.
Specifically orderBy
needs to be settable from outside the component, and I see in the demo that you do a dispatch
with an "internal" SET_ORDER
message. This works but feels weird. I think ideally there would be no state
and dispatch
exposed as props, and instead have orderBy
and setOrderBy
managed by the parent?
Alternatively there could be an ExternalState
type with both selectionAndAnchor
and also orderBy
?
I feel like the ideal world is that InternalState
is managed by an internal reducer that doesn't need to be exposed. We hoist only the state and setters needed as props to expose to the parent.
The thing I am worried about is losing the benefits of a reducer of doing predictable atomic state updates. For example: if we need to clear the selection when orderBy changes, does the parent component need to sync two useState
s? Or is there an InternalReducer in HighTable and also be an ExternalReducer in the parent component which receives ExternalAction
and updates the ExternalState
accordingly?
Re orderBy: indeed, I only did the change for selection, but not for order yet, so the current state is a weird mix. But I also agree that the reducer is useful when a change in one property has consequences on the other ones (order by resets the selection for example). And I'm not sure yet how to handle it best. |
…nChange to Hightable
and by selectionAndAnchor and setSelectionAndAnchor in ControlledHighTable
e6218c2
to
f177d37
Compare
I try to follow the same logic as https://react.dev/reference/react-dom/components/input and https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components The selection in HighTable is controlled, or uncontrolled, and it cannot be changed once the component has been created. Four modes: * - controlled (selection and onSelectionChange are defined): the parent controls the selection and receives the user interactions. No local state. * - controlled read-only (selection is defined, onSelectionChange is undefined): the parent controls the selection and the user interactions are disabled. No local state. * - uncontrolled (selection is undefined, onSelectionChange is defined): the component controls the selection and the user interactions. Local state. * - disabled (selection and onSelectionChange are undefined): the selection is hidden and the user interactions are disabled. No local state. Next steps: - do the same for orderBy - merge ControlledHighTable back to HighTable - fix tests - handle selection with keyboard (onChange on checkboxes)
d30143d
to
db88dc9
Compare
…t + if controlled
c8cfcce
to
6aea297
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid refactoring, with much more clear, typesafe, and flexible API! Using hooks does seem like the right choice here. Thanks for fighting through this I'm sure this was not easy to get right.
scroll event cannot be simulated with userEvent. Otherwise, userEvent is recommended over fireEvent, see https://testing-library.com/docs/user-event/intro#differences-from-fireevent
Add four props to control the selection and the sort:
selection
,onSelectionChange
,orderBy
andonOrderByChange
.See how we use it in an app: https://github.com/hyparam/hyperparam-cli/pull/139/files
Replaces #21, since having a reducer depending on props seems to be a bad practice.
Note that it removes the need for tableControl (I removed it).