diff --git a/src/components/Alert.tsx b/src/components/Alert.tsx index b4a339d04e85670c8baa7ea0818dec2777606dda..9c25c5c67ee0370c228ba6cdec58db1f86a753a7 100644 --- a/src/components/Alert.tsx +++ b/src/components/Alert.tsx @@ -1,4 +1,5 @@ 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> ); }; diff --git a/src/components/Button.tsx b/src/components/Button.tsx index 677b4e026bee2196c7d0749313717cafcd9d24d5..8c184b4a7d1b0051ca46f84118dfcdeea96d0e2f 100644 --- a/src/components/Button.tsx +++ b/src/components/Button.tsx @@ -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); + 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={ - icon && ( - <Icon - name={icon} - customClass={clsx(iconClass, "w-[16px] h-[16px]")} - /> - ) - } + 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> ); }; diff --git a/src/components/Spinner.tsx b/src/components/Spinner.tsx new file mode 100644 index 0000000000000000000000000000000000000000..8d61490ff3dee4c24d11aa15ec3fb55fc8ed0448 --- /dev/null +++ b/src/components/Spinner.tsx @@ -0,0 +1,19 @@ +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 diff --git a/src/components/Table.tsx b/src/components/Table.tsx index eeeec422f3087c1a5555650d9534532150eaf3e9..f0a6143962c9c67f1f8683e9b1e6eede754a974d 100644 --- a/src/components/Table.tsx +++ b/src/components/Table.tsx @@ -1,7 +1,7 @@ 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> diff --git a/src/index.ts b/src/index.ts index c421a4f5563d6f83c0633d946f31f0991026ebad..724ec577611a9b871cade6371b07cbb48dee5fd7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/stories/Alert.stories.ts b/src/stories/Alert.stories.ts index 016ceb8a3c3c5353c33c8b95655012e9bfb6bb61..cf207f859dcf91d5ba2184fd54190ca0a0afb3cd 100644 --- a/src/stories/Alert.stories.ts +++ b/src/stories/Alert.stories.ts @@ -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 diff --git a/src/stories/Button.stories.tsx b/src/stories/Button.stories.tsx index 276f5009bbb15d89551468d443bfd18cc67cbdbb..4eddf9c2ae7cf1edbfe36efd7913cb4e463813da 100644 --- a/src/stories/Button.stories.tsx +++ b/src/stories/Button.stories.tsx @@ -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); + }} + /> + ); +}; diff --git a/src/stories/Spinner.stories.tsx b/src/stories/Spinner.stories.tsx new file mode 100644 index 0000000000000000000000000000000000000000..8e6065ff3e14922a382f42abe2856b46323f50d2 --- /dev/null +++ b/src/stories/Spinner.stories.tsx @@ -0,0 +1,15 @@ +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 diff --git a/src/stories/Table.stories.tsx b/src/stories/Table.stories.tsx index 1a7acb10e697eb52ada0cdd2bd0ce7fe255d9824..3d76b23b7535adbbeb185ce1c1ebd929ebca82fa 100644 --- a/src/stories/Table.stories.tsx +++ b/src/stories/Table.stories.tsx @@ -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,8 +344,10 @@ export const TableWithEditableCells = () => { setEditableRows(newRows); }; - return ( - <Table + 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]"> <Button @@ -343,5 +362,6 @@ export const TableWithEditableCells = () => { rows={editableRows} editMode="cell" /> - ); + {alert} + </> }; diff --git a/tailwind.config.js b/tailwind.config.js index fb61e45644763fe6994166019a011a5d8efff565..bc9fa1eb519aa90ef0d68f7fbeac46010ad19780 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -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: [