Skip to content

Commit 4ff0c27

Browse files
committed
Customizable email field and add basic story to test it.
1 parent c529941 commit 4ff0c27

File tree

2 files changed

+85
-3
lines changed

2 files changed

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

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

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

611
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
@@ -10,8 +15,13 @@ import { genericMemo } from './genericMemo';
1015
const EmailFieldImpl = <
1116
RecordType extends Record<string, any> = Record<string, any>,
1217
>(
13-
props: EmailFieldProps<RecordType>
18+
inProps: EmailFieldProps<RecordType>
1419
) => {
20+
const props = useThemeProps({
21+
props: inProps,
22+
name: PREFIX,
23+
});
24+
1525
const { className, emptyText, ...rest } = props;
1626
const value = useFieldValue(props);
1727
const translate = useTranslate();
@@ -30,15 +40,15 @@ const EmailFieldImpl = <
3040
}
3141

3242
return (
33-
<Link
43+
<StyledLink
3444
className={className}
3545
href={`mailto:${value}`}
3646
onClick={stopPropagation}
3747
variant="body2"
3848
{...sanitizeFieldRestProps(rest)}
3949
>
4050
{value}
41-
</Link>
51+
</StyledLink>
4252
);
4353
};
4454
EmailFieldImpl.displayName = 'EmailFieldImpl';
@@ -52,3 +62,29 @@ export interface EmailFieldProps<
5262

5363
// useful to prevent click bubbling in a Datagrid with rowClick
5464
const stopPropagation = e => e.stopPropagation();
65+
66+
const PREFIX = 'RaEmailField';
67+
68+
const StyledLink = styled(Link, {
69+
name: PREFIX,
70+
overridesResolver: (props, styles) => styles.root,
71+
})({});
72+
73+
declare module '@mui/material/styles' {
74+
interface ComponentNameToClassKey {
75+
[PREFIX]: 'root';
76+
}
77+
78+
interface ComponentsPropsList {
79+
[PREFIX]: Partial<EmailFieldProps>;
80+
}
81+
82+
interface Components {
83+
[PREFIX]?: {
84+
defaultProps?: ComponentsPropsList[typeof PREFIX];
85+
styleOverrides?: ComponentsOverrides<
86+
Omit<Theme, 'components'>
87+
>[typeof PREFIX];
88+
};
89+
}
90+
}

0 commit comments

Comments
 (0)