Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): show error at the end if inputs are not valid #398

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ef28853
fix(form): show error at the end if inputs are not valid
NicoSerranoP Oct 15, 2024
ab761bf
fix: use callback for jsx handler
NicoSerranoP Oct 16, 2024
b653452
fix: red error text + align right validation errors message
NicoSerranoP Oct 16, 2024
f88cbb2
fix: delete error message after 5s
NicoSerranoP Oct 16, 2024
a4710b7
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 16, 2024
3f87bfe
fix: clear timeout
NicoSerranoP Oct 21, 2024
083b2cb
fix: dont submit form in image upload
NicoSerranoP Oct 21, 2024
be6a35a
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 22, 2024
c288b84
feat: create application button specific for each round
NicoSerranoP Oct 22, 2024
4cfb64b
fix: delete unused ui buttons
NicoSerranoP Oct 23, 2024
66e1fbf
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 23, 2024
34fed22
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 23, 2024
aeb13ee
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 25, 2024
3131d52
Update packages/interface/src/components/ui/Form.tsx
NicoSerranoP Oct 28, 2024
7167889
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 28, 2024
8f58786
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 29, 2024
6f6530e
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Oct 29, 2024
1171a72
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Nov 8, 2024
1e6e792
Merge branch 'main' into fix/validation-errors-in-form
NicoSerranoP Nov 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/interface/src/components/ui/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
type ComponentProps,
forwardRef,
cloneElement,
useState,
useCallback,
} from "react";
import {
FormProvider,
Expand Down Expand Up @@ -62,7 +64,7 @@ export const Label = createComponent(
}),
);

export const ErrorMessage = createComponent("div", tv({ base: "pt-1 text-xs text-red-500" }));
export const ErrorMessage = createComponent("div", tv({ base: "pt-1 text-xs text-red" }));

export const Textarea = createComponent("textarea", tv({ base: [...inputBase, "w-full"] }));

Expand Down Expand Up @@ -227,16 +229,27 @@ export const Form = <S extends z.Schema>({
resolver: zodResolver(schema),
mode: "onBlur",
});
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const onSubmitForm = useCallback(
form.handleSubmit(
(data) => {
setErrorMessage(null);
onSubmit(data, form);
},
() => {
setErrorMessage("There are errors in the form. Please go back and check the warnings.");
},
),
[],
NicoSerranoP marked this conversation as resolved.
Show resolved Hide resolved
);

// Pass the form methods to a FormProvider. This lets us access the form from components with useFormContext
return (
<FormProvider {...form}>
<form
onSubmit={form.handleSubmit((data) => {
onSubmit(data, form);
})}
>
<form onSubmit={onSubmitForm}>
{children}

{errorMessage && <ErrorMessage style={{ textAlign: "end" }}>{errorMessage}</ErrorMessage>}
</form>
</FormProvider>
);
Expand Down
Loading