/* eslint-disable jsx-a11y/no-static-element-interactions */ import React, { Fragment, useState } from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; import { IconButton, Icon, Box, ItemContainer, ActionItem, IconContainer, Circle, CheckBoxContainer, } from '../../styledComponents'; import { Props } from './interfaces'; import { EllipsisAction } from '../../interfaces'; import Marker from './components/Marker'; import Dropdown from '../Dropdown/index'; import Checkbox from '../../../Checkbox/index'; import RadioButton from '../../../RadioButton/index'; import IconEllipsisBlue from '../../../images/icon-ellipsis-blue.svg'; import IconEllipsisDark from '../../../images/icon-ellipsis-dark-small.svg'; import IconEllipsisWhite from '../../../images/icon-ellipsis-white-small.svg'; import { getTheme } from '../../../utils/theme'; import ListViewItem from '../ListViewItem'; const icons = { blue: { ellipsis: IconEllipsisBlue, ellipsisHover: IconEllipsisWhite, }, dark: { ellipsis: IconEllipsisDark, ellipsisHover: IconEllipsisWhite, }, }; const ListViewContent = (props: Props): JSX.Element => { const { theme, columns, data, selectRow, isSelectable, actionsOnHover, rowActions, actionOnClick, disableMultipleSelection, markerConfiguration, } = props; const updatedTheme = getTheme(theme, 'listView'); const [showActionOnHover, setShowActionOnHover] = useState( null ); const [isButtonHovered, setIsButtonHovered] = useState(null); const [isActionOpened, setIsActionOpened] = useState(null); const displayEllipsisButton = (index) => { return ( {index === showActionOnHover || index === isActionOpened ? ( ) : ( )} ); }; const handleClick = (event) => (clickFunction, row, index) => { if (row.isNotSelectable) { return; } if (event && event.stopPropagation) { event.stopPropagation(); } clickFunction(row, index); }; const injectValueInAction = (actions: EllipsisAction[], value: unknown) => { return actions.map((action) => ({ ...action, ...(action.actionIcon ? { actionIcon: () => action.actionIcon && action.actionIcon(value) } : {}), ...(action.isDisabled ? { isDisabled: () => action.isDisabled && action.isDisabled(value) } : { isDisabled: () => false }), ...(action.isHidden ? { isHidden: () => action.isHidden && action.isHidden(value) } : { isHidden: () => false }), ...(action.isHighlighted ? { isHighlighted: () => action.isHighlighted && action.isHighlighted(value), } : { isHighlighted: () => false }), ...(action.disabledTooltipText ? { disabledTooltipText: () => action.disabledTooltipText && action.disabledTooltipText(value), } : {}), actionLabel: () => action.actionLabel(value), handleAction: () => action.handleAction(value), })); }; const renderActionOnHover = (item, index) => { const actions = actionsOnHover(item); return ( {index === showActionOnHover && !!actionsOnHover && !!actions.length && actions.map((action, indexActionHover) => ( handleClick(e)(action.handleAction, item, index) } onMouseEnter={() => setIsButtonHovered(indexActionHover)} onMouseLeave={() => setIsButtonHovered(null)} > ))} ); }; return ( <> {data && data.map((item, index) => { return ( {markerConfiguration && !markerConfiguration.isHidden(item) && ( )} setShowActionOnHover(index)} onMouseLeave={() => setShowActionOnHover(null)} onClick={() => actionOnClick && actionOnClick(item, index)} > {isSelectable && !disableMultipleSelection && ( handleClick(e)(selectRow, item, index)} theme={getTheme(theme, 'checkbox')} > {}} isDisabled={item.isNotSelectable} /> )} {disableMultipleSelection && ( handleClick(e)(selectRow, item, index)} theme={getTheme(theme, 'checkbox')} > handleClick(e)(selectRow, item, index)} size="small" isReadOnly={item.isNotSelectable} /> )} {columns && columns.map( ({ name, propertyKey, large, minWidth, narrow, render, displayBigger, hidden, }) => { const value = get(item, propertyKey); if (hidden) { return ; } return ( ); } )} {!!actionsOnHover && renderActionOnHover(item, index)} {rowActions && !!rowActions.length && ( )} ); })} ); }; ListViewContent.propTypes = { columns: PropTypes.arrayOf( PropTypes.shape({ render: PropTypes.func, name: PropTypes.string.isRequired, propertyKey: PropTypes.string.isRequired, large: PropTypes.bool, minWidth: PropTypes.number, displayBigger: PropTypes.bool, }) ), // eslint-disable-next-line react/forbid-prop-types data: PropTypes.arrayOf(PropTypes.object), actionsOnHover: PropTypes.func, rowActions: PropTypes.arrayOf( PropTypes.shape({ actionLabel: PropTypes.func.isRequired, handleAction: PropTypes.func.isRequired, actionIcon: PropTypes.func, render: PropTypes.func, disabledTooltipText: PropTypes.func, }) ), selectRow: PropTypes.func.isRequired, isSelectable: PropTypes.bool.isRequired, actionOnClick: PropTypes.func, // eslint-disable-next-line react/forbid-prop-types theme: PropTypes.objectOf(PropTypes.any), markerConfiguration: PropTypes.shape({ isHidden: PropTypes.func.isRequired, backgroundColor: PropTypes.string.isRequired, icon: PropTypes.shape({ src: PropTypes.string.isRequired, }), }), }; ListViewContent.defaultProps = { data: [], columns: [], actionsOnHover: null, rowActions: null, actionOnClick: null, theme: null, markerConfiguration: null, }; export default ListViewContent;