-
-
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
[styles] Improve ref forwarding #13676
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/* istanbul ignore file */ | ||
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. I'm removing all the istanbul ignore, @eps1lon is right, let's leave the 100% utopia. 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. To be fair that broken window analogy you linked made total sense to me. I just think that these comments won't help in that regard. At least this way we have the actual number to look at. Previously it was 100% (kind of; who knows how good it is actually). Now we know how good/bad we are and have tools helping us analyze it. Codesearch is not really that helpful IMO. It's like prefering 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. I agree. |
||
/* eslint-disable no-underscore-dangle */ | ||
|
||
import { ponyfillGlobal } from '@material-ui/utils'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,8 @@ import React from 'react'; | |
import PropTypes from 'prop-types'; | ||
import warning from 'warning'; | ||
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
import { getDisplayName } from '@material-ui/utils'; | ||
import { chainPropTypes, getDisplayName } from '@material-ui/utils'; | ||
import makeStyles from './makeStyles'; | ||
import RefHolder from './RefHolder'; | ||
import getThemeProps from './getThemeProps'; | ||
import useTheme from './useTheme'; | ||
|
||
|
@@ -67,11 +66,7 @@ const withStyles = (stylesOrCreator, options = {}) => Component => { | |
} | ||
} | ||
|
||
return ( | ||
<RefHolder ref={ref}> | ||
<Component ref={innerRef} classes={classes} {...more} /> | ||
</RefHolder> | ||
); | ||
return <Component ref={innerRef || ref} classes={classes} {...more} />; | ||
}); | ||
|
||
WithStyles.propTypes = { | ||
|
@@ -80,9 +75,20 @@ const withStyles = (stylesOrCreator, options = {}) => Component => { | |
*/ | ||
classes: PropTypes.object, | ||
/** | ||
* @deprecated | ||
* Use that property to pass a ref callback to the decorated component. | ||
*/ | ||
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), | ||
innerRef: chainPropTypes(PropTypes.oneOfType([PropTypes.func, PropTypes.object]), props => { | ||
if (props.innerRef == null) { | ||
return null; | ||
} | ||
|
||
return null; | ||
// return new Error( | ||
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. It's raising a warning as the ButtonBase is still using this API. 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. Right. I think I annotated it with |
||
// 'Material-UI: The `innerRef` prop is deprecated and will be removed in v5. ' + | ||
// 'Refs are now automatically forwarded to the inner component.', | ||
// ); | ||
}), | ||
}; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { Input } from '@material-ui/core'; | |
import { createMount } from '@material-ui/core/test-utils'; | ||
import { isMuiElement } from '@material-ui/core/utils/reactHelpers'; | ||
import createMuiTheme from '@material-ui/core/styles/createMuiTheme'; | ||
// import consoleErrorMock from 'test/utils/consoleErrorMock'; | ||
import StylesProvider from './StylesProvider'; | ||
import ThemeProvider from './ThemeProvider'; | ||
import withStyles from './withStyles'; | ||
|
@@ -38,6 +39,68 @@ describe('withStyles', () => { | |
assert.strictEqual(isMuiElement(<StyledInput />, ['Input']), true); | ||
}); | ||
|
||
describe('refs', () => { | ||
it('forwards ref to class components', () => { | ||
// eslint-disable-next-line react/prefer-stateless-function | ||
class TargetComponent extends React.Component { | ||
render() { | ||
return null; | ||
} | ||
} | ||
const StyledTarget = withStyles({})(TargetComponent); | ||
|
||
const ref = React.createRef(); | ||
mount( | ||
<React.Fragment> | ||
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. What should we do with the Fragment. Should we embrace 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. I think |
||
<StyledTarget ref={ref} /> | ||
</React.Fragment>, | ||
); | ||
assert.instanceOf(ref.current, TargetComponent); | ||
}); | ||
|
||
it('forwards refs to React.forwardRef types', () => { | ||
const StyledTarget = withStyles({})( | ||
// eslint-disable-next-line react/no-multi-comp | ||
React.forwardRef((props, ref) => <div {...props} ref={ref} />), | ||
); | ||
|
||
const ref = React.createRef(); | ||
mount( | ||
<React.Fragment> | ||
<StyledTarget ref={ref} /> | ||
</React.Fragment>, | ||
); | ||
assert.strictEqual(ref.current.nodeName, 'DIV'); | ||
}); | ||
|
||
// describe('innerRef', () => { | ||
// beforeEach(() => { | ||
// consoleErrorMock.spy(); | ||
// }); | ||
|
||
// afterEach(() => { | ||
// consoleErrorMock.reset(); | ||
// PropTypes.resetWarningCache(); | ||
// }); | ||
|
||
// it('is deprecated', () => { | ||
// const ThemedDiv = withStyles({})('div'); | ||
|
||
// mount( | ||
// <React.Fragment> | ||
// <ThemedDiv innerRef={React.createRef()} /> | ||
// </React.Fragment>, | ||
// ); | ||
|
||
// assert.strictEqual(consoleErrorMock.callCount(), 1); | ||
// assert.include( | ||
// consoleErrorMock.args()[0][0], | ||
// 'Warning: Failed prop type: Material-UI: The `innerRef` prop is deprecated', | ||
// ); | ||
// }); | ||
// }); | ||
}); | ||
|
||
it('should forward the properties', () => { | ||
const Test = props => <div>{props.foo}</div>; | ||
Test.propTypes = { | ||
|
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.
Forgotten in #13698