All files / ts/components/tables table.tsx

100% Statements 14/14
100% Branches 24/24
100% Functions 2/2
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10320x 20x 20x                                                                                                   20x 20x 21x 21x 14x 63x                                                     7x                             20x   20x  
import * as classNames from 'classnames';
import * as React from 'react';
import { HTMLProps, PureComponent } from 'react';
import { ComponentProps } from '../../types';
 
export interface TableProps extends ComponentProps, HTMLProps<HTMLElement> {
  /**
   * Currently unused.
   * @default "'sm'"
   */
  collapse?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
  /**
   * Allows the table to scroll horizontally.
   */
  scrollable?: boolean;
  /**
   * Applies striped styles to the table.
   */
  striped?: boolean;
  /**
   * Adds border styles to the table.
   */
  bordered?: boolean;
  /**
   * Applies styles to table rows on hover.
   */
  hover?: boolean;
  /**
   * Condensed table padding.
   */
  condensed?: boolean;
  /**
   * Fill the parent element.
   */
  fill?: boolean;
  /**
   * Applies `table-layout: fixed;` style so that all columns are the same width.
   */
  fixed?: boolean;
  /**
   * Fix the first cell of every row so they do not scroll.
   */
  fixRowHeaders?: boolean;
  /**
   * Set a width for the first column when fixed.
   */
  rowHeaderWidth?: number;
}
 
/**
 * Table component with additional styles & functionality.
 */
export class Table extends PureComponent<TableProps, {}> {
  public render () {
    const {
      className,
      children,
      collapse = 'sm',
      scrollable = true,
      fixRowHeaders,
      rowHeaderWidth,
      striped,
      bordered,
      hover,
      condensed,
      fill,
      fixed,
      component: Component = 'table',
      ...remainingProps
    } = this.props;
 
    const myClassNames = [
      'table',
      `${collapse}-collapse`,
      fixRowHeaders ? 'fix-row-headers' : null,
      striped ? 'striped' : null,
      bordered ? 'bordered' : null,
      hover ? 'hover' : null,
      condensed ? 'condensed' : null,
      fill ? 'fill' : null,
      fixed ? 'fixed' : null,
      className
    ];
 
    return (
      <div className="table-wrapper">
        <div style={{paddingLeft: fixRowHeaders ? rowHeaderWidth : null}}>
          <div className={scrollable ? 'table-scroller' : undefined}>
            <Component
              {...remainingProps}
              className={classNames(myClassNames)}
            >
              {children}
            </Component>
          </div>
        </div>
      </div>
    );
  }
}
 
export default Table;