Skip to content

Commit

Permalink
Entities table
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Mar 25, 2024
1 parent f73e767 commit 32593c6
Show file tree
Hide file tree
Showing 10 changed files with 3,498 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@surfnet/sds",
"version": "0.0.102",
"version": "0.0.103",
"description": "SURF Design System for React",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand Down
46 changes: 45 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function isEmpty(obj: any) {
if (typeof obj === "string") {
return obj.trim().length === 0;
}
if (obj && obj.getTime) {
if (obj && obj.getTime && typeof obj.getTime === "function") {
return obj.getTime() !== obj.getTime();
}
if (typeof obj === "object") {
Expand All @@ -36,3 +36,47 @@ export function pseudoGuid() {
return (crypto.randomUUID && typeof crypto.randomUUID === "function" && crypto.randomUUID()) ||
Math.round((new Date().getTime() * Math.random() * 1000)).toString()
}

export function sortObjects(objects: Array<Object>, attribute: string, reverse: boolean) {
return [...objects].sort((a, b) => {
const val1 = valueForSort(attribute, a);
const val2 = valueForSort(attribute, b);
if (typeof val1 === "number" && typeof val2 === "number") {
return (val1 - val2) * (reverse ? -1 : 1);
}
const aS = val1.toString();
const bS = val2.toString();
if (aS.length === 0) {
return (reverse ? -1 : 1);
}
if (bS.length === 0) {
return (reverse ? 1 : -1);
}
return aS.localeCompare(bS) * (reverse ? -1 : 1);
});
}

export function valueForSort(attribute: string, obj: Object) : any{
if (attribute.endsWith("_date")) {
// @ts-ignore
return obj[attribute] || Number.MAX_SAFE_INTEGER;
}
// @ts-ignore
const val = obj[attribute];
if (val.getTime && typeof val.getTime === "function") {
return val.getTime();
}
if (!isEmpty(val)) {
return val;
}
const parts = attribute.replace(/__/g, ".").split(".");
const res = parts.reduce((acc, e) => {
if (isEmpty(acc)) {
return "";
}
// @ts-ignore
return acc[e];
}, obj);
return res || "";

}
2 changes: 0 additions & 2 deletions src/components/Pagination/algorithm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ describe("Pagination algorithm", () => {
for (let i = 1, l = 20; i <= l; i++) {
expect(expectations[i - 1]).toEqual(pagination(i, l));
}


});
});
11 changes: 11 additions & 0 deletions src/components/Table/Table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.sds--entities {

.entities-search {
display: flex;
gap: 40px;
div.search {
margin-left: auto;
min-width: 140px;
}
}
}
39 changes: 39 additions & 0 deletions src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import {Meta, StoryFn} from '@storybook/react';
import {ReactComponent as ArrowDownIcon} from "../../icons/functional-icons/arrow-down-2.svg";
import Table, {defaultTableProps} from './Table';
import Button from "../Button/Button";

// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
export default {
title: 'ReactComponentLibrary/Table',
component: Table,
args: {
...defaultTableProps
}
} as Meta<typeof Table>;

// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
const Template: StoryFn<typeof Table> = (args) => <Table {...args} />;

export const Default = Template.bind({});
// More on args: https://storybook.js.org/docs/react/writing-stories/args
Default.args = {
newEntityFunc: () => alert("New entity"),
searchAttributes: ["name"],
children: <Button txt={"Custom children"}/>,
columns: [
{key: "name", hasLink: false, showHeader: true},
{key: "name", hasLink: false, mapper: (obj: any) => obj.birthday.toDateString(), showHeader: true}
],
title: "Persons",
defaultSort: "name",
newLabel: "New Person",
searchPlaceHolder: "Search...",
entities:[
{name:"John Doe", birthday: new Date("1978-04-09")},
{name:"Mary Doe", birthday: new Date("1977-04-09")},
{name:"Paul Doe", birthday: new Date("1979-04-09")}
],
};

10 changes: 10 additions & 0 deletions src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import {render} from "@testing-library/react";

import Table,{defaultTableProps} from "./Table";

describe("Table", () => {
test("renders the Table component", () => {
render(<Table entities={[]} defaultSort={"name"} columns={[]}/>);
});
});
Loading

0 comments on commit 32593c6

Please sign in to comment.