Skip to content

Commit 98359d5

Browse files
committed
Customizable text field and add basic story to test it.
1 parent dd9c7e0 commit 98359d5

File tree

2 files changed

+82
-3
lines changed

2 files changed

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

packages/ra-ui-materialui/src/field/TextField.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 { ElementType } from 'react';
33
import Typography, { TypographyProps } from '@mui/material/Typography';
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,14 +15,19 @@ import { genericMemo } from './genericMemo';
1015
const TextFieldImpl = <
1116
RecordType extends Record<string, any> = Record<string, any>,
1217
>(
13-
props: TextFieldProps<RecordType>
18+
inProps: TextFieldProps<RecordType>
1419
) => {
20+
const props = useThemeProps({
21+
props: inProps,
22+
name: PREFIX,
23+
});
24+
1525
const { className, emptyText, ...rest } = props;
1626
const translate = useTranslate();
1727
const value = useFieldValue(props);
1828

1929
return (
20-
<Typography
30+
<StyledTypography
2131
component="span"
2232
variant="body2"
2333
className={className}
@@ -27,7 +37,7 @@ const TextFieldImpl = <
2737
? value.toString()
2838
: value ||
2939
(emptyText ? translate(emptyText, { _: emptyText }) : null)}
30-
</Typography>
40+
</StyledTypography>
3141
);
3242
};
3343

@@ -43,3 +53,29 @@ export interface TextFieldProps<
4353
// TypographyProps do not expose the component props, see https://github.com/mui/material-ui/issues/19512
4454
component?: ElementType<any>;
4555
}
56+
57+
const PREFIX = 'RaTextField';
58+
59+
const StyledTypography = styled(Typography, {
60+
name: PREFIX,
61+
overridesResolver: (props, styles) => styles.root,
62+
})({});
63+
64+
declare module '@mui/material/styles' {
65+
interface ComponentNameToClassKey {
66+
[PREFIX]: 'root';
67+
}
68+
69+
interface ComponentsPropsList {
70+
[PREFIX]: Partial<TextFieldProps>;
71+
}
72+
73+
interface Components {
74+
[PREFIX]?: {
75+
defaultProps?: ComponentsPropsList[typeof PREFIX];
76+
styleOverrides?: ComponentsOverrides<
77+
Omit<Theme, 'components'>
78+
>[typeof PREFIX];
79+
};
80+
}
81+
}

0 commit comments

Comments
 (0)