1.0.2 • Published 4 years ago

afms v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Afms

npm version npm downloads license

Ant Design form render solution.

It helps you render a form easily through configuration data instead of using the original antd form api.

Features

  • ✔︎ Out of the box, base on the most popular react ui library Ant Design, easy to get started.
  • ✔︎ Data driven, any action on the form can be done by manipulating the configuration data.
  • ✔︎ High maintainability, maintaining the form requires only maintaining the configuration data.
  • ✔︎ High scalability, can highly customize your form field and assemble the field to meeting individual requirements.
  • ✔︎ Status Switching, has multiple status(such as preview, edit, disabled), which can be switched quickly.
  • ✔︎ Complex layout, flexible layout, no longer afraid of complex forms.

Install

npm install afms --save

Getting Started

import React from 'react';
import { Form, Button } from 'antd';
import { FormRender } from 'afms';
import 'afms/dist/afms.css';

const FormItem = Form.Item;

const formConfig = {
  fields: [
    { field: 'input', id: 'name', config: { placeholder: 'UserName' }, },
    { field: 'input', id: 'password', type: 'password',config: { placeholder: 'Password' }, },
  ],
};

class App extends React.Component {
  handleSubmit = () => {
    const { form } = this.formRef.props;
    console.log(form.getFieldsValue());
  }
  render() {
    return (
      <div>
        <FormRender
          config={formConfig}
          wrappedComponentRef={(ref) => {
            this.formRef = ref;
          }}
        />
        <FormItem>
          <Button type="primary" onClick={this.handleSubmit}>
            Login
          </Button>
        </FormItem>
      </div>
    );
  }
};

ReactDOM.render(<App />, document.getElementById('root'));

Then you can get a form like this:

kk

You can see more examples in this.

API Reference

FormRender

PropertyDescriptionTypeDefault
configThe config of the FormRender, see more about in FormRender.ConfigObject-
dataValues of the fieldsObjectnull
onChangeCalled when all fields value change(field, ..arg) => voidnull
childrenChild elementany-
classNameClassName of FormRenderstring-
styleStyle of FormRenderobject-
needWrapFormRenderCoreSet if need div wrap for FormRenderCorebooleantrue

How to use:

const formConfig = {...};
<FormRender
  className="form-render"
  config={formConfig}
  data={{ name: 'beyondxgb' }}
  onChange={(field, event) => {}}
/>

FormRender.Config

PropertyDescriptionTypeDefault
statusThe status of all fields, can be covered by field config'preview' 'disabled' 'readonly' 'none''edit'
fieldsDefine form fields, it is a field array. Field detail config you can see in FormRender.Config.FieldArray-
layoutDefine form layout'horizontal' 'vertical' 'inline' 'multi-column''horizontal'
columnColumns of multi-clomun layoutnumber1
gutterSpacing between grids, could be a number or a object like { xs: 8, sm: 16, md: 24}Number Object Array0
emptyContentSet the content when field value is null or undefined, only available when then status is preview, can be covered by field config-
....Extend all props of Ant Design Form, such as layout, labelCol, wrapperCol and so on--

How to use:

const formConfig = {
  status: 'edit',
  layout: 'horizontal',
  labelCol: {
    span: 4,
  },
  wrapperCol: {
    span: 10,
  },
  fields: [{ field: 'input', id: 'name' }],
  emptyContent: '-',
};
<FormRender
  config={formConfig}
/>

FormRender.Config.Field

PropertyDescriptionTypeDefault
fieldDefine field type, such as input, select or custom fieldstring-
idThe unique identifier of form fieldstring-
typeThe extend type of the component fieldstring-
valueThe field content valueany-
defaultValueThe initial field content valueany-
statusThe status of the field'preview' 'disabled' 'readonly' 'none''edit'
formItemExtend Ant Design Form.Item configFormItemProps-
decoratorExtend Ant Design getFieldDecorator configGetFieldDecoratorOptions-
configComponent config of the field, extend Ant Design components configObject-
colSpanHow many columns the field should take upNumber1
emptyContentSet the content when field value is null or undefined, only available when then status is previewany-
previewRenderDefine the content when the field status is preview(field: FieldProps) => React.ReactNode-
disabledRenderDefine the content when the field status is disabled(field: FieldProps) => React.ReactNode-
readonlyRenderDefine the content when the field status is readonly(field: FieldProps) => React.ReactNodenull

How to use:

const formConfig = {
  fields: [{
    field: 'input',
    id: 'password',
    type: 'password',
    value: '5201314',
    status: 'edit',
    formItem: {
      label: 'Password',
    },
    decorator: {
      rules: [{
        required: true,
        message: 'Please input your password',
      }],
    },
    config: {
      placeholder: 'password',
    },
    previewRender: field => field.value,
    emptyContent: '-',
  }],
};
<FormRender
  config={formConfig}
/>

FormRenderCore

The props of FormRenderCore is the same as FormRender.

It can use in the scene that there are many form modules:

<FormRender>
  <h3>BaseInfo</h3>
  <FormRenderCore
    config={form1Config}
  />
  <h3>MoreInfo</h3>
  <FormRenderCore
    className="form-panel"
    config={form2Config}
  />
</FormRender>

registerFormField

import { FormRenderCore } from 'afms';

FormRenderCore.registerFormField('price-input', PriceInputField);

registerFormFields

import { FormRenderCore } from 'afms';

FormRenderCore.registerFormFields({
  'price-input': PriceInputField,
  ...
});

FormRender.FormConfig

It helps you manipulate configuration data easily.

How to use:

const formConfig = {...}
const formConfigHelper = new FormRender.FormConfig(formConfig);

<FormRender config={formConfigHelper.getConfig()} />
MethodDescriptionTypeVersion
getConfigGet form render configFunction()
getFieldByIdGet field by idFunction(id: string)
setFieldByIdSet field by idFunction(id: string, callback: (field: FieldProps) => FieldProps)
insertFieldInsert field in target index, default in the lastFunction(field: FieldProps, targetIndex: number)
setFieldOrderAdjust the field positionFunction(currentIndex: number, targetIndex: number)
deleteFieldByIdDelete field by idFunction(id: string)

Using in TypeScript

import React from 'react';
import { Form, Button } from 'antd';
import { FormComponentProps } from 'antd/lib/form';
import { FormRender } from 'afms';
import { FormConfigProps } from 'afms/lib/types';
import 'afms/dist/afms.css';

const FormItem = Form.Item;

const formConfig: FormConfigProps = {
  fields: [
    { field: 'input', id: 'name', config: { placeholder: 'UserName' }, },
    { field: 'input', id: 'password', type: 'password',config: { placeholder: 'Password' }, },
  ],
};

class App extends React.Component {
  private formRef: React.Component<FormComponentProps>
  handleSubmit = () => {
    const { form } = this.formRef.props;
    console.log(form.getFieldsValue());
  }
  render() {
    return (
      <div>
        <FormRender
          config={formConfig}
          wrappedComponentRef={(c: React.Component<FormComponentProps>) => {
            this.formRef = c;
          }}
        />
        <FormItem>
          <Button type="primary" onClick={this.handleSubmit}>
            Login
          </Button>
        </FormItem>
      </div>
    );
  }
};

ReactDOM.render(<App />, document.getElementById('root'));

Import on Demand

There is a small problem that if you use configuration data to render a form, all form fields is imported. Because We don't know which form field will be used.

If your application pursues high performance, you can use the method of assembling form fields, like this example.

We strongly recommend using babel-plugin-import, which can convert the following code to the 'afms/es/xxx' way:

import { InputField } from 'afms';

And this plugin can load styles too. Read usage for more details.

Contribute

LICENSE

MIT