Skip to main content
Version: 4.xx.xx

useRadioGroup

useRadioGroup hook allows you to manage an Ant Design Radio.Group component when records in a resource needs to be used as radio options.

Usage

We will demonstrate how to get data at /languages endpoint from the https://api.fake-rest.refine.dev REST API.

https://api.fake-rest.refine.dev/languages
{
[
{
id: 1,
title: "Turkish",
},
{
id: 2,
title: "English",
},
{
id: 3,
title: "German",
},
];
}
pages/posts/create.tsx
import { useRadioGroup } from "@refinedev/antd";
import { Form, Radio } from "antd";

export const PostCreate = () => {
const { radioGroupProps } = useRadioGroup<ILanguage>({
resource: "languages",
});

return (
<Form>
<Form.Item label="Languages" name="languages">
<Radio.Group {...radioGroupProps} />
</Form.Item>
</Form>
);
};

interface ILanguage {
id: number;
title: string;
}
Radio group

All we have to do is pass the radioGroupProps it returns to the <Radio.Group> component.

useRadioGroup uses the useList hook for fetching data.

For more information, refer to the Refine Core's useList hook documentation

Options

resource

const { radioGroupProps } = useRadioGroup({
resource: "languages",
});

resource property determines API resource endpoint to fetch records from dataProvider. It returns properly configured options values for radio buttons.

If you have multiple resources with the same name, you can pass the identifier instead of the name of the resource. It will only be used as the main matching key for the resource, data provider methods will still work with the name of the resource defined in the <Refine/> component.

For more information, refer to the identifier section of the <Refine/> component documentation

For more information, refer to the Ant Design's Radio.Group component documentation

defaultValue

const { selectProps } = useRadioGroup({
resource: "languages",
defaultValue: 1,
});

selectedOptionsOrder

selectedOptionsOrder allows us to sort selectedOptions on defaultValue. It can be:

  • "in-place": sort selectedOptions at the bottom. It is by default.
  • "selected-first": sort selectedOptions at the top.
const { selectProps } = useRadioGroup({
resource: "languages",
defaultValue: 1,
selectedOptionsOrder: "selected-first", // in-place | selected-first
});

The easiest way to selecting a default value for an radio button field is by passing in defaultValue.

optionLabel and optionValue

const { radioGroupProps } = useRadioGroup({
resource: "languages",
optionLabel: "title",
optionValue: "id",
});

optionLabel and optionValue allows you to change the values and appearances of your options. Default values are optionLabel = "title" and optionValue = "id".

These properties also support nested property access with Object path syntax.

const { options } = useRadioGroup({
resource: "categories",
optionLabel: "nested.title",
optionValue: "nested.id",
});

It's also possible to pass function to these props. These functions will receive item argument.

const { options } = useRadioGroup({
optionLabel: (item) => `${item.firstName} ${item.lastName}`,
optionValue: (item) => item.id,
});

searchField

Can be used to specify which field will be searched with value given to onSearch function.

const { onSearch } = useRadioGroup({ searchField: "name" });

onSearch("John"); // Searches by `name` field with value John.

By default, it uses optionLabel's value, if optionLabel is a string. Uses title field otherwise.

// When `optionLabel` is string.
const { onSearch } = useRadioGroup({ optionLabel: "name" });

onSearch("John"); // Searches by `name` field with value John.

// When `optionLabel` is function.
const { onSearch } = useRadioGroup({
optionLabel: (item) => `${item.id} - ${item.name}`,
});

onSearch("John"); // Searches by `title` field with value John.

filters

filters allows us to add filters while fetching the data. For example, if you want to list only the titles that are equal to "German":

const { radioGroupProps } = useRadioGroup({
resource: "languages",
filters: [
{
field: "title",
operator: "eq",
value: "German",
},
],
});

sorters

sorters allows us to sort the options. For example, if you want to sort your list according to title by ascending:

const { radioGroupProps } = useRadioGroup({
resource: "languages",
sorters: [
{
field: "title",
order: "asc",
},
],
});

fetchSize

fetchSize is the amount of records to fetch in checkboxes.

const { selectProps } = useRadioGroup({
resource: "languages",
fetchSize: 20,
});

queryOptions

Passing the queryOptions property allows us to set the useQuery options

const { radioGroupProps } = useRadioGroup({
resource: "languages",
queryOptions: {
onError: () => {
console.log("triggers when on query return Error");
},
},
});

pagination

pagination allows us to set page and items per page values.

For example, lets say that we have 1000 post records:

const { selectProps } = useRadioGroup({
resource: "categories",
pagination: { current: 3, pageSize: 8 },
});

The listing will start from page 3, showing 8 records per page.

sort
deprecated

Use sorters instead.

API Reference

Properties

PropertyTypeDescriptionDefault
sort

Use sorters instead

CrudSort[]

Allow us to sort the options

resource

string

Resource name for API data interactions

successNotification

false

OpenNotificationParams

((data?: GetListResponse<TData>, values?: { config?: UseListConfig; ... 6 more ...; dataProviderName?: string

undefined; }

undefined, resource?: string

undefined) => false

OpenNotificationParams)

undefined

Success notification configuration to be displayed when the mutation is successful.

'"There was an error creating resource (status code: statusCode)" or "Error when updating resource (status code: statusCode)"'

errorNotification

false

OpenNotificationParams

((error?: TError, values?: { config?: UseListConfig; pagination?: Pagination

undefined; ... 5 more ...; dataProviderName?: string

undefined; }

undefined, resource?: string

undefined) => false

OpenNotificationParams)

undefined

Error notification configuration to be displayed when the mutation fails.

'"There was an error creating resource (status code: statusCode)" or "Error when updating resource (status code: statusCode)"'

meta

MetaQuery

Additional meta data to pass to the useMany from the data provider

metaData

metaData is deprecated with refine@4, refine will pass meta instead, however, we still support metaData for backward compatibility.

MetaQuery

Additional meta data to pass to the useMany from the data provider

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.

"off"

liveParams

Params to pass to liveProvider's subscribe method if liveMode is enabled.

undefined

dataProviderName

string

If there is more than one dataProvider, you should use the dataProviderName that you will use.

default

onLiveEvent

Callback to handle all related live events of this hook.

undefined

queryOptions

UseQueryOptions<GetListResponse<TQueryFnData>, TError, GetListResponse<TData>, QueryKey>

react-query useQuery options

overtimeOptions

UseLoadingOvertimeCoreOptions

onSearch

((value: string) => CrudFilter[])

If defined, this callback allows us to override all filters for every search request.

undefined

hasPagination

hasPagination is deprecated, use pagination.mode instead.

boolean

Disabling pagination option from useList()

false

pagination

{ current? : number; pageSize?: number;}

Pagination option from useList()

undefined

filters

CrudFilter[]

Resource name for API data interactions

sorters

CrudSort[]

Allow us to sort the options

optionLabel

string

((item: TData) => string)

Set the option's label value

"title"

optionValue

string

((item: TData) => string)

Set the option's value

"id"

searchField

string

If provided optionLabel is a string, uses optionLabel's value.

"title"

selectedOptionsOrder

"in-place" | "selected-first"

Allow us to sort the selection options

in-place

debounce

number

The number of milliseconds to delay

300

defaultValueQueryOptions

UseQueryOptions<GetManyResponse<TQueryFnData>, TError, GetManyResponse<TQueryFnData>, QueryKey>

react-query useQuery options

fetchSize

use pagination instead

number

Amount of records to fetch in select box list.

undefined

defaultValue

Sets the default value

Type Parameters

PropertyDescriptionTypeDefault
TQueryFnDataResult data returned by the query function. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TDataResult data returned by the select function. Extends BaseRecord. If not specified, the value of TQueryFnData will be used as the default value.BaseRecordTQueryFnData

Return values

PropertyDescriptionType
radioGroupPropsAnt design radio group propsRadio Group
queryResultResults of the query of a recordQueryObserverResult<{ data: TData }>

Example

Run on your local
npm create refine-app@latest -- --example field-antd-use-radio-group