import React, { PureComponent } from 'react'; import cls from 'classnames'; import PropTypes from 'prop-types'; import { cssClasses, strings } from '@douyinfe/semi-foundation/list/constants'; import { noop } from 'lodash'; import { Col } from '../grid'; import ListContext, { ListContextValue } from './list-context'; export interface ListItemProps { extra?: React.ReactNode; header?: React.ReactNode; main?: React.ReactNode; align?: 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch'; className?: string; children?: React.ReactNode; style?: React.CSSProperties; onClick?: React.MouseEventHandler; onRightClick?: React.MouseEventHandler; onMouseLeave?: React.MouseEventHandler; onMouseEnter?: React.MouseEventHandler } const prefixCls = cssClasses.PREFIX; export default class ListItem extends PureComponent { static contextType = ListContext; static propTypes = { extra: PropTypes.node, header: PropTypes.node, main: PropTypes.node, align: PropTypes.oneOf(strings.ALIGN), className: PropTypes.string, children: PropTypes.node, style: PropTypes.object, onClick: PropTypes.func, onRightClick: PropTypes.func, onMouseEnter: PropTypes.func, onMouseLeave: PropTypes.func, }; static defaultProps = { align: 'flex-start', onMouseEnter: noop, onMouseLeave: noop, }; context: ListContextValue; wrapWithGrid(content: React.ReactNode) { const { grid } = this.context; const { gutter, justify, type, align, ...rest } = grid; return ( {content} ); } render() { const { header, main, className, style, extra, children, align, onClick, onRightClick, onMouseEnter, onMouseLeave } = this.props; const { onRightClick: contextOnRightClick, onClick: contextOnClick, grid: contextGrid } = this.context; const handleContextMenu = onRightClick ? onRightClick : contextOnRightClick; const handleClick = onClick ? onClick : contextOnClick; const itemCls = cls(`${prefixCls}-item`, className); const bodyCls = cls(`${prefixCls}-item-body`, { [`${prefixCls}-item-body-${align}`]: align, } ); let body; if (header || main) { body = (
{header ?
{header}
: null} {main ?
{main}
: null}
); } let content = ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
  • {body ? body : null} {children} {extra ?
    {extra}
    : null}
  • ); if (this.context && contextGrid) { content = this.wrapWithGrid(content); } return content; } }