From 40383868e1ea9c73a6710cabf6f740a2764c5405 Mon Sep 17 00:00:00 2001 From: kostasdano Date: Tue, 9 Apr 2024 16:39:22 +0300 Subject: [PATCH] feat: Table component --- src/components/Table/Table.mdx | 34 ++++++++++++++++++ src/components/Table/Table.stories.tsx | 27 +++++++++++++++ src/components/Table/Table.style.ts | 27 +++++++++++++++ src/components/Table/Table.tsx | 48 ++++++++++++++++++++++++++ src/components/Table/constants.tsx | 22 ++++++++++++ src/components/Table/index.ts | 4 +++ src/components/Table/types.ts | 27 +++++++++++++++ 7 files changed, 189 insertions(+) create mode 100644 src/components/Table/Table.mdx create mode 100644 src/components/Table/Table.stories.tsx create mode 100644 src/components/Table/Table.style.ts create mode 100644 src/components/Table/Table.tsx create mode 100644 src/components/Table/constants.tsx create mode 100644 src/components/Table/index.ts create mode 100644 src/components/Table/types.ts diff --git a/src/components/Table/Table.mdx b/src/components/Table/Table.mdx new file mode 100644 index 000000000..bddfd87b5 --- /dev/null +++ b/src/components/Table/Table.mdx @@ -0,0 +1,34 @@ +import { Meta, Canvas, ArgTypes } from '@storybook/blocks'; +import Table from './Table'; +import * as TableStories from './Table.stories'; +import { DisplayColumnStory } from './utils/TableStoryComponents'; + + + + + +## Overview + +A universal Table component that applies Orfium basic usages. + +## Table Props + + + +## DisplayColumn Type + + + + + +### Table Sizes + + diff --git a/src/components/Table/Table.stories.tsx b/src/components/Table/Table.stories.tsx new file mode 100644 index 000000000..47b6a2de5 --- /dev/null +++ b/src/components/Table/Table.stories.tsx @@ -0,0 +1,27 @@ +import Table from './Table'; +import { SimpleData, simpleColumns, simpleData } from './constants'; + +export default { + title: 'Updated Components/Table/Table', + component: Table, + + args: { + rowSize: 'sm', + }, +}; + +export const TableSizes = { + render: (args) => { + const { rowSize } = args; + + return data={simpleData} columns={simpleColumns} rowSize={rowSize} />; + }, + + name: 'Table Sizes', + + parameters: { + controls: { + include: ['rowSize'], + }, + }, +}; diff --git a/src/components/Table/Table.style.ts b/src/components/Table/Table.style.ts new file mode 100644 index 000000000..759363136 --- /dev/null +++ b/src/components/Table/Table.style.ts @@ -0,0 +1,27 @@ +import type { SerializedStyles } from '@emotion/react'; +import { css } from '@emotion/react'; +import type { Theme } from 'theme'; + +export const tableContainer = + () => + (theme: Theme): SerializedStyles => { + return css` + display: inline-block; + border: 1px solid ${theme.tokens.colors.get('borderColor.decorative.default')}; + border-radius: 4px; + background: ${theme.globals.colors.get('neutral.1')}; + `; + }; + +export const tableStyles = (): SerializedStyles => { + return css` + thead > tr > th:last-child, + tbody > tr > td:last-child { + border-right: none; + } + + tbody > tr:last-child > td { + border-bottom: none; + } + `; +}; diff --git a/src/components/Table/Table.tsx b/src/components/Table/Table.tsx new file mode 100644 index 000000000..4fed8da44 --- /dev/null +++ b/src/components/Table/Table.tsx @@ -0,0 +1,48 @@ +import { flexRender } from '@tanstack/react-table'; +import React from 'react'; + +import type { TableProps } from '.'; +import { TBody, TH, TD, THead, TR } from './components'; +import useTable from './hooks/useTable'; +import { tableContainer, tableStyles } from './Table.style'; + +const Table = ({ data, columns, rowSize = 'sm' }: TableProps) => { + const table = useTable({ data, columns }); + + return ( +
+ + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => ( + + ))} + + ))} + + + {table.getRowModel().rows.map((row) => { + return ( + + {row.getVisibleCells().map((cell) => { + return ( + + ); + })} + + ); + })} + +
+ {header.isPlaceholder + ? null + : flexRender(header.column.columnDef.header, header.getContext())} +
+ {flexRender(cell.column.columnDef.cell, cell.getContext())} +
+
+ ); +}; + +export default Table; diff --git a/src/components/Table/constants.tsx b/src/components/Table/constants.tsx new file mode 100644 index 000000000..ced8dac6b --- /dev/null +++ b/src/components/Table/constants.tsx @@ -0,0 +1,22 @@ +export type SimpleData = { + firstName: string; + lastName: string; + age: string; + job: string; +}; + +export const simpleColumns = [ + { id: 'firstName', header: 'First Name' }, + { id: 'lastName', header: 'Last Name' }, + { id: 'age', header: 'Age' }, + { id: 'job', header: 'Job' }, +]; + +export const simpleData = [ + { cells: { firstName: 'Rachel', lastName: 'Berry', age: '30', job: 'Fashion Executive' } }, + { cells: { firstName: 'Ross', lastName: 'Geller', age: '32', job: 'Paleontologist' } }, + { cells: { firstName: 'Monica', lastName: 'Geller', age: '31', job: 'Chef' } }, + { cells: { firstName: 'Chandler', lastName: 'Bing', age: '32', job: '?' } }, + { cells: { firstName: 'Joey', lastName: 'Tribbiani', age: '30', job: 'Actor' } }, + { cells: { firstName: 'Phoebe', lastName: 'Buffay', age: '31', job: 'Massage Therapist' } }, +]; diff --git a/src/components/Table/index.ts b/src/components/Table/index.ts new file mode 100644 index 000000000..f4d270373 --- /dev/null +++ b/src/components/Table/index.ts @@ -0,0 +1,4 @@ +export { default } from './Table'; +export * from './components'; +export * from './hooks'; +export * from './types'; diff --git a/src/components/Table/types.ts b/src/components/Table/types.ts new file mode 100644 index 000000000..0c4b94aad --- /dev/null +++ b/src/components/Table/types.ts @@ -0,0 +1,27 @@ +export type TableProps = { + /** The Columns configuration of the Table */ + columns: DisplayColumn[]; + /** The Data of the Table */ + data: TableData; + /** Size of Row */ + rowSize?: RowSize; +}; + +export type UseTableProps = Pick, 'columns' | 'data'>; + +/** Columns */ + +export type DisplayColumn = { + /** The id of the column; must be the same as the column key in the Data type */ + id: string; + /** The label of the column on the table */ + header: string; +}; + +/** Rows & Cells */ + +export type TableData = { + cells: TData; +}[]; + +export type RowSize = 'sm' | 'md' | 'lg';