mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
style table
This commit is contained in:
@@ -1,4 +1,15 @@
|
|||||||
import React, { FC, useState } from "react";
|
import React, { FC, useState } from "react";
|
||||||
|
import styled from "styled-components";
|
||||||
|
import { SortTypes } from "./types";
|
||||||
|
import Icon from "../Icon";
|
||||||
|
|
||||||
|
const StyledTable = styled.table.attrs(() => ({
|
||||||
|
className: "table content is-hoverable"
|
||||||
|
}))``;
|
||||||
|
|
||||||
|
const IconWithMarginLeft = styled(Icon)`
|
||||||
|
margin-left: 0.25em;
|
||||||
|
`;
|
||||||
|
|
||||||
type SortableTableProps = {
|
type SortableTableProps = {
|
||||||
data: any[];
|
data: any[];
|
||||||
@@ -7,11 +18,14 @@ type SortableTableProps = {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const Table: FC<SortableTableProps> = ({ data, children }) => {
|
const Table: FC<SortableTableProps> = ({ data, children }) => {
|
||||||
const [tableData, setTableData] = useState(data);
|
const [tableData, setTableData] = useState(data);
|
||||||
const [ascending, setAscending] = useState(true);
|
const [ascending, setAscending] = useState(false);
|
||||||
const [lastSortBy, setlastSortBy] = useState(0);
|
const [lastSortBy, setlastSortBy] = useState(0);
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const sortFunctions = React.Children.map(children, child => child.props.createComparator(child.props));
|
const sortFunctions = React.Children.map(children, child =>
|
||||||
|
// @ts-ignore
|
||||||
|
child.props.createComparator ? child.props.createComparator(child.props) : undefined
|
||||||
|
);
|
||||||
|
|
||||||
const mapDataToColumns = (row: any) => {
|
const mapDataToColumns = (row: any) => {
|
||||||
return (
|
return (
|
||||||
@@ -46,19 +60,34 @@ const Table: FC<SortableTableProps> = ({ data, children }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
tableData.length > 0 && (
|
tableData.length > 0 && (
|
||||||
<table className="text-left">
|
<StyledTable>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
{React.Children.map(children, (child, index) => (
|
{React.Children.map(children, (child, index) => (
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
<th onClick={() => tableSort(index)}>{child.props.header}</th>
|
<th
|
||||||
|
className={child.props.createComparator && "has-cursor-pointer"}
|
||||||
|
onClick={child.props.createComparator ? () => tableSort(index) : undefined}
|
||||||
|
>
|
||||||
|
{child.props.header}
|
||||||
|
|
||||||
|
{child.props.createComparator && renderSortIcon(child.props.sortType, ascending)}
|
||||||
|
</th>
|
||||||
))}
|
))}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>{tableData.map(mapDataToColumns)}</tbody>
|
<tbody>{tableData.map(mapDataToColumns)}</tbody>
|
||||||
</table>
|
</StyledTable>
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const renderSortIcon = (contentType: string, ascending: boolean) => {
|
||||||
|
if (contentType === SortTypes.Text) {
|
||||||
|
return <IconWithMarginLeft name={ascending ? "sort-alpha-down-alt" : "sort-alpha-down"} />;
|
||||||
|
} else {
|
||||||
|
return <IconWithMarginLeft name={ascending ? "sort-amount-down-alt" : "sort-amount-down"} />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export default Table;
|
export default Table;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { FC } from "react";
|
import React, {FC} from "react";
|
||||||
import {ColumnProps} from "./types";
|
import {ColumnProps, SortTypes} from "./types";
|
||||||
|
|
||||||
type Props = ColumnProps & {
|
type Props = ColumnProps & {
|
||||||
dataKey: string;
|
dataKey: string;
|
||||||
@@ -20,7 +20,8 @@ TextColumn.defaultProps = {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
},
|
||||||
|
sortType: SortTypes.Text
|
||||||
};
|
};
|
||||||
|
|
||||||
export default TextColumn;
|
export default TextColumn;
|
||||||
|
|||||||
@@ -6,4 +6,10 @@ export type ColumnProps = {
|
|||||||
header: ReactNode;
|
header: ReactNode;
|
||||||
row?: any;
|
row?: any;
|
||||||
createComparator?: (props: any) => Comparator;
|
createComparator?: (props: any) => Comparator;
|
||||||
|
sortType: SortTypes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum SortTypes {
|
||||||
|
Text = "text",
|
||||||
|
Other = "other"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user