Skip to content

Commit 89f610f

Browse files
authored
[TextField] Migrate to emotion (#25286)
1 parent 2587f64 commit 89f610f

File tree

9 files changed

+92
-24
lines changed

9 files changed

+92
-24
lines changed

docs/pages/api-docs/text-field.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"description": "'medium'<br>&#124;&nbsp;'small'<br>&#124;&nbsp;string"
4646
}
4747
},
48+
"sx": { "type": { "name": "object" } },
4849
"type": { "type": { "name": "string" } },
4950
"value": { "type": { "name": "any" } },
5051
"variant": {
@@ -62,6 +63,6 @@
6263
"filename": "/packages/material-ui/src/TextField/TextField.js",
6364
"inheritance": { "component": "FormControl", "pathname": "/api/form-control/" },
6465
"demos": "<ul><li><a href=\"/components/autocomplete/\">Autocomplete</a></li>\n<li><a href=\"/components/pickers/\">Pickers</a></li>\n<li><a href=\"/components/text-fields/\">Text Fields</a></li></ul>",
65-
"styledComponent": false,
66+
"styledComponent": true,
6667
"cssComponent": false
6768
}

docs/translations/api-docs/text-field/text-field.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"select": "Render a <a href=\"/api/select/\"><code>Select</code></a> element while passing the Input element to <code>Select</code> as <code>input</code> parameter. If this option is set you must pass the options of the select as children.",
3030
"SelectProps": "Props applied to the <a href=\"/api/select/\"><code>Select</code></a> element.",
3131
"size": "The size of the component.",
32+
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/basics/#the-sx-prop\">`sx` page</a> for more details.",
3233
"type": "Type of the <code>input</code> element. It should be <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types\">a valid HTML5 input type</a>.",
3334
"value": "The value of the <code>input</code> element, required for a controlled component.",
3435
"variant": "The variant to use."

framer/scripts/framerConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ export const componentSettings = {
345345
'value',
346346
'size',
347347
'color',
348+
'sx',
348349
],
349350
propValues: {
350351
helperText: "''",

packages/material-ui/src/TextField/TextField.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { SxProps } from '@material-ui/system';
23
import { OverridableStringUnion } from '@material-ui/types';
34
import { InternalStandardProps as StandardProps } from '..';
45
import { FormControlProps } from '../FormControl';
@@ -9,6 +10,7 @@ import { FilledInputProps } from '../FilledInput';
910
import { OutlinedInputProps } from '../OutlinedInput';
1011
import { InputLabelProps } from '../InputLabel';
1112
import { SelectProps } from '../Select';
13+
import { Theme } from '../styles';
1214

1315
export interface TextFieldPropsColorOverrides {}
1416
export interface TextFieldPropsSizeOverrides {}
@@ -143,6 +145,10 @@ export interface BaseTextFieldProps
143145
* The size of the component.
144146
*/
145147
size?: OverridableStringUnion<Record<'small' | 'medium', true>, TextFieldPropsSizeOverrides>;
148+
/**
149+
* The system prop that allows defining system overrides as well as additional CSS styles.
150+
*/
151+
sx?: SxProps<Theme>;
146152
/**
147153
* Type of the `input` element. It should be [a valid HTML5 input type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types).
148154
*/

packages/material-ui/src/TextField/TextField.js

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import { refType } from '@material-ui/utils';
4+
import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled';
5+
import { deepmerge, refType } from '@material-ui/utils';
6+
import experimentalStyled from '../styles/experimentalStyled';
7+
import useThemeProps from '../styles/useThemeProps';
58
import Input from '../Input';
69
import FilledInput from '../FilledInput';
710
import OutlinedInput from '../OutlinedInput';
811
import InputLabel from '../InputLabel';
912
import FormControl from '../FormControl';
1013
import FormHelperText from '../FormHelperText';
1114
import Select from '../Select';
12-
import withStyles from '../styles/withStyles';
15+
import { getTextFieldUtilityClass } from './textFieldClasses';
1316

1417
const variantComponent = {
1518
standard: Input,
1619
filled: FilledInput,
1720
outlined: OutlinedInput,
1821
};
1922

20-
export const styles = {
21-
/* Styles applied to the root element. */
22-
root: {},
23+
const overridesResolver = (props, styles) => {
24+
return deepmerge(styles.root || {}, {});
2325
};
2426

27+
const useUtilityClasses = (styleProps) => {
28+
const { classes } = styleProps;
29+
30+
const slots = {
31+
root: ['root'],
32+
};
33+
34+
return composeClasses(slots, getTextFieldUtilityClass, classes);
35+
};
36+
37+
const TextFieldRoot = experimentalStyled(
38+
FormControl,
39+
{},
40+
{
41+
name: 'MuiTextField',
42+
slot: 'Root',
43+
overridesResolver,
44+
},
45+
)({});
46+
2547
/**
2648
* The `TextField` is a convenience wrapper for the most common cases (80%).
2749
* It cannot be all things to all people, otherwise the API would grow out of control.
@@ -54,12 +76,12 @@ export const styles = {
5476
* - using the upper case props for passing values directly to the components
5577
* - using the underlying components directly as shown in the demos
5678
*/
57-
const TextField = React.forwardRef(function TextField(props, ref) {
79+
const TextField = React.forwardRef(function TextField(inProps, ref) {
80+
const props = useThemeProps({ props: inProps, name: 'MuiTextField' });
5881
const {
5982
autoComplete,
6083
autoFocus = false,
6184
children,
62-
classes,
6385
className,
6486
color = 'primary',
6587
defaultValue,
@@ -92,6 +114,21 @@ const TextField = React.forwardRef(function TextField(props, ref) {
92114
...other
93115
} = props;
94116

117+
const styleProps = {
118+
...props,
119+
autoFocus,
120+
color,
121+
disabled,
122+
error,
123+
fullWidth,
124+
multiline,
125+
required,
126+
select,
127+
variant,
128+
};
129+
130+
const classes = useUtilityClasses(styleProps);
131+
95132
if (process.env.NODE_ENV !== 'production') {
96133
if (select && !children) {
97134
console.error(
@@ -154,7 +191,7 @@ const TextField = React.forwardRef(function TextField(props, ref) {
154191
);
155192

156193
return (
157-
<FormControl
194+
<TextFieldRoot
158195
className={clsx(classes.root, className)}
159196
disabled={disabled}
160197
error={error}
@@ -163,6 +200,7 @@ const TextField = React.forwardRef(function TextField(props, ref) {
163200
required={required}
164201
color={color}
165202
variant={variant}
203+
styleProps={styleProps}
166204
{...other}
167205
>
168206
{label && (
@@ -191,7 +229,7 @@ const TextField = React.forwardRef(function TextField(props, ref) {
191229
{helperText}
192230
</FormHelperText>
193231
)}
194-
</FormControl>
232+
</TextFieldRoot>
195233
);
196234
});
197235

@@ -353,6 +391,10 @@ TextField.propTypes /* remove-proptypes */ = {
353391
PropTypes.oneOf(['medium', 'small']),
354392
PropTypes.string,
355393
]),
394+
/**
395+
* The system prop that allows defining system overrides as well as additional CSS styles.
396+
*/
397+
sx: PropTypes.object,
356398
/**
357399
* Type of the `input` element. It should be [a valid HTML5 input type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types).
358400
*/
@@ -368,4 +410,4 @@ TextField.propTypes /* remove-proptypes */ = {
368410
variant: PropTypes.oneOf(['filled', 'outlined', 'standard']),
369411
};
370412

371-
export default withStyles(styles, { name: 'MuiTextField' })(TextField);
413+
export default TextField;

packages/material-ui/src/TextField/TextField.test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import * as React from 'react';
22
import { expect } from 'chai';
3-
import { getClasses, createMount, createClientRender, describeConformance } from 'test/utils';
4-
import FormControl from '../FormControl';
5-
import TextField from './TextField';
6-
import MenuItem from '../MenuItem';
7-
import { inputBaseClasses } from '../InputBase';
8-
import { outlinedInputClasses } from '../OutlinedInput';
3+
import { createMount, createClientRender, describeConformanceV5 } from 'test/utils';
4+
import FormControl from '@material-ui/core/FormControl';
5+
import { inputBaseClasses } from '@material-ui/core/InputBase';
6+
import MenuItem from '@material-ui/core/MenuItem';
7+
import { outlinedInputClasses } from '@material-ui/core/OutlinedInput';
8+
import TextField, { textFieldClasses as classes } from '@material-ui/core/TextField';
99

1010
describe('<TextField />', () => {
11-
let classes;
1211
const mount = createMount();
1312
const render = createClientRender();
1413

15-
before(() => {
16-
classes = getClasses(<TextField variant="standard" />);
17-
});
18-
19-
describeConformance(<TextField variant="standard" />, () => ({
14+
describeConformanceV5(<TextField variant="standard" />, () => ({
2015
classes,
2116
inheritComponent: FormControl,
17+
render,
2218
mount,
19+
muiName: 'MuiTextField',
2320
refInstanceof: window.HTMLDivElement,
24-
skip: ['componentProp'],
21+
testVariantProps: { variant: 'outlined' },
22+
skip: ['componentProp', 'componentsProp'],
2523
}));
2624

2725
describe('structure', () => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export { default } from './TextField';
2+
3+
export { default as textFieldClasses } from './textFieldClasses';
4+
export * from './textFieldClasses';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TextFieldClassKey } from './TextField';
2+
3+
declare const textFieldClasses: Record<TextFieldClassKey, string>;
4+
5+
export function getTextFieldUtilityClass(slot: string): string;
6+
7+
export default textFieldClasses;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled';
2+
3+
export function getTextFieldUtilityClass(slot) {
4+
return generateUtilityClass('MuiTextField', slot);
5+
}
6+
7+
const textFieldClasses = generateUtilityClasses('MuiTextField', ['root']);
8+
9+
export default textFieldClasses;

0 commit comments

Comments
 (0)