Skip to Content
文档

二维码

根据提供的数据生成二维码的组件。

SourceStorybookRecipeArk

用法

¥Usage

import { QrCode } from "@chakra-ui/react"
<QrCode.Root value="...">
  <QrCode.Frame>
    <QrCode.Pattern />
  </QrCode.Frame>
</QrCode.Root>
info

如果你更喜欢封闭的组件组合,请查看 以下代码片段

¥If you prefer a closed component composition, check out the snippet below.

示例

¥Examples

尺寸

¥Sizes

使用 size 属性设置二维码的大小。

¥Use the size prop to set the size of the QR code.

徽标叠加

¥Logo Overlay

将 children 属性传递给 QrCode.Overlay 组件以在二维码上添加徽标或叠加层。

¥Pass the children prop to the QrCode.Overlay component to add a logo or overlay to the QR code.

填充

¥Fill

使用 fill 属性设置二维码的填充颜色。

¥Use the fill prop to set the fill color of the QR code.

下载

¥Download

使用 QrCode.DownloadTrigger 下载二维码。

¥Use the QrCode.DownloadTrigger to download the QR code.

fileNamemimeType 属性是必需的。

错误更正

¥Error Correction

如果链接过长或徽标覆盖范围过大,可以提高错误纠正级别。

¥In cases where the link is too long or the logo overlay covers a significant area, the error correction level can be increased.

使用 encoding.eccencoding.boostEcc 属性设置错误校正级别:

¥Use the encoding.ecc or encoding.boostEcc property to set the error correction level:

  • L:允许恢复高达 7% 的数据丢失(默认)

  • M:允许恢复高达 15% 的数据丢失

  • Q:允许恢复高达 25% 的数据丢失

  • H:允许恢复高达 30% 的数据丢失

商店

¥Store

RootProvider 组件为二维码提供上下文。

¥The RootProvider component provides a context for the QR code.

它接受 useQrCode 钩子的值。你可以利用它从二维码外部访问组件状态和方法。

¥It accepts the value of the useQrCode hook. You can leverage it to access the component state and methods from outside the QR code.

输入框

¥Input

以下是如何将 QrCode 组件与 Input 组件结合使用的示例。

¥Here's an example of how to use the QrCode component with an Input component.

旋转器

¥Spinner

以下是如何将 QrCode 组件与 Spinner 组件结合使用的示例。

¥Here's an example of how to use the QrCode component with a Spinner component.

已关闭组件

¥Closed Component

以下是如何设置封闭组件组合的二维码。

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

import { QrCode as ChakraQrCode } from "@chakra-ui/react"
import * as React from "react"

export interface QrCodeProps
  extends Omit<ChakraQrCode.RootProps, "fill" | "overlay"> {
  fill?: string
  overlay?: React.ReactNode
}

export const QrCode = React.forwardRef<HTMLDivElement, QrCodeProps>(
  function QrCode(props, ref) {
    const { children, fill, overlay, ...rest } = props
    return (
      <ChakraQrCode.Root ref={ref} {...rest}>
        <ChakraQrCode.Frame style={{ fill }}>
          <ChakraQrCode.Pattern />
        </ChakraQrCode.Frame>
        {overlay}
        {children && <ChakraQrCode.Overlay>{children}</ChakraQrCode.Overlay>}
      </ChakraQrCode.Root>
    )
  },
)

如果你想自动将封闭式组件添加到项目中,请运行以下命令:

¥If you want to automatically add the closed component to your project, run the command:

npx @chakra-ui/cli snippet add qr-code

属性

¥Props

根元素

¥Root

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

The color palette of the component

size 'md'
'2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full'

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.

defaultValue
string

The initial value to encode when rendered. Use when you don't need to control the value of the qr code.

encoding
QrCodeGenerateOptions

The qr code encoding options.

id
string

The unique identifier of the machine.

ids
Partial<{ root: string; frame: string }>

The element ids.

onValueChange
(details: ValueChangeDetails) => void

Callback fired when the value changes.

pixelSize
number

The pixel size of the qr code.

value
string

The controlled value to encode.

DownloadTrigger

PropDefaultType
fileName *
string

The name of the file.

mimeType *
DataUrlType

The mime type of the image.

asChild
boolean

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

For more details, read our Composition guide.
quality
number

The quality of the image.

Previous

图标

Next

状态