|
1 | 1 | import * as React from 'react'; |
2 | | -import { NavigateFunction, Route } from 'react-router'; |
3 | | -import { Link } from 'react-router-dom'; |
| 2 | +import { NavigateFunction, Route, Routes } from 'react-router'; |
| 3 | +import { Link, useParams, useLocation } from 'react-router-dom'; |
4 | 4 | import { TestMemoryRouter } from '../routing'; |
5 | 5 | import { Resource } from './Resource'; |
6 | 6 | import { CoreAdmin } from './CoreAdmin'; |
| 7 | +import { Browser } from '../storybook/FakeBrowser'; |
7 | 8 |
|
8 | 9 | export default { |
9 | 10 | title: 'ra-core/core/Resource', |
@@ -61,10 +62,186 @@ export const Basic = ({ |
61 | 62 | navigateCallback?: (n: NavigateFunction) => void; |
62 | 63 | }) => ( |
63 | 64 | <TestMemoryRouter navigateCallback={navigateCallback}> |
64 | | - <CoreAdmin loading={Loading}> |
65 | | - <Resource {...resource} /> |
66 | | - </CoreAdmin> |
| 65 | + <Browser> |
| 66 | + <CoreAdmin loading={Loading}> |
| 67 | + <Resource {...resource} /> |
| 68 | + </CoreAdmin> |
| 69 | + </Browser> |
67 | 70 | </TestMemoryRouter> |
68 | 71 | ); |
69 | 72 |
|
70 | 73 | const Loading = () => <div>Loading...</div>; |
| 74 | + |
| 75 | +export const OnlyList = ({ |
| 76 | + navigateCallback, |
| 77 | +}: { |
| 78 | + navigateCallback?: (n: NavigateFunction) => void; |
| 79 | +}) => ( |
| 80 | + <TestMemoryRouter navigateCallback={navigateCallback}> |
| 81 | + <Browser> |
| 82 | + <CoreAdmin loading={Loading}> |
| 83 | + <Resource name="posts" list={PostList} /> |
| 84 | + </CoreAdmin> |
| 85 | + </Browser> |
| 86 | + </TestMemoryRouter> |
| 87 | +); |
| 88 | + |
| 89 | +export const WithAllDialogs = ({ |
| 90 | + navigateCallback, |
| 91 | +}: { |
| 92 | + navigateCallback?: (n: NavigateFunction) => void; |
| 93 | +}) => ( |
| 94 | + <TestMemoryRouter navigateCallback={navigateCallback}> |
| 95 | + <Browser> |
| 96 | + <CoreAdmin loading={Loading}> |
| 97 | + <Resource name="posts" list={PostListWithAllDialogs} /> |
| 98 | + </CoreAdmin> |
| 99 | + </Browser> |
| 100 | + </TestMemoryRouter> |
| 101 | +); |
| 102 | + |
| 103 | +const PostListWithAllDialogs = () => ( |
| 104 | + <div> |
| 105 | + <div>PostList</div> |
| 106 | + <Link to="/posts/create">create</Link> <Link to="/posts/123">edit</Link>{' '} |
| 107 | + <Link to="/posts/123/show">show</Link> |
| 108 | + <PostEditDialog /> |
| 109 | + <PostCreateDialog /> |
| 110 | + <PostShowDialog /> |
| 111 | + </div> |
| 112 | +); |
| 113 | + |
| 114 | +const PostCreateDialog = () => ( |
| 115 | + <Routes> |
| 116 | + <Route |
| 117 | + path="create/*" |
| 118 | + element={ |
| 119 | + <div |
| 120 | + style={{ |
| 121 | + border: '1px solid black', |
| 122 | + margin: '1em', |
| 123 | + padding: '1em', |
| 124 | + maxWidth: '400px', |
| 125 | + }} |
| 126 | + > |
| 127 | + <div> |
| 128 | + <Link to="/posts">close</Link> |
| 129 | + </div> |
| 130 | + <div>PostCreate</div> |
| 131 | + </div> |
| 132 | + } |
| 133 | + /> |
| 134 | + </Routes> |
| 135 | +); |
| 136 | + |
| 137 | +const PostEditDialog = () => { |
| 138 | + return ( |
| 139 | + <Routes> |
| 140 | + <Route path=":id/*" element={<PostEditDialogView />} /> |
| 141 | + </Routes> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +const PostEditDialogView = () => { |
| 146 | + const params = useParams<'id'>(); |
| 147 | + const location = useLocation(); |
| 148 | + const isMatch = |
| 149 | + params.id && |
| 150 | + params.id !== 'create' && |
| 151 | + location.pathname.indexOf('/show') === -1; |
| 152 | + return isMatch ? ( |
| 153 | + <div |
| 154 | + style={{ |
| 155 | + border: '1px solid black', |
| 156 | + margin: '1em', |
| 157 | + padding: '1em', |
| 158 | + maxWidth: '400px', |
| 159 | + }} |
| 160 | + > |
| 161 | + <div> |
| 162 | + <Link to="/posts">close</Link> |
| 163 | + </div> |
| 164 | + <div>PostEdit</div> |
| 165 | + </div> |
| 166 | + ) : null; |
| 167 | +}; |
| 168 | + |
| 169 | +const PostShowDialog = () => { |
| 170 | + return ( |
| 171 | + <Routes> |
| 172 | + <Route path=":id/show/*" element={<PostShowDialogView />} /> |
| 173 | + </Routes> |
| 174 | + ); |
| 175 | +}; |
| 176 | + |
| 177 | +const PostShowDialogView = () => { |
| 178 | + const params = useParams<'id'>(); |
| 179 | + const isMatch = params.id && params.id !== 'create'; |
| 180 | + return isMatch ? ( |
| 181 | + <div |
| 182 | + style={{ |
| 183 | + border: '1px solid black', |
| 184 | + margin: '1em', |
| 185 | + padding: '1em', |
| 186 | + maxWidth: '400px', |
| 187 | + }} |
| 188 | + > |
| 189 | + <div> |
| 190 | + <Link to="/posts">close</Link> |
| 191 | + </div> |
| 192 | + <div>PostShow</div> |
| 193 | + </div> |
| 194 | + ) : null; |
| 195 | +}; |
| 196 | + |
| 197 | +export const WithCreateDialog = ({ |
| 198 | + navigateCallback, |
| 199 | +}: { |
| 200 | + navigateCallback?: (n: NavigateFunction) => void; |
| 201 | +}) => ( |
| 202 | + <TestMemoryRouter navigateCallback={navigateCallback}> |
| 203 | + <Browser> |
| 204 | + <CoreAdmin loading={Loading}> |
| 205 | + <Resource |
| 206 | + name="posts" |
| 207 | + list={PostListWithCreateDialog} |
| 208 | + edit={PostEdit} |
| 209 | + /> |
| 210 | + </CoreAdmin> |
| 211 | + </Browser> |
| 212 | + </TestMemoryRouter> |
| 213 | +); |
| 214 | + |
| 215 | +const PostListWithCreateDialog = () => ( |
| 216 | + <div> |
| 217 | + <div>PostList</div> |
| 218 | + <Link to="/posts/create">create</Link> <Link to="/posts/123">edit</Link>{' '} |
| 219 | + <PostCreateDialog /> |
| 220 | + </div> |
| 221 | +); |
| 222 | + |
| 223 | +export const WithShowDialog = ({ |
| 224 | + navigateCallback, |
| 225 | +}: { |
| 226 | + navigateCallback?: (n: NavigateFunction) => void; |
| 227 | +}) => ( |
| 228 | + <TestMemoryRouter navigateCallback={navigateCallback}> |
| 229 | + <Browser> |
| 230 | + <CoreAdmin loading={Loading}> |
| 231 | + <Resource |
| 232 | + name="posts" |
| 233 | + list={PostListWithShowDialog} |
| 234 | + edit={PostEdit} |
| 235 | + /> |
| 236 | + </CoreAdmin> |
| 237 | + </Browser> |
| 238 | + </TestMemoryRouter> |
| 239 | +); |
| 240 | + |
| 241 | +const PostListWithShowDialog = () => ( |
| 242 | + <div> |
| 243 | + <div>PostList</div> |
| 244 | + <Link to="/posts/123">edit</Link> <Link to="/posts/123/show">show</Link> |
| 245 | + <PostShowDialog /> |
| 246 | + </div> |
| 247 | +); |
0 commit comments