-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[InputBase] Remove legacy lifecycle methods #13487
Merged
oliviertassinari
merged 3 commits into
mui:master
from
eps1lon:fix/core/InputBase/legacy-lifecycle-methods
Nov 5, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,47 +154,21 @@ export function formControlState({ props, states, context }) { | |
* It contains a load of style reset and some state logic. | ||
*/ | ||
class InputBase extends React.Component { | ||
static getDerivedStateFromProps(props, state) { | ||
// The blur won't fire when the disabled state is set on a focused input. | ||
// We need to book keep the focused state manually. | ||
if (props.disabled && state.focused) { | ||
return { focused: false }; | ||
} | ||
return null; | ||
} | ||
|
||
constructor(props, context) { | ||
super(props, context); | ||
this.isControlled = props.value != null; | ||
if (this.isControlled) { | ||
this.checkDirty(props); | ||
} | ||
|
||
const componentWillReceiveProps = (nextProps, nextContext) => { | ||
// The blur won't fire when the disabled state is set on a focused input. | ||
// We need to book keep the focused state manually. | ||
if ( | ||
!formControlState({ props: this.props, context: this.context, states: ['disabled'] }) | ||
.disabled && | ||
formControlState({ props: nextProps, context: nextContext, states: ['disabled'] }).disabled | ||
) { | ||
this.setState({ | ||
focused: false, | ||
}); | ||
} | ||
}; | ||
|
||
const componentWillUpdate = (nextProps, nextState, nextContext) => { | ||
// Book keep the focused state. | ||
if ( | ||
!formControlState({ props: this.props, context: this.context, states: ['disabled'] }) | ||
.disabled && | ||
formControlState({ props: nextProps, context: nextContext, states: ['disabled'] }).disabled | ||
) { | ||
const { muiFormControl } = this.context; | ||
if (muiFormControl && muiFormControl.onBlur) { | ||
muiFormControl.onBlur(); | ||
} | ||
} | ||
}; | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
this.componentWillReceiveProps = componentWillReceiveProps; | ||
this.componentWillReceiveProps.__suppressDeprecationWarning = true; | ||
this.componentWillUpdate = componentWillUpdate; | ||
this.componentWillUpdate.__suppressDeprecationWarning = true; | ||
/* eslint-enable no-underscore-dangle */ | ||
} | ||
|
||
state = { | ||
|
@@ -215,7 +189,14 @@ class InputBase extends React.Component { | |
} | ||
} | ||
|
||
componentDidUpdate() { | ||
componentDidUpdate(prevProps) { | ||
// Book keep the focused state. | ||
if (!prevProps.disabled && this.props.disabled) { | ||
const { muiFormControl } = this.context; | ||
if (muiFormControl && muiFormControl.onBlur) { | ||
muiFormControl.onBlur(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something that should go to the chopping block in a major. Why would someone put the component into the context if he then controls it via props on |
||
} | ||
} | ||
if (this.isControlled) { | ||
this.checkDirty(this.props); | ||
} // else performed in the onChange | ||
|
@@ -354,13 +335,15 @@ class InputBase extends React.Component { | |
states: ['disabled', 'error', 'margin', 'required', 'filled'], | ||
}); | ||
|
||
const focused = muiFormControl ? muiFormControl.focused : this.state.focused; | ||
|
||
const className = classNames( | ||
classes.root, | ||
{ | ||
[classes.disabled]: fcs.disabled, | ||
[classes.error]: fcs.error, | ||
[classes.fullWidth]: fullWidth, | ||
[classes.focused]: this.state.focused, | ||
[classes.focused]: focused, | ||
[classes.formControl]: muiFormControl, | ||
[classes.marginDense]: fcs.margin === 'dense', | ||
[classes.multiline]: multiline, | ||
|
@@ -424,7 +407,7 @@ class InputBase extends React.Component { | |
? renderPrefix({ | ||
...fcs, | ||
startAdornment, | ||
focused: this.state.focused, | ||
focused, | ||
}) | ||
: null} | ||
{startAdornment} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it's the InputBase that should take care of it by calling
handleBlur
. Why do we need this logic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InputBase already calls handleBlur separately if it's own focused state changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow, the disable change will propagate down to the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need an integration test? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's context not props propagation. If you think that some behavior was already covered feel free to add a test. Especially focused was not considered correctly.
But why should InputBase call onBlur in it's parent when the parent got changed and can take care of itself? No need to delegate that logic into the child.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The form control should propagate the disabled state to the input with the context. The input can then collect it. This way, we can avoid logic duplication. I think that we should remove the old context usage at the same time this logic is very dependant on the context implementation. I think that it will be simpler and less error prone to do both at the same time.
Yeah, it would be great to have an integration tests. It's tricky. Well, I'm also fine with logic duplication if it's simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only logic duplication if you have a mental model with the old context and lifecycle methods. I don't think the discussion is helpful here. If you know how to access context in gDSFP or cDU I'd be happy to change it but I don't know how.
I can only work with what's been given to me. InputBase was always tightly coupled with the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't, it's why I'm raising the new context api, where we would be injecting the context as properties.