- 独立访客
- 192.1k
import { Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>Unique visitors</Stat.Label>
<Stat.ValueText>192.1k</Stat.ValueText>
</Stat.Root>
)
}
用法
import { Stat } from "@chakra-ui/react"
<Stat.Root>
<Stat.Label />
<Stat.ValueText />
<Stat.HelpText />
<Stat.UpIndicator />
</Stat.Root>
示例
格式化选项
在 Stat.ValueText
中使用 FormatNumber
组件来格式化值。
- 营收
- $935.40
import { FormatNumber, Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>Revenue</Stat.Label>
<Stat.ValueText>
<FormatNumber value={935.4} style="currency" currency="USD" />
</Stat.ValueText>
</Stat.Root>
)
}
指示器
这是一个显示带有指示器的统计数据的示例。
- 独立访客
- 192.1k 1.9%
import { Badge, Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>Unique visitors</Stat.Label>
<Stat.ValueText>192.1k</Stat.ValueText>
<Badge colorPalette="red" variant="plain" px="0">
<Stat.DownIndicator />
1.9%
</Badge>
</Stat.Root>
)
}
信息提示
组合 InfoTip
和 Stat.Label
组件以显示信息提示。
- 唯一一些信息
- 192.1k
import { Stat } from "@chakra-ui/react"
import { InfoTip } from "@/components/ui/toggle-tip"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>
Unique
<InfoTip>Some info</InfoTip>
</Stat.Label>
<Stat.ValueText>192.1k</Stat.ValueText>
</Stat.Root>
)
}
值单位
这是一个显示带有单位的值的示例。
- 完成时间
- 3 小时20 分钟
import { Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>Time to complete</Stat.Label>
<Stat.ValueText alignItems="baseline">
3 <Stat.ValueUnit>hr</Stat.ValueUnit>
20 <Stat.ValueUnit>min</Stat.ValueUnit>
</Stat.ValueText>
</Stat.Root>
)
}
进度条
这是一个显示带有进度条的统计数据的示例。
- 本周
- $1,340 比上周增加12%
import { FormatNumber, Progress, Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root maxW="240px">
<Stat.Label>This week</Stat.Label>
<Stat.ValueText>
<FormatNumber
value={1340}
style="currency"
currency="USD"
maximumFractionDigits={0}
/>
</Stat.ValueText>
<Stat.HelpText mb="2">+12% from last week</Stat.HelpText>
<Progress.Root>
<Progress.Track>
<Progress.Range />
</Progress.Track>
</Progress.Root>
</Stat.Root>
)
}
图标
这是一个显示带有图标的统计数据的示例。
- 销售额
- $4.24k
import { HStack, Icon, Stat } from "@chakra-ui/react"
import { LuDollarSign } from "react-icons/lu"
const Demo = () => {
return (
<Stat.Root maxW="240px" borderWidth="1px" p="4" rounded="md">
<HStack justify="space-between">
<Stat.Label>Sales</Stat.Label>
<Icon color="fg.muted">
<LuDollarSign />
</Icon>
</HStack>
<Stat.ValueText>$4.24k</Stat.ValueText>
</Stat.Root>
)
}
趋势
这是一个显示带有趋势指示器的统计数据的示例。
- 唯一
- $8,456.40 12%
import { Badge, FormatNumber, HStack, Stat } from "@chakra-ui/react"
const Demo = () => {
return (
<Stat.Root>
<Stat.Label>Unique </Stat.Label>
<HStack>
<Stat.ValueText>
<FormatNumber value={8456.4} style="currency" currency="USD" />
</Stat.ValueText>
<Badge colorPalette="green" gap="0">
<Stat.UpIndicator />
12%
</Badge>
</HStack>
<Stat.HelpText>since last month</Stat.HelpText>
</Stat.Root>
)
}
封闭组件
这是一个如何为封闭组件组合设置 Stat 的示例。
import { Badge, Stat as ChakraStat, FormatNumber, Show } from "@chakra-ui/react"
import { InfoTip } from "@/components/ui/toggle-tip"
import * as React from "react"
interface StatProps extends ChakraStat.RootProps {
label?: React.ReactNode
value?: number
info?: React.ReactNode
valueText?: React.ReactNode
formatOptions?: Intl.NumberFormatOptions
change?: number
}
export const Stat = React.forwardRef<HTMLDivElement, StatProps>(
function Stat(props, ref) {
const { label, value, valueText, change, info, formatOptions, ...rest } =
props
return (
<ChakraStat.Root {...rest}>
{label && (
<ChakraStat.Label>
{label}
{info && <InfoTip>{info}</InfoTip>}
</ChakraStat.Label>
)}
<ChakraStat.ValueText {...rest} ref={ref}>
{valueText ||
(value != null && formatOptions && (
<FormatNumber value={value} {...formatOptions} />
))}
</ChakraStat.ValueText>
{change != null && (
<Badge colorPalette={change > 0 ? "green" : "red"} gap="0">
<Show when={change > 0} fallback={<ChakraStat.DownIndicator />}>
<ChakraStat.UpIndicator />
</Show>
<FormatNumber
value={Math.abs(change)}
style="percent"
maximumFractionDigits={2}
/>
</Badge>
)}
</ChakraStat.Root>
)
},
)
属性
根
属性 | 默认值 | 类型 |
---|---|---|
colorPalette | '灰色' | '灰色' | '红色' | '橙色' | '黄色' | '绿色' | '蓝绿色' | '蓝色' | '青色' | '紫色' | '粉色' 组件的颜色调色板 |
size | 'md' | 'sm' | 'md' | 'lg' 组件的尺寸 |
as | React.ElementType 要渲染的底层元素。 | |
asChild | ||
unstyled | 布尔值 是否移除组件的样式。 |