mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
replaced hook form with json schema from
This commit is contained in:
@@ -34,7 +34,8 @@
|
||||
"liquidjs": "^10.15.0",
|
||||
"lodash-es": "^4.17.21",
|
||||
"oauth4webapi": "^2.11.1",
|
||||
"swr": "^2.2.5"
|
||||
"swr": "^2.2.5",
|
||||
"json-schema-form-react": "link:json-schema-form-react"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@aws-sdk/client-s3": "^3.613.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { typeboxResolver } from "@hookform/resolvers/typebox";
|
||||
import { Type } from "core/utils";
|
||||
import type { ValueError } from "@sinclair/typebox/value";
|
||||
import { type TSchema, Type, Value } from "core/utils";
|
||||
import { Form, type Validator } from "json-schema-form-react";
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
import { Button } from "ui/components/buttons/Button";
|
||||
import * as Formy from "ui/components/form/Formy";
|
||||
@@ -11,6 +11,13 @@ export type LoginFormProps = Omit<ComponentPropsWithoutRef<"form">, "onSubmit">
|
||||
formData?: any;
|
||||
};
|
||||
|
||||
class TypeboxValidator implements Validator<ValueError> {
|
||||
async validate(schema: TSchema, data: any) {
|
||||
return Value.Check(schema, data) ? [] : [...Value.Errors(schema, data)];
|
||||
}
|
||||
}
|
||||
const validator = new TypeboxValidator();
|
||||
|
||||
const schema = Type.Object({
|
||||
email: Type.String({
|
||||
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"
|
||||
@@ -20,25 +27,26 @@ const schema = Type.Object({
|
||||
})
|
||||
});
|
||||
|
||||
export function LoginForm({ formData, className, method = "POST", ...props }: LoginFormProps) {
|
||||
const {
|
||||
register,
|
||||
formState: { isValid, errors }
|
||||
} = useForm({
|
||||
mode: "onChange",
|
||||
defaultValues: formData,
|
||||
resolver: typeboxResolver(schema)
|
||||
});
|
||||
|
||||
export function LoginForm({ formData, className, ...props }: LoginFormProps) {
|
||||
return (
|
||||
<form {...props} method={method} className={twMerge("flex flex-col gap-3 w-full", className)}>
|
||||
<Form
|
||||
method="POST"
|
||||
{...props}
|
||||
schema={schema}
|
||||
validator={validator}
|
||||
validationMode="change"
|
||||
className={twMerge("flex flex-col gap-3 w-full", className)}
|
||||
>
|
||||
{({ errors, submitting }) => (
|
||||
<>
|
||||
<pre>{JSON.stringify(errors, null, 2)}</pre>
|
||||
<Formy.Group>
|
||||
<Formy.Label htmlFor="email">Email address</Formy.Label>
|
||||
<Formy.Input type="email" {...register("email")} />
|
||||
<Formy.Input type="email" name="email" />
|
||||
</Formy.Group>
|
||||
<Formy.Group>
|
||||
<Formy.Label htmlFor="password">Password</Formy.Label>
|
||||
<Formy.Input type="password" {...register("password")} />
|
||||
<Formy.Input type="password" name="password" />
|
||||
</Formy.Group>
|
||||
|
||||
<Button
|
||||
@@ -46,10 +54,12 @@ export function LoginForm({ formData, className, method = "POST", ...props }: Lo
|
||||
variant="primary"
|
||||
size="large"
|
||||
className="w-full mt-2 justify-center"
|
||||
disabled={!isValid}
|
||||
disabled={errors.length > 0 || submitting}
|
||||
>
|
||||
Sign in
|
||||
</Button>
|
||||
</form>
|
||||
</>
|
||||
)}
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ export function AuthLogin() {
|
||||
},
|
||||
{}
|
||||
) as Record<string, AppAuthOAuthStrategy>;
|
||||
//console.log("oauth", oauth, strategies);
|
||||
|
||||
return (
|
||||
<AppShell.Root>
|
||||
|
||||
Reference in New Issue
Block a user