Skip to content
Snippets Groups Projects
Commit 9597393e authored by Robbie Luijben's avatar Robbie Luijben
Browse files

feat: Editable cell style in Table and expose body template property, toasty alerts, spinner

parent 5e689bb7
No related branches found
No related tags found
1 merge request!44Add editable cell style to Table and expose body template property, toasty alerts, spinner
import { clsx } from "clsx";
import { useState } from "react";
import Icon, { IconName } from "./Icon.tsx";
import Typography from "./Typography.tsx";
import { baseColorType } from "./utils/colors.ts";
......@@ -8,6 +9,7 @@ type AlertProps = {
title: string;
message: string;
alertType?: baseColorType;
expireAfter?: number;
};
/**
......@@ -17,16 +19,42 @@ type AlertProps = {
* @param title: string
* @param message: string
* @param alertType: "default" | "warning" | "positive" | "negative"
* @param expireAfter: number
*/
const Alert = ({ title, message, alertType = "primary" }: AlertProps) => {
const Alert = ({ title, message, alertType = "primary", expireAfter }: AlertProps) => {
const [hasExpired, setHasExpired] = useState(false);
const [shouldRemoveElement, setShouldRemoveElement] = useState(false);
if (shouldRemoveElement) {
return null;
}
const isToasty = !!expireAfter;
if (expireAfter) {
setTimeout(() => {
setHasExpired(true);
setTimeout(() => {
setShouldRemoveElement(true);
}, 1000);
}, expireAfter);
}
const icon = {
primary: "InfoCircle",
warning: "AttentionCircle",
positive: "CheckmarkCircle",
negative: "CrossCircle",
};
const baseClass =
"flex flex-row items-start py-[13px] px-[15px] rounded-[8px] w-min border-l-8 border-y-0 border-r-0 bg-baseWhite dark:bg-mediumGrey shadow-2xl";
clsx(
{"transition-opacity ease-out duration-700 opacity-0": hasExpired },
"flex flex-row items-start py-[13px] px-[15px] rounded-[8px] w-min border-l-8 border-y-0 border-r-0 bg-baseWhite dark:bg-mediumGrey shadow-2xl",
{"animate-fadeIn bg-baseWhite/80 dark:bg-mediumGrey/80 fixed top-5 right-5": isToasty});
const iconElement = isToasty ? null : <Icon name="Cross" color="body" customClass="cursor-pointer" />
return (
<div className={clsx(baseClass, borderColor(alertType))}>
<Icon name={icon[alertType] as IconName} color={alertType} />
......@@ -40,7 +68,7 @@ const Alert = ({ title, message, alertType = "primary" }: AlertProps) => {
<Typography text={message} variant="paragraph" />
</div>
{/* if used more often than below and in Modal, consider making it a CloseIcon */}
<Icon name="Cross" color="body" customClass="cursor-pointer" />
{iconElement}
</div>
);
};
......
......@@ -4,6 +4,7 @@ import Icon, { IconName } from "./Icon.tsx";
import Typography from "./Typography.tsx";
import { ColorType } from "./utils/colors.ts";
import { bgColor, cursorPointer } from "./utils/classes.ts";
import Spinner from "./Spinner.tsx";
export type ButtonProps = {
label?: string | null;
......@@ -12,6 +13,8 @@ export type ButtonProps = {
iconClass?: string;
isDisabled?: boolean;
type?: "button" | "submit";
showLoader?: boolean;
loadingText?: string;
onClick?: () => void;
};
......@@ -33,6 +36,8 @@ const Button = ({
iconClass,
isDisabled = false,
type = "button",
showLoader = false,
loadingText = "Loading...",
onClick = () => {},
}: ButtonProps) => {
if (!label && !icon) throw Error("Button cannot be empty");
......@@ -40,28 +45,34 @@ const Button = ({
const bgClass = (isDisabled: boolean) =>
isDisabled ? "bg-lightGrey !opacity-100" : bgColor(color);
return (
<NextButton
startContent={
icon && (
const iconElement = icon &&
<Icon
name={icon}
customClass={clsx(iconClass, "w-[16px] h-[16px]")}
/>
)
}
;
const labelText = showLoader ? loadingText : label;
const labelElement = labelText &&
<Typography text={labelText} variant="paragraph" customColor={true} />
;
const loaderElement = showLoader && <Spinner customClass="!w-4 !h-4" />;
return (
<NextButton
startContent={loaderElement || iconElement}
className={clsx(
bgClass(isDisabled),
cursorPointer(isDisabled),
buttonBaseClass
)}
isDisabled={isDisabled}
isDisabled={showLoader || isDisabled}
type={type}
onClick={onClick}
>
{label && (
<Typography text={label} variant="paragraph" customColor={true} />
)}
{labelElement}
</NextButton>
);
};
......
import clsx from "clsx";
export type SpinnerProps = {
customClass?: string;
}
const Spinner = ({ customClass } : SpinnerProps) => {
const className = clsx("fill-primary w-8 h-8 text-gray-200 animate-spin dark:text-gray-600", customClass);
return <div role="status">
<svg aria-hidden="true" className={className} viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span className="sr-only">Loading...</span>
</div>
};
export default Spinner;
\ No newline at end of file
import { Dispatch, ReactNode, SetStateAction } from "react";
import { DataTable, type DataTableValueArray } from "primereact/datatable";
import { Column } from "primereact/column";
import type { ColumnEditorOptions, ColumnEvent } from "primereact/column";
import type { ColumnBodyOptions, ColumnEditorOptions, ColumnEvent } from "primereact/column";
import { clsx } from "clsx";
import { textColor } from "./utils/classes.ts";
import Typography from "./Typography.tsx";
......@@ -14,6 +14,7 @@ type ColumnProps = {
style?: React.CSSProperties;
editor?: (options: ColumnEditorOptions) => ReactNode;
onCellEditComplete?: (event: ColumnEvent) => void;
bodyTemplate?: (rowData: unknown, options: ColumnBodyOptions) => ReactNode;
};
type TableProps = {
......@@ -46,6 +47,9 @@ const Table = ({
editMode,
emptyMessage = defaultEmptyMessage
}: TableProps) => {
const isColumnEditable = (field: string) => columns.some((col) => col.field === field && col.editor);
return (
<DataTable
editMode={editMode}
......@@ -64,7 +68,7 @@ const Table = ({
rowClassName={() =>
clsx("border-t border-grey/50 text-[13px] font-body", textColor("body"))
}
cellClassName={() => "h-12 pl-[5px]"}
cellClassName={(_value, options) => clsx("h-12 pl-[5px]", isColumnEditable(options.field) && "cursor-pointer hover:bg-lightGrey/30 hover:dark:bg-darkBlue rounded-[4px] p-2")}
>
{isSelectable && (
<Column selectionMode="multiple" headerClassName="pb-[10px] pl-[5px]" />
......@@ -88,6 +92,7 @@ const Table = ({
}
editor={col.editor}
onCellEditComplete={col.onCellEditComplete}
body={col.bodyTemplate}
/>
))}
</DataTable>
......
......@@ -19,6 +19,7 @@ export { default as Modal } from "./components/Modal";
export { default as ProgressBar } from "./components/ProgressBar";
export { default as Radio } from "./components/Radio";
export { default as Slider } from "./components/Slider";
export { default as Spinner } from "./components/Spinner";
export { default as Table } from "./components/Table";
export { default as TextArea } from "./components/TextArea";
export { default as TextInput } from "./components/TextInput";
......
......@@ -20,3 +20,11 @@ export const PlainAlert: StoryObj = {
message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
},
};
export const Toast: StoryObj = {
args: {
title: "I will fade away in 5 seconds",
message: "Bye bye",
expireAfter: 5000,
}
}
\ No newline at end of file
......@@ -68,3 +68,22 @@ export const SubmitButton = () => {
</form>
);
};
export const ButtonWithLoader = () => {
const [isLoading, setIsLoading] = useState(false);
return (
<Button
label="Download"
icon="Download"
isDisabled={isLoading}
showLoader={isLoading}
onClick={() => {
setIsLoading(true);
setTimeout(() => {
setIsLoading(false);
}, 2000);
}}
/>
);
};
import { Meta } from "@storybook/react";
import Spinner from "src/components/Spinner.tsx";
const meta = {
title: "Components/Spinner",
component: Spinner,
} satisfies Meta;
export default meta;
export const DefaultSpinner = () => {
return (
<Spinner />
)
};
\ No newline at end of file
......@@ -9,6 +9,7 @@ import Badge from "src/components/Badge.tsx";
import Button from "src/components/Button.tsx";
import Typography from "src/components/Typography.tsx";
import TextInput from "src/components/TextInput.tsx";
import { Alert, Icon } from "src";
const meta = {
title: "Components/Table",
......@@ -217,6 +218,8 @@ const editableColumnRows = [
/** Cells can be edited, cell values can be validated */
export const TableWithEditableCells = () => {
const [isDeleting, setIsDeleting] = useState(false);
const textEditor = (options: ColumnEditorOptions) => {
return (
<TextInput
......@@ -269,6 +272,15 @@ export const TableWithEditableCells = () => {
}
};
const nameBodytemplate = (rowData: unknown) => {
return (
<div className="flex justify-between">
<div>{(rowData as Row).name}</div>
<Icon name="Edit" />
</div>
)
};
const editableColumns = [
{ field: "row_id", header: "ID", style: { width: "30%" } },
{
......@@ -277,6 +289,7 @@ export const TableWithEditableCells = () => {
style: { width: "20%" },
editor: textEditor,
onCellEditComplete: onCellEditComplete,
bodyTemplate: nameBodytemplate
},
{ field: "role", header: "Role", style: { width: "20%" } },
{
......@@ -296,18 +309,22 @@ export const TableWithEditableCells = () => {
setEditableRows(editableColumnRows);
}, []);
const deleteButton = (row: Row) => (
const DeleteButton = (row: Row) => (
<Button
label="Delete"
icon="Cross"
onClick={() => {
deleteEmployee(row);
setIsDeleting(true);
setTimeout(() => {
setIsDeleting(false);
}, 6000)
}}
/>
);
for (const row of editableRows) {
row.action = deleteButton(row);
row.action = DeleteButton(row);
}
const addEmployee = () => {
......@@ -318,7 +335,7 @@ export const TableWithEditableCells = () => {
salary: 0,
notes: "",
} as Row;
newRow.action = deleteButton(newRow);
newRow.action = DeleteButton(newRow);
setEditableRows((old) => [...old, newRow]);
};
......@@ -327,7 +344,9 @@ export const TableWithEditableCells = () => {
setEditableRows(newRows);
};
return (
const alert = isDeleting ? <Alert message="Employee terminated" title="Boom baby" expireAfter={5000} /> : null;
return <>
<Table
headerContent={
<div className="flex flex-row gap-2 pb-[10px]">
......@@ -343,5 +362,6 @@ export const TableWithEditableCells = () => {
rows={editableRows}
editMode="cell"
/>
);
{alert}
</>
};
......@@ -38,6 +38,16 @@ export default {
boxShadow: {
"3xl": "0 0 8px rgba(0, 0, 0, 0.15)",
},
animation: {
fadeIn: 'fadeIn .7s ease-in-out',
},
keyframes: {
fadeIn: {
from: { opacity: 0 },
to: { opacity: 1 },
},
},
},
},
plugins: [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment