-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Chore(ArrayField): add support for storeKey
to manage independent selection states
#10390
base: next
Are you sure you want to change the base?
Conversation
storeKey
to manage independent selection states**
storeKey
to manage independent selection states**storeKey
to manage independent selection states
@fzaninotto Can you review? thanks |
Thanks for the PR! As this is a new feature, can you target the |
Yes, I can. Thanks |
d8c1f4f
to
d852f0c
Compare
@djhi @fzaninotto I have updated the target branch as well as written the necessary stories and tests for the feature. Please re-review. Thanks |
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.
Too bad you took a wrong direction with the fix, but we appreciate the effort to provide good stories, tests, and documentation! Thanks!
useEffect(() => { | ||
if (!resource) return; | ||
// Check if storeKey exists in the pagination store | ||
const currentPagination = storeKeyPaginationRef.current[resource]; | ||
if (currentPagination) { | ||
// Restore existing pagination state for the storeKey | ||
if ( | ||
page !== currentPagination.page || | ||
perPage !== currentPagination.perPage | ||
) { | ||
setPage(currentPagination.page); | ||
setPerPage(currentPagination.perPage); | ||
} | ||
} else { | ||
setPage(initialPage); | ||
setPerPage(initialPerPage); | ||
} | ||
storeKeyPaginationRef.current[resource] = { page, perPage }; | ||
}, [ | ||
resource, | ||
setPage, | ||
setPerPage, | ||
initialPage, | ||
initialPerPage, | ||
page, | ||
perPage, | ||
]); | ||
|
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 afraid you are going in the wrong direction here 😬 . The fix for #10382 suggested by @fzaninotto was to allow to override the storeKey
used to store the selection state (i.e. used in useRecordSelection
), but not the pagination state.
You can have a look at what was done in useReferenceManyFieldController
for an example:
react-admin/packages/ra-core/src/controller/field/useReferenceManyFieldController.ts
Lines 90 to 92 in 7b319c4
const [selectedIds, selectionModifiers] = useRecordSelection({ | |
resource: storeKey, | |
}); |
export const ListsWithoutStoreKeys = () => ( | ||
<CoreAdminContext store={localStorageStore()} dataProvider={dataProvider}> | ||
<CoreAdminUI layout={Layout}> | ||
<CustomRoutes> | ||
<Route path="/store" element={StorePosts} /> | ||
<Route path="/nostore" element={NoStorePosts} /> | ||
</CustomRoutes> | ||
<Resource name="posts" /> | ||
</CoreAdminUI> | ||
</CoreAdminContext> | ||
); |
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.
This Story is a bit misleading, StorePosts
is still using a storeKey
.
Besides, storeKey={false}
is not the same as having no storeKey
(for NoStorePosts
). I'd expect both lists in ListsWithoutStoreKeys
to have no storeKey
.
const Layout = (props: CoreLayoutProps) => ( | ||
<div style={styles.mainContainer}> | ||
<Link aria-label="top" to={`/top`}> | ||
Go to Top Posts | ||
</Link> | ||
<Link aria-label="flop" to={`/flop`}> | ||
Go to Flop Posts | ||
</Link> | ||
<Link aria-label="store" to={`/store`}> | ||
Go to Store List | ||
</Link> | ||
<Link aria-label="nostore" to={`/nostore`}> | ||
Go to No-Store List | ||
</Link> | ||
|
||
<br /> | ||
{props.children} | ||
</div> | ||
); |
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.
Layout
should not be shared across stories since both stories do not implement the same routes
|
||
When displaying multiple lists with the same data source, you may need to distinguish their selection states. To achieve this, assign a unique `storeKey` to each `ArrayField`. This allows each list to maintain its own selection state independently. | ||
|
||
In the example below, two `ArrayField` components display the same data source (`books`), but each stores its selection state under a different key (`books.selectedIds` and `custom.selectedIds`). This ensures that both components can coexist on the same page without interfering with each other's state. |
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.
In the example below, two `ArrayField` components display the same data source (`books`), but each stores its selection state under a different key (`books.selectedIds` and `custom.selectedIds`). This ensures that both components can coexist on the same page without interfering with each other's state. | |
In the example below, two `ArrayField` components display the same data source (`books`), but each stores its selection state under a different key (`customOne.selectedIds` and `customTwo.selectedIds`). This ensures that both components can coexist on the same page without interfering with each other's state. |
Problem
#10382
Describe the problem this PR solves
Solution
storeKey
property forArrayField
to enable distinct selection state storage for multiple instances of the same data source.Describe the solution this PR implements
How To Test
Describe the steps required to test the changes
Additional Checks
master
for a bugfix, ornext
for a featureAlso, please make sure to read the contributing guidelines.