产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm">
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
用法
import { Table } from "@chakra-ui/react"
<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader />
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell />
</Table.Row>
</Table.Body>
<Table.Footer>
<Table.Row>
<Table.Cell />
</Table.Row>
</Table.Footer>
</Table.Root>
示例
尺寸
使用 size
属性来更改表格的尺寸。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { For, Stack, Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="10">
<For each={["sm", "md", "lg"]}>
{(size) => (
<Table.Root key={size} size={size}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)}
</For>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
变体
使用 variant
属性来更改表格的外观。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { For, Stack, Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="10">
<For each={["line", "outline"]}>
{(variant) => (
<Table.Root key={variant} size="sm" variant={variant}>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)}
</For>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
斑马纹
使用 striped
属性为表格添加斑马纹。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" striped>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
列边框
使用 showColumnBorder
属性在列之间添加边框。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" showColumnBorder>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
溢出
渲染 Table.ScrollArea
组件以启用水平滚动。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.ScrollArea borderWidth="1px" maxW="xl">
<Table.Root size="sm" variant="outline">
<Table.Header>
<Table.Row>
<Table.ColumnHeader minW="400px">Product</Table.ColumnHeader>
<Table.ColumnHeader minW="400px">Category</Table.ColumnHeader>
<Table.ColumnHeader minW="200px" textAlign="end">
Price
</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
</Table.ScrollArea>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
固定表头
使用 stickyHeader
属性使表格表头固定。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.ScrollArea borderWidth="1px" rounded="md" height="160px">
<Table.Root size="sm" stickyHeader>
<Table.Header>
<Table.Row bg="bg.subtle">
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
</Table.ScrollArea>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
悬停高亮
使用 interactive
属性在悬停时高亮行。
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" interactive>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
分页
这是一个表格与分页组合的示例。
产品
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
"use client"
import {
ButtonGroup,
Heading,
IconButton,
Pagination,
Stack,
Table,
} from "@chakra-ui/react"
import { LuChevronLeft, LuChevronRight } from "react-icons/lu"
const Demo = () => {
return (
<Stack width="full" gap="5">
<Heading size="xl">Products</Heading>
<Table.Root size="sm" variant="outline" striped>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
<Pagination.Root count={items.length * 5} pageSize={5} page={1}>
<ButtonGroup variant="ghost" size="sm" wrap="wrap">
<Pagination.PrevTrigger asChild>
<IconButton>
<LuChevronLeft />
</IconButton>
</Pagination.PrevTrigger>
<Pagination.Items
render={(page) => (
<IconButton variant={{ base: "ghost", _selected: "outline" }}>
{page.value}
</IconButton>
)}
/>
<Pagination.NextTrigger asChild>
<IconButton>
<LuChevronRight />
</IconButton>
</Pagination.NextTrigger>
</ButtonGroup>
</Pagination.Root>
</Stack>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
操作栏
这是一个表格与操作栏和复选框组合的示例。这对于显示选定表格行的操作非常有用。
产品 | 类别 | 价格 | |
---|---|---|---|
笔记本电脑 | 电子产品 | $999.99 | |
咖啡机 | 家用电器 | $49.99 | |
办公椅 | 家具 | $150 | |
智能手机 | 电子产品 | $799.99 | |
耳机 | 配件 | $199.99 |
"use client"
import {
ActionBar,
Button,
Checkbox,
Kbd,
Portal,
Table,
} from "@chakra-ui/react"
import { useState } from "react"
const Demo = () => {
const [selection, setSelection] = useState<string[]>([])
const hasSelection = selection.length > 0
const indeterminate = hasSelection && selection.length < items.length
const rows = items.map((item) => (
<Table.Row
key={item.name}
data-selected={selection.includes(item.name) ? "" : undefined}
>
<Table.Cell>
<Checkbox.Root
size="sm"
top="0.5"
aria-label="Select row"
checked={selection.includes(item.name)}
onCheckedChange={(changes) => {
setSelection((prev) =>
changes.checked
? [...prev, item.name]
: selection.filter((name) => name !== item.name),
)
}}
>
<Checkbox.HiddenInput />
<Checkbox.Control />
</Checkbox.Root>
</Table.Cell>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell>${item.price}</Table.Cell>
</Table.Row>
))
return (
<>
<Table.Root>
<Table.Header>
<Table.Row>
<Table.ColumnHeader w="6">
<Checkbox.Root
size="sm"
top="0.5"
aria-label="Select all rows"
checked={indeterminate ? "indeterminate" : selection.length > 0}
onCheckedChange={(changes) => {
setSelection(
changes.checked ? items.map((item) => item.name) : [],
)
}}
>
<Checkbox.HiddenInput />
<Checkbox.Control />
</Checkbox.Root>
</Table.ColumnHeader>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader>Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>{rows}</Table.Body>
</Table.Root>
<ActionBar.Root open={hasSelection}>
<Portal>
<ActionBar.Positioner>
<ActionBar.Content>
<ActionBar.SelectionTrigger>
{selection.length} selected
</ActionBar.SelectionTrigger>
<ActionBar.Separator />
<Button variant="outline" size="sm">
Delete <Kbd>⌫</Kbd>
</Button>
<Button variant="outline" size="sm">
Share <Kbd>T</Kbd>
</Button>
</ActionBar.Content>
</ActionBar.Positioner>
</Portal>
</ActionBar.Root>
</>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
列组
使用 Table.ColumnGroup
组件通过 HTML colgroup
元素分配列宽。
警告
此组件唯一可用的属性是 htmlWidth
产品 | 类别 | 价格 |
---|---|---|
笔记本电脑 | 电子产品 | 999.99 |
咖啡机 | 家用电器 | 49.99 |
办公椅 | 家具 | 150 |
智能手机 | 电子产品 | 799.99 |
耳机 | 配件 | 199.99 |
import { Table } from "@chakra-ui/react"
const Demo = () => {
return (
<Table.Root size="sm" variant="outline">
<Table.ColumnGroup>
<Table.Column htmlWidth="50%" />
<Table.Column htmlWidth="40%" />
<Table.Column />
</Table.ColumnGroup>
<Table.Header>
<Table.Row>
<Table.ColumnHeader>Product</Table.ColumnHeader>
<Table.ColumnHeader>Category</Table.ColumnHeader>
<Table.ColumnHeader textAlign="end">Price</Table.ColumnHeader>
</Table.Row>
</Table.Header>
<Table.Body>
{items.map((item) => (
<Table.Row key={item.id}>
<Table.Cell>{item.name}</Table.Cell>
<Table.Cell>{item.category}</Table.Cell>
<Table.Cell textAlign="end">{item.price}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table.Root>
)
}
const items = [
{ id: 1, name: "Laptop", category: "Electronics", price: 999.99 },
{ id: 2, name: "Coffee Maker", category: "Home Appliances", price: 49.99 },
{ id: 3, name: "Desk Chair", category: "Furniture", price: 150.0 },
{ id: 4, name: "Smartphone", category: "Electronics", price: 799.99 },
{ id: 5, name: "Headphones", category: "Accessories", price: 199.99 },
]
属性
根元素
属性 | 默认值 | 类型 |
---|---|---|
颜色调色板 | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' 组件的颜色调色板 |
变体 | 'line' | 'line' | 'outline' 组件的变体 |
尺寸 | 'md' | 'sm' | 'md' | 'lg' 组件的尺寸 |
交互性 | 'true' | 'false' 组件的交互性 | |
固定表头 | 'true' | 'false' 组件的固定表头 | |
斑马纹 | 'true' | 'false' 组件的斑马纹 | |
显示列边框 | 'true' | 'false' 组件的显示列边框 | |
原生 | 布尔值 如果为 `true`,表格将使用嵌套选择器为其子元素设置样式 | |
作为 | React.ElementType 要渲染的底层元素。 | |
作为子元素 | ||
无样式 | 布尔值 是否移除组件的样式。 |