remove ts-ignore, added empty message and small bug fixes

This commit is contained in:
Sebastian Sdorra
2019-12-03 16:04:11 +01:00
parent a08d47df82
commit 54ac5a1f14
4 changed files with 67 additions and 45 deletions

View File

@@ -3,15 +3,19 @@ import { storiesOf } from "@storybook/react";
import Table from "./Table"; import Table from "./Table";
import Column from "./Column"; import Column from "./Column";
import TextColumn from "./TextColumn"; import TextColumn from "./TextColumn";
import { ColumnProps } from "./table";
storiesOf("Table|Table", module) storiesOf("Table|Table", module)
.add("Default", () => ( .add("Default", () => (
<Table data={[{ firstname: "Tricia", lastname: "McMillan", email: "tricia@hitchhiker.com" }, { firstname: "Arthur", lastname: "Dent", email: "arthur@hitchhiker.com" }]}> <Table
data={[
{ firstname: "Tricia", lastname: "McMillan", email: "tricia@hitchhiker.com" },
{ firstname: "Arthur", lastname: "Dent", email: "arthur@hitchhiker.com" }
]}
>
<Column header={"First Name"}>{(row: any) => <h4>{row.firstname}</h4>}</Column> <Column header={"First Name"}>{(row: any) => <h4>{row.firstname}</h4>}</Column>
<Column <Column
header={"Last Name"} header={"Last Name"}
createComparator={(props: ColumnProps, columnIndex: number) => { createComparator={() => {
return (a: any, b: any) => { return (a: any, b: any) => {
if (a.lastname > b.lastname) { if (a.lastname > b.lastname) {
return -1; return -1;
@@ -29,9 +33,21 @@ storiesOf("Table|Table", module)
</Table> </Table>
)) ))
.add("TextColumn", () => ( .add("TextColumn", () => (
<Table data={[{ id: "21", title: "Pommes", desc: "Fried potato sticks" },{ id: "42", title: "Quarter-Pounder", desc: "Big burger" }, {id: "-84", title: "Icecream", desc: "Cold dessert"}]}> <Table
<TextColumn header={"Id"} dataKey={"id"} /> data={[
<TextColumn header={"Name"} dataKey={"title"} /> { id: "21", title: "Pommes", desc: "Fried potato sticks" },
<TextColumn header={"Description"} dataKey={"desc"} /> { id: "42", title: "Quarter-Pounder", desc: "Big burger" },
{ id: "-84", title: "Icecream", desc: "Cold dessert" }
]}
>
<TextColumn header="Id" dataKey="id" />
<TextColumn header="Name" dataKey="title" />
<TextColumn header="Description" dataKey="desc" />
</Table>
))
.add("Empty", () => (
<Table data={[]} emptyMessage="No data found.">
<TextColumn header="Id" dataKey="id" />
<TextColumn header="Name" dataKey="name" />
</Table> </Table>
)); ));

View File

@@ -1,7 +1,8 @@
import React, { FC, useState } from "react"; import React, { FC, ReactElement, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { Comparator } from "./table"; import { Comparator } from "./table";
import SortIcon from "./SortIcon"; import SortIcon from "./SortIcon";
import Notification from "../Notification";
const StyledTable = styled.table.attrs(() => ({ const StyledTable = styled.table.attrs(() => ({
className: "table content is-hoverable" className: "table content is-hoverable"
@@ -10,23 +11,23 @@ const StyledTable = styled.table.attrs(() => ({
type Props = { type Props = {
data: any[]; data: any[];
sortable?: boolean; sortable?: boolean;
emptyMessage?: string;
children: Array<ReactElement>;
}; };
// @ts-ignore const Table: FC<Props> = ({ data, sortable, children, emptyMessage }) => {
const Table: FC<Props> = ({ data, sortable, children }) => {
const [tableData, setTableData] = useState(data); const [tableData, setTableData] = useState(data);
const [ascending, setAscending] = useState(false); const [ascending, setAscending] = useState(false);
const [lastSortBy, setlastSortBy] = useState<number | undefined>(); const [lastSortBy, setlastSortBy] = useState<number | undefined>();
const [hoveredColumnIndex, setHoveredColumnIndex] = useState<number | undefined>(); const [hoveredColumnIndex, setHoveredColumnIndex] = useState<number | undefined>();
const isSortable = (child: any) => { const isSortable = (child: ReactElement) => {
return sortable && child.props.createComparator; return sortable && child.props.createComparator;
}; };
const sortFunctions: Comparator | undefined[] = []; const sortFunctions: Comparator | undefined[] = [];
React.Children.forEach(children, (child, index) => { React.Children.forEach(children, (child, index) => {
if (isSortable(child)) { if (child && isSortable(child)) {
// @ts-ignore
sortFunctions.push(child.props.createComparator(child.props, index)); sortFunctions.push(child.props.createComparator(child.props, index));
} else { } else {
sortFunctions.push(undefined); sortFunctions.push(undefined);
@@ -37,7 +38,6 @@ const Table: FC<Props> = ({ data, sortable, children }) => {
return ( return (
<tr> <tr>
{React.Children.map(children, (child, columnIndex) => { {React.Children.map(children, (child, columnIndex) => {
// @ts-ignore
return <td>{React.cloneElement(child, { ...child.props, columnIndex, row })}</td>; return <td>{React.cloneElement(child, { ...child.props, columnIndex, row })}</td>;
})} })}
</tr> </tr>
@@ -51,14 +51,17 @@ const Table: FC<Props> = ({ data, sortable, children }) => {
}; };
const tableSort = (index: number) => { const tableSort = (index: number) => {
const sortFn = sortFunctions[index];
if (!sortFn) {
throw new Error(`column with index ${index} is not sortable`);
}
const sortableData = [...tableData]; const sortableData = [...tableData];
let sortOrder = ascending; let sortOrder = ascending;
if (lastSortBy !== index) { if (lastSortBy !== index) {
setAscending(true); setAscending(true);
sortOrder = true; sortOrder = true;
} }
// @ts-ignore const sortFunction = sortOrder ? sortFn : sortDescending(sortFn);
const sortFunction = sortOrder ? sortFunctions[index] : sortDescending(sortFunctions[index]);
sortableData.sort(sortFunction); sortableData.sort(sortFunction);
setTableData(sortableData); setTableData(sortableData);
setAscending(!sortOrder); setAscending(!sortOrder);
@@ -69,8 +72,15 @@ const Table: FC<Props> = ({ data, sortable, children }) => {
return index === lastSortBy || index === hoveredColumnIndex; return index === lastSortBy || index === hoveredColumnIndex;
}; };
if (!tableData || tableData.length <= 0) {
if (emptyMessage) {
return <Notification type="info">{emptyMessage}</Notification>;
} else {
return null;
}
}
return ( return (
tableData.length > 0 && (
<StyledTable> <StyledTable>
<thead> <thead>
<tr> <tr>
@@ -81,10 +91,7 @@ const Table: FC<Props> = ({ data, sortable, children }) => {
onMouseEnter={() => setHoveredColumnIndex(index)} onMouseEnter={() => setHoveredColumnIndex(index)}
onMouseLeave={() => setHoveredColumnIndex(undefined)} onMouseLeave={() => setHoveredColumnIndex(undefined)}
> >
{ {child.props.header}
// @ts-ignore
child.props.header
}
{isSortable(child) && renderSortIcon(child, ascending, shouldShowIcon(index))} {isSortable(child) && renderSortIcon(child, ascending, shouldShowIcon(index))}
</th> </th>
))} ))}
@@ -92,7 +99,6 @@ const Table: FC<Props> = ({ data, sortable, children }) => {
</thead> </thead>
<tbody>{tableData.map(mapDataToColumns)}</tbody> <tbody>{tableData.map(mapDataToColumns)}</tbody>
</StyledTable> </StyledTable>
)
); );
}; };
@@ -100,7 +106,7 @@ Table.defaultProps = {
sortable: true sortable: true
}; };
const renderSortIcon = (child: any, ascending: boolean, showIcon: boolean) => { const renderSortIcon = (child: ReactElement, ascending: boolean, showIcon: boolean) => {
if (child.props.ascendingIcon && child.props.descendingIcon) { if (child.props.ascendingIcon && child.props.descendingIcon) {
return <SortIcon name={ascending ? child.props.ascendingIcon : child.props.descendingIcon} isVisible={showIcon} />; return <SortIcon name={ascending ? child.props.ascendingIcon : child.props.descendingIcon} isVisible={showIcon} />;
} else { } else {

View File

@@ -10,7 +10,7 @@ const TextColumn: FC<Props> = ({ row, dataKey }) => {
}; };
TextColumn.defaultProps = { TextColumn.defaultProps = {
createComparator: (props: Props, columnIndex) => { createComparator: (props: Props) => {
return (a: any, b: any) => { return (a: any, b: any) => {
if (a[props.dataKey] < b[props.dataKey]) { if (a[props.dataKey] < b[props.dataKey]) {
return -1; return -1;