- Home
- Forum
- React - EJ 2
- how to use dropdown list component with react hook form (form library)
how to use dropdown list component with react hook form (form library)
Code:
i am including a test project with code sandbox which is available in below link
https://codesandbox.io/s/react-hook-form-with-ui-library-ts-forked-9yd17?file=/src/index.tsx:400-433
https://react-hook-form.com/get-started#IntegratingwithUIlibraries
i have this road block to implement forms further with sync fusion components, where i can't move forward.
Could you please ,help me asap to get rid of this problem
Let me know still any information needed
Thank for your patience.
could you please give me solution ASAP to get rid of this problem as it blocks me on project
As it is blocking me to move forward, could you please provide me solution ASAP.
Thank you.
|
const htmlAttr = { name: "sports" };
<Controller
render={({ field }) => (
<DropDownListComponent {...field} dataSource={sportsData} placeholder="Select a game" htmlAttributes={htmlAttr}/>
)} |
Still i am not able to bind the value from code sandbox in your example, could you please check again?
Sorry for posting again, I appreciate your work on this problem.
But still I am looking for solution, This is blocking me to move forward, Please provide me at least work around.All other form controls also not working like checkbox,date picker, only textbox is working.
|
<form onSubmit={handleSubmit(onSubmit)}>
<label>Textbox</label>
<Controller
render={({ field }) => <TextBoxComponent {...field} />}
name="text"
control={control}
defaultValue=""
/>
<label>Numeric Textbox</label>
<Controller
render={({ field }) => <NumericTextBoxComponent {...field} />}
name="numeric"
control={control}
defaultValue=""
/>
<label>Maksed Textbox</label>
<Controller
render={({ field }) => <MaskedTextBoxComponent {...field} />}
name="mask"
control={control}
defaultValue=""
/>
<label>DatePicker</label>
<Controller
render={({ field }) => <DatePickerComponent {...field} />}
name="date"
control={control}
defaultValue=""
/>
<label>DateTimePicker</label>
<Controller
render={({ field }) => <TimePickerComponent {...field} />}
name="time"
control={control}
defaultValue=""
/>
<label>Checkbox</label>
<Controller
render={({ field }) => (
<CheckBoxComponent
checked={field.value}
change={(e: CheckboxArgs) => field.onChange(e.checked)}
{...field}
/>
)}
name="check"
control={control}
/>
<input type="submit" />
</form>
|
|
|
Hi Team,
I'm facing the same issue and not able to get a workaround while as per this thread the final solution provided did not have a dropdownlistcomponent example.
Can you please help out with a dropdownlistcomponent sample
To use a dropdown list (or select input) with React Hook Form, follow these steps:
1. Install React Hook Form
If you haven’t installed it yet, run:
npm install react-hook-form
2. Basic Dropdown List with register
Here’s how you can integrate a dropdown using React Hook Form’s register method:
import React from "react";
import { useForm } from "react-hook-form";
const MyForm = () => {
const { register, handleSubmit, formState: { errors } } = useForm();
const onSubmit = (data) => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>Select an option:</label>
<select {...register("category", { required: "This field is required" })}>
<option value="">-- Select --</option>
<option value="technology">Technology</option>
<option value="science">Science</option>
<option value="health">Health</option>
</select>
{errors.category && <p>{errors.category.message}</p>}
<button type="submit">Submit</button>
</form>
);
};
export default MyForm;
3. Using Controller for Custom Dropdown Components
If you're using a third-party dropdown component like react-select, use the Controller component:
import React from "react";
import { useForm, Controller } from "react-hook-form";
import Select from "react-select";
const MyForm = () => {
const { control, handleSubmit } = useForm();
const options = [
{ value: "technology", label: "Technology" },
{ value: "science", label: "Science" },
{ value: "health", label: "Health" }
];
const onSubmit = (data) => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>Select a category:</label>
<Controller
name="category"
control={control}
rules={{ required: "This field is required" }}
render={({ field }) => <Select {...field} options={options} />}
/>
<button type="submit">Submit</button>
</form>
);
};
export default MyForm;
Summary
Use register for basic <select> elements.
Use Controller for custom dropdown components (e.g., react-select).
Always handle errors for better UX.
- 20 Replies
- 7 Participants
- Marked answer
-
DJ Darek Johnson
- May 19, 2021 08:49 PM UTC
- Feb 21, 2025 08:05 AM UTC