-
Notifications
You must be signed in to change notification settings - Fork 0
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
aaa #1
Comments
import React from 'react';
import { Controller, useFormContext } from 'react-hook-form';
import TextField from '@mui/material/TextField';
interface TextInputProps {
name: string;
label: string;
defaultValue?: string;
}
const TextInput: React.FC<TextInputProps> = ({ name, label, defaultValue = '' }) => {
const { control } = useFormContext();
return (
<Controller
name={name}
control={control}
defaultValue={defaultValue}
render={({ field, fieldState }) => (
<TextField
{...field}
label={label}
error={!!fieldState.error}
helperText={fieldState.error ? fieldState.error.message : ''}
variant="outlined"
fullWidth
/>
)}
/>
);
};
export default TextInput; import React from 'react';
import { useForm, FormProvider, SubmitHandler } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import TextInput from './TextInput';
import Button from '@mui/material/Button';
// Define a schema using Zod (optional)
const schema = z.object({
firstName: z.string().nonempty({ message: "First name is required" }),
lastName: z.string().nonempty({ message: "Last name is required" }),
});
type FormData = z.infer<typeof schema>;
const FeatureForm: React.FC = () => {
const methods = useForm<FormData>({
resolver: zodResolver(schema),
});
const onSubmit: SubmitHandler<FormData> = (data) => {
console.log(data);
};
return (
<FormProvider {...methods}>
<form onSubmit={methods.handleSubmit(onSubmit)}>
<TextInput name="firstName" label="First Name" />
<TextInput name="lastName" label="Last Name" />
<Button type="submit" variant="contained" color="primary">
Submit
</Button>
</form>
</FormProvider>
);
};
export default FeatureForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://confengine.com/conferences/regional-scrum-gathering-tokyo-2024/proposal/19280/10
The text was updated successfully, but these errors were encountered: