genetic-orange
genetic-orange9mo ago

Select not sending as number

How to make select send as a number on create?
2 Replies
kapa.ai
kapa.ai9mo ago
Hi @silentnyte. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To make a select send as a number on create, you can use the useSelect hook provided by Refine. This hook allows you to manage the <Select> component when records in a resource need to be used as select options. It also ensures that the defaultValue is included in the options. Here is an example from the Refine documentation:
import { useSelect } from "@refinedev/antd";
import { Select } from "antd";

interface IRole {
id: number;
name: string;
}

const RoleEdit: React.FC = () => {
const { selectProps } = useSelect<IRole>({
resource: "roles",
defaultValue: 11,
});

return (
<Select
placeholder="Select a role"
style={{ width: 300 }}
{...selectProps}
/>
);
};
import { useSelect } from "@refinedev/antd";
import { Select } from "antd";

interface IRole {
id: number;
name: string;
}

const RoleEdit: React.FC = () => {
const { selectProps } = useSelect<IRole>({
resource: "roles",
defaultValue: 11,
});

return (
<Select
placeholder="Select a role"
style={{ width: 300 }}
{...selectProps}
/>
);
};
In this example, useSelect is used to fetch the roles and the defaultValue is set to the id of the role that should be selected by default. The selectProps returned by useSelect are then spread into the <Select> component to populate it with options and preselect the default value. For the issue of ID going as string, you can handle it in the submit function by converting the string ID back to a number before sending it to the server. Sources: - Discord support thread: useSelect with useForm in antd > Answers (https://discord.com/channels/837692625737613362/1167986528643981392) - Documentation > Docs > Api-reference > Antd > Hooks > Field > useSelect > FAQ > How to ensure defaultValue is included in the options? (https://refine.dev/docs/api-reference/antd/hooks/field/useSelect/) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@395261846862561301> kapa.ai is still learning and improving, please let us know how it did by reacting below