Table.js
65 lines
| 1 | const { __ } = wp.i18n; |
| 2 | const { useRef, useEffect, useState } = wp.element; |
| 3 | const { Card, CardBody ,Flex, FlexBlock, Button, ButtonGroup } = wp.components; |
| 4 | |
| 5 | import classNames from 'classnames'; |
| 6 | |
| 7 | export default ({ columns , data , onRowClick , title }) => { |
| 8 | |
| 9 | if (!data.length) { |
| 10 | return ( |
| 11 | <Card size="large" className="presto-card table-card"> |
| 12 | <CardBody className="presto-flow"> |
| 13 | <div className="presto-card__title"> |
| 14 | {title} |
| 15 | </div> |
| 16 | <div style={{ opacity: 0.65 }}> |
| 17 | {__("No data available.", "presto-player")} |
| 18 | </div> |
| 19 | </CardBody> |
| 20 | </Card> |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | return ( |
| 25 | <Card size="large" className="presto-card table-card"> |
| 26 | <CardBody className="presto-flow"> |
| 27 | <div className="presto-card__title"> |
| 28 | {title} |
| 29 | </div> |
| 30 | <table role="table" className={classNames('presto-table', { 'is-clickable': onRowClick })}> |
| 31 | <thead role="rowgroup"> |
| 32 | <tr role="row"> |
| 33 | {columns && columns.map((column) => { |
| 34 | return ( |
| 35 | <th key={column.key} role="columnheader"> |
| 36 | {column.label} |
| 37 | </th> |
| 38 | ); |
| 39 | })} |
| 40 | </tr> |
| 41 | </thead> |
| 42 | |
| 43 | <tbody role="rowgroup"> |
| 44 | {data.map((row , rowIndex) => { |
| 45 | return ( |
| 46 | <tr role="row" key={`row-${rowIndex}`} |
| 47 | onClick={() => onRowClick && onRowClick(row)} |
| 48 | > |
| 49 | {columns.map((column , columnIndex ) => { |
| 50 | return ( |
| 51 | <td role="cell" data-title={column.label} key={`row-${rowIndex}-${columnIndex}`} aria-label={column.label}> |
| 52 | {column.render ? column.render(row) : (<div>{column.value(row)}</div>)} |
| 53 | </td> |
| 54 | ); |
| 55 | })} |
| 56 | </tr> |
| 57 | ); |
| 58 | })} |
| 59 | </tbody> |
| 60 | </table> |
| 61 | </CardBody> |
| 62 | </Card> |
| 63 | ); |
| 64 | }; |
| 65 |