Skip to content

Commit 34973a0

Browse files
committed
Allow to set default props for Create, Edit and Show.
1 parent 1049ba0 commit 34973a0

File tree

6 files changed

+68
-33
lines changed

6 files changed

+68
-33
lines changed

packages/ra-ui-materialui/src/detail/Create.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
RaRecord,
88
useCheckMinimumRequiredProps,
99
} from 'ra-core';
10+
import { useThemeProps } from '@mui/material/styles';
1011

1112
import { CreateView, CreateViewProps } from './CreateView';
1213
import { Loading } from '../layout';
@@ -58,8 +59,13 @@ export const Create = <
5859
RecordType extends Omit<RaRecord, 'id'> = any,
5960
ResultRecordType extends RaRecord = RecordType & { id: Identifier },
6061
>(
61-
props: CreateProps<RecordType, Error, ResultRecordType>
62+
inProps: CreateProps<RecordType, Error, ResultRecordType>
6263
): ReactElement => {
64+
const props = useThemeProps({
65+
props: inProps,
66+
name: PREFIX,
67+
});
68+
6369
useCheckMinimumRequiredProps('Create', ['children'], props);
6470
const {
6571
resource,
@@ -100,3 +106,5 @@ export interface CreateProps<
100106
Omit<CreateViewProps, 'children'> {}
101107

102108
const defaultLoading = <Loading />;
109+
110+
const PREFIX = 'RaCreate'; // Types declared in CreateView.

packages/ra-ui-materialui/src/detail/CreateView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useCreateContext } from 'ra-core';
1212
import clsx from 'clsx';
1313

1414
import { Title } from '../layout';
15+
import { CreateProps } from './Create';
1516

1617
export const CreateView = (inProps: CreateViewProps) => {
1718
const props = useThemeProps({
@@ -94,7 +95,7 @@ declare module '@mui/material/styles' {
9495
}
9596

9697
interface ComponentsPropsList {
97-
RaCreate: Partial<CreateViewProps>;
98+
RaCreate: Partial<CreateProps>;
9899
}
99100

100101
interface Components {

packages/ra-ui-materialui/src/detail/Edit.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
RaRecord,
66
EditBaseProps,
77
} from 'ra-core';
8+
import { useThemeProps } from '@mui/material/styles';
9+
810
import { EditView, EditViewProps } from './EditView';
911
import { Loading } from '../layout';
1012

@@ -54,8 +56,13 @@ import { Loading } from '../layout';
5456
* export default App;
5557
*/
5658
export const Edit = <RecordType extends RaRecord = any>(
57-
props: EditProps<RecordType, Error>
59+
inProps: EditProps<RecordType, Error>
5860
) => {
61+
const props = useThemeProps({
62+
props: inProps,
63+
name: PREFIX,
64+
});
65+
5966
useCheckMinimumRequiredProps('Edit', ['children'], props);
6067
const {
6168
resource,
@@ -91,3 +98,5 @@ export interface EditProps<RecordType extends RaRecord = any, ErrorType = Error>
9198
Omit<EditViewProps, 'children'> {}
9299

93100
const defaultLoading = <Loading />;
101+
102+
const PREFIX = 'RaEdit'; // Types declared in EditView.

packages/ra-ui-materialui/src/detail/EditView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useEditContext, useResourceDefinition } from 'ra-core';
1414

1515
import { EditActions } from './EditActions';
1616
import { Title } from '../layout';
17+
import { EditProps } from './Edit';
1718

1819
const defaultActions = <EditActions />;
1920

@@ -106,7 +107,7 @@ declare module '@mui/material/styles' {
106107
}
107108

108109
interface ComponentsPropsList {
109-
RaEdit: Partial<EditViewProps>;
110+
RaEdit: Partial<EditProps>;
110111
}
111112

112113
interface Components {

packages/ra-ui-materialui/src/detail/Show.tsx

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import { ReactElement } from 'react';
33
import { ShowBase, RaRecord, ShowBaseProps } from 'ra-core';
4+
import { useThemeProps } from '@mui/material/styles';
5+
46
import { ShowView, ShowViewProps } from './ShowView';
57
import { Loading } from '../layout';
68

@@ -43,40 +45,53 @@ import { Loading } from '../layout';
4345
* );
4446
* export default App;
4547
*
46-
* @param {ShowProps} props
47-
* @param {ReactElement|false} props.actions An element to display above the page content, or false to disable actions.
48-
* @param {string} props.className A className to apply to the page content.
49-
* @param {ElementType} props.component The component to use as root component (div by default).
50-
* @param {boolean} props.emptyWhileLoading Do not display the page content while loading the initial data.
51-
* @param {string} props.id The id of the resource to display (grabbed from the route params if not defined).
52-
* @param {Object} props.queryClient Options to pass to the react-query useQuery hook.
53-
* @param {string} props.resource The resource to fetch from the data provider (grabbed from the ResourceContext if not defined).
54-
* @param {Object} props.sx Custom style object.
55-
* @param {ElementType|string} props.title The title of the page. Defaults to `#{resource} #${id}`.
48+
* @param {ShowProps} inProps
49+
* @param {ReactElement|false} inProps.actions An element to display above the page content, or false to disable actions.
50+
* @param {string} inProps.className A className to apply to the page content.
51+
* @param {ElementType} inProps.component The component to use as root component (div by default).
52+
* @param {boolean} inProps.emptyWhileLoading Do not display the page content while loading the initial data.
53+
* @param {string} inProps.id The id of the resource to display (grabbed from the route params if not defined).
54+
* @param {Object} inProps.queryClient Options to pass to the react-query useQuery hook.
55+
* @param {string} inProps.resource The resource to fetch from the data provider (grabbed from the ResourceContext if not defined).
56+
* @param {Object} inProps.sx Custom style object.
57+
* @param {ElementType|string} inProps.title The title of the page. Defaults to `#{resource} #${id}`.
5658
*
5759
* @see ShowView for the actual rendering
5860
*/
59-
export const Show = <RecordType extends RaRecord = any>({
60-
id,
61-
resource,
62-
queryOptions,
63-
disableAuthentication,
64-
loading = defaultLoading,
65-
...rest
66-
}: ShowProps<RecordType>): ReactElement => (
67-
<ShowBase<RecordType>
68-
id={id}
69-
disableAuthentication={disableAuthentication}
70-
queryOptions={queryOptions}
71-
resource={resource}
72-
loading={loading}
73-
>
74-
<ShowView {...rest} />
75-
</ShowBase>
76-
);
61+
export const Show = <RecordType extends RaRecord = any>(
62+
inProps: ShowProps<RecordType>
63+
): ReactElement => {
64+
const props = useThemeProps({
65+
props: inProps,
66+
name: PREFIX,
67+
});
68+
69+
const {
70+
id,
71+
resource,
72+
queryOptions,
73+
disableAuthentication,
74+
loading = defaultLoading,
75+
...rest
76+
} = props;
77+
78+
return (
79+
<ShowBase<RecordType>
80+
id={id}
81+
disableAuthentication={disableAuthentication}
82+
queryOptions={queryOptions}
83+
resource={resource}
84+
loading={loading}
85+
>
86+
<ShowView {...rest} />
87+
</ShowBase>
88+
);
89+
};
7790

7891
export interface ShowProps<RecordType extends RaRecord = any>
7992
extends ShowBaseProps<RecordType>,
8093
Omit<ShowViewProps, 'children'> {}
8194

8295
const defaultLoading = <Loading />;
96+
97+
const PREFIX = 'RaShow'; // Types declared in ShowView.

packages/ra-ui-materialui/src/detail/ShowView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import clsx from 'clsx';
1212
import { useShowContext, useResourceDefinition } from 'ra-core';
1313
import { ShowActions } from './ShowActions';
1414
import { Title } from '../layout';
15+
import { ShowProps } from './Show';
1516

1617
const defaultActions = <ShowActions />;
1718

@@ -101,7 +102,7 @@ declare module '@mui/material/styles' {
101102
}
102103

103104
interface ComponentsPropsList {
104-
RaShow: Partial<ShowViewProps>;
105+
RaShow: Partial<ShowProps>;
105106
}
106107

107108
interface Components {

0 commit comments

Comments
 (0)