optionsFrom
Turns a list endpoint into options for a Select or ProFormSelect, with search on what the user types. Use it for customer, staff and product pickers.
Usage
app/tickets/new/page.tsx
import { ProForm, ProFormSelect } from "@ant-design/pro-components";
import { useApiClient } from "app/api";
import { optionsFrom, submit } from "app/hooks";
export default function NewTicketPage() {
const api = useApiClient();
return (
<ProForm onFinish={submit((v) => api.tickets.create(v), { done: "Device received" })}>
<ProFormSelect name="customerId" label="Customer" showSearch
request={optionsFrom(api.customers.list, "name")} />
<ProFormSelect name="technicianId" label="Technician"
request={optionsFrom(api.users.members, (u) => `${u.name} (${u.role})`)} />
</ProForm>
);
}Signature
app/hooks.ts
function optionsFrom(
list: (q?: any) => Promise<any>,
label?: string | ((r: any) => string),
value?: string,
): (params?: { keyWords?: string; [k: string]: any }) => Promise<Array<{ label: string; value: any }>>;Parameters
list: a list function from useApiClient, such asapi.customers.listorapi.users.members.label: the field used as the label,"name"by default; or a function that takes the row and returns the label.value: the field used as the value,"id"by default.
Returns
A function in the shape ProFormSelect’s request expects. Each run calls list with pageSize: 50; when the field has showSearch, what the user types is sent as search. An array or { data, total } both work, and the result is [{ label, value }].
Examples
A customer picker that searches by name or phone: declare the search columns on the server with listOf(customers, query, { search: [customers.name, customers.phone] }), then turn on showSearch as above.
- Only the first 50 rows load. For long lists, always turn on
showSearch. api.users.members()returns an array of active users and any signed-in user may call it; it fits assignee pickers.
Next steps
- submit: send the form this picker sits in.
- listSchema, listOf: declare the search columns on the server.
- Forms: common form layouts.
Last updated