1
1
import * as React from 'react' ;
2
- import {
3
- isValidElement ,
4
- ReactNode ,
5
- ReactElement ,
6
- useEffect ,
7
- useState ,
8
- } from 'react' ;
2
+ import { isValidElement , ReactNode , ReactElement , useCallback } from 'react' ;
9
3
import PropTypes from 'prop-types' ;
10
4
import {
11
5
Avatar ,
@@ -18,7 +12,7 @@ import {
18
12
ListItemText ,
19
13
} from '@material-ui/core' ;
20
14
import { makeStyles } from '@material-ui/core/styles' ;
21
- import { Link } from 'react-router-dom' ;
15
+ import { useHistory } from 'react-router-dom' ;
22
16
import {
23
17
Identifier ,
24
18
linkToRecord ,
@@ -140,7 +134,7 @@ const SimpleList = <RecordType extends Record = Record>(
140
134
resource = { resource }
141
135
>
142
136
< ListItem
143
- button = { ! ! linkType as any }
137
+ component = "div"
144
138
style = {
145
139
rowStyle
146
140
? rowStyle ( data [ id ] , rowIndex )
@@ -283,38 +277,46 @@ const LinkOrNot = (props: LinkOrNotProps) => {
283
277
resource,
284
278
} = props ;
285
279
const classes = useLinkOrNotStyles ( { classes : classesOverride } ) ;
286
- const [ effect , setEffect ] = useState < string > ( ( ) =>
287
- linkType === 'edit' || linkType === true
288
- ? linkToRecord ( basePath || `/${ resource } ` , id )
289
- : linkToRecord ( basePath || `/${ resource } ` , id , 'show' )
290
- ) ;
280
+ const history = useHistory ( ) ;
291
281
292
- useEffect ( ( ) => {
293
- if ( typeof linkType !== 'function' ) {
294
- return ;
295
- }
296
- const getEffect = async ( ) => {
297
- if ( typeof linkType === 'function' ) {
298
- setEffect (
299
- await linkType ( record , id , basePath || `/${ resource } ` )
300
- ) ;
301
- }
302
- } ;
282
+ const handleClick = useCallback (
283
+ async event => {
284
+ if ( ! linkType ) return ;
285
+ event . persist ( ) ;
303
286
304
- getEffect ( ) ;
305
- } , [ basePath , id , linkType , record , resource ] ) ;
287
+ const effect =
288
+ typeof linkType === 'function'
289
+ ? await linkType ( record , id , basePath || `/${ resource } ` )
290
+ : linkType ;
291
+ switch ( effect ) {
292
+ case true :
293
+ case 'edit' :
294
+ history . push ( linkToRecord ( basePath || `/${ resource } ` , id ) ) ;
295
+ return ;
296
+ case 'show' :
297
+ history . push (
298
+ linkToRecord ( basePath || `/${ resource } ` , id , 'show' )
299
+ ) ;
300
+ return ;
301
+ default :
302
+ if ( effect ) history . push ( effect ) ;
303
+ return ;
304
+ }
305
+ } ,
306
+ [ basePath , history , id , record , resource , linkType ]
307
+ ) ;
306
308
307
- return typeof effect === 'string' ? (
308
- < Link to = { effect } className = { classes . link } >
309
+ return linkType !== false ? (
310
+ < button onClick = { handleClick } className = { classes . link } >
309
311
{ children }
310
- </ Link >
312
+ </ button >
311
313
) : (
312
314
< span > { children } </ span >
313
315
) ;
314
316
} ;
315
317
316
318
export type FunctionLinkType = (
317
- record : Record ,
319
+ recordOrId : Record ,
318
320
id : Identifier ,
319
321
basePath ?: string
320
322
) => string | Promise < string > ;
0 commit comments