-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@babylonlabs-io/bbn-core-ui": minor | ||
--- | ||
|
||
add Form widget |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import * as yup from "yup"; | ||
|
||
import { Form } from "./Form"; | ||
import { useField } from "./hooks"; | ||
import { Input } from "@/components/Form"; | ||
|
||
const meta: Meta<typeof Form> = { | ||
component: Form, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const Field = () => { | ||
const { error, invalid, ...inputProps } = useField({ name: "test", autoFocus: true, defaultValue: "test" }); | ||
|
||
return <Input {...inputProps} state={invalid ? "error" : "default"} stateText={error} />; | ||
Check failure on line 20 in src/widgets/Form/Form.stories.tsx GitHub Actions / lint_test / build
|
||
}; | ||
|
||
const schema = yup | ||
.object() | ||
.shape({ | ||
test: yup.string().required(), | ||
}) | ||
.required(); | ||
|
||
export const Default: Story = { | ||
args: { | ||
onChange: console.log, | ||
schema, | ||
}, | ||
render: (props) => ( | ||
<Form {...props}> | ||
<Field /> | ||
</Form> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { type PropsWithChildren, useEffect, HTMLProps } from "react"; | ||
import { | ||
type DefaultValues, | ||
type Mode, | ||
type SubmitHandler, | ||
type DeepPartial, | ||
FormProvider, | ||
useForm, | ||
Resolver, | ||
} from "react-hook-form"; | ||
import { yupResolver } from "@hookform/resolvers/yup"; | ||
import { type ObjectSchema } from "yup"; | ||
import { twJoin } from "tailwind-merge"; | ||
|
||
export interface FormProps<V extends object> extends PropsWithChildren { | ||
className?: string; | ||
name?: string; | ||
mode?: Mode; | ||
reValidateMode?: Exclude<Mode, "onTouched" | "all">; | ||
defaultValues?: DefaultValues<V>; | ||
schema?: ObjectSchema<V>; | ||
formProps?: HTMLProps<HTMLFormElement>; | ||
onSubmit?: SubmitHandler<V>; | ||
onChange?: (data: DeepPartial<V>) => void; | ||
} | ||
|
||
export function Form<V extends object>({ | ||
className, | ||
name, | ||
children, | ||
mode = "onBlur", | ||
reValidateMode = "onBlur", | ||
defaultValues, | ||
schema, | ||
formProps, | ||
onSubmit = () => null, | ||
onChange, | ||
}: FormProps<V>) { | ||
const methods = useForm({ | ||
mode, | ||
reValidateMode, | ||
defaultValues, | ||
resolver: schema ? (yupResolver(schema) as unknown as Resolver<V>) : undefined, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!onChange) return; | ||
|
||
const { unsubscribe } = methods.watch(onChange); | ||
|
||
return unsubscribe; | ||
}, [onChange, methods.watch]); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<form | ||
className={twJoin("bbn-form", className)} | ||
name={name} | ||
onSubmit={methods.handleSubmit(onSubmit)} | ||
{...formProps} | ||
> | ||
{children} | ||
</form> | ||
</FormProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect } from "react"; | ||
import { useController, useFormContext } from "react-hook-form"; | ||
|
||
interface FieldProps<V> { | ||
name: string; | ||
defaultValue?: V; | ||
disabled?: boolean; | ||
autoFocus?: boolean; | ||
shouldUnregister?: boolean; | ||
} | ||
|
||
export function useField<V = string>({ | ||
name, | ||
defaultValue, | ||
disabled = false, | ||
autoFocus = false, | ||
shouldUnregister = false, | ||
}: FieldProps<V>) { | ||
const { setFocus } = useFormContext(); | ||
const { field, fieldState } = useController({ name, defaultValue, disabled, shouldUnregister }); | ||
const { invalid, isTouched, error } = fieldState; | ||
|
||
useEffect(() => { | ||
if (autoFocus) { | ||
setFocus(name); | ||
} | ||
}, [name]); | ||
|
||
return { | ||
...field, | ||
value: field.value as V, | ||
invalid: invalid && isTouched, | ||
error: error?.message ?? "", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { useFormContext, useFormState, useWatch } from "react-hook-form"; | ||
export * from "./Form"; | ||
export * from "./hooks"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters