diff --git a/docs/src/pages/demos/selects/NativeSelects.js b/docs/src/pages/demos/selects/NativeSelects.js index 4e8bc9136b2361..ff09fb50a2bea5 100644 --- a/docs/src/pages/demos/selects/NativeSelects.js +++ b/docs/src/pages/demos/selects/NativeSelects.js @@ -1,5 +1,4 @@ import React from 'react'; -import ReactDOM from 'react-dom'; import { makeStyles } from '@material-ui/core/styles'; import Input from '@material-ui/core/Input'; import OutlinedInput from '@material-ui/core/OutlinedInput'; @@ -31,10 +30,10 @@ function NativeSelects() { name: 'hai', }); - const inputLabelRef = React.useRef(null); + const inputLabel = React.useRef(null); const [labelWidth, setLabelWidth] = React.useState(0); React.useEffect(() => { - setLabelWidth(ReactDOM.findDOMNode(inputLabelRef.current).offsetWidth); + setLabelWidth(inputLabel.current.offsetWidth); }, []); const handleChange = name => event => { @@ -189,7 +188,7 @@ function NativeSelects() { Required - + Age { - setLabelWidth(ReactDOM.findDOMNode(inputLabelRef.current).offsetWidth); + setLabelWidth(inputLabel.current.offsetWidth); }, []); function handleChange(event) { @@ -223,7 +222,7 @@ function SimpleSelect() { Required - + Age ({ }); class ComposedTextField extends React.Component { + label = React.createRef(); + state = { + labelWidth: 0, name: 'Composed TextField', }; componentDidMount() { - this.forceUpdate(); + this.setState({ labelWidth: this.label.current.offsetWidth }); } handleChange = event => { @@ -33,19 +35,20 @@ class ComposedTextField extends React.Component { }; render() { + const { labelWidth, name } = this.state; const { classes } = this.props; return ( Name - + Name @@ -54,14 +57,14 @@ class ComposedTextField extends React.Component { Name - + Disabled Name @@ -69,24 +72,19 @@ class ComposedTextField extends React.Component { Error - { - this.labelRef = ReactDOM.findDOMNode(ref); - }} - htmlFor="component-outlined" - > + Name Name - + ); diff --git a/docs/src/pages/demos/text-fields/ComposedTextField.tsx b/docs/src/pages/demos/text-fields/ComposedTextField.tsx index 1a7661f16d5fb7..8c96ffe158a60b 100644 --- a/docs/src/pages/demos/text-fields/ComposedTextField.tsx +++ b/docs/src/pages/demos/text-fields/ComposedTextField.tsx @@ -1,5 +1,4 @@ import React, { ComponentClass } from 'react'; -import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import { createStyles, Theme, withStyles, WithStyles } from '@material-ui/core/styles'; import FilledInput from '@material-ui/core/FilledInput'; @@ -23,18 +22,20 @@ const styles = (theme: Theme) => export interface Props extends WithStyles {} interface State { + labelWidth: number; name: string; } class ComposedTextField extends React.Component { - labelRef: HTMLElement | null | undefined; + label = React.createRef(); state = { + labelWidth: 0, name: 'Composed TextField', }; componentDidMount() { - this.forceUpdate(); + this.setState({ labelWidth: this.label.current!.offsetWidth }); } handleChange = (event: React.ChangeEvent) => { @@ -42,19 +43,20 @@ class ComposedTextField extends React.Component { }; render() { + const { labelWidth, name } = this.state; const { classes } = this.props; return ( Name - + Name @@ -62,38 +64,33 @@ class ComposedTextField extends React.Component { Name - + Disabled Name Error - { - this.labelRef = ReactDOM.findDOMNode(ref!) as HTMLLabelElement | null; - }} - htmlFor="component-outlined" - > + Name Name - + ); diff --git a/packages/material-ui/src/FormLabel/FormLabel.d.ts b/packages/material-ui/src/FormLabel/FormLabel.d.ts index eb077775734168..d1dca65d280832 100644 --- a/packages/material-ui/src/FormLabel/FormLabel.d.ts +++ b/packages/material-ui/src/FormLabel/FormLabel.d.ts @@ -7,6 +7,11 @@ export interface FormLabelProps extends StandardProps` is used. + */ + ref?: React.Ref; required?: boolean; } diff --git a/packages/material-ui/src/InputLabel/InputLabel.d.ts b/packages/material-ui/src/InputLabel/InputLabel.d.ts index c2356b8958154f..7cb5088c418c4b 100644 --- a/packages/material-ui/src/InputLabel/InputLabel.d.ts +++ b/packages/material-ui/src/InputLabel/InputLabel.d.ts @@ -26,4 +26,6 @@ export type InputLabelClassKey = | 'filled' | 'outlined'; -export default class InputLabel extends React.Component {} +declare const InputLabel: React.ComponentType; + +export default InputLabel;