-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Default value not showing the correct selected value #1361
Comments
I am having a similar issue too. It seems that "dynamically" generated
I am not seeing the value selected: However, Zod is getting the value successfully: Now, if I replace the dynamically generated values with static values as below, it will work, and I will be able to see what I selected:
Something is wrong when we generate the |
Here is codesandbox view for your reference |
@yousihy , have you resolved this? |
@yousihy @codingwithashu I hope you are aware of the fact that |
I was also having trouble getting this to work. But @hardikchopra242 nailed it for me. The example of usage in a form in the docs leads to this mistake (defaultValue instead of value). |
For my case, the issue was that I needed to strictly specify that the value is a So, I had to convert the
So, the
|
This did not work for me or it didn't have any effect on my problem. My controls are working fine with |
@yousihy Thanks!
this saved me to waste much more time on this |
First of all, if you are using value, there is no reason to also use defaultValue. Inside attributes comment "value" and "onValueChange". And add .toString() inside defaultValue like this: defaultValue={field.value.toString()} try if it works |
It works 👍 For set the defaulValues for the form use:
|
As the Radix docs state: |
solved 💯 ;) 👍 🥇 Actually, due to the fact that shadcn uses radix in itself and when you are importing the select component, it is probably wrongly imported from radix and you just need to import it from the select component related to shadcn. import { SelectItem, SelectValue } from "@radix-ui/react-select"; //wrong import import {SelectItem, SelectValue } from "@/components/ui/select"; //correct import |
This fixed my issue. |
Where is the entire code that works? |
I've found the best way is to set the I wonder if it's because of the number -> string conversion?
|
Hi all, What if I want the For example I want to select an item but that item is an object "behind the scenes" and not a simple string. Is there a possibility to do that? Thanks. |
Pass what you want as a default as props Select onValueChange={handleChange} defaultValue={value.toString()} it worked for me. |
Is there a solution ? |
I did it like this and it worked... Using nextjs 14 in a form with zod: const form = useForm<z.infer<typeof UpdateCompanyFormSchema>>({
resolver: zodResolver(UpdateCompanyFormSchema),
defaultValues: {
companyId: companySelected?.id.toString() || "",
},
});
useEffect(() => {
if (companySelected) {
form.setValue("companyId", companySelected.id.toString());
}
}, [companySelected, form]);
<FormItem>
<FormLabel>Nome da empresa</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value || ""}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Selecione a empresa" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectGroup>
<SelectLabel>ATIVAS</SelectLabel>
{companies
.filter((company) => company.active)
.map((company) => (
<SelectItem
key={company.id}
value={company.id.toString()}
>
{company.name}
</SelectItem>
))}
</SelectGroup>
<SelectGroup>
<SelectLabel>INATIVAS</SelectLabel>
{companies
.filter((company) => !company.active)
.map((company) => (
<SelectItem
key={company.id}
value={company.id.toString()}
className="text-gray-700"
>
{company.name}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<FormDescription>Nome da empresa</FormDescription>
<FormMessage />
</FormItem>
)} |
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you. |
Guys you can parse default values straight into react-hook-form https://github.com/orgs/react-hook-form/discussions/7491#discussioncomment-4942804
When you do it like this, default values will work. |
i solved my problem putting "default Value" and "value" on select tag, thanks for every one his put his comment her |
If you are using Controlled Form use "value" instead of "defaultValue". If you are using uncontrolled form you may use "defaultValue". Thanks |
I have a button with action to set a sorting on select input. On that button i change the value on useState from latest -> price-low-high.
When the button clicked the value inside render-value class changed. but the default value for the select not changed.
const [sort, setSort] = useState<string>('latest')
The text was updated successfully, but these errors were encountered: