2020-01-16 00:48:55 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
2020-02-20 12:54:33 -05:00
|
|
|
import cx from 'classnames';
|
2020-02-27 12:30:52 -05:00
|
|
|
import { TableInstance } from 'react-table';
|
2020-09-09 16:18:55 -04:00
|
|
|
import { styled } from '@superset-ui/core';
|
2020-06-16 20:30:10 -04:00
|
|
|
import Icon from 'src/components/Icon';
|
2020-01-16 00:48:55 -05:00
|
|
|
|
2020-08-13 17:46:56 -04:00
|
|
|
interface TableCollectionProps {
|
2020-01-16 00:48:55 -05:00
|
|
|
getTableProps: (userProps?: any) => any;
|
|
|
|
getTableBodyProps: (userProps?: any) => any;
|
2020-02-27 12:30:52 -05:00
|
|
|
prepareRow: TableInstance['prepareRow'];
|
|
|
|
headerGroups: TableInstance['headerGroups'];
|
|
|
|
rows: TableInstance['rows'];
|
2020-08-21 13:32:37 -04:00
|
|
|
columns: TableInstance['column'][];
|
2020-01-16 00:48:55 -05:00
|
|
|
loading: boolean;
|
2020-10-05 19:21:59 -04:00
|
|
|
highlightRowId?: number;
|
2020-01-16 00:48:55 -05:00
|
|
|
}
|
2020-06-23 17:17:28 -04:00
|
|
|
|
2020-10-14 14:15:34 -04:00
|
|
|
export const Table = styled.table`
|
2020-10-21 12:27:48 -04:00
|
|
|
background-color: ${({ theme }) => theme.colors.grayscale.light5};
|
2020-08-13 17:46:56 -04:00
|
|
|
border-collapse: separate;
|
2020-10-09 16:06:26 -04:00
|
|
|
border-radius: ${({ theme }) => theme.borderRadius}px;
|
2020-08-13 17:46:56 -04:00
|
|
|
|
2020-10-09 16:06:26 -04:00
|
|
|
thead > tr > th {
|
|
|
|
border: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tbody {
|
|
|
|
tr:first-of-type > td {
|
|
|
|
border-top: 0;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 17:17:28 -04:00
|
|
|
th {
|
2020-08-13 17:46:56 -04:00
|
|
|
background: ${({ theme }) => theme.colors.grayscale.light5};
|
2020-10-03 01:21:10 -04:00
|
|
|
position: sticky;
|
2020-08-13 17:46:56 -04:00
|
|
|
top: 0;
|
2021-01-13 02:29:43 -05:00
|
|
|
|
2020-08-13 17:46:56 -04:00
|
|
|
&:first-of-type {
|
|
|
|
padding-left: ${({ theme }) => theme.gridUnit * 4}px;
|
|
|
|
}
|
|
|
|
|
2020-06-23 17:17:28 -04:00
|
|
|
&.xs {
|
|
|
|
min-width: 25px;
|
|
|
|
}
|
|
|
|
&.sm {
|
|
|
|
min-width: 50px;
|
|
|
|
}
|
|
|
|
&.md {
|
|
|
|
min-width: 75px;
|
|
|
|
}
|
|
|
|
&.lg {
|
|
|
|
min-width: 100px;
|
|
|
|
}
|
|
|
|
&.xl {
|
|
|
|
min-width: 150px;
|
|
|
|
}
|
|
|
|
&.xxl {
|
|
|
|
min-width: 200px;
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:04:53 -05:00
|
|
|
span {
|
|
|
|
white-space: nowrap;
|
2021-01-13 02:29:43 -05:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
line-height: 2;
|
2021-01-09 11:04:53 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 17:17:28 -04:00
|
|
|
svg {
|
|
|
|
display: inline-block;
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 17:46:56 -04:00
|
|
|
|
2020-06-23 17:17:28 -04:00
|
|
|
td {
|
|
|
|
&.xs {
|
|
|
|
width: 25px;
|
|
|
|
}
|
|
|
|
&.sm {
|
|
|
|
width: 50px;
|
|
|
|
}
|
|
|
|
&.md {
|
|
|
|
width: 75px;
|
|
|
|
}
|
|
|
|
&.lg {
|
|
|
|
width: 100px;
|
|
|
|
}
|
|
|
|
&.xl {
|
|
|
|
width: 150px;
|
|
|
|
}
|
|
|
|
&.xxl {
|
|
|
|
width: 200px;
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 17:46:56 -04:00
|
|
|
|
|
|
|
.table-cell-loader {
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
.loading-bar {
|
|
|
|
background-color: ${({ theme }) => theme.colors.secondary.light4};
|
|
|
|
border-radius: 7px;
|
|
|
|
|
|
|
|
span {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&:after {
|
|
|
|
position: absolute;
|
|
|
|
transform: translateY(-50%);
|
|
|
|
top: 50%;
|
|
|
|
left: 0;
|
|
|
|
content: '';
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
height: 48px;
|
|
|
|
background-image: linear-gradient(
|
|
|
|
100deg,
|
|
|
|
rgba(255, 255, 255, 0),
|
|
|
|
rgba(255, 255, 255, 0.5) 60%,
|
|
|
|
rgba(255, 255, 255, 0) 80%
|
|
|
|
);
|
|
|
|
background-size: 200px 48px;
|
|
|
|
background-position: -100px 0;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
animation: loading-shimmer 1s infinite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.actions {
|
|
|
|
white-space: nowrap;
|
|
|
|
min-width: 100px;
|
|
|
|
|
|
|
|
svg,
|
|
|
|
i {
|
|
|
|
margin-right: 8px;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
path {
|
|
|
|
fill: ${({ theme }) => theme.colors.primary.base};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-row {
|
|
|
|
.actions {
|
|
|
|
opacity: 0;
|
2020-10-28 18:46:24 -04:00
|
|
|
font-size: ${({ theme }) => theme.typography.sizes.xl}px;
|
2020-08-13 17:46:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${({ theme }) => theme.colors.secondary.light5};
|
|
|
|
|
|
|
|
.actions {
|
|
|
|
opacity: 1;
|
|
|
|
transition: opacity ease-in ${({ theme }) => theme.transitionTiming}s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-row-selected {
|
|
|
|
background-color: ${({ theme }) => theme.colors.secondary.light4};
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background-color: ${({ theme }) => theme.colors.secondary.light4};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-cell {
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
max-width: 300px;
|
|
|
|
line-height: 1;
|
|
|
|
vertical-align: middle;
|
|
|
|
&:first-of-type {
|
|
|
|
padding-left: ${({ theme }) => theme.gridUnit * 4}px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@keyframes loading-shimmer {
|
|
|
|
40% {
|
|
|
|
background-position: 100% 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
100% {
|
|
|
|
background-position: 100% 0;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 17:17:28 -04:00
|
|
|
`;
|
|
|
|
|
2020-10-14 14:15:34 -04:00
|
|
|
Table.displayName = 'table';
|
|
|
|
|
2021-01-08 15:07:51 -05:00
|
|
|
export default React.memo(
|
|
|
|
({
|
|
|
|
getTableProps,
|
|
|
|
getTableBodyProps,
|
|
|
|
prepareRow,
|
|
|
|
headerGroups,
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
loading,
|
|
|
|
highlightRowId,
|
|
|
|
}: TableCollectionProps) => (
|
2020-10-14 19:12:01 -04:00
|
|
|
<Table
|
|
|
|
{...getTableProps()}
|
|
|
|
className="table table-hover"
|
|
|
|
data-test="listview-table"
|
|
|
|
>
|
2020-01-16 00:48:55 -05:00
|
|
|
<thead>
|
2020-02-20 12:54:33 -05:00
|
|
|
{headerGroups.map(headerGroup => (
|
2020-01-16 00:48:55 -05:00
|
|
|
<tr {...headerGroup.getHeaderGroupProps()}>
|
2020-06-16 20:30:10 -04:00
|
|
|
{headerGroup.headers.map(column => {
|
|
|
|
let sortIcon = <Icon name="sort" />;
|
2020-06-23 17:17:28 -04:00
|
|
|
if (column.isSorted && column.isSortedDesc) {
|
2020-06-16 20:30:10 -04:00
|
|
|
sortIcon = <Icon name="sort-desc" />;
|
2020-06-23 17:17:28 -04:00
|
|
|
} else if (column.isSorted && !column.isSortedDesc) {
|
2020-06-16 20:30:10 -04:00
|
|
|
sortIcon = <Icon name="sort-asc" />;
|
|
|
|
}
|
|
|
|
return column.hidden ? null : (
|
2020-02-27 12:30:52 -05:00
|
|
|
<th
|
2020-03-13 15:35:00 -04:00
|
|
|
{...column.getHeaderProps(
|
2020-06-29 00:37:04 -04:00
|
|
|
column.canSort ? column.getSortByToggleProps() : {},
|
2020-03-13 15:35:00 -04:00
|
|
|
)}
|
2020-02-27 12:30:52 -05:00
|
|
|
data-test="sort-header"
|
2020-06-23 17:17:28 -04:00
|
|
|
className={cx({
|
|
|
|
[column.size || '']: column.size,
|
|
|
|
})}
|
2020-02-27 12:30:52 -05:00
|
|
|
>
|
2020-06-23 17:17:28 -04:00
|
|
|
<span>
|
|
|
|
{column.render('Header')}
|
2020-06-29 00:37:04 -04:00
|
|
|
{column.canSort && sortIcon}
|
2020-06-23 17:17:28 -04:00
|
|
|
</span>
|
2020-02-27 12:30:52 -05:00
|
|
|
</th>
|
2020-06-16 20:30:10 -04:00
|
|
|
);
|
|
|
|
})}
|
2020-01-16 00:48:55 -05:00
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</thead>
|
|
|
|
<tbody {...getTableBodyProps()}>
|
2020-08-21 13:32:37 -04:00
|
|
|
{loading &&
|
|
|
|
rows.length === 0 &&
|
|
|
|
[...new Array(25)].map((_, i) => (
|
|
|
|
<tr key={i}>
|
|
|
|
{columns.map((column, i2) => {
|
|
|
|
if (column.hidden) return null;
|
2020-01-16 00:48:55 -05:00
|
|
|
return (
|
2020-04-15 01:04:24 -04:00
|
|
|
<td
|
2020-08-21 13:32:37 -04:00
|
|
|
key={i2}
|
2020-06-23 17:17:28 -04:00
|
|
|
className={cx('table-cell', {
|
|
|
|
'table-cell-loader': loading,
|
2020-08-21 13:32:37 -04:00
|
|
|
[column.size || '']: column.size,
|
2020-06-23 17:17:28 -04:00
|
|
|
})}
|
2020-04-15 01:04:24 -04:00
|
|
|
>
|
2021-03-11 17:08:16 -05:00
|
|
|
<span className="loading-bar" role="progressbar">
|
2020-08-21 13:32:37 -04:00
|
|
|
<span>LOADING</span>
|
2020-06-23 17:17:28 -04:00
|
|
|
</span>
|
2020-01-16 00:48:55 -05:00
|
|
|
</td>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tr>
|
2020-08-21 13:32:37 -04:00
|
|
|
))}
|
|
|
|
{rows.length > 0 &&
|
|
|
|
rows.map(row => {
|
|
|
|
prepareRow(row);
|
2020-10-14 14:15:34 -04:00
|
|
|
// @ts-ignore
|
|
|
|
const rowId = row.original.id;
|
2020-08-21 13:32:37 -04:00
|
|
|
return (
|
|
|
|
<tr
|
2020-10-14 19:12:01 -04:00
|
|
|
data-test="table-row"
|
2020-08-21 13:32:37 -04:00
|
|
|
{...row.getRowProps()}
|
|
|
|
className={cx('table-row', {
|
2020-10-05 19:21:59 -04:00
|
|
|
'table-row-selected':
|
2020-10-14 14:15:34 -04:00
|
|
|
row.isSelected ||
|
|
|
|
(typeof rowId !== 'undefined' && rowId === highlightRowId),
|
2020-08-21 13:32:37 -04:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{row.cells.map(cell => {
|
|
|
|
if (cell.column.hidden) return null;
|
|
|
|
|
|
|
|
const columnCellProps = cell.column.cellProps || {};
|
|
|
|
return (
|
|
|
|
<td
|
2020-10-27 14:38:05 -04:00
|
|
|
data-test="table-row-cell"
|
2020-08-21 13:32:37 -04:00
|
|
|
className={cx('table-cell', {
|
|
|
|
'table-cell-loader': loading,
|
|
|
|
[cell.column.size || '']: cell.column.size,
|
|
|
|
})}
|
|
|
|
{...cell.getCellProps()}
|
|
|
|
{...columnCellProps}
|
|
|
|
>
|
2021-03-11 17:08:16 -05:00
|
|
|
<span
|
|
|
|
className={cx({ 'loading-bar': loading })}
|
|
|
|
role={loading ? 'progressbar' : undefined}
|
|
|
|
>
|
2020-10-14 19:12:01 -04:00
|
|
|
<span data-test="cell-text">{cell.render('Cell')}</span>
|
2020-08-21 13:32:37 -04:00
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
})}
|
2020-01-16 00:48:55 -05:00
|
|
|
</tbody>
|
2020-06-23 17:17:28 -04:00
|
|
|
</Table>
|
2021-01-08 15:07:51 -05:00
|
|
|
),
|
|
|
|
);
|