documentation/versioned_docs/version-3.xx.xx/api-reference/antd/hooks/field/useCheckboxGroup.md
useCheckboxGroup hook allows you to manage an Ant Design Checkbox.Group component when records in a resource needs to be used as checkbox options.
We will demonstrate how to get data at the /tags endpoint from the https://api.fake-rest.refine.dev REST API.
{
[
{
id: 1,
title: "Driver Deposit",
},
{
id: 2,
title: "Index Compatible Synergistic",
},
{
id: 3,
title: "Plum",
},
];
}
import { Form, Checkbox, useCheckboxGroup } from "@pankod/refine-antd";
export const PostCreate: React.FC = () => {
// highlight-start
const { checkboxGroupProps } = useCheckboxGroup<ITag>({
resource: "tags",
});
// highlight-end
return (
<Form>
<Form.Item label="Tags" name="tags">
// highlight-next-line
<Checkbox.Group {...checkboxGroupProps} />
</Form.Item>
</Form>
);
};
interface ITag {
id: number;
title: string;
}
All we have to do is pass the checkboxGroupProps it returns to the <Checkbox.Group> component.
useCheckboxGroup uses the useList hook for fetching data. Refer to useList hook for details. →
resourceconst { checkboxGroupProps } = useCheckboxGroup({
resource: "tags",
});
resource property determines which? API resource endpoint to fetch records from dataProvider. It returns properly configured options values for checkboxes.
Refer to Ant Design Checkbox.Group component documentation for detailed info for options. →
defaultValueconst { selectProps } = useCheckboxGroup({
resource: "languages",
// highlight-next-line
defaultValue: [1, 2],
});
The easiest way to selecting a default values for checkbox fields is by passing in defaultValue.
optionLabel and optionValueconst { checkboxGroupProps } = useCheckboxGroup({
resource: "tags",
// highlight-start
optionLabel: "title",
optionValue: "id",
// highlight-end
});
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",
// highlight-start
optionLabel: "nested.title",
optionValue: "nested.id",
// highlight-end
});
:::
filtersconst { checkboxGroupProps } = useCheckboxGroup({
resource: "tags",
// highlight-start
filters: [
{
field: "title",
operator: "eq",
value: "Driver Deposit",
},
],
// highlight-end
});
It allows us to add some filters while fetching the data. For example, if you want to list only the titles that are equal to "Driver Deposit" records.
sortconst { checkboxGroupProps } = useCheckboxGroup({
resource: "tags",
// highlight-start
sort: [
{
field: "title",
order: "asc",
},
],
// highlight-end
});
It allows us to sort the options. For example, if you want to sort your list according to title by ascending.
fetchSizeconst { selectProps } = useCheckboxGroup({
resource: "languages",
// highlight-next-line
fetchSize: 20,
});
Amount of records to fetch in checkboxes.
queryOptionsconst { checkboxGroupProps } = useCheckboxGroup({
resource: "tags",
// highlight-start
queryOptions: {
onError: () => {
console.log("triggers when on query return Error");
},
},
// highlight-end
});
useQuery options can be set by passing queryOptions property.
paginationAllows us to set page and items per page values.
For example imagine that we have 1000 post records:
const { selectProps } = useSelect({
resource: "categories",
// highlight-next-line
pagination: { current: 3, pageSize: 8 },
});
Listing will start from page 3 showing 8 records.