/** * @flow * @file Item list component * @author Box */ import React from 'react'; import classNames from 'classnames'; import { Table, Column } from 'react-virtualized/dist/es/Table'; import AutoSizer from 'react-virtualized/dist/es/AutoSizer'; import KeyBinder from '../common/KeyBinder'; import nameCellRenderer from '../common/item/nameCellRenderer'; import iconCellRenderer from '../common/item/iconCellRenderer'; import { isFocusableElement, focus } from '../../utils/dom'; import shareAccessCellRenderer from './shareAccessCellRenderer'; import checkboxCellRenderer from './checkboxCellRenderer'; import isRowSelectable from './cellRendererHelper'; import { VIEW_SELECTED, FIELD_NAME, FIELD_ID, FIELD_SHARED_LINK, TYPE_FOLDER } from '../../constants'; import './ItemList.scss'; type Props = { canSetShareAccess: boolean, currentCollection: Collection, extensionsWhitelist: string[], focusedRow: number, hasHitSelectionLimit: boolean, isSmall: boolean, onFocusChange: Function, onItemClick: Function, onItemSelect: Function, onShareAccessChange: Function, rootElement?: HTMLElement, rootId: string, selectableType: string, tableRef: Function, view: View, }; const ItemList = ({ view, rootId, isSmall, rootElement, focusedRow, selectableType, canSetShareAccess, hasHitSelectionLimit, extensionsWhitelist, onItemSelect, onItemClick, onShareAccessChange, onFocusChange, currentCollection, tableRef, }: Props) => { const iconCell = iconCellRenderer(); const nameCell = nameCellRenderer(rootId, view, onItemClick); const checkboxCell = checkboxCellRenderer(onItemSelect, selectableType, extensionsWhitelist, hasHitSelectionLimit); const shareAccessCell = shareAccessCellRenderer( onShareAccessChange, canSetShareAccess, selectableType, extensionsWhitelist, hasHitSelectionLimit, ); const { id, items = [] }: Collection = currentCollection; const rowCount: number = items.length; const rowClassName = ({ index }) => { if (index === -1) { return ''; } const { selected, type } = items[index]; const isSelectable = isRowSelectable(selectableType, extensionsWhitelist, hasHitSelectionLimit, items[index]); return classNames(`bcp-item-row bcp-item-row-${index}`, { 'bcp-item-row-selected': selected && view !== VIEW_SELECTED, 'bcp-item-row-unselectable': type !== TYPE_FOLDER && !isSelectable, // folder row should never dim }); }; const onRowClick = ({ event, rowData, index, }: { event: Event & { target: HTMLElement }, index: number, rowData: BoxItem, }) => { // If the click is happening on a clickable element on the item row, ignore row selection if ( isRowSelectable(selectableType, extensionsWhitelist, hasHitSelectionLimit, rowData) && !isFocusableElement(event.target) ) { onItemSelect(rowData); } else { onFocusChange(index); } }; return ( focus(rootElement, `.bcp-item-row-${scrollToRow}`)} > {({ onSectionRendered, scrollToRow, focusOnRender }) => ( {({ width, height }) => ( items[index]} ref={tableRef} rowClassName={rowClassName} onRowClick={onRowClick} scrollToIndex={scrollToRow} onRowsRendered={({ startIndex, stopIndex }) => { onSectionRendered({ rowStartIndex: startIndex, rowStopIndex: stopIndex, }); if (focusOnRender) { focus(rootElement, `.bcp-item-row-${scrollToRow}`); } }} > {isSmall ? null : ( )}
)}
)}
); }; export default ItemList;