I believe you can fix this by casting the type of the "value".
This is undocumented, and potentially could break with future releases, but you can do this:
import { Select } from "@mantine/core";
import { useForm } from "@mantine/form";
const data = [
{ value: '', label: 'Select' },
{ value: 2 as any, label: 'New' } // <-- here's the magic
];
export default function MyForm() {
const form = useForm({
initialValues: {
status: ''
},
});
return (
<form onSubmit={form.onSubmit((values) => console.log(values))}>
<Select label="Status" placeholder="Status"
data={data}
{...form.getInputProps('status')} />
<Group position="right" mt="md">
<Button type="submit">Submit</Button>
</Group>
</form>
);
}
I assume that what's happening inside the Select
ultimately is some equality checks that will pass with numbers just as well as strings, so you get the correct item selected when it renders, and the correct value out when it submits.
I personally use this with a zodResolver
that would complain bitterly about providing strings where it expects a number, and I'm loathe to make my zod
schemas more complex to handle this problem.
To be fair, I can appreciate Mantine taking the approach of only supporting strings, since native HTML <select />
elements will always give you strings, and opening the door to anything else is probably a lot of work.
Still, seems like just providing the number value, and keeping typescript happy with as any
(or you could do as unknown as string
if you're not into the whole brevity thing) works a treat - at least for now!