All files / src/stories Table.stories.tsx

0% Statements 0/41
0% Branches 0/20
0% Functions 0/12
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190                                                                                                                                                                                                                                                                                                                                                                                           
import React, { useEffect, useState } from 'react';
import { Story } from '@storybook/react';
import { Box } from '@mui/material';
import Table, { EnhancedTopBarSelectionProps, Props as TableProps } from '../components/table/index';
import CellRenderer from '../components/table/TableCellRenderer';
import { RendererProps } from '../components/table/types';
 
export default {
  title: 'Table',
  component: Table,
  argTypes: {
    density: {
      options: ['comfortable', 'compact', 'roomy'],
      control: { type: 'radio' },
    },
  },
};
 
function Renderer(props: RendererProps<any>): JSX.Element {
  const { column } = props;
 
  if (column.key === 'middlename') {
    return <CellRenderer {...props} sx={{ fontStyle: 'italic' }} />;
  }
 
  return <CellRenderer {...props} />;
}
 
const Template: Story<Omit<TableProps<{ name: string; lastname: string }>, 'rows'> & { rowCount: number }> = (args) => {
  const [selectedRows, setSelectedRows] = useState<any>([]);
  const [rows, setRows] = useState<any>([]);
 
  args.columns[1].sx = { fontStyle: 'italic' };
 
  useEffect(() => {
    const nextRows = Array(args.rowCount)
      .fill({ name: '', middlename: '', lastname: '', occupation: '' })
      .map((i, j) => {
        if (j % 2 === 0) {
          return {
            name: `Constanza_${j}`,
            middlename: '',
            lastname: 'Uanini',
            occupation: 'Artist',
            date: '2023-02-03',
            pets: 5,
          };
        } else if (j % 3 === 0) {
          return {
            name: `Carlos_${j}`,
            middlename: '--',
            lastname: 'Thurber',
            occupation: 'Freelancer',
            available: 'false',
            date: '2023-04-12',
            pets: 10,
          };
        } else {
          return {
            name: `Jane${j}`,
            middlename: 'John',
            lastname: 'Doe',
            occupation: 'Business analyst',
            available: true,
            date: '2023-04-27',
            pets: 12,
            previousStudies: 'natural Sciences, social Sciences',
          };
        }
      });
 
    setRows(nextRows);
  }, [args.rowCount]);
 
  return (
    <Box paddingTop={args.showTopBar ? '50px' : 0} display='flex' flexGrow={1} flexDirection='column'>
      <Table
        {...args}
        rows={rows}
        Renderer={Renderer}
        selectedRows={selectedRows}
        setSelectedRows={setSelectedRows}
        topBarButtons={[
          {
            buttonType: 'productive',
            buttonText: 'Click',
            onButtonClick: () => window.alert('click'),
          },
          {
            buttonType: 'passive',
            buttonText: 'Disabled',
            onButtonClick: () => window.alert('you should not see this'),
            disabled: true,
          },
          {
            buttonType: 'passive',
            buttonText: 'Tooltip',
            onButtonClick: () => window.alert('you should not see this either'),
            disabled: true,
            tooltipTitle:
              'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
          },
        ]}
        tableComments='* Data collected in 2024'
      />
    </Box>
  );
};
 
export const Default = Template.bind({});
export const Selectable = Template.bind({});
export const Presorted = Template.bind({});
export const ShowTopBar = Template.bind({});
export const ShowTopBarV2 = Template.bind({});
 
Default.args = {
  orderBy: 'name',
  columns: [
    { key: 'name', name: 'Name', type: 'string' },
    { key: 'middlename', name: 'Middlename', type: 'string', tooltipTitle: 'Middle name is optional' },
    { key: 'lastname', name: 'Lastname', type: 'string' },
    { key: 'occupation', name: 'Occupation', type: 'string' },
    { key: 'available', name: 'Available', type: 'boolean' },
    { key: 'previousStudies', name: 'Previous Studies', type: 'string', alignment: 'right' },
    { key: 'date', name: 'Date', type: 'string', tooltipTitle: 'Right aligend with tooltip', alignment: 'right' },
    { key: 'pets', name: 'Pets', type: 'string', alignment: 'right' },
  ],
  rowCount: 150,
  showTopBar: false,
  booleanFalseText: 'No',
  booleanTrueText: 'Yes',
  editText: 'Edit',
  renderNumSelectedText: (num) => `${num} selected`,
  renderPaginationText: (from, to, total) => `${from} to ${to} of ${total}`,
  density: 'comfortable',
};
 
Selectable.args = {
  ...Default.args,
  showCheckbox: true,
  controlledOnSelect: true,
};
 
Presorted.args = {
  ...Default.args,
  isPresorted: true,
};
 
ShowTopBar.args = {
  ...Default.args,
  showCheckbox: true,
  controlledOnSelect: true,
  showTopBar: true,
};
 
const enhancedTopBarSelectionConfig: EnhancedTopBarSelectionProps = {
  renderEnhancedNumSelectedText(selectedCount: number, pageCount: number): string {
    switch (true) {
      case selectedCount === 1 && pageCount === 1: {
        return `${selectedCount} row selected.`;
      }
      case selectedCount > 1 && pageCount === 1: {
        return `${selectedCount} rows selected.`;
      }
      case selectedCount === 1 && pageCount > 1: {
        return `${selectedCount} row selected across ${pageCount} pages.`;
      }
      case selectedCount > 1 && pageCount > 1: {
        return `${selectedCount} rows selected across ${pageCount} pages.`;
      }
    }
 
    return '';
  },
  renderSelectAllText(rowsCount: number): string {
    return `Select all ${rowsCount} rows`;
  },
  renderSelectNoneText(): string {
    return 'Clear selection';
  },
};
 
ShowTopBarV2.args = {
  ...Default.args,
  showCheckbox: true,
  controlledOnSelect: true,
  showTopBar: true,
  enhancedTopBarSelectionConfig,
};