@brizy/ui
Version:
React elements in Brizy style
38 lines (37 loc) • 2.51 kB
JavaScript
import React, { useMemo, useCallback } from "react";
import AntTable from "antd/lib/table";
import { ConfigProvider } from "../ConfigProvider";
import { classNames } from "../classNamesFn";
import { getHexByColorType } from "../utils";
import { BRZ_PREFIX } from "../constants";
export var RowsColumns;
(function (RowsColumns) {
RowsColumns["rows"] = "rows";
RowsColumns["columns"] = "columns";
})(RowsColumns || (RowsColumns = {}));
const customizeRenderEmpty = () => null;
export function Table({ dataSource, columns, dataKey, theme, size = "large", footer, showHeader = true, type = "default", rowsAlign, rowHomeIndex, }) {
const _dataSource = dataSource.map((item, index) => (Object.assign(Object.assign({}, item), { key: dataKey ? item[dataKey] : index })));
const style = useMemo(() => {
var _a, _b, _c, _d;
return (theme && {
[`--${BRZ_PREFIX}-table-rows-odd-bg-color`]: ((_a = theme.rows) === null || _a === void 0 ? void 0 : _a.oddBgColor) && getHexByColorType(theme.rows.oddBgColor),
[`--${BRZ_PREFIX}-table-rows-even-bg-color`]: ((_b = theme.rows) === null || _b === void 0 ? void 0 : _b.evenBgColor) && getHexByColorType(theme.rows.evenBgColor),
[`--${BRZ_PREFIX}-table-columns-odd-bg-color`]: ((_c = theme.columns) === null || _c === void 0 ? void 0 : _c.oddBgColor) && getHexByColorType(theme.columns.oddBgColor),
[`--${BRZ_PREFIX}-table-columns-even-bg-color`]: ((_d = theme.columns) === null || _d === void 0 ? void 0 : _d.evenBgColor) && getHexByColorType(theme.columns.evenBgColor),
});
}, [theme]);
const tableClassName = classNames()("table", {
"table__order-type": type === "order",
"table__list-type": type === "list",
});
const rowsClassName = useCallback((_, rowIndex) => classNames()({
[`table__row-align__${rowsAlign}`]: rowsAlign,
table__row_home: rowIndex === rowHomeIndex,
}), [rowsAlign, rowHomeIndex]);
return (React.createElement(ConfigProvider, { renderEmpty: customizeRenderEmpty },
React.createElement(AntTable, Object.assign({ showHeader: showHeader }, (style && { style }), { className: tableClassName, dataSource: _dataSource,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
columns: columns, pagination: false, size: size }, (footer && { footer }), { tableLayout: columns.some(col => col.width) ? "fixed" : "auto", rowClassName: rowsClassName }))));
}