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. Refer to Ant Design useList hook for details. &#8594

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.

Refer to the Ant Design's Radio.Group component documentation for detailed info on options. &#8594

defaultValue

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

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".

tip

Supports use with optionLabel and optionValue Object path syntax.

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

filters

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

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" records.

sorters

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

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

fetchSize

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

Amount of records to fetch in radio group buttons.

queryOptions

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

useQuery options can be set by passing queryOptions property.

pagination

Allows us to set page and items per page values.

For example imagine that we have 1000 post records:

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

Listing will start from page 3 showing 8 records.

sort

Deprecated

Use sorters instead.

API Reference

Properties

Type Parameters

PropertyDesriptionTypeDefault
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 IN YOUR LOCAL
npm create refine-app@latest -- --example field-antd-use-radio-group