From dc169ecff9c937d53b14a61370bddf1195fbe285 Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Fri, 29 Nov 2019 17:13:39 +0100 Subject: [PATCH] style table --- scm-ui/ui-components/src/table/Table.tsx | 39 ++++++++++++++++--- scm-ui/ui-components/src/table/TextColumn.tsx | 7 ++-- scm-ui/ui-components/src/table/types.ts | 6 +++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/scm-ui/ui-components/src/table/Table.tsx b/scm-ui/ui-components/src/table/Table.tsx index 766ebf9317..847fe05d05 100644 --- a/scm-ui/ui-components/src/table/Table.tsx +++ b/scm-ui/ui-components/src/table/Table.tsx @@ -1,4 +1,15 @@ 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 = { data: any[]; @@ -7,11 +18,14 @@ type SortableTableProps = { // @ts-ignore const Table: FC = ({ data, children }) => { const [tableData, setTableData] = useState(data); - const [ascending, setAscending] = useState(true); + const [ascending, setAscending] = useState(false); const [lastSortBy, setlastSortBy] = useState(0); // @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) => { return ( @@ -46,19 +60,34 @@ const Table: FC = ({ data, children }) => { return ( tableData.length > 0 && ( - + {React.Children.map(children, (child, index) => ( // @ts-ignore - + ))} {tableData.map(mapDataToColumns)} -
tableSort(index)}>{child.props.header} tableSort(index) : undefined} + > + {child.props.header} + + {child.props.createComparator && renderSortIcon(child.props.sortType, ascending)} +
+ ) ); }; +const renderSortIcon = (contentType: string, ascending: boolean) => { + if (contentType === SortTypes.Text) { + return ; + } else { + return ; + } +}; + export default Table; diff --git a/scm-ui/ui-components/src/table/TextColumn.tsx b/scm-ui/ui-components/src/table/TextColumn.tsx index eeca01eea7..0ccc69be81 100644 --- a/scm-ui/ui-components/src/table/TextColumn.tsx +++ b/scm-ui/ui-components/src/table/TextColumn.tsx @@ -1,5 +1,5 @@ -import React, { FC } from "react"; -import {ColumnProps} from "./types"; +import React, {FC} from "react"; +import {ColumnProps, SortTypes} from "./types"; type Props = ColumnProps & { dataKey: string; @@ -20,7 +20,8 @@ TextColumn.defaultProps = { return 0; } }; - } + }, + sortType: SortTypes.Text }; export default TextColumn; diff --git a/scm-ui/ui-components/src/table/types.ts b/scm-ui/ui-components/src/table/types.ts index b05a402711..683566da6d 100644 --- a/scm-ui/ui-components/src/table/types.ts +++ b/scm-ui/ui-components/src/table/types.ts @@ -6,4 +6,10 @@ export type ColumnProps = { header: ReactNode; row?: any; createComparator?: (props: any) => Comparator; + sortType: SortTypes; }; + +export enum SortTypes { + Text = "text", + Other = "other" +}