Skip to content

Commit

Permalink
client: Add style parameters to Table component
Browse files Browse the repository at this point in the history
  • Loading branch information
XxRoloxX committed Oct 14, 2024
1 parent 6afd219 commit 59bd6fd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 197 deletions.
8 changes: 3 additions & 5 deletions client/src/components/Navbar/Navbar.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@import '@/variables';

.navbar {
background-image: url('@/assets/login-banner.png');
position: fixed;
width: 15rem;
height: 100%;
Expand All @@ -17,19 +16,18 @@
left: 0;
background-color: $navbar-backgroud-color;


&__logo {
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;

&__image {
width: 3.5rem;
height: 3rem;
border-radius: 7rem;
}

&__name {
font-size: 1.5rem;
color: $navbar-text-color;
Expand All @@ -48,4 +46,4 @@
margin-left: 1rem;
margin-bottom: 1rem;
}
}
}
7 changes: 2 additions & 5 deletions client/src/components/Table/Table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
grid-template-columns: subgrid;
grid-column: span 100;
overflow-y: scroll;

/* max-height: 30vh; */
}

&__row {
Expand All @@ -38,13 +36,12 @@

&__cell,
&__header {
grid: grid;
grid-template-columns: subgrid;

/* grid-column: span 30; */
text-wrap: wrap;
padding: 0 1rem;
font-size: 1rem;
display: flex;
align-items: center;
}

&__header {
Expand Down
50 changes: 43 additions & 7 deletions client/src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ interface TableRow {
export interface TableProps<Row extends TableRow> {
columns: TableColumn<Row>[];
rows: Row[];

/**
* Max height of the table defined in the css units (px, rem, ect.)
* Used to accomodate sticky table headers.
* By default table doesn't have a max height.
*/
maxHeight?: string;

/**
* AlignLeft forces all columns to take as little space as possible
* making all columns tightly packed on the left side of the table.
* Useful if we need a column that is always the size of its contents.
* Default value is false
*/
alignLeft?: boolean;
}

export interface TableColumn<Row extends TableRow> {
Expand All @@ -17,39 +32,60 @@ export interface TableColumn<Row extends TableRow> {
export interface TableRowProps<Row extends TableRow> {
row: Row;
columns: TableColumn<Row>[];
alignLeft: boolean;
}

export const getCellRowSpan = <Row extends TableRow>(
columns: TableColumn<Row>[],
alignLeft: boolean,
): { gridColumn: string } => {
return { gridColumn: `span ${columns.length}` };
if (columns.length == 0 || alignLeft) {
return { gridColumn: '' };
}

return { gridColumn: `span ${Math.floor(100 / columns.length)}` };
};

function TableBodyRow<Row extends TableRow>({ row, columns }: TableRowProps<Row>): React.ReactNode {
function getTableStyle(maxHeight: string) {
return { maxHeight };
}

function TableBodyRow<Row extends TableRow>({
row,
columns,
alignLeft,
}: TableRowProps<Row>): React.ReactNode {
return (
<div className="table__row">
{columns.map((column: TableColumn<Row>) => (
<div key="index" className={'table__cell'}>
<div key="index" className={'table__cell'} style={getCellRowSpan(columns, alignLeft)}>
{column.customComponent ? column.customComponent(row) : row[column.columnKey]}
</div>
))}
</div>
);
}

function Table<T extends TableRow>({ columns, rows }: TableProps<T>): React.ReactNode {
function Table<T extends TableRow>({
columns,
rows,
maxHeight,
alignLeft = false,
}: TableProps<T>): React.ReactNode {
const tableStyle = maxHeight ? getTableStyle(maxHeight) : {};

return (
<div className="table">
<div className="table__headers">
{columns.map((column: TableColumn<T>, index: number) => (
<div key={index} className="table__header">
<div key={index} className="table__header" style={getCellRowSpan(columns, alignLeft)}>
{column.header}
</div>
))}
</div>
<div className="table__body">
<div className="table__body" style={tableStyle}>
{rows.map((row: T, index: number) => (
<TableBodyRow key={index} row={row} columns={columns} />
<TableBodyRow key={index} row={row} columns={columns} alignLeft={alignLeft} />
))}
</div>
</div>
Expand Down
38 changes: 4 additions & 34 deletions client/src/index.css → client/src/index.scss
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
/* stylelint-disable-next-line import-notation */
@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap");
@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
@import '@/variables';

:root {
font-family: Roboto, sans-serif;
line-height: 1.5;
font-weight: 400;
box-sizing: content-box;

/* background-image: linear-gradient(rgb(217 217 217 / 100%), rgb(219 255 241 / 100%)); */
background-color: #07111b;
background-color: $background;
color-scheme: dark;

/* color: rgb(0 0 0 / 87%); */
color: white;

/* color: rgb(49 49 49 / 100%); */
color: $font-color;
font-synthesis: none;
text-rendering: optimizelegibility;
-webkit-font-smoothing: antialiased;
Expand All @@ -23,14 +18,9 @@

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}

a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
Expand All @@ -53,30 +43,10 @@ button {
font-weight: 500;
font-family: inherit;
cursor: pointer;
background-color: rgb(244 244 244 / 30%);
transition: border-color 0.25s;
}

button:hover {
border-color: #f91515;
}

button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #fff;
}

a:hover {
color: #747bff;
}

button {
background-color: #f9f9f9;
}
}
2 changes: 1 addition & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import './index.scss';
import Router from 'pages/Router.tsx';
import { RouterProvider } from 'react-router-dom';

Expand Down
136 changes: 1 addition & 135 deletions client/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,139 +1,5 @@
import Table, { TableColumn } from 'components/Table/Table';

interface UserRow {
name: string;
surname: string;
phoneNumber: number;
[key: string]: string | number;
}

const customTextField = (row: UserRow): React.ReactNode => {
return (
<div> Customdsaksjdsdasfkjsdfksjldjldjasdaslkhdasjlhdasjhdlkasjdkjaajshdlak {row.name}</div>
);
};

const TableData: {
columns: TableColumn<UserRow>[];
rows: UserRow[];
} = {
columns: [
{
header: 'Name',
columnKey: 'name',
// customComponent: customTextField,
},
{
header: 'Surname',
columnKey: 'surname',
customComponent: customTextField,
},
{
header: 'Phone Number',
columnKey: 'phoneNumber',
customComponent: customTextField,
},
],
rows: [
{
name: 'John',
surname: 'One',
phoneNumber: 132,
},
{
name: 'John',
surname: 'Two',
phoneNumber: 132,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'One',
phoneNumber: 132,
},
{
name: 'John',
surname: 'Two',
phoneNumber: 132,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
{
name: 'John',
surname: 'Three',
phoneNumber: 152,
},
],
};

const Home = () => {
return (
<div style={{ width: '80vw', height: '50vh', backgroundColor: '#0C1926' }}>
<Table rows={TableData.rows} columns={TableData.columns} />
</div>
);
return <div>Hello</div>;
};

export default Home;
11 changes: 5 additions & 6 deletions client/src/pages/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route, createBrowserRouter, createRoutesFromElements, defer } from 'react-router-dom';
import Login from './Login/Login';
// import { ProtectedLayout } from 'providers/AuthProvider/ProtectedLayout';
import { ProtectedLayout } from 'providers/AuthProvider/ProtectedLayout';
import { AuthLayout } from 'providers/AuthProvider/AuthLayout';
import Home from './Home/Home';
import { getAuthInfo } from 'providers/AuthProvider/AuthProvider';
Expand All @@ -14,15 +14,14 @@ const router = createBrowserRouter(
authData: getAuthInfo(),
});
}}
errorElement={<Home />}
errorElement={<Login />}
>
<Route path="/login" element={<Login />} />
<Route path="/" element={<Home />} />
<Route path="/" element={<ProtectedLayout />}>
<Route path="/" element={<Home />} />
</Route>
</Route>,
),
);

// <Route path="/" element={<ProtectedLayout />}>
// </Route>

export default router;
Loading

0 comments on commit 59bd6fd

Please sign in to comment.