# use-dynamic-form-lxh

> use-dynamic-form

Latest version **1.0.5** (published 2022-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-dynamic-form-lxh
pnpm add use-dynamic-form-lxh
yarn add use-dynamic-form-lxh
bun add use-dynamic-form-lxh
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-03-03 |
| First published | 2022-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 49.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | lvxinghai |

## Links

- npm: https://www.npmjs.com/package/use-dynamic-form-lxh
- npm.io page: https://npm.io/package/use-dynamic-form-lxh

## Recent versions

- 1.0.5 (latest) — 2022-03-03
- 1.0.4 — 2022-03-03
- 1.0.3 — 2022-03-03
- 1.0.2 — 2022-03-03
- 1.0.1 — 2022-03-03
- 1.0.0 — 2022-03-03

## README

# use-dynamic-form-lxh

## Install

Using npm:

```bash
$ npm install --save  use-dynamic-form-lxh
```

or using yarn:

```bash
$ yarn add use-dynamic-form-lxh
```

## 使用

useDynamicForm 可以帮助我们管理表单联动。实际上就是在 [SchemaForm](https://procomponents.ant.design/components/schema-form) 表单的 columns 数据上添加了 dynamic 属性，记录动态数据信息，根据信息控制表单元素的显示隐藏。

```js
import React, { useMemo } from 'react'
import type { ProFormColumnsType } from '@ant-design/pro-form'
import { BetaSchemaForm } from '@ant-design/pro-form'
import useDynamicForm, { ProFormColumnsTypeDynamic } from 'use-dynamic-form-lxh'
import { Form } from 'antd'

const labels: ProFormColumnsType = {
  title: '标签',
  dataIndex: 'labels',
  width: 'm',
}
const createName: ProFormColumnsType = {
  title: '创建时间',
  key: 'showTime',
  dataIndex: 'createName',
  valueType: 'date',
}
const formList: ProFormColumnsType = {
  title: '列表',
  valueType: 'formList',
  dataIndex: 'list',
  initialValue: [{ state: 'all', title: '标题' }],
  columns: [
    {
      valueType: 'group',
      columns: [
        {
          title: '状态',
          dataIndex: 'state',
          valueType: 'select',
          width: 'xs',
          valueEnum,
        },
        {
          title: '标题',
          dataIndex: 'title',
          formItemProps: {
            rules: [
              {
                required: true,
                message: '此项为必填项',
              },
            ],
          },
          width: 'm',
        },
      ],
    },
  ],
}

const dynamicColumns: ProFormColumnsTypeDynamic<DataItem>[] = [
  {
    title: '切换表单',
    dataIndex: 'selectType',
    valueType: 'select',
    valueEnum,
    width: 'm',
    dynamic: {
      id: 'selectType',
      addColumns: {
        labels: [labels],
        createName: [createName],
        formList: [formList],
      },
    },
  },
]

export default () => {
  ...
  const [columns, onValuesChange] = useDynamicForm(dynamicColumns, form, initialValues);
  return (
    <BetaSchemaForm<DataItem>
      columns={columns}
      onValuesChange={onValuesChange}
      initialValues={initialValues}
      form={form}
    />
  );
};
```
## API

| 字段名称 | 类型 | 说明 |
| --- | --- | --- |
| `dynamicColumns` | `ProFormColumnsTypeDynamic<T, ValueType>[]` | 参数，带有 dynamic 属性的 columns 数组【需要使用 useMemo 包裹】 |
| `form` | `(FormInstance<T> & FormInstance<any>)` | 参数，表单的 form 实例 |
| `initialValues` | `Store` | 参数，初始值 |
| `columns` | `ProFormColumnsType<T, ValueType>[]` \| `ProFormColumnsType<T, ValueType>[][]` | 返回值，SchemaForm 表单的 columns |
| `onValuesChange` | `(changedValues: any, values: Values) => void` | 返回值，SchemaForm 表单的 onValuesChange |

### dynamic

Dynamic {id, addColumns, delColumns?, children?, index?}， 存放动态数据的对象

| 字段名称 | 类型 | 说明 |
| --- | --- | --- |
| `id` | `string` | 建议与 dataIndex 保持一致 |
| `addColumns` | `Record<string, ProFormColumnsTypeDynamic<T, ValueType>[]>` | 指定要动态添加的表单元素集合，格式：{ key: columns }，比如{ ActiveMQ: [host, port] }，表示 select 值等于 ActiveMQ 时展示 host 和 port |
| `delColumns` | `Record<string, ProFormColumnsTypeDynamic<T, ValueType>[]>` | 格式同 addColumns，只是作用相反，记录的是选中时需要特别删掉的表单元素集合 |
| `children` | `string[]` | 所有动态的后代元素的 id 集合（包括子级、孙子级...所有动态的后代元素），切换的时候需要去清除子级的数据 |
| `index` | `number` | 指定动态元素插入的位置，可以为正整数或者负整数，正整数往后插入，负整数往前插入 |

---
_Source: https://npm.io/package/use-dynamic-form-lxh · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
