import { Label } from "@cloudflare/kumo";
export function LabelBasicDemo() {
return (
<div className="flex flex-col gap-4">
<Label>Default Label</Label>
<Label showOptional>Optional Label</Label>
<Label tooltip="More information about this field">
Label with Tooltip
</Label>
</div>
);
}安装
桶式导入
import { Label } from "@cloudflare/kumo";子路径导入
import { Label } from "@cloudflare/kumo/components/label";用法
与表单组件搭配使用(推荐)
通过 required 和 labelTooltip 属性,Input、Select、Checkbox、Switch 等表单组件会自动获得 Label 的各项功能。
import { Input } from "@cloudflare/kumo";
export default function Example() {
return (
<>
{/* Optional field with "(optional)" text */}
<Input label="Phone" required={false} placeholder="+1 555-0000" />
{/* With tooltip */}
<Input
label="API Key"
labelTooltip="Find this in your dashboard settings"
/>
</>
);
}独立使用的 Label
对于自定义表单布局,可直接使用 Label 组件。
import { Label } from "@cloudflare/kumo";
export default function Example() {
return <Label tooltip="This field is mandatory">Username</Label>;
}示例
可选字段
当 required={false} 时显示灰色“(可选)”文本。
import { Input } from "@cloudflare/kumo";
export function LabelOptionalFieldDemo() {
return (
<Input label="Phone Number" required={false} placeholder="+1 555-0000" />
);
}带提示
显示一个信息图标,悬停时提供额外的说明信息。
import { Input } from "@cloudflare/kumo";
export function LabelWithTooltipDemo() {
return (
<Input
label="API Key"
labelTooltip="Find this in your dashboard settings under API > Keys"
placeholder="sk_live_..."
/>
);
}ReactNode 标签内容
标签支持 ReactNode 内容,可实现丰富的格式。
import { Checkbox } from "@cloudflare/kumo";
export function LabelReactNodeDemo() {
return (
<Checkbox
label={
<span>
I agree to the <strong>Terms of Service</strong>
</span>
}
/>
);
}混合字段表单
展示必填与可选字段并存的真实示例。
Country
import { Input, Select } from "@cloudflare/kumo";
export function LabelFormMixedDemo() {
return (
<div className="flex max-w-md flex-col gap-4">
<Input label="Full Name" placeholder="John Doe" />
<Input
label="Email"
labelTooltip="We'll send your receipt here"
placeholder="john@example.com"
type="email"
/>
<Input label="Company" required={false} placeholder="Acme Inc." />
<Select label="Country" placeholder="Select a country">
<Select.Option value="us">United States</Select.Option>
<Select.Option value="uk">United Kingdom</Select.Option>
<Select.Option value="ca">Canada</Select.Option>
</Select>
</div>
);
}独立使用的 Label
在自定义布局或非表单场景下直接使用 Label。
import { Label } from "@cloudflare/kumo";
export function LabelStandaloneDemo() {
return (
<div className="flex flex-col gap-3">
<Label>Default</Label>
<Label showOptional>Optional</Label>
<Label tooltip="Important field">With Tooltip</Label>
</div>
);
}API 参考
Label 属性
独立 Label 组件的属性:
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| children | ReactNode | - | 标签内容(必填) |
| showOptional | boolean | false | 显示灰色“(可选)”文本(仅当 required 为 false 时) |
| tooltip | ReactNode | - | 通过信息图标显示的提示内容 |
| className | string | - | 额外 CSS 类 |
表单组件 Label 属性
以下属性适用于 Input、InputArea、Select、Checkbox、Switch、 SensitiveInput 和 Combobox:
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| label | ReactNode | - | 标签内容(启用 Field 包装器) |
| required | boolean | - | 为 false 时:显示“(可选)”文本,同时设置 HTML required 属性。 |
| labelTooltip | ReactNode | - | 通过标签旁的信息图标显示的提示内容 |