summaryrefslogtreecommitdiff
path: root/src/components/ListTable.tsx
blob: 85b86aa9a45720ea0349850b7969232dc59f8e91 (plain)
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
import React from 'react';

interface Field {
  key: string;
  label: string;
}

interface Props {
  items?: any[];
  fields: Field[];
  handleRowClick?: (index: number) => void;
}


const ListTable: React.FC<Props> = ({ items = [], fields, handleRowClick = () => {} }) => {
  if (!items.length) return <div className="text-center p-6">No data</div>;
  return (
    <table className="table-auto w-full">
      <thead>
        <tr className="border-b select-none">
          {fields.map(field => <th>{field.label}</th>)}
        </tr>
      </thead>
      <tbody>
        {items.map((item, index) => (
          <tr
            className={`border-b hover:bg-gray-100 cursor-pointer ${index % 2 && 'bg-gray-50'}`}
            onClick={() => handleRowClick(index)}
          >
            {fields.map(field => <td className="p-3">{item[field.key]}</td>)}
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default ListTable;