Skip to content

Commit 0beabe6

Browse files
committed
Customizable date field and add basic story to test it.
1 parent 1b8b5ee commit 0beabe6

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Paper, Stack } from '@mui/material';
2+
import * as React from 'react';
3+
4+
import { DateField, DateFieldProps } from './DateField';
5+
import { AdminContext } from '../AdminContext';
6+
import { defaultDarkTheme, defaultLightTheme } from '../theme';
7+
8+
export default { title: 'ra-ui-materialui/fields/DateField' };
9+
10+
export const Basic = ({
11+
value,
12+
theme,
13+
...props
14+
}: Partial<DateFieldProps> & { value?: Date | string; theme?: string }) => {
15+
return (
16+
<AdminContext
17+
theme={theme === 'light' ? defaultLightTheme : defaultDarkTheme}
18+
>
19+
<Paper sx={{ p: 2 }}>
20+
<Stack direction="row">
21+
<DateField record={{ value }} source="value" {...props} />
22+
</Stack>
23+
</Paper>
24+
</AdminContext>
25+
);
26+
};
27+
28+
Basic.argTypes = {
29+
value: {
30+
options: ['now', 'string', 'empty', 'undefined'],
31+
mapping: {
32+
now: new Date(),
33+
string: '2025-03-25 12:52:11',
34+
empty: '',
35+
undefined: undefined,
36+
},
37+
control: { type: 'select' },
38+
},
39+
theme: {
40+
options: ['light', 'dark'],
41+
control: { type: 'select' },
42+
},
43+
};
44+
Basic.args = {
45+
theme: 'light',
46+
value: 'now',
47+
};

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
22
import { Typography, TypographyProps } from '@mui/material';
3+
import {
4+
ComponentsOverrides,
5+
styled,
6+
useThemeProps,
7+
} from '@mui/material/styles';
38
import { useFieldValue, useTranslate } from 'ra-core';
49

510
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
@@ -33,8 +38,13 @@ import { genericMemo } from './genericMemo';
3338
const DateFieldImpl = <
3439
RecordType extends Record<string, any> = Record<string, any>,
3540
>(
36-
props: DateFieldProps<RecordType>
41+
inProps: DateFieldProps<RecordType>
3742
) => {
43+
const props = useThemeProps({
44+
props: inProps,
45+
name: PREFIX,
46+
});
47+
3848
const {
3949
className,
4050
emptyText,
@@ -95,14 +105,14 @@ const DateFieldImpl = <
95105
}
96106

97107
return (
98-
<Typography
108+
<StyledTypography
99109
component="span"
100110
variant="body2"
101111
className={className}
102112
{...sanitizeFieldRestProps(rest)}
103113
>
104114
{dateString}
105-
</Typography>
115+
</StyledTypography>
106116
);
107117
};
108118
DateFieldImpl.displayName = 'DateFieldImpl';
@@ -136,3 +146,29 @@ const toLocaleStringSupportsLocales = (() => {
136146
}
137147
return false;
138148
})();
149+
150+
const PREFIX = 'RaDateField';
151+
152+
const StyledTypography = styled(Typography, {
153+
name: PREFIX,
154+
overridesResolver: (props, styles) => styles.root,
155+
})({});
156+
157+
declare module '@mui/material/styles' {
158+
interface ComponentNameToClassKey {
159+
[PREFIX]: 'root';
160+
}
161+
162+
interface ComponentsPropsList {
163+
[PREFIX]: Partial<DateFieldProps>;
164+
}
165+
166+
interface Components {
167+
[PREFIX]?: {
168+
defaultProps?: ComponentsPropsList[typeof PREFIX];
169+
styleOverrides?: ComponentsOverrides<
170+
Omit<Theme, 'components'>
171+
>[typeof PREFIX];
172+
};
173+
}
174+
}

0 commit comments

Comments
 (0)