Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormControl] Convert to function component #15208

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
177 changes: 89 additions & 88 deletions packages/material-ui/src/FormControl/FormControl.js
Expand Up @@ -4,7 +4,6 @@ import clsx from 'clsx';
import { isFilled, isAdornedStart } from '../InputBase/utils';
import withStyles from '../styles/withStyles';
import { capitalize } from '../utils/helpers';
import withForwardedRef from '../utils/withForwardedRef';
import { isMuiElement } from '../utils/reactHelpers';
import FormControlContext from './FormControlContext';

Expand Down Expand Up @@ -49,114 +48,121 @@ export const styles = {
*
* ⚠️ Only one input can be used within a FormControl.
*/
class FormControl extends React.Component {
static getDerivedStateFromProps(props, state) {
if (props.disabled && state.focused) {
return { focused: false };
}
return null;
}
const FormControl = React.forwardRef(function FormControl(props, ref) {
const {
children,
classes,
className,
component: Component,
disabled,
error,
fullWidth,
margin,
required,
variant,
...other
} = props;
const [adornedStart] = React.useState(() => {
// We need to iterate through the children and find the Input in order
// to fully support server-side rendering.
let initialAdornedStart = false;
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved

constructor(props) {
super();
if (children) {
React.Children.forEach(children, child => {
if (!isMuiElement(child, ['Input', 'Select'])) {
return;
}

const input = isMuiElement(child, ['Select']) ? child.props.input : child;

this.state = {
adornedStart: false,
filled: false,
focused: false,
};
if (input && isAdornedStart(input.props)) {
initialAdornedStart = true;
}
});
}
return initialAdornedStart;
});

const [filled, setFilled] = React.useState(() => {
// We need to iterate through the children and find the Input in order
// to fully support server-side rendering.
const { children } = props;
let initialFilled = false;

if (children) {
React.Children.forEach(children, child => {
if (!isMuiElement(child, ['Input', 'Select'])) {
return;
}

if (isFilled(child.props, true)) {
this.state.filled = true;
}

const input = isMuiElement(child, ['Select']) ? child.props.input : child;

if (input && isAdornedStart(input.props)) {
this.state.adornedStart = true;
initialFilled = true;
}
});
}

return initialFilled;
});

const [focused, setFocused] = React.useState(false);

if (disabled && focused) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, at first I was like: wtf is this. But then, looking twice at the logic, it looks valid 👌.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the function component version of

static getDerivedStateFromProps(props, state) {
  if (props.disabled && state.focused) {
    return { focused: false };
  }
  return null;
}

Hooks FAQ: How do I implement getDerivedStateFromProps?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link.

setFocused(false);
}

handleFocus = () => {
this.setState(state => (!state.focused ? { focused: true } : null));
const handleFocus = () => {
setFocused(true);
};

handleBlur = () => {
this.setState(state => (state.focused ? { focused: false } : null));
const handleBlur = () => {
setFocused(false);
};

handleDirty = () => {
if (!this.state.filled) {
this.setState({ filled: true });
const handleDirty = () => {
if (!filled) {
setFilled(true);
}
};

handleClean = () => {
if (this.state.filled) {
this.setState({ filled: false });
const handleClean = () => {
if (filled) {
setFilled(false);
}
};

render() {
const {
classes,
className,
component: Component,
disabled,
error,
fullWidth,
innerRef,
margin,
required,
variant,
...other
} = this.props;
const { adornedStart, filled, focused } = this.state;

const childContext = {
adornedStart,
disabled,
error,
filled,
focused,
margin,
onBlur: this.handleBlur,
onEmpty: this.handleClean,
onFilled: this.handleDirty,
onFocus: this.handleFocus,
required,
variant,
};

return (
<FormControlContext.Provider value={childContext}>
<Component
className={clsx(
classes.root,
{
[classes[`margin${capitalize(margin)}`]]: margin !== 'none',
[classes.fullWidth]: fullWidth,
},
className,
)}
ref={innerRef}
{...other}
/>
</FormControlContext.Provider>
);
}
}
const childContext = {
adornedStart,
disabled,
error,
filled,
focused,
margin,
onBlur: handleBlur,
onEmpty: handleClean,
onFilled: handleDirty,
onFocus: handleFocus,
required,
variant,
};

return (
<FormControlContext.Provider value={childContext}>
<Component
className={clsx(
classes.root,
{
[classes[`margin${capitalize(margin)}`]]: margin !== 'none',
[classes.fullWidth]: fullWidth,
},
className,
)}
ref={ref}
{...other}
>
{children}
</Component>
</FormControlContext.Provider>
);
});

FormControl.propTypes = {
/**
Expand Down Expand Up @@ -189,11 +195,6 @@ FormControl.propTypes = {
* If `true`, the component will take up the full width of its container.
*/
fullWidth: PropTypes.bool,
/**
* @ignore
* from `withForwardRef`
*/
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
*/
Expand All @@ -218,4 +219,4 @@ FormControl.defaultProps = {
variant: 'standard',
};

export default withStyles(styles, { name: 'MuiFormControl' })(withForwardedRef(FormControl));
export default withStyles(styles, { name: 'MuiFormControl' })(FormControl);