File size: 2,598 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Icon, plugins } from '@wordpress/icons';
import clsx from 'clsx';
import TextPlaceholder from 'calypso/jetpack-cloud/sections/partner-portal/text-placeholder';
import PluginCommonAction from '../plugin-common-actions';
import type { Columns, RowFormatterArgs } from '../../types';
import type { ReactElement, ReactNode } from 'react';

import './style.scss';

interface Props {
	isLoading: boolean;
	columns: Columns;
	items: Array< any >;
	rowFormatter: ( args: RowFormatterArgs ) => ReactNode;
	primaryKey: string;
	renderActions?: ( args: any ) => ReactElement;
	className?: string;
}

export default function PluginCommonTable( {
	isLoading,
	columns,
	items,
	rowFormatter,
	primaryKey,
	renderActions,
	className,
}: Props ) {
	return (
		<table className={ clsx( 'plugin-common-table__table', className ) }>
			<thead>
				<tr>
					{ columns
						.filter( ( column ) => column.header )
						.map( ( column ) => (
							<th colSpan={ column.colSpan || 1 } key={ column.key }>
								{ column.header }
							</th>
						) ) }
				</tr>
			</thead>
			<tbody>
				{ isLoading ? (
					<tr>
						{ columns.map( ( column ) => (
							<td key={ column.key }>
								{ column.key === 'plugin' && (
									<Icon
										size={ 32 }
										icon={ plugins }
										className="plugin-common-table__plugin-icon plugin-default-icon is-loading"
									/>
								) }
								<TextPlaceholder />
							</td>
						) ) }
					</tr>
				) : (
					items.map( ( item ) => {
						const id = item[ primaryKey ];
						return (
							<tr key={ `table-row-${ id }` } className="plugin-common-table__table-row">
								{ columns.map( ( column ) => {
									if ( column.key === 'bulk-actions' ) {
										// We don't want to render an empty table cell for the bulk actions.
										return null;
									}
									return (
										<td
											className={ clsx(
												column.smallColumn && 'plugin-common-table__small-column'
											) }
											key={ `table-data-${ column.key }-${ id }` }
											// As we don't render the bulk actions cell, we can
											// expand the update column a bit further.
											colSpan={ column.key === 'update' ? 2 : undefined }
										>
											{ rowFormatter( { columnKey: column.key, item } ) }
										</td>
									);
								} ) }
								{ renderActions && (
									<td className={ clsx( 'plugin-common-table__actions' ) }>
										<PluginCommonAction item={ item } renderActions={ renderActions } />
									</td>
								) }
							</tr>
						);
					} )
				) }
			</tbody>
		</table>
	);
}