11import React from 'react' ;
2- import PropTypes from 'prop-types' ;
32import classNames from 'classnames' ;
43import { type Placement } from 'react-bootstrap/Overlay' ;
54import { OverlayTrigger } from '../Overlay' ;
65import Tooltip from '../Tooltip' ;
76import Icon from '../Icon' ;
87
98interface Props extends React . HTMLAttributes < HTMLButtonElement > {
9+ /** Component that renders the icon, currently defaults to `Icon` */
1010 iconAs ?: React . ComponentType < any > ,
1111 /** Additional CSS class[es] to apply to this button */
1212 className ?: string ;
@@ -26,33 +26,33 @@ interface Props extends React.HTMLAttributes<HTMLButtonElement> {
2626 iconClassNames ?: string ;
2727 /** Click handler for the button */
2828 onClick ?: React . MouseEventHandler < HTMLButtonElement > ;
29- /** whether to show the `IconButton` in an active state, whose styling is distinct from default state */
29+ /** Whether to show the `IconButton` in an active state, whose styling is distinct from default state */
3030 isActive ?: boolean ;
3131 /** @deprecated Using FontAwesome icons is deprecated. Instead, pass iconAs={Icon} src={...} */
3232 icon ?: { prefix ?: string ; iconName ?: string , icon ?: any [ ] } ,
33- /** Type of button (uses Bootstrap options) */
33+ /** Type of button (uses Bootstrap options). */
3434 variant ?: 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'light' | 'dark' | 'black' | 'brand' ;
35- /** size of button to render */
35+ /** Size of button to render */
3636 size ?: 'sm' | 'md' | 'inline' ;
37- /** no children */
37+ /** No children */
3838 children ?: never ;
3939}
4040
41- const IconButton = React . forwardRef < HTMLButtonElement , Props > ( ( {
41+ const IconButton = React . forwardRef ( ( {
4242 className,
4343 alt,
44- invertColors,
44+ invertColors = false ,
4545 icon,
4646 src,
4747 iconClassNames,
48- onClick,
49- size,
50- variant,
51- iconAs,
52- isActive,
48+ onClick = ( ) => { } ,
49+ size = 'md' ,
50+ variant = 'primary' ,
51+ iconAs = Icon ,
52+ isActive = false ,
5353 children, // unused, just here because we don't want it to be part of 'attrs'
5454 ...attrs
55- } , ref ) => {
55+ } : Props , ref : React . ForwardedRef < HTMLButtonElement > ) => {
5656 const invert = invertColors ? 'inverse-' : '' ;
5757 const activeStyle = isActive ? `${ variant } -` : '' ;
5858 const IconComponent = iconAs ;
@@ -87,66 +87,18 @@ const IconButton = React.forwardRef<HTMLButtonElement, Props>(({
8787 ) ;
8888} ) ;
8989
90- IconButton . defaultProps = {
91- iconAs : Icon ,
92- src : undefined ,
93- icon : undefined ,
94- iconClassNames : undefined ,
95- className : undefined ,
96- invertColors : false ,
97- variant : 'primary' ,
98- size : 'md' ,
99- onClick : ( ) => { } ,
100- isActive : false ,
101- children : undefined ,
102- } ;
103-
104- IconButton . propTypes = {
105- /** A custom class name. */
106- className : PropTypes . string ,
107- /** Component that renders the icon, currently defaults to `Icon` */
108- iconAs : PropTypes . elementType as any ,
109- /** An icon component to render. Example import of a Paragon icon component:
110- * `import { Check } from '@openedx/paragon/icons';`
111- * */
112- src : PropTypes . elementType as any ,
113- /** Alt text for your icon. For best practice, avoid using alt text to describe
114- * the image in the `IconButton`. Instead, we recommend describing the function
115- * of the button. */
116- alt : PropTypes . string . isRequired ,
117- /** Changes icon styles for dark background */
118- invertColors : PropTypes . bool ,
119- /** Accepts a [Paragon icon](https://paragon-openedx.netlify.app/foundations/icons) */
120- icon : PropTypes . shape ( {
121- prefix : PropTypes . string ,
122- iconName : PropTypes . string ,
123- // eslint-disable-next-line react/forbid-prop-types
124- icon : PropTypes . array ,
125- } ) as any ,
126- /** Extra class names that will be added to the icon */
127- iconClassNames : PropTypes . string ,
128- /** Click handler for the button */
129- onClick : PropTypes . func ,
130- /** Type of button (uses Bootstrap options) */
131- variant : PropTypes . oneOf ( [ 'primary' , 'secondary' , 'success' , 'warning' , 'danger' , 'light' , 'dark' , 'black' , 'brand' ] ) ,
132- /** size of button to render */
133- size : PropTypes . oneOf ( [ 'sm' , 'md' , 'inline' ] ) ,
134- /** whether to show the `IconButton` in an active state, whose styling is distinct from default state */
135- isActive : PropTypes . bool ,
136- } ;
137-
13890interface PropsWithTooltip extends Props {
139- /** choose from https://popper.js.org/docs/v2/constructors/#options */
140- tooltipPlacement : Placement ,
141- /** any content to pass to tooltip content area */
91+ /** Tooltip placement can be top, left, right etc, choose from https://popper.js.org/docs/v2/constructors/#options */
92+ tooltipPlacement ? : Placement ,
93+ /** Any content to pass to tooltip content area */
14294 tooltipContent : React . ReactNode ,
14395}
14496
14597/**
14698 * An icon button wrapped in overlaytrigger to display a tooltip.
14799 */
148100function IconButtonWithTooltip ( {
149- tooltipPlacement, tooltipContent, ...props
101+ tooltipPlacement = 'top' , tooltipContent, ...props
150102} : PropsWithTooltip ) {
151103 const invert = props . invertColors ? 'inverse-' : '' ;
152104 return (
@@ -166,22 +118,6 @@ function IconButtonWithTooltip({
166118 ) ;
167119}
168120
169- IconButtonWithTooltip . defaultProps = {
170- ...IconButton . defaultProps ,
171- tooltipPlacement : 'top' ,
172- } ;
173-
174- IconButtonWithTooltip . propTypes = {
175- /** tooltip placement can be top, left, right etc, per https://popper.js.org/docs/v2/constructors/#options */
176- tooltipPlacement : PropTypes . string ,
177- /** any valid JSX or text to be rendered as tooltip contents */
178- tooltipContent : PropTypes . node . isRequired ,
179- /** Type of button (uses Bootstrap options) */
180- variant : PropTypes . oneOf ( [ 'primary' , 'secondary' , 'success' , 'warning' , 'danger' , 'light' , 'dark' , 'black' , 'brand' ] ) ,
181- /** Changes icon styles for dark background */
182- invertColors : PropTypes . bool ,
183- } ;
184-
185121( IconButton as any ) . IconButtonWithTooltip = IconButtonWithTooltip ;
186122
187123export default IconButton as typeof IconButton & {
0 commit comments