Skip to Content
文档

状态

用于显示带有标题和值的统计信息。

SourceStorybookRecipe
Unique visitors
192.1k

用法

¥Usage

import { Stat } from "@chakra-ui/react"
<Stat.Root>
  <Stat.Label />
  <Stat.ValueText />
  <Stat.HelpText />
  <Stat.UpIndicator />
</Stat.Root>

示例

¥Examples

格式选项

¥Format Options

Stat.ValueText 中使用 FormatNumber 组件格式化值。

¥Use the FormatNumber component within Stat.ValueText to format the value.

Revenue
$935.40

指示器

¥Indicator

以下是如何使用指标显示统计数据的示例。

¥Here's an example of how to display a statistic with an indicator.

Unique visitors
192.1k
1.9%

信息提示

¥Info Tip

组合 InfoTipStat.Label 组件,使其显示信息提示。

¥Compose the InfoTip and Stat.Label components to display an info tip.

Unique
192.1k

值单位

¥Value Unit

以下是如何使用单位显示数值的示例。

¥Here's an example of how to display a value with a unit.

Time to complete
3 hr20 min

进度条

¥Progress Bar

以下示例展示了如何使用进度条显示统计数据。

¥Here's an example of how to display a statistic with a progress bar.

This week
$1,340
+12% from last week

图标

¥Icon

以下示例展示了如何使用图标显示统计数据。

¥Here's an example of how to display a statistic with an icon.

Sales
$4.24k

趋势

¥Trend

以下示例展示了如何使用趋势指示器显示统计数据。

¥Here's an example of how to display a statistic with a trend indicator.

Unique
$8,456.40
12%
since last month

已关闭组件

¥Closed Component

以下是如何设置封闭组件组合的统计数据。

¥Here's how to setup the Stat for a closed component composition.

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>
    )
  },
)

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'md'
'sm' | 'md' | 'lg'

The size of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

Previous

二维码

Next

表格