Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import ButtonPrimary from "./ButtonPrimary";
|
||||
import { forwardRef, MouseEventHandler, ReactNode, Ref, useImperativeHandle, useState } from "react";
|
||||
import clsx from "clsx";
|
||||
import { useNavigation } from "react-router";
|
||||
|
||||
export interface RefLoadingButton {
|
||||
start: () => void;
|
||||
stop: () => void;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
type?: "button" | "submit" | "reset" | undefined;
|
||||
disabled?: boolean;
|
||||
children: ReactNode;
|
||||
onClick?: MouseEventHandler<HTMLButtonElement>;
|
||||
to?: string;
|
||||
actionName?: string;
|
||||
isLoading?: boolean;
|
||||
dataUiId?: string;
|
||||
}
|
||||
|
||||
const LoadingButton = ({ className, type = "button", children, disabled, onClick, to, actionName, isLoading, dataUiId }: Props, ref: Ref<RefLoadingButton>) => {
|
||||
const navigation = useNavigation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
start,
|
||||
stop,
|
||||
}));
|
||||
|
||||
function start() {
|
||||
setLoading(true);
|
||||
}
|
||||
function stop() {
|
||||
setLoading(false);
|
||||
}
|
||||
const submitting = navigation.state === "submitting";
|
||||
|
||||
function checkIsLoading() {
|
||||
if (isLoading !== undefined) {
|
||||
return isLoading;
|
||||
}
|
||||
const loadingOrSubmitting = loading || submitting;
|
||||
if (actionName) {
|
||||
return loadingOrSubmitting && navigation.state === "submitting" && navigation.formData?.get("action") === actionName;
|
||||
}
|
||||
return loadingOrSubmitting;
|
||||
}
|
||||
|
||||
return (
|
||||
<ButtonPrimary
|
||||
disabled={disabled || checkIsLoading()}
|
||||
className={clsx(className, "relative justify-center", checkIsLoading() && "base-spinner cursor-not-allowed")}
|
||||
type={type}
|
||||
onClick={onClick}
|
||||
to={to}
|
||||
dataUiId={dataUiId}
|
||||
>
|
||||
{/* {checkIsLoading() && <ReloadIcon className="mr-2 h-4 w-4 animate-spin" />} */}
|
||||
|
||||
{children}
|
||||
</ButtonPrimary>
|
||||
);
|
||||
};
|
||||
|
||||
export default forwardRef(LoadingButton);
|
||||
Reference in new issue
Block a user