---
url: /reference/interface/ui-components/form.md
---
# Form

通过字段配置渲染表单，收集并校验用户输入。

## 使用

::: code-group

```ts [Angular]
import { NxForm, NxFormFooter } from "@pc-nexus/angular";
```

```ts [React]
import { NxForm, NxFormFooter } from "@pc-nexus/react";
```

:::

## 属性

### Form

:::comp-props

| 名称               | 类型                                      | 必填 | 描述                                                                                       |
| ------------------ | ----------------------------------------- | ---- | ------------------------------------------------------------------------------------------ |
| `fields`           | `NxFormFieldSchema[]`                     |      | 字段配置列表，默认 `[]`                                                                    |
| `value`            | `NxFormValue`                             |      | 当前表单值（表单协议）                                                                     |
| `size`             | `'small' \| 'medium' \| 'large'`          |      | 表单内控件尺寸，默认 `medium`                                                              |
| `layout`           | `'horizontal' \| 'vertical' \| 'inline'`  |      | 表单布局，默认 `vertical`                                                                  |
| `labelWidth`       | `string`                                  |      | Horizontal 下 label 固定宽度，默认 `120px`；`vertical` / `inline` 不生效                   |
| `valueChange`      | `(value: NxFormValue) => void`            |      | 表单值变化时触发；React 对应 `onValueChange`                                               |
| `fieldValueChange` | `(event: NxFormFieldValueChange) => void` |      | 单个字段值变化时触发；React 对应 `onFieldValueChange`                                      |
| `statusChange`     | `(status: NxFormStatus) => void`          |      | 校验/提交状态变化时触发（`valid` / `invalid` / `submitting`）；React 对应 `onStatusChange` |
| `submit`           | `(value: NxFormValue) => void`            |      | 校验通过并提交时触发；React 对应 `onSubmit`                                                |

:::

### FormFooter

:::comp-props

| 名称    | 类型                            | 必填 | 描述                          |
| ------- | ------------------------------- | ---- | ----------------------------- |
| `align` | `'left' \| 'right' \| 'center'` |      | 页脚内容对齐方式，默认 `left` |

:::

## 类型

### NxFormFieldSchema

:::comp-props

| 名称              | 类型                              | 必填 | 描述                                                            |
| ----------------- | --------------------------------- | ---- | --------------------------------------------------------------- |
| `key`             | `string`                          | Y    | 字段唯一标识，对应表单值中的键                                  |
| `kind`            | `string`（内置见下方）            | Y    | 字段类型                                                        |
| `col`             | `number`                          |      | 栅格占位（1–12），默认 12；`inline` 布局下忽略                  |
| `defaultValue`    | `unknown`                         |      | 字段默认值                                                      |
| `errorMessages`   | `Partial<Record<string, string>>` |      | 错误文案，key 为错误 `kind`，支持 `{minlength}` 等占位          |
| `debounce`        | `number \| 'blur'`                |      | 控件值写入表单模型前的延迟；数字为毫秒，`'blur'` 表示失焦后更新 |
| `validators`      | `NxFormFieldValidator[]`          |      | 同步自定义校验                                                  |
| `asyncValidators` | `NxFormFieldAsyncValidator[]`     |      | 异步自定义校验                                                  |
| `props`           | 见下方各 props 类型               |      | 控件展示与内置校验相关配置                                      |

:::

### NxFormFieldBaseProps

各 `kind` 的 `props` 公共字段如下；下方按 `kind` 列出各自的 props 类型。

:::comp-props

| 名称          | 类型      | 必填 | 描述     |
| ------------- | --------- | ---- | -------- |
| `label`       | `string`  |      | 字段标签 |
| `placeholder` | `string`  |      | 占位文案 |
| `required`    | `boolean` |      | 是否必填 |
| `readonly`    | `boolean` |      | 是否只读 |
| `disabled`    | `boolean` |      | 是否禁用 |

:::

### NxFormInputFieldProps

:::comp-props

| 名称        | 类型     | 必填 | 描述                                             |
| ----------- | -------- | ---- | ------------------------------------------------ |
| `type`      | `string` |      | 原生 input type，如 `text`、`number`、`password` |
| `minlength` | `number` |      | 最小输入长度                                     |
| `maxlength` | `number` |      | 最大输入长度                                     |
| `pattern`   | `string` |      | 正则校验模式                                     |

:::

### NxFormTextareaFieldProps

:::comp-props

| 名称        | 类型                                             | 必填 | 描述                 |
| ----------- | ------------------------------------------------ | ---- | -------------------- |
| `minlength` | `number`                                         |      | 最小输入长度         |
| `maxlength` | `number`                                         |      | 最大输入长度         |
| `rows`      | `number`                                         |      | 可见行数             |
| `resize`    | `'none' \| 'vertical' \| 'horizontal' \| 'both'` |      | 是否允许拖拽调整尺寸 |
| `pattern`   | `string`                                         |      | 正则校验模式         |

:::

### NxFormNumberFieldProps

:::comp-props

| 名称     | 类型               | 必填 | 描述   |
| -------- | ------------------ | ---- | ------ |
| `min`    | `number \| string` |      | 最小值 |
| `max`    | `number \| string` |      | 最大值 |
| `step`   | `number \| string` |      | 步进值 |
| `suffix` | `string`           |      | 后缀   |

:::

### NxFormSelectFieldProps

:::comp-props

| 名称              | 类型               | 必填 | 描述                   |
| ----------------- | ------------------ | ---- | ---------------------- |
| `options`         | `NxSelectOption[]` |      | 选项列表               |
| `multiple`        | `boolean`          |      | 是否多选               |
| `searchable`      | `boolean`          |      | 是否可搜索             |
| `clearable`       | `boolean`          |      | 是否可清空             |
| `emptyText`       | `string`           |      | 选项为空时的提示文案   |
| `searchEmptyText` | `string`           |      | 搜索无结果时的提示文案 |

:::

### NxFormCheckboxFieldProps

:::comp-props

| 名称          | 类型      | 必填 | 描述     |
| ------------- | --------- | ---- | -------- |
| `label`       | `string`  |      | 字段标签 |
| `placeholder` | `string`  |      | 占位文案 |
| `required`    | `boolean` |      | 是否必填 |
| `readonly`    | `boolean` |      | 是否只读 |
| `disabled`    | `boolean` |      | 是否禁用 |

:::

### NxFormRadioFieldProps

:::comp-props

| 名称      | 类型              | 必填 | 描述                         |
| --------- | ----------------- | ---- | ---------------------------- |
| `options` | `NxRadioOption[]` |      | 选项列表                     |
| `inline`  | `boolean`         |      | 是否同一行展示，默认 `false` |

:::

### NxFormSwitchFieldProps

:::comp-props

| 名称    | 类型                                           | 必填 | 描述     |
| ------- | ---------------------------------------------- | ---- | -------- |
| `color` | `'primary' \| 'info' \| 'warning' \| 'danger'` |      | 开关颜色 |

:::

### NxFormTimeFieldProps

:::comp-props

| 名称            | 类型      | 必填 | 描述         |
| --------------- | --------- | ---- | ------------ |
| `format`        | `string`  |      | 时间格式     |
| `allowClear`    | `boolean` |      | 是否允许清空 |
| `showSelectNow` | `boolean` |      | 是否显示此刻 |

:::

### NxFormDateFieldProps

:::comp-props

| 名称         | 类型      | 必填 | 描述                         |
| ------------ | --------- | ---- | ---------------------------- |
| `format`     | `string`  |      | 日期格式                     |
| `min`        | `number`  |      | 最小日期（毫秒或秒级时间戳） |
| `max`        | `number`  |      | 最大日期（毫秒或秒级时间戳） |
| `allowClear` | `boolean` |      | 是否允许清空                 |
| `showTime`   | `boolean` |      | 是否显示时间选择             |

:::

### NxFormDateRangeFieldProps

:::comp-props

| 名称          | 类型                 | 必填 | 描述                         |
| ------------- | -------------------- | ---- | ---------------------------- |
| `placeholder` | `string \| string[]` |      | 占位文案，可传起止两项       |
| `format`      | `string`             |      | 日期格式                     |
| `min`         | `number`             |      | 最小日期（毫秒或秒级时间戳） |
| `max`         | `number`             |      | 最大日期（毫秒或秒级时间戳） |
| `allowClear`  | `boolean`            |      | 是否允许清空                 |
| `showTime`    | `boolean`            |      | 是否显示时间选择             |

:::

## 示例

### Basic

基础登录表单，展示字段配置与页脚提交。

:::demo src=form-basic
:::

### Validation

字段校验示例，覆盖常用校验能力与全部字段类型。

:::demo src=form-validation
:::

### Linkage

字段联动示例。通过 `fieldValueChange` 更新关联字段选项，通过 `statusChange` 监听 `valid` / `invalid` / `submitting`。

:::demo src=form-linkage
:::

### Layout

通过 `layout` 设置布局，取值可以为 `'horizontal' | 'vertical' | 'inline'`；可用 `labelWidth` 调整 horizontal 下 label 宽度。

:::demo src=form-layout
:::

### Size

通过 `size` 设置尺寸，取值可以为 `'small' | 'medium' | 'large'`。

:::demo src=form-size
:::

### Disabled

通过字段 `props.disabled` 禁用控件；也可在选项上设置 `disabled`。

:::demo src=form-disabled
:::
