Skip to content

Commit 43e20fb

Browse files
committed
Make default props themeable
1 parent 44b7cbf commit 43e20fb

File tree

3 files changed

+104
-27
lines changed

3 files changed

+104
-27
lines changed

docs/RecordField.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@ When the record contains no value for the `source` prop, `RecordField` renders a
142142

143143
If you use the `render` prop, you can even use a React element as `empty` value.
144144

145+
{% raw %}
145146
```jsx
146147
<RecordField
147148
source="title"
148149
empty={<span style={{ color: 'red' }}>Missing title</span>}
149150
render={record => record.title}
150151
/>
151152
```
153+
{% endraw %}
152154

153155
Note that `empty` is ignored when you pass a custom field component as child. In this case, it's the child's responsibility to handle the empty value.
154156

@@ -352,6 +354,29 @@ But since you generally need to do it for several fields, it's preferable to do
352354
```
353355
{% endraw %}
354356

357+
**Tip**: If you want all your fields to be displayed inline, you can define the default variant for `RecordField` [in a custom application Theme](https://marmelab.com/react-admin/AppTheme.html#theming-individual-components):
358+
359+
```jsx
360+
import { defaultTheme } from 'react-admin';
361+
import { deepmerge } from '@mui/utils';
362+
363+
const theme = deepmerge(defaultTheme, {
364+
components: {
365+
RaRecordField: {
366+
defaultProps: {
367+
variant: 'inline',
368+
},
369+
},
370+
},
371+
});
372+
373+
const App = () => (
374+
<Admin theme={theme}>
375+
// ...
376+
</Admin>
377+
);
378+
```
379+
355380
## TypeScript
356381

357382
`<RecordField>` is a generic component. You can pass a type parameter to get hints for the `source` prop and type safety for the `record` argument of the `render` function.

packages/ra-ui-materialui/src/field/RecordField.stories.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as React from 'react';
22
import { Stack, Typography } from '@mui/material';
3+
import { ThemeProvider, createTheme } from '@mui/material/styles';
34
import {
45
I18nContextProvider,
56
RecordContextProvider,
67
ResourceContext,
78
} from 'ra-core';
9+
810
import { NumberField, TextField } from '.';
911
import { RecordField } from './RecordField';
1012

@@ -182,17 +184,40 @@ export const Variant = () => (
182184
/>
183185
</Stack>
184186
<Typography gutterBottom>Custom label width</Typography>
185-
<Stack sx={{ '& .RaRecordField-label': { width: 200 } }}>
186-
<RecordField variant="inline" source="id" />
187-
<RecordField variant="inline" source="title" />
188-
<RecordField variant="inline" source="author" />
189-
<RecordField variant="inline" source="summary" />
190-
<RecordField
191-
variant="inline"
192-
source="year"
193-
field={NumberField}
194-
/>
195-
</Stack>
187+
<ThemeProvider
188+
theme={createTheme({
189+
components: {
190+
RaRecordField: {
191+
defaultProps: {
192+
variant: 'inline',
193+
},
194+
},
195+
},
196+
})}
197+
>
198+
<Stack sx={{ '& .RaRecordField-label': { width: 200 } }}>
199+
<RecordField variant="inline" source="id" />
200+
<RecordField variant="inline" source="title" />
201+
<RecordField variant="inline" source="author" />
202+
<RecordField variant="inline" source="summary" />
203+
<RecordField
204+
variant="inline"
205+
source="year"
206+
field={NumberField}
207+
/>
208+
</Stack>
209+
<Typography gutterBottom>Default variant via theme</Typography>
210+
<Stack>
211+
<RecordField
212+
source="id"
213+
TypographyProps={{ sx: { width: 200 } }}
214+
/>
215+
<RecordField source="title" />
216+
<RecordField source="author" />
217+
<RecordField source="summary" />
218+
<RecordField source="year" field={NumberField} />
219+
</Stack>
220+
</ThemeProvider>
196221
</RecordContextProvider>
197222
</ResourceContext.Provider>
198223
);

packages/ra-ui-materialui/src/field/RecordField.tsx

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as React from 'react';
22
import { type ReactNode, type ElementType } from 'react';
3-
import { styled } from '@mui/material/styles';
3+
import { styled, type ComponentsOverrides } from '@mui/material/styles';
44
import {
55
Stack,
66
Typography,
7+
useThemeProps,
78
type StackProps,
89
type SxProps,
910
type TypographyProps,
@@ -21,21 +22,30 @@ import clsx from 'clsx';
2122

2223
import { TextField } from './TextField';
2324

25+
const PREFIX = 'RaRecordField';
26+
2427
export const RecordField = <
2528
RecordType extends Record<string, any> = Record<string, any>,
26-
>({
27-
children,
28-
className,
29-
empty,
30-
field,
31-
label,
32-
render,
33-
source,
34-
sx,
35-
TypographyProps,
36-
variant,
37-
...rest
38-
}: RecordFieldProps<RecordType>) => {
29+
>(
30+
inProps: RecordFieldProps<RecordType>
31+
) => {
32+
const props = useThemeProps({
33+
props: inProps,
34+
name: PREFIX,
35+
});
36+
const {
37+
children,
38+
className,
39+
empty,
40+
field,
41+
label,
42+
render,
43+
source,
44+
sx,
45+
TypographyProps,
46+
variant,
47+
...rest
48+
} = props;
3949
const resource = useResourceContext();
4050
const record = useRecordContext<RecordType>();
4151
const translate = useTranslate();
@@ -113,8 +123,6 @@ export interface RecordFieldProps<
113123
variant?: 'default' | 'inline';
114124
}
115125

116-
const PREFIX = 'RaRecordField';
117-
118126
export const RecordFieldClasses = {
119127
label: `${PREFIX}-label`,
120128
value: `${PREFIX}-value`,
@@ -142,3 +150,22 @@ const Root = styled(Stack, {
142150
flex: 1,
143151
},
144152
}));
153+
154+
declare module '@mui/material/styles' {
155+
interface ComponentNameToClassKey {
156+
RaRecordField: 'root' | 'content' | 'button' | 'icon';
157+
}
158+
159+
interface ComponentsPropsList {
160+
RaRecordField: Partial<RecordFieldProps>;
161+
}
162+
163+
interface Components {
164+
RaRecordField?: {
165+
defaultProps?: ComponentsPropsList['RaRecordField'];
166+
styleOverrides?: ComponentsOverrides<
167+
Omit<Theme, 'components'>
168+
>['RaRecordField'];
169+
};
170+
}
171+
}

0 commit comments

Comments
 (0)