// Copyright 2021 99cloud
//
// Licensed 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';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { get, isString, isEmpty, isEqual, has } from 'lodash';
import { Table, Typography } from 'antd';
import {
getColumnSorter,
getSortOrder,
updateColumnSort,
checkIsStatusColumn,
getStatusRender,
getRender,
getNameRender,
getNameRenderByRouter,
getValueMapRender,
getUnitRender,
getProjectRender,
} from 'utils/table';
import { getNoValue } from 'utils/index';
import styles from './index.less';
const { Paragraph } = Typography;
export default class SimpleTable extends React.Component {
static propTypes = {
data: PropTypes.array.isRequired,
filters: PropTypes.object,
searchFilters: PropTypes.array,
columns: PropTypes.array.isRequired,
className: PropTypes.string,
onChange: PropTypes.func,
isLoading: PropTypes.bool,
rowSelection: PropTypes.object,
pagination: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
filterByBackend: PropTypes.bool,
// eslint-disable-next-line react/no-unused-prop-types
isSortByBack: PropTypes.bool,
// eslint-disable-next-line react/no-unused-prop-types
defaultSortKey: PropTypes.string,
// eslint-disable-next-line react/no-unused-prop-types
defaultSortOrder: PropTypes.string,
onRow: PropTypes.func,
childrenColumnName: PropTypes.string,
};
static defaultProps = {
filters: {},
searchFilters: [],
isLoading: false,
rowSelection: null,
pagination: {},
filterByBackend: false,
isSortByBack: false,
defaultSortKey: '',
defaultSortOrder: '',
};
handleChange = (pagination, filters, sorter, extra) => {
const { onChange } = this.props;
onChange && onChange(pagination, filters, sorter, extra);
};
getBaseColumns = (columns) =>
columns.map((column) => {
const {
sortable,
dataIndex,
valueRender,
sorter,
sortOrder,
render,
isStatus,
isName,
isPrice,
isLink,
routeName,
linkPrefix,
valueMap,
unit,
copyable,
...rest
} = column;
if (column.key === 'operation') {
return column;
}
const newSorter = getColumnSorter(column, this.props);
const newSortOrder =
sortOrder || newSorter ? getSortOrder(dataIndex, this.props) : null;
let newRender = render || getRender(valueRender);
if (valueMap) {
newRender = getValueMapRender(column);
}
if (unit) {
newRender = getUnitRender(column);
}
if (checkIsStatusColumn(dataIndex, isStatus)) {
newRender = getStatusRender(newRender);
}
if (dataIndex === 'description') {
newRender = this.getNoValueRender(newRender);
}
if (dataIndex === 'project_name') {
newRender = getProjectRender(newRender);
}
if ((dataIndex === 'name' && routeName) || isLink) {
const { rowKey } = this.props;
newRender = getNameRenderByRouter(newRender, column, rowKey);
}
if ((dataIndex === 'name' && linkPrefix) || isName) {
newRender = getNameRender(newRender, column);
}
if (dataIndex === 'cost' || isPrice) {
newRender = this.getPriceRender(newRender, column);
}
if (copyable) {
newRender = (value) => {
if (value && value !== '-') {
return