-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathShowBase.tsx
68 lines (62 loc) · 1.97 KB
/
ShowBase.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as React from 'react';
import { RaRecord } from '../../types';
import { useShowController, ShowControllerProps } from './useShowController';
import { ShowContextProvider } from './ShowContextProvider';
import { OptionalResourceContextProvider } from '../../core';
import { useIsAuthPending } from '../../auth';
/**
* Call useShowController and put the value in a ShowContext
*
* Base class for <Show> components, without UI.
*
* Accepts any props accepted by useShowController:
* - id: The record identifier
* - resource: The resource
*
* @example // Custom show layout
*
* const PostShow = () => (
* <ShowBase resource="posts">
* <Grid container>
* <Grid item xs={8}>
* <SimpleForm>
* ...
* </SimpleForm>
* </Grid>
* <Grid item xs={4}>
* Show instructions...
* </Grid>
* </Grid>
* <div>
* Post related links...
* </div>
* </ShowBase>
* );
*/
export const ShowBase = <RecordType extends RaRecord = any>({
children,
loading = null,
...props
}: ShowBaseProps<RecordType>) => {
const controllerProps = useShowController<RecordType>(props);
const isAuthPending = useIsAuthPending({
resource: controllerProps.resource,
action: 'show',
});
if (isAuthPending && !props.disableAuthentication) {
return loading;
}
return (
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided
<OptionalResourceContextProvider value={props.resource}>
<ShowContextProvider value={controllerProps}>
{children}
</ShowContextProvider>
</OptionalResourceContextProvider>
);
};
export interface ShowBaseProps<RecordType extends RaRecord = RaRecord>
extends ShowControllerProps<RecordType> {
children: React.ReactNode;
loading?: React.ReactNode;
}