Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refs): migrate more components to properly forward their refs
Migrates Image and ProgressBar components to forward their refs properly. Relevant to #4194.
- Loading branch information
Showing
2 changed files
with
135 additions
and
119 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,72 @@ | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import React, { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { createBootstrapComponent } from './ThemeProvider'; | ||
import { useBootstrapPrefix } from './ThemeProvider'; | ||
|
||
class Image extends React.Component { | ||
static propTypes = { | ||
/** | ||
* @default 'img' | ||
*/ | ||
bsPrefix: PropTypes.string, | ||
/** | ||
* Sets image as fluid image. | ||
*/ | ||
fluid: PropTypes.bool, | ||
const propTypes = { | ||
/** | ||
* @default 'img' | ||
*/ | ||
bsPrefix: PropTypes.string, | ||
/** | ||
* Sets image as fluid image. | ||
*/ | ||
fluid: PropTypes.bool, | ||
|
||
/** | ||
* Sets image shape as rounded. | ||
*/ | ||
rounded: PropTypes.bool, | ||
/** | ||
* Sets image shape as rounded. | ||
*/ | ||
rounded: PropTypes.bool, | ||
|
||
/** | ||
* Sets image shape as circle. | ||
*/ | ||
roundedCircle: PropTypes.bool, | ||
/** | ||
* Sets image shape as circle. | ||
*/ | ||
roundedCircle: PropTypes.bool, | ||
|
||
/** | ||
* Sets image shape as thumbnail. | ||
*/ | ||
thumbnail: PropTypes.bool, | ||
}; | ||
/** | ||
* Sets image shape as thumbnail. | ||
*/ | ||
thumbnail: PropTypes.bool, | ||
}; | ||
|
||
static defaultProps = { | ||
fluid: false, | ||
rounded: false, | ||
roundedCircle: false, | ||
thumbnail: false, | ||
}; | ||
const defaultProps = { | ||
fluid: false, | ||
rounded: false, | ||
roundedCircle: false, | ||
thumbnail: false, | ||
}; | ||
|
||
render() { | ||
const { | ||
bsPrefix, | ||
className, | ||
fluid, | ||
rounded, | ||
roundedCircle, | ||
thumbnail, | ||
...props | ||
} = this.props; | ||
const Image = React.forwardRef( | ||
( | ||
{ bsPrefix, className, fluid, rounded, roundedCircle, thumbnail, ...props }, | ||
ref, | ||
) => { | ||
bsPrefix = useBootstrapPrefix(bsPrefix, 'img'); | ||
|
||
const classes = classNames( | ||
fluid && `${bsPrefix}-fluid`, | ||
rounded && `rounded`, | ||
roundedCircle && `rounded-circle`, | ||
thumbnail && `${bsPrefix}-thumbnail`, | ||
const classes = useMemo( | ||
() => | ||
classNames( | ||
fluid && `${bsPrefix}-fluid`, | ||
rounded && `rounded`, | ||
roundedCircle && `rounded-circle`, | ||
thumbnail && `${bsPrefix}-thumbnail`, | ||
), | ||
[fluid, rounded, roundedCircle, thumbnail, bsPrefix], | ||
); | ||
|
||
return ( | ||
<img // eslint-disable-line jsx-a11y/alt-text | ||
ref={ref} | ||
{...props} | ||
className={classNames(className, classes)} | ||
/> | ||
); | ||
} | ||
} | ||
}, | ||
); | ||
|
||
export default createBootstrapComponent(Image, 'img'); | ||
Image.displayName = 'Image'; | ||
Image.propTypes = propTypes; | ||
Image.defaultProps = defaultProps; | ||
|
||
export default Image; |
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