useSelect
useSelect
hook allows you to manage an Ant Design Select component when records in a resource needs to be used as select options.
Usage
We'll demonstrate how to get data at /categories
endpoint from https://api.fake-rest.refine.dev
REST API.
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
{
id: 3,
title: "Unbranded",
},
];
}
import { Form, Select, useSelect } from "@pankod/refine";
export const PostCreate = () => {
const { selectProps } = useSelect<ICategory>({
resource: "categories",
});
return (
<Form>
<Form.Item label="Categories" name="categories">
<Select {...selectProps} />
</Form.Item>
</Form>
);
};
interface ICategory {
id: string;
title: string;
}
All we have to do is pass the selectProps
it returns to the <Select>
component.
By default, refine does the search using the useList
hook and passes it to the search parameter. If you get a problem you should check your getList
function in your Data Provider. If you want to change this behavior to make client-side filtering, you can examine this example.
useSelect
uses the useList
hook for fetching data. Refer to useList
hook for details. →
Options
resource
const { selectProps } = useSelect({
resource: "categories",
});
resource
property determines API resource endpoint to fetch records from dataProvider
. It returns properly configured options
values for select options.
Refer to Ant Design Select
component documentation for detailed info for options
. →
defaultValue
const { selectProps } = useSelect({
resource: "categories",
defaultValue: "1",
});
Adds extra options
to <Select>
component. It uses useMany
so defaultValue
can be an array of strings like follows.
defaultValue: ["1", "2"],
Refer to the useMany
documentation for detailed usage. →
Can use defaultValue
property when edit a record in <Edit>
component.
optionLabel
and optionValue
const { selectProps } = useSelect({
resource: "categories",
optionLabel: "title",
optionValue: "id",
});
Allows you to change the values and appearance of your options. Default values are optionLabel = "title"
and optionValue = "id"
.
filters
const { selectProps } = useSelect({
resource: "categories",
filters: [
{
field: "isActive",
operator: "eq",
value: true,
},
],
});
It allows us to add some filters while fetching the data. For example, if you want to list only the active records.
sort
const { selectProps } = useSelect({
resource: "categories",
sort: [
{
field: "title",
order: "asc",
},
],
});
It allows us to sort the options
. For example, if you want to sort your list according to title
by ascending.
fetchSize
const { selectProps } = useSelect({
resource: "categories",
fetchSize: 20,
});
Amount of records to fetch in select box.
onSearch
const { selectProps } = useSelect({
resource: "categories",
onSearch: (value) => [
{
field: "title",
operator: "containss",
value,
},
],
});
If defined, it allows us to override the filters to use when fetching list of records. It should return CrudFilters
.
queryOptions
const { selectProps } = useSelect({
resource: "categories",
queryOptions: {
onError: () => {
console.log("triggers when on query return Error");
},
},
});
useQuery options can be set by passing queryOptions
property.
defaultValueQueryOptions
const { selectProps } = useSelect({
resource: "categories",
defaultValueQueryOptions: {
onSuccess: (data) => {
console.log("triggers when on query return on success");
},
},
});
useQuery options for default value query can be set by passing queryOptions
property.
API Reference
Properties
Property | Description | Type | Default |
---|---|---|---|
resource Required | Resource name for API data interactions | string | |
defaultValue | Adds extra options | string | Array<string> | |
optionValue | Set the option's value | string | "id" |
optionLabel | Set the option's label value | string | "title" |
filters | Add filters while fetching the data | CrudFilters | |
sort | Allow us to sort the options | CrudSorting | |
debounce | The number of milliseconds to delay | number | 300 |
queryOptions | react-query useQuery options | UseQueryOptions<GetListResponse<TData>, TError> | |
fetchSize | Amount of records to fetch in select box list. | number | undefined |
onSearch | If defined, this callback allows us to override all filters for every search request. | (value: string) => CrudFilters | Promise<CrudFilters> | undefined |
defaultValueQueryOptions | react-query useQuery options | UseQueryOptions<GetManyResponse<TData>, TError> | |
metaData | Metadata query for dataProvider | MetaDataQuery | {} |
liveMode | Whether to update data automatically ("auto" ) or not ("manual" ) if a related live event is received. The "off" value is used to avoid creating a subscription. | "auto" | "manual" | "off" | "off" |
liveParams | Params to pass to liveProvider 's subscribe method if liveMode is enabled. | { ids?: string[]; [key: string]: any; } | undefined |
onLiveEvent | Callback to handle all related live events of this hook. | (event: LiveEvent) => void | undefined |
Return values
Property | Description | Type |
---|---|---|
selectProps | Ant design Select props | Select |
queryResult | Result of the query of a record | QueryObserverResult<{ data: TData }> |
defaultValueQueryResult | Result of the query of a defaultValue record | QueryObserverResult<{ data: TData }> |
defaultValueQueryOnSuccess | Default value onSuccess method | () => void |