import * as React from 'react' import * as CSS from 'csstype' import { Tooltip } from './tooltip' export const descending = 'desc' export const ascending = 'asc' const dataTableHeaderClassName = 'data-table-header' const left: CSS.TextAlignProperty = "left" const right: CSS.TextAlignProperty = "right" const indeterminateSrc = 'https://assets.quill.org/images/icons/indeterminate.svg' const removeSrc = 'https://assets.quill.org/images/icons/remove.svg' const moreHorizontalSrc = 'https://assets.quill.org/images/icons/more-horizontal.svg' const smallWhiteCheckSrc = 'https://assets.quill.org/images/shared/check-small-white.svg' const arrowSrc = 'https://assets.quill.org/images/shared/arrow.svg' interface DataTableRow { id: number|string; actions?: Array<{name: string, action: Function}> [key:string]: any; } interface DataTableHeader { width: string; name: string; attribute: string; noTooltip?: boolean; rowSectionClassName?: string; headerClassName?: string; isSortable?: boolean; } interface DataTableProps { headers: Array; rows: Array; averageFontWidth?: number; className?: string; defaultSortAttribute?: string; defaultSortDirection?: string; showCheckboxes?: boolean; showRemoveIcon?: boolean; showActions?: boolean; removeRow?: (event: any) => void; checkRow?: (event: any) => void; uncheckRow?: (event: any) => void; uncheckAllRows?: (event: any) => void; checkAllRows?: (event: any) => void; } interface DataTableState { sortAttribute?: string; sortAscending?: boolean; rowWithActionsOpen?: number|string; } export class DataTable extends React.Component { private selectedStudentActions: any // eslint-disable-line react/sort-comp static defaultProps: { averageFontWidth: number } // eslint-disable-line react/sort-comp constructor(props) { super(props) this.state = { sortAttribute: props.defaultSortAttribute || null, sortAscending: props.defaultSortDirection !== descending } } componentWillMount() { document.addEventListener('mousedown', this.handleClick, false) } componentWillUnmount() { document.removeEventListener('mousedown', this.handleClick, false) } handleClick = (e) => { if (this.selectedStudentActions && !this.selectedStudentActions.contains(e.target)) { this.setState({ rowWithActionsOpen: null }) } } attributeAlignment(attributeName): CSS.TextAlignProperty { const { rows, } = this.props const numbersRegex = new RegExp(/^[\d#%\.\$]+$/) return rows.every(row => numbersRegex.test(row[attributeName]) || !row[attributeName]) ? right : left } changeSortDirection = () => { this.setState(prevState => ({ sortAscending: !prevState.sortAscending })) } sortRows() { const { sortAttribute, sortAscending, } = this.state const { rows } = this.props if (sortAttribute) { return sortAscending ? rows.sort((a, b) => a[sortAttribute] - b[sortAttribute]) : rows.sort((a, b) => b[sortAttribute] - a[sortAttribute]) } return rows } clickAction(action, id) { action(id) this.setState({ rowWithActionsOpen: null }) } renderHeaderCheckbox() { const { showCheckboxes, rows, uncheckAllRows, checkAllRows } = this.props if (!showCheckboxes) { return } const allDisabled = rows.every(row => row.checkDisabled) if (allDisabled) { return } const allChecked = rows.every(row => row.checked) if (allChecked) { return () } const anyChecked = rows.some(row => row.checked) if (anyChecked) { return () } return } if (row.checkDisabled) { return } return } return } renderActions(row) { if (!row.actions) { return } const { rowWithActionsOpen } = this.state const actionsIsOpen = rowWithActionsOpen === row.id; return ( {actionsIsOpen ? this.renderOpenActions(row) : this.renderClosedActions(row)} ) } renderOpenActions(row) { const rowActions = row.actions.map(act => ) return (
this.selectedStudentActions = node}>
{rowActions}
) } renderClosedActions(row) { return () } renderHeader(header) { const { sortAscending, } = this.state let sortArrow, onClick let tabIndex = -1 let className = `${dataTableHeaderClassName} ${header.headerClassName}` let style: React.CSSProperties = { width: `${header.width}`, minWidth: `${header.width}`, textAlign: `${this.attributeAlignment(header.attribute)}` as CSS.TextAlignProperty } if (header.isSortable) { const sortDirection = sortAscending ? ascending : descending onClick = this.changeSortDirection sortArrow = arrow className+= ' sortable' tabIndex = 0 } return () } renderHeaders() { const { headers, } = this.props const headerItems = headers.map(header => this.renderHeader(header)) return
{this.renderHeaderCheckbox()}{headerItems}{this.renderHeaderForRemoval()}{this.renderActionsHeader()}
} renderRowSection(row, header) { const { averageFontWidth, } = this.props let style: React.CSSProperties = { width: `${header.width}`, minWidth: `${header.width}`, textAlign: `${this.attributeAlignment(header.attribute)}` as CSS.TextAlignProperty } const sectionText = row[header.attribute] const headerWidthNumber = Number(header.width.slice(0, -2)) const dataTableRowSectionClassName = `data-table-row-section ${header.rowSectionClassName}` const key = `${header.attribute}-${row.id || sectionText}` if (!header.noTooltip && (String(sectionText).length * averageFontWidth) >= headerWidthNumber) { return () } else { return ( {sectionText} ) } } renderRow(row) { const { headers, } = this.props const rowClassName = `data-table-row ${row.checked ? 'checked' : ''} ${row.className || ''}` const rowSections = headers.map(header => this.renderRowSection(row, header)) return
{this.renderRowCheckbox(row)}{rowSections}{this.renderRowRemoveIcon(row)}{this.renderActions(row)}
} renderRows() { const rows = this.sortRows().map(row => this.renderRow(row)) return
{rows}
} render() { const { className, } = this.props return (
{this.renderHeaders()} {this.renderRows()}
) } } DataTable.defaultProps = { averageFontWidth: 7 }