React Bootstrap logoReact Bootstrap

Form

基础表单

基于 Bootstrap 5 的表单组合组件,提供 Form 表单容器与 FormGroup 分组,配合 FormLabel、FormControl、FormCheck、FormSelect、FormFeedback、FloatingLabel 等组件快速搭建表单,支持 validated 原生约束校验、controlId 自动关联标签与控件、栅格化水平布局、浮动标签与整体禁用

Component Demo

Basic usage and examples of the Form component

基础用法

我们不会将您的邮箱分享给任何人。

自动关联

点击标签即可聚焦输入框,无需手动设置 id 与 htmlFor。

FormGroup 的 controlId 会通过 Context 自动作为 FormLabel / FormCheckLabel 的 htmlFor 与 FormControl / FormCheckInput 的 id,显式设置的 id / htmlFor 优先

表单校验

请输入有效的邮箱地址。
密码至少需要 8 个字符。
请先勾选同意条款。

validated 属性为表单添加 was-validated 类,提交或输入后按原生约束校验显示有效/无效样式与 FormFeedback 反馈

栅格布局

将 FormGroup 的 as 设为 Row,配合 FormLabel 的 column 属性与 Col 即可构建水平布局表单

浮动标签

整体禁用

使用原生 fieldset 的 disabled 属性可一次性禁用组内所有控件

交互演示

API Documentation

Complete API reference for the Form component

Props

Common Props

属性名类型默认值描述
classNamestring-自定义类名
validatedbooleanfalse是否应用 `was-validated` 类以启用浏览器原生约束校验的视觉反馈,通常配合 `required`、`minLength` 等原生校验属性与 `noValidate` 使用
...restFormHTMLAttributes<HTMLFormElement>-表单元素的所有原生属性(如 `onSubmit`、`action`、`method`、`noValidate` 等)

FormGroup

属性名类型默认值描述
asElementType'div'渲染的根元素类型,可传入 `Row` 等以复用栅格布局
childrenReactNode-分组内的控件与标签内容
classNamestring-自定义类名
controlIdstring-分组内控件的关联 id,通过 Context 自动作为 `FormLabel` / `FormCheckLabel` 的 `htmlFor` 与 `FormControl` / `FormCheckInput` 的 `id`,子控件显式设置的 id / htmlFor 优先
...restHTMLAttributes<HTMLElement>-根元素的所有原生属性(如 `style`、`aria-*` 等)

Type Definitions

FormContextValue

表单分组上下文值类型

export interface FormContextValue {
  controlId?: string;
}

FormGroupProps

表单分组组件属性接口

export interface FormGroupProps extends HTMLAttributes<HTMLElement> {
  as?: ElementType;
  controlId?: string;
}

FormProps

表单组件属性接口

export interface FormProps extends FormHTMLAttributes<HTMLFormElement> {
  validated?: boolean;
}