PromucFlow_constructor/app/client/src/components/designSystems/appsmith/TableComponent.tsx

124 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-11-07 11:17:53 +00:00
import BaseTable, { Column } from "react-base-table";
2019-11-20 08:10:01 +00:00
import styled, { keyframes } from "styled-components";
2019-11-07 11:17:53 +00:00
import React from "react";
import { noop } from "../../../utils/AppsmithUtils";
2019-11-20 08:10:01 +00:00
const move = keyframes`
from {
transform: translateX(-100%);
}
to {
transform: translateX(100%);
}
`;
const InlineLoader = styled.div`
overflow: hidden;
height: 100%;
width: 100%;
position: relative;
background-color: #eee;
&::after {
content: "";
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-position: left top;
background-repeat: no-repeat;
background-image: linear-gradient(to right, transparent, #ccc, transparent);
animation: ${move} 1.5s linear infinite;
}
`;
const RowLoader = styled(InlineLoader)`
height: 16px !important;
margin: 0 15px;
`;
const Row = (props: any) => (
<div {...props}>
<RowLoader />
</div>
);
const rowProps = ({ rowData }: { rowData: { isLoading: boolean } }) => {
return rowData.isLoading ? { tagName: Row } : undefined;
};
2019-11-07 11:17:53 +00:00
const StyledTable = styled(BaseTable)`
.row-selected {
background-color: ${props =>
props.theme.widgets.tableWidget.selectHighlightColor};
}
`;
export interface Column {
key: string;
dataKey: string;
title: string;
width: number;
}
export interface ReactBaseTableProps {
width: number;
height: number;
columns: Column[];
data: object[];
maxHeight: number;
selectedRowIndex?: number;
}
export interface SelectableTableProps extends ReactBaseTableProps {
onRowClick: (rowData: object, rowIndex: number) => void;
2019-11-20 08:10:01 +00:00
isLoading: boolean;
2019-11-07 11:17:53 +00:00
}
export default class SelectableTable extends React.PureComponent<
SelectableTableProps
> {
static defaultProps = {};
_onClick = ({ rowData, rowIndex }: { rowData: object; rowIndex: number }) => {
if (this.props.selectedRowIndex !== rowIndex) {
this.props.onRowClick(rowData, rowIndex);
}
};
_rowClassName = ({ rowIndex }: { rowIndex: number }) => {
const { selectedRowIndex } = this.props;
return selectedRowIndex === rowIndex ? "row-selected" : "";
};
render() {
2019-11-20 08:10:01 +00:00
const { data, isLoading, onRowClick, ...rest } = this.props;
const dataWithLoadingState = data.map(rowData => {
return {
...rowData,
isLoading: this.props.isLoading,
};
});
2019-11-07 11:17:53 +00:00
return (
<StyledTable
{...rest}
2019-11-20 08:10:01 +00:00
data={dataWithLoadingState}
2019-11-07 11:17:53 +00:00
rowClassName={this._rowClassName}
rowEventHandlers={{
onClick: this._onClick,
}}
2019-11-20 08:10:01 +00:00
rowProps={rowProps}
2019-11-07 11:17:53 +00:00
/>
);
}
}
SelectableTable.defaultProps = {
...BaseTable.defaultProps,
onRowSelect: noop,
2019-11-20 08:10:01 +00:00
isLoading: false,
2019-11-07 11:17:53 +00:00
onSelectedRowsChange: noop,
};