🚀 Use Button

This commit is contained in:
Asim Shrestha
2023-04-07 13:36:41 -07:00
parent 3ec6701fb4
commit d8c8f18f05
2 changed files with 7 additions and 6 deletions

56
src/components/Button.tsx Normal file
View File

@@ -0,0 +1,56 @@
import type { ForwardedRef } from "react";
import React, { forwardRef, useState } from "react";
import Loader from "./loader";
export interface ButtonProps {
type?: "button" | "submit" | "reset";
className?: string;
icon?: React.ReactNode;
children?: React.ReactNode;
loader?: boolean;
disabled?: boolean;
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void> | void;
}
const Button = forwardRef(
(props: ButtonProps, ref: ForwardedRef<HTMLButtonElement>) => {
const [loading, setLoading] = useState(false);
const onClick = (e: React.MouseEvent<HTMLButtonElement>) => {
if (props.loader == true) setLoading(true);
try {
void Promise.resolve(props.onClick?.(e)).then();
} catch (e) {
setLoading(false);
}
};
return (
<button
ref={ref}
type={props.type}
disabled={loading || props.disabled}
className={
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
"transition-all " + props.className
}
onClick={onClick}
>
<div className="flex items-center">
{loading ? (
<Loader />
) : (
<>
{props.icon ? <div className="mr-2">{props.icon}</div> : null}
{props.children}
</>
)}
</div>
</button>
);
}
);
Button.displayName = "Button";
export default Button;