Core
Lynx.ts Core
Requirements
Install
yarn add @lynxts/corenpm i @lynxts/coreUsage
import { FormProvider, fieldOf } from "@lynxts/core";
import { ReactElement, memo, useCallback } from "react";
import { ObjectSchema, object, string } from "yup";
interface Login {
email: string;
password: string;
}
const schema: ObjectSchema<Login> = object({
email: string().email().required(),
password: string().required(),
});
const Field = fieldOf<Login>();
const SignIn = memo((): ReactElement => {
const handleSubmit = useCallback((values: Login): void => {
const { email, password } = values;
// Use the validated value to sign in!
}, []);
return (
<FormProvider onSubmit={handleSubmit} validation={schema}>
{({ submit }) => (
<>
<Field name="email" fallback="">
{({ handleChange, setTouched, value, required, error }) => (
<label>
{`Email:${required ? "*" : ""}`}
<input
type="email"
onChange={handleChange(e => e.target.value)}
onBlur={setTouched}
value={value}
/>
{error && <small>{error}</small>}
</label>
)}
</Field>
<Field name="password" fallback="">
{({ handleChange, setTouched, value, required, error }) => (
<label>
{`Password:${required ? "*" : ""}`}
<input
type="password"
onChange={handleChange(e => e.target.value)}
onBlur={setTouched}
value={value}
/>
{error && <small>{error}</small>}
</label>
)}
</Field>
<button type="button" onClick={submit}>
{"SignIn"}
</button>
</>
)}
</FormProvider>
);
});Accessing the form state
Custom fields
Dynamic array field
Validation adapters
API Reference
Something's missing?
Contributions
License
Last updated
