-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
feat: use storeKey
in useRecordSelection
#10953
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
base: next
Are you sure you want to change the base?
feat: use storeKey
in useRecordSelection
#10953
Conversation
This is just a draft to make sure the solution is applicable for the framework. Btw, is there any chance to get this feature in minor release? |
can you explain why you need to introduce the notion of namespace, which seems to overlap with the storeKey feature? |
It's maybe more like storeKey, i just named it like this... Do you suggest to pass List's storeKey to useRecordSelection instead of adding separate & independent key for selection state? |
Exactly |
@fzaninotto ok, will do. |
Why will it be a breaking change? You only need to make the |
Any existing usage of List with non-empty |
So you're right, it's a breaking change, but one that we're willing to accept given =i see no use case for two lists with different store keys that would require an identical selection. You can continue on |
Ok. What about storing selection state in 2 separate keys ( @fzaninotto , can you please have a look at the question above? |
storeKey
in useRecordSelection
…me storeKey selection state
@fzaninotto as far as I can understand the requirements for the PR it seems to be ready for the review. |
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.
Sorry we're taking some time to review your PR but the team is fairly busy ATM 😅 .
Still, we appreciate your work and your will to contribute! 💪
unselect( | ||
idsToRemove: RecordType['id'][], | ||
fromAllStoreKeys?: boolean | ||
) { |
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'm not sure what @fzaninotto had in mind implied introducing a new fromAllStoreKeys
parameters.
My understanding is:
- Calling
unselect()
orclearSelection()
with nostoreKey
will clear the selection for the default store key (${resource}.selectedIds
) - Calling
unselect()
orclearSelection()
with astoreKey
will clear the selection only for that store key (${storeKey}.selectedIds
) and that's OK. We agree that it won't clear selection from all store keys because we don't see a use-case where the same items would appear in two list with two storekeys, and even if they do, it should be fairly easy to fix
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.
UPDATE: actually my bad, in all cases when we clear the selection we want to clear the ids from all stored selection states.
So basically this comes down to always having fromAllStoreKeys
set to true
(but we still don't need the prop).
Also, I better understand now why you had to introduce the notion of namespaces. Otherwise you would have no way to know all the storeKeys from which you need to remove the ids.
However, we'd prefer if you could use the following model instead:
- Keep using one store key per
storeKey
(i.e.useStore(`${storeKey ?? resource}.selectedIds`, [])
) - Add an additional store value to store all storeKeys associated to a resource (e.g.
`${resource}.storeKeys` = ['a', 'b']
), which will allow you to find all the storeKeys from which to remove the ids when callingunselect()
orclearSelection()
I hope my explanations are clear enough. Feel free to ask for more details if it's not the case.
Again, thanks for offering to work on this.
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.
Thanks. Yes, the idea of having a separate store for listing storeKeys is nice, I'll switch to it...
Regarding introducing fromAllStoreKeys
- it seemed to me that removing selected IDs from all storeKeys by default may be not intuitive (e.g. unexpected side-effects) for the users of useRecordSelection
hook. From the perspective of existing code in React-Admin (delete cases) - well, yes, this argument is always true
.
In other words, IMO, unselecting usually will happen in scope of current storeKey (fromAllStoreKeys === false), but deletion case is more specific and requires clearing from all storeKeys... I could have added a new method for this VS adding a parameter, but have chosen to keep the API surface smaller and be able to force unselection everywhere for the delete case.
Problem
It's not possible to create distinct List views that use separate record selection states.
Previous discussion was started in PR: #10951
Solution
Store selected IDs under in a
Record<string, Identifier[]>
structure inuseRecordSelection
. List component passes thestoreKey
touseRecordSelection
. If storeKey is non-empty, then it's used as a key in the selection object. Delete controllers pass the flag to unselect deleted records from all storedKeys.How To Test
Run the story for record selections states with different storyKeys:
http://localhost:9010/?path=/story/ra-ui-materialui-list-list--record-selection
Additional Checks
master
for a bugfix or a documentation fix, ornext
for a feature