Skip to content

Commit 8e6cb74

Browse files
authored
Fix submit button loading indicator (#346)
Signed-off-by: Nik Nasr <[email protected]>
1 parent 0ce2271 commit 8e6cb74

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

libs/ui/button/src/lib/SubmitButton.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { useRef, type PropsWithChildren, useDeferredValue } from 'react';
1+
import {
2+
type PropsWithChildren,
3+
useCallback,
4+
useDeferredValue,
5+
useState,
6+
} from 'react';
27
import { useFetchers, useHref, useNavigation } from 'react-router';
38
import { Button } from './Button';
49
import { tv } from '@restate/util/styles';
@@ -32,8 +37,9 @@ function useIsSubmitting(action?: string) {
3237
}
3338
const formActionPathname =
3439
basename === '/'
35-
? actionUrl?.pathname
36-
: actionUrl?.pathname.split(basename.replace(/\/$/, '')).at(-1);
40+
? String(actionUrl?.pathname) + actionUrl?.search
41+
: String(actionUrl?.pathname.split(basename.replace(/\/$/, '')).at(-1)) +
42+
actionUrl?.search;
3743
const fetchers = useFetchers();
3844
const submitFetcher = fetchers.find(
3945
(fetcher) => fetcher.formAction === formActionPathname,
@@ -59,16 +65,18 @@ export function SubmitButton({
5965
isPending,
6066
...props
6167
}: PropsWithChildren<SubmitButtonProps>) {
62-
const ref = useRef<HTMLButtonElement | null>(null);
63-
const formElement = ref.current?.form;
64-
const isSubmitting =
65-
useIsSubmitting(formElement?.action) || Boolean(isPending);
68+
const [action, setAction] = useState<string>();
69+
const isSubmitting = useIsSubmitting(action) || Boolean(isPending);
6670
const deferredIsSubmitting = useDeferredValue(isSubmitting);
71+
const setFormAction = useCallback((el: HTMLButtonElement | null) => {
72+
setAction(el?.form?.action);
73+
}, []);
74+
6775
return (
6876
<Button
6977
{...props}
7078
type="submit"
71-
ref={ref}
79+
ref={setFormAction}
7280
disabled={deferredIsSubmitting || disabled}
7381
>
7482
{deferredIsSubmitting && !hideSpinner ? (

0 commit comments

Comments
 (0)