@data-driven-forms/form-builder

中文文档
0.0.13-rc1发布于 2021/11/09MITESM + CJS近 30 天下载 2414

Interactive drag and drop editor for creating data driven forms schema

原始使用文档
文档目录36 节
  1. Requirements
  2. Install with yarn
  3. Start the playground
  4. Lint
  5. Tests
  6. Build
  7. render or children
  8. componentMapper
  9. builderMapper
  10. FieldLayout
  11. PropertiesEditor
  12. FormContainer
  13. BUILDER_FIELD
  14. BuilderColumn
  15. PropertyGroup
  16. DragHandle
  17. EmptyTarget
  18. componentProperties
  19. attributes
  20. disableValidators
  21. disableInitialValue
  22. Example
  23. pickerMapper
  24. propertiesMapper
  25. mode
  26. schema
  27. schemaTemplate
  28. cloneWhileDragging
  29. debug
  30. openEditor
  31. disabledAdd
  32. disableDrag
  33. Material UI
  34. Version 5
  35. PatternFly 4
  36. Example

codecov CircleCI npm version Tweet Twitter Follow

Data Driven Form logo

FormBuilder

THIS PROJECT IS WORK IN PROGRESS. ALL THE API IS CONSIDERED UNSTABLE. EVERYTHING CAN BE CHANGED. NEW FEATURES/BUG FIXES CAN TAKE A LONG TIME TO IMPLEMENT.

This component allows to build Data Driven Forms forms via DnD feature.

Table of contents

Installation

npm install --save @data-driven-forms/form-builder

or

yarn add @data-driven-forms/form-builder

Development

Requirements

  • UNIX like system (Linux, MacOS)
  • Node 12+

Install with yarn

yarn

Start the playground

yarn start

Running on http://localhost:8080/

Lint

yarn lint

For automatic fix: yarn lint --fix

Tests

yarn test

For watching changes: yarn test --watchAll

Build

yarn build

To clean built files: yarn clean-build. This is also done automatically before build.

Usage

Import form builder:

import FormBuilder from '@data-driven-forms/form-builder/form-builder';

and render the component with following props:

Props

render or children

({ ComponentPicker, PropertiesEditor, DropTarget, isValid, getSchema, children }) => React.Element

Use children or a render function to render the whole editor.

componentMapper

object

Data Driven Forms component mapper. See here.

builderMapper

object

A set of components that creates the form builder.

import { builderComponentTypes } from '../src/constants';


const builderMapper = {
    FieldLayout,
    PropertiesEditor,
    FormContainer,
    [builderComponentTypes.BUILDER_FIELD]: builderField,
    BuilderColumn,
    PropertyGroup,
    DragHandle,
    EmptyTarget
}

image

FieldLayout

({ children, disableDrag, dragging, selected }) => React.Element

A wrapper around a single field = BUILDER_FORM + DragHandle.

PropertiesEditor

({ propertiesChildren, validationChildren, addValidator, avaiableValidators, handleClose, handleDelete, hasPropertyError }) => React.Element

An editor.

FormContainer

({ children, isDraggingOver }) => React.Element

A wrapper around the form in the form column.

BUILDER_FIELD

({ innerProps: { hideField, snapshot }, Component, propertyName, fieldId, propertyValidation, hasPropertyError, ...props }) => React.Element

A wrapper around the field.

BuilderColumn

({ children, isDraggingOver }) => React.Element

A column.

PropertyGroup

({ className, children, title, handleDelete }) => React.Element

A wrapper around a single property group in the properties editor > validation.

DragHandle

({ dragHandleProps, hasPropertyError, disableDrag }) => React.Element

A drag handle. Is passed as a child to FieldLayout.

EmptyTarget

() => React.Element

EmptyTarget is shown when there are no fields created.

componentProperties

object

A mapper of editable properties for each component. A property is a object with following attributes:

attributes

Editable attributes of the component.

propertyName

string

Corresponds to an attribute in the schema.

label

string

A label shown for the property in the editor.

component

string

A component corresponding to a key in properties mapper.

disableValidators

Disables validator selection in PropertiesEditor.

Automatically disabled in fields that are not registered in the form state.

disableInitialValue

Disables initial value field in PropertiesEditor.

Automatically disabled in fields that are not registered in the form state.

Example

const LABEL = {
    propertyName: 'label',
    label: 'Label',
    component: 'input'
}

const componentProperties = {
    [componentTypes.TEXT_FIELD]: {
        attributes: [LABEL, IS_REQUIRED]
    },
    [componentTypes.PLAIN_TEXT]: {
        attributes: [LABEL],
        disableValidators: true,
        disableInitialValue: true
    }
}

pickerMapper

object

A mapper of components available in the editor.

const pickerMapper = {
    [componentTypes.TEXT_FIELD]: ({ component }) => <div>{component}</div>
}

propertiesMapper

object

A mapper of components available in component properties.

const propertiesMapper = {
    input: ({ label, value, fieldId, innerProps: { propertyValidation }, ...rest }) => <input {...rest} />
}

mode

one of 'subset' | 'default' optional

If 'subset', options will be only editable to certain degree. See schemaTemplate.

schema

object optional

A Data Driven Forms schema. See here.

schemaTemplate

object optional

An original schema from which a subset is created. If not specified, editable boundaries will be created from the schema.

cloneWhileDragging

boolean optional

Components from the components list are being cloned when dragged.

debug

boolean optional

Turns on debug mode. Will show current field as a JSON object.

openEditor

boolean optional

Opens the first field on mount.

disabledAdd

boolean optional

Disables adding new fields.

disableDrag

boolean optional

Disables dragging.

Mappers

Form builder contains two mappers of components: PatternFly 4 and Material UI versions.

Material UI

import {
  pickerMapper,
  propertiesMapper,
  builderMapper,
  BuilderTemplate,
  fieldProperties
} from '@data-driven-forms/form-builder/mui-builder-mappers';

Version 5

MUI-BUILDER is using MUI of version 5. To use version 4 (@material-ui/core), please use the builder of version 0.0.12-rc1.

PatternFly 4

import {
  pickerMapper,
  propertiesMapper,
  builderMapper,
  BuilderTemplate,
  fieldProperties
} from '@data-driven-forms/form-builder/pf4-builder-mappers';

Example

render={({ isValid, getSchema, ...props }) => (
  <BuilderTemplate {...props} className={classes.builderWrapper}>
    <CodeEditor value={JSON.stringify(getSchema(), null, 2)} />
  </BuilderTemplate>
)}

版本与发布记录

@data-driven-forms/form-builder 共发布 53 个版本,首次发布于 2019/12/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.13-rc1latest2021/11/095 年前
0.0.12-rc12021/09/135 年前
0.0.11-rc42021/05/065 年前
0.0.11-rc32021/04/155 年前
0.0.11-rc22021/04/145 年前
0.0.11-rc12021/04/145 年前

其他发布通道:beta → 0.0.10-rc5。

关于 @data-driven-forms/form-builder 的常见问题

@data-driven-forms/form-builder 是什么?

Interactive drag and drop editor for creating data driven forms schema

如何安装 @data-driven-forms/form-builder?

在项目目录运行 npm install @data-driven-forms/form-builder,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.13.1 || ^17.0.2)、react-dom(^16.13.1 || ^17.0.2)、@data-driven-forms/react-form-renderer(>=3.4.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@data-driven-forms/form-builder 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/data-driven-forms__form-builder 是否存在。

@data-driven-forms/form-builder 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@data-driven-forms/form-builder 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@data-driven-forms/form-builder 可以商用吗?

@data-driven-forms/form-builder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@data-driven-forms/form-builder 的下载量有多少?

根据 npm 官方下载统计,@data-driven-forms/form-builder 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,414 次,日均约 80 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@data-driven-forms/form-builder 还在维护吗?

最新版本 0.0.13-rc1 发布于 2021/11/09(5 年前);至今共发布 53 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/12/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。