-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[Doc] Impove scrollToTop in buttons doc and document _scrollToTop in useRedirect
#10449
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ac4153
document `<XxxxButton scrollToTop />`
erwanMarmelab df90a5a
document `useRedirect` with `scrollToTop`
erwanMarmelab fdaf387
refactor `useRedirect` doc to search it easily with algolia
erwanMarmelab 71e12c1
Improve `useRedirect`'s `_scrollToTop`'s doc
erwanMarmelab b24856a
Apply suggestions from code review
erwanMarmelab bdc97be
add a second example for redirect function
erwanMarmelab 466f2b8
Merge branch 'doc/scrollToTop' of github.com:marmelab/react-admin int…
erwanMarmelab 15aa2b1
Improve `scrollToTop` doc
erwanMarmelab eaa085d
improve other doc sections
erwanMarmelab b19a98d
Fix `<CloneButton>` missing doc update + document `<ListButton scroll…
erwanMarmelab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ title: "useRedirect" | |
|
|
||
| This hook returns a function that redirects the user to another page. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```jsx | ||
| import { useRedirect } from 'react-admin'; | ||
|
|
||
|
|
@@ -20,13 +22,14 @@ const DashboardButton = () => { | |
| ``` | ||
|
|
||
| The callback takes 5 arguments: | ||
| - The page to redirect the user to ('list', 'create', 'edit', 'show', a function or a custom path) | ||
| - The current `resource` | ||
| - The `id` of the record to redirect to (if any) | ||
| - A record-like object to be passed to the first argument, when the first argument is a function | ||
| - A `state` to be set to the location | ||
|
|
||
| Here are more examples of `useRedirect` calls: | ||
| - The page to redirect the user to ('list', 'create', 'edit', 'show', a function or a custom path) | ||
| - The current `resource` | ||
| - The `id` of the record to redirect to (if any) | ||
| - A record-like object to be passed to the first argument, when the first argument is a function | ||
| - A `state` to be set to the location | ||
|
|
||
| Here are more examples of `useRedirect` calls: | ||
|
|
||
| ```jsx | ||
| // redirect to the post list page | ||
|
|
@@ -35,14 +38,8 @@ redirect('list', 'posts'); | |
| redirect('edit', 'posts', 1); | ||
| // redirect to the post creation page: | ||
| redirect('create', 'posts'); | ||
| // redirect to the result of a function | ||
| redirect((resource, id, data) => { | ||
| return data.hasComments ? '/comments' : '/posts'; | ||
| }, 'posts', 1, { hasComments: true }); | ||
| // redirect to edit view with state data | ||
| redirect('edit', 'posts', 1, {}, { record: { post_id: record.id } }); | ||
| // do not redirect (resets the record form) | ||
| redirect(false); | ||
| ``` | ||
|
|
||
| Note that `useRedirect` allows redirection to an absolute URL outside the current React app. | ||
|
|
@@ -68,4 +65,48 @@ const MyPageButton = () => { | |
| } | ||
| return <button onClick={handleClick}>My page</button>; | ||
| }; | ||
| ``` | ||
| ``` | ||
|
|
||
| ## Redirect function | ||
|
|
||
| `useRedirect` allows you to redirect to the result of a function as follows: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This deserves more examples as you can also return an object as expected by |
||
|
|
||
| ```jsx | ||
| redirect((resource, id, data) => { | ||
| return data.hasComments ? '/comments' : '/posts'; | ||
| }, 'posts', 1, { hasComments: true }); | ||
| ``` | ||
|
|
||
| Your function can also return an object containing a `pathname` and optionally some keys of [a `NavigateOptions` object](https://api.reactrouter.com/dev/interfaces/react_router.NavigateOptions.html). | ||
|
|
||
| ```jsx | ||
| redirect((resource, id, data) => { | ||
| return { | ||
| pathname: `/${resource}/1`, | ||
| state: { record: { id: 1, foo: 'bar' } }, | ||
| flushSync: true, | ||
| preventScrollReset: true, | ||
| replace: false, | ||
| viewTransition: true, | ||
| }; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Disable Scroll To Top | ||
|
|
||
| By default, react-admin scrolls to top on each redirection. You can disable it by passing a `_scrollToTop: false` option in the 5th argument: | ||
|
|
||
| ```jsx | ||
| redirect(`/deals/${deal.id}/show`, undefined, undefined, undefined, { | ||
| _scrollToTop: false, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Reset the record form | ||
|
|
||
| `useRedirect` resets the record form, so you can use the `redirect` function to reset it without redirecting as follows: | ||
|
|
||
| ```jsx | ||
| // do not redirect (resets the record form) | ||
| redirect(false); | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why did you remove those?
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.
To isolate it in their own chapter