/** * Drop-down list item * * @author Brauer Ilya * @date 2020-03-02 */ import * as React from 'react'; import * as styles from './dropdown.m.scss'; import {renderIcon} from '../../utils/renderIcon'; import {createHtmlPropsFilter} from '../../utils/createHtmlPropsFilter'; import {joinClassNames} from '../../utils/joinClassNames'; import {Tooltip} from '../tooltip/Tooltip'; import {PLACEMENT, INTENT} from '../../constants'; import {safeInvoke} from '../../utils/safeInvoke'; import {getIntentThemeKey} from '../../utils/getIntentThemeKey'; type IProps = React.HTMLAttributes & { isMinimal?: boolean; isDisabled?: boolean; isReadonly?: boolean; isHeader?: boolean; rightIcon?: React.ReactNode; leftIcon?: React.ReactNode; forwardRef?: React.Ref; 'data-qaid'?: string; // ID for tests; isSelected?: boolean; onClick?: () => void; // Now we support only danger and default intent intent?: INTENT; } type IntentClassNames = 'ddItemIntentDefault' | 'ddItemIntentDanger'; const filterProps = createHtmlPropsFilter>([ 'isDisabled', 'isMinimal', 'isReadonly', 'rightIcon', 'children', 'leftIcon', 'forwardRef', 'isSelected', 'isHeader', 'intent' ]); export class Item extends React.Component { override componentDidMount () { document.addEventListener('keydown', this.onKeyDown); } override componentWillUnmount () { document.removeEventListener('keydown', this.onKeyDown); } onKeyDown = (e: KeyboardEvent) => { if (e.key === 'Enter' && this.props.isSelected === true) { safeInvoke(this.props.onClick); } } handleClick = (e: React.SyntheticEvent) => { e.stopPropagation(); e.preventDefault(); if (this.props.isDisabled !== true) { safeInvoke(this.props.onClick); } } override render () { const intent = this.props.intent || INTENT.DEFAULT; const intentThemeKey = getIntentThemeKey('ddItem', intent); const className = joinClassNames( styles.ddItem, [styles.ddItemMinimal, Boolean(this.props.isMinimal)], [styles.ddItemReadonly, Boolean(this.props.isReadonly)], [styles.ddItemDisabled, Boolean(this.props.isDisabled)], [styles.ddItemWithLeftIcon, Boolean(this.props.leftIcon)], [styles.ddItemSelected, Boolean(this.props.isSelected)], [styles.ddItemHeader, Boolean(this.props.isHeader)], styles[intentThemeKey] ); const ItemProps = { ...filterProps(this.props), className, ref: this.props.forwardRef, onClick: this.handleClick }; const isChildrenString = typeof this.props.children === 'string'; return (
{this.props.leftIcon !== undefined && (renderIcon( this.props.leftIcon, {}, { className: styles.ddItemLeftIconContainer } ))} { isChildrenString ? (
{this.props.children}
) : this.props.children } {this.props.rightIcon !== undefined && (renderIcon( this.props.rightIcon, {}, { className: styles.ddItemRightIconContainer } ))}
); } }