| 1 |
import { Component, createRef } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/SimpleList/simple-list'; |
| 4 |
import { SimpleListContext } from './SimpleList'; |
| 5 |
|
| 6 |
export interface SimpleListItemProps { |
| 7 |
/** id for the item. */ |
| 8 |
itemId?: number | string; |
| 9 |
/** Content rendered inside the SimpleList item */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** Additional classes added to the SimpleList <li> */ |
| 12 |
className?: string; |
| 13 |
/** Component type of the SimpleList item */ |
| 14 |
component?: 'button' | 'a'; |
| 15 |
/** Additional classes added to the SimpleList <a> or <button> */ |
| 16 |
componentClassName?: string; |
| 17 |
/** Additional props added to the SimpleList <a> or <button> */ |
| 18 |
componentProps?: any; |
| 19 |
/** Indicates if the link is current/highlighted */ |
| 20 |
isActive?: boolean; |
| 21 |
/** OnClick callback for the SimpleList item */ |
| 22 |
onClick?: (event: React.MouseEvent | React.ChangeEvent) => void; |
| 23 |
/** Type of button SimpleList item */ |
| 24 |
type?: 'button' | 'submit' | 'reset'; |
| 25 |
/** Default hyperlink location */ |
| 26 |
href?: string; |
| 27 |
} |
| 28 |
|
| 29 |
class SimpleListItem extends Component<SimpleListItemProps> { |
| 30 |
static displayName = 'SimpleListItem'; |
| 31 |
ref = createRef<any>(); |
| 32 |
static defaultProps: SimpleListItemProps = { |
| 33 |
children: null, |
| 34 |
className: '', |
| 35 |
isActive: false, |
| 36 |
component: 'button', |
| 37 |
componentClassName: '', |
| 38 |
type: 'button', |
| 39 |
href: '', |
| 40 |
onClick: () => {} |
| 41 |
}; |
| 42 |
|
| 43 |
render() { |
| 44 |
const { |
| 45 |
children, |
| 46 |
isActive, |
| 47 |
className, |
| 48 |
component: Component, |
| 49 |
componentClassName, |
| 50 |
componentProps, |
| 51 |
onClick, |
| 52 |
type, |
| 53 |
href, |
| 54 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 55 |
itemId, |
| 56 |
...props |
| 57 |
} = this.props; |
| 58 |
return ( |
| 59 |
<SimpleListContext.Consumer> |
| 60 |
{({ currentRef, updateCurrentRef, isControlled }) => { |
| 61 |
const isButton = Component === 'button'; |
| 62 |
const isCurrentItem = |
| 63 |
this.ref && currentRef && isControlled ? currentRef.current === this.ref.current : isActive; |
| 64 |
|
| 65 |
const additionalComponentProps = isButton |
| 66 |
? { |
| 67 |
type |
| 68 |
} |
| 69 |
: { |
| 70 |
tabIndex: 0, |
| 71 |
href |
| 72 |
}; |
| 73 |
|
| 74 |
return ( |
| 75 |
<li className={css('pf-v6-c-simple-list__item', className)} {...props}> |
| 76 |
<Component |
| 77 |
className={css( |
| 78 |
styles.simpleListItemLink, |
| 79 |
isCurrentItem && styles.modifiers.current, |
| 80 |
!isButton && styles.modifiers.link, |
| 81 |
componentClassName |
| 82 |
)} |
| 83 |
onClick={(evt: React.MouseEvent) => { |
| 84 |
onClick(evt); |
| 85 |
updateCurrentRef(this.ref, this.props); |
| 86 |
}} |
| 87 |
ref={this.ref} |
| 88 |
{...componentProps} |
| 89 |
{...additionalComponentProps} |
| 90 |
> |
| 91 |
{children} |
| 92 |
</Component> |
| 93 |
</li> |
| 94 |
); |
| 95 |
}} |
| 96 |
</SimpleListContext.Consumer> |
| 97 |
); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
export { SimpleListItem }; |