diff options
| author | Sebastien Castiel <sebastien@castiel.me> | 2023-12-06 12:22:24 -0500 |
|---|---|---|
| committer | Sebastien Castiel <sebastien@castiel.me> | 2023-12-06 12:22:24 -0500 |
| commit | 6c4ced0f79f801d85da5e4973ae4154f01b04dba (patch) | |
| tree | dbb8eab58ef6d3e8cec6f3c7187e2a4596134d4a /src/components | |
| parent | ed55c696cd083bfaa5429082a9c5edd4ebde0cd8 (diff) | |
Loading screens & layouts
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/expense-form.tsx | 6 | ||||
| -rw-r--r-- | src/components/group-form.tsx | 9 | ||||
| -rw-r--r-- | src/components/submit-button.tsx | 23 |
3 files changed, 30 insertions, 8 deletions
diff --git a/src/components/expense-form.tsx b/src/components/expense-form.tsx index 056d25e..f7826c6 100644 --- a/src/components/expense-form.tsx +++ b/src/components/expense-form.tsx @@ -1,5 +1,5 @@ 'use client' -import { Button } from '@/components/ui/button' +import { SubmitButton } from '@/components/submit-button' import { Card, CardContent, @@ -175,9 +175,7 @@ export function ExpenseForm({ group, expense, onSubmit }: Props) { </CardContent> <CardFooter> - <Button variant="secondary" type="submit"> - Submit - </Button> + <SubmitButton loadingContent="Submitting…">Submit</SubmitButton> </CardFooter> </Card> </form> diff --git a/src/components/group-form.tsx b/src/components/group-form.tsx index 0b6e560..9841cc5 100644 --- a/src/components/group-form.tsx +++ b/src/components/group-form.tsx @@ -1,4 +1,5 @@ 'use client' +import { SubmitButton } from '@/components/submit-button' import { Button } from '@/components/ui/button' import { Card, @@ -25,7 +26,7 @@ import { useFieldArray, useForm } from 'react-hook-form' export type Props = { group?: NonNullable<Awaited<ReturnType<typeof getGroup>>> - onSubmit: (groupFormValues: GroupFormValues) => void + onSubmit: (groupFormValues: GroupFormValues) => Promise<void> } export function GroupForm({ group, onSubmit }: Props) { @@ -49,8 +50,8 @@ export function GroupForm({ group, onSubmit }: Props) { return ( <Form {...form}> <form - onSubmit={form.handleSubmit((values) => { - onSubmit(values) + onSubmit={form.handleSubmit(async (values) => { + await onSubmit(values) })} className="space-y-8" > @@ -129,7 +130,7 @@ export function GroupForm({ group, onSubmit }: Props) { </CardFooter> </Card> - <Button type="submit">Submit</Button> + <SubmitButton loadingContent="Submitting…">Submit</SubmitButton> </form> </Form> ) diff --git a/src/components/submit-button.tsx b/src/components/submit-button.tsx new file mode 100644 index 0000000..7489c6c --- /dev/null +++ b/src/components/submit-button.tsx @@ -0,0 +1,23 @@ +import { Button } from '@/components/ui/button' +import { Loader2 } from 'lucide-react' +import { PropsWithChildren, ReactNode } from 'react' +import { useFormState } from 'react-hook-form' + +type Props = PropsWithChildren<{ + loadingContent: ReactNode +}> + +export function SubmitButton({ children, loadingContent }: Props) { + const { isSubmitting } = useFormState() + return ( + <Button type="submit" disabled={isSubmitting}> + {isSubmitting ? ( + <> + <Loader2 className="w-4 h-4 mr-2 animate-spin" /> {loadingContent} + </> + ) : ( + children + )} + </Button> + ) +} |
