Обзор
Компоненты
- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Auth Surface
- Avatar
- Badge
- Browse Catalog Dialog
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Command
- Connector Setup Dialog
- Cookie Banner
- Dialog
- Directory Card
- Directory Detail
- Directory Skeleton
- DrawerНовое
- Token Parts Input
- Empty
- Field
- Fieldset
- File Preview Modal
- File Preview Skeleton
- Form
- Frame
- Group
- Icon
- Input
- Input Group
- Kbd
- Label
- Legal Shell
- Menu
- Mermaid Diagram
- Mind Map Diagram
- Not Found Screen
- Onboarding Frame
- Popover
- PDF Thumbnail
- Personalization Landing
- Preview Card
- Pricing Page
- Progress
- Radio Group
- Ring Spinner
- Scroll Area
- Select
- Separator
- Settings Page
- Settings Skills
- Settings Connectors
- Settings Capabilities
- Settings Usage
- Settings Account
- Settings Billing
- Sheet
- Sidebar
- Skeleton
- Skill Create Dialog
- Slider
- Spinner
- Stat
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Tooltip
AI-компоненты
- Компоненты AI
- Chat Conversation
- Chat Message
- Chat Response
- Chat Suggestion
- Chat Prompt Input
- Slash Highlighted Textarea
- Chat Search Dialog
- Chat Skill Doc
- Chat Connector Detail
- Chat Attachments
- Chat File Card
- Chat Token Chip
- Chat Code Block
- Chat Image
- Chat Inline Citation
- Chat Sources
- Chat Web Search
- Chat Research
- Chat Source
- Chat Actions
- Chat Context
- Chat Loader
- Chat Compaction
- Chat Timeline
- Chat Snippet
- Chat Terminal
- Chat Stack Trace
- Chat Test Results
- Chat File Tree
- Chat Environment Variables
- Chat Audio Player
- Chat Transcription
- Chat Speech Input
- Chat Mic Selector
- Chat Voice Selector
- Chat Agent
- Chat Persona
- Chat Connection
- Chat Connector Suggestion
- Chat Queue
- Chat Checkpoint
- Chat Confirmation
- Chat Artifact
- Chat JSX Preview
- Chat Schema Display
- Chat Package Info
- Chat Commit
- Chat Plan
- Chat Open In Chat
- Chat Sandbox
- Chat Model Selector
- Chat Canvas
- Chat Node
- Chat Edge
Ресурсы
Checkbox Group
Предоставляет общее состояние для набора чекбоксов.
import { Checkbox } from "@/components/ui/checkbox";
import { CheckboxGroup } from "@/components/ui/checkbox-group";
import { Label } from "@/components/ui/label";
export default function Particle() {
return (
<CheckboxGroup aria-label="Выберите фреймворки" defaultValue={["next"]}>
<Label>
<Checkbox value="next" />
Next.js
</Label>
<Label>
<Checkbox value="vite" />
Vite
</Label>
<Label>
<Checkbox value="astro" />
Astro
</Label>
</CheckboxGroup>
);
}
Установка
pnpm dlx shadcn@latest add @oracul/checkbox-group
Использование
import { Checkbox } from "@/components/ui/checkbox"
import { CheckboxGroup } from "@/components/ui/checkbox-group"<CheckboxGroup>
<Label>
<Checkbox defaultChecked />
Next.js
</Label>
<Label>
<Checkbox />
Vite
</Label>
<Label>
<Checkbox />
Astro
</Label>
</CheckboxGroup>API
CheckboxGroup
Стилизованная обёртка над CheckboxGroup из Base UI. Обеспечивает управление общим состоянием для группы чекбоксов с расположением в виде flex-колонки.
Примеры
Для доступной маркировки группы и валидации лучше оборачивать группы чекбоксов в Field и Fieldset. Смотрите связанный пример: Поле группы чекбоксов.
С отключённым элементом
import { Checkbox } from "@/components/ui/checkbox";
import { CheckboxGroup } from "@/components/ui/checkbox-group";
import { Label } from "@/components/ui/label";
export default function Particle() {
return (
<CheckboxGroup aria-label="Выберите фреймворки" defaultValue={["next"]}>
<Label>
<Checkbox value="next" />
Next.js
</Label>
<Label>
<Checkbox disabled value="vite" />
Vite
</Label>
<Label>
<Checkbox value="astro" />
Astro
</Label>
</CheckboxGroup>
);
}
Родительский чекбокс
"use client";
import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { CheckboxGroup } from "@/components/ui/checkbox-group";
import { Label } from "@/components/ui/label";
const frameworks = [
{ id: "next", name: "Next.js" },
{ id: "vite", name: "Vite" },
{ id: "astro", name: "Astro" },
];
export default function Particle() {
const [value, setValue] = useState<string[]>([]);
return (
<CheckboxGroup
allValues={frameworks.map((framework) => framework.id)}
aria-labelledby="frameworks-caption"
onValueChange={setValue}
value={value}
>
<Label id="frameworks-caption">
<Checkbox name="frameworks" parent />
Фреймворки
</Label>
{frameworks.map((framework) => (
<Label className="ms-4" key={framework.id}>
<Checkbox value={framework.id} />
{framework.name}
</Label>
))}
</CheckboxGroup>
);
}
Вложенный родительский чекбокс
"use client";
import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { CheckboxGroup } from "@/components/ui/checkbox-group";
import { Label } from "@/components/ui/label";
const mainPermissions = [
{ id: "view-dashboard", name: "Просмотр панели" },
{ id: "manage-users", name: "Управление пользователями" },
{ id: "access-reports", name: "Доступ к отчётам" },
];
const userManagementPermissions = [
{ id: "create-user", name: "Создание пользователей" },
{ id: "edit-user", name: "Редактирование пользователей" },
{ id: "delete-user", name: "Удаление пользователей" },
{ id: "assign-roles", name: "Назначение ролей" },
];
export default function Particle() {
const [mainValue, setMainValue] = useState<string[]>([]);
const [managementValue, setManagementValue] = useState<string[]>([]);
const managementIsPartial =
managementValue.length > 0 &&
managementValue.length !== userManagementPermissions.length;
return (
<CheckboxGroup
allValues={mainPermissions.map((p) => p.id)}
aria-labelledby="user-permissions-caption"
onValueChange={(value) => {
if (value.includes("manage-users")) {
setManagementValue(userManagementPermissions.map((p) => p.id));
} else if (
managementValue.length === userManagementPermissions.length
) {
setManagementValue([]);
}
setMainValue(value);
}}
value={mainValue}
>
<Label id="user-permissions-caption">
<Checkbox indeterminate={managementIsPartial} parent />
Права пользователя
</Label>
{mainPermissions
.filter((p) => p.id !== "manage-users")
.map((p) => (
<Label className="ms-4" key={p.id}>
<Checkbox value={p.id} />
{p.name}
</Label>
))}
<CheckboxGroup
allValues={userManagementPermissions.map((p) => p.id)}
aria-labelledby="manage-users-caption"
className="ms-4"
onValueChange={(value) => {
if (value.length === userManagementPermissions.length) {
setMainValue((prev) =>
Array.from(new Set([...prev, "manage-users"])),
);
} else {
setMainValue((prev) => prev.filter((v) => v !== "manage-users"));
}
setManagementValue(value);
}}
value={managementValue}
>
<Label id="manage-users-caption">
<Checkbox parent />
Управление пользователями
</Label>
{userManagementPermissions.map((p) => (
<Label className="ms-4" key={p.id}>
<Checkbox value={p.id} />
{p.name}
</Label>
))}
</CheckboxGroup>
</CheckboxGroup>
);
}
Интеграция с формой
"use client";
import type { FormEvent } from "react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { CheckboxGroup } from "@/components/ui/checkbox-group";
import { Field, FieldItem, FieldLabel } from "@/components/ui/field";
import { Fieldset, FieldsetLegend } from "@/components/ui/fieldset";
import { Form } from "@/components/ui/form";
export default function Particle() {
const [loading, setLoading] = useState(false);
const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
setLoading(true);
await new Promise((r) => setTimeout(r, 800));
setLoading(false);
const frameworks = formData.getAll("frameworks") as string[];
alert(`Выбрано: ${frameworks.join(", ") || "ничего"}`);
};
return (
<Form className="flex w-full max-w-40 flex-col gap-4" onSubmit={onSubmit}>
<Field name="frameworks" render={(props) => <Fieldset {...props} />}>
<FieldsetLegend className="font-medium text-sm">
Фреймворки
</FieldsetLegend>
<CheckboxGroup defaultValue={["next"]}>
<FieldItem>
<FieldLabel>
<Checkbox value="next" />
Next.js
</FieldLabel>
</FieldItem>
<FieldItem>
<FieldLabel>
<Checkbox value="vite" />
Vite
</FieldLabel>
</FieldItem>
<FieldItem>
<FieldLabel>
<Checkbox value="astro" />
Astro
</FieldLabel>
</FieldItem>
</CheckboxGroup>
</Field>
<Button loading={loading} type="submit">
Отправить
</Button>
</Form>
);
}