基于Ant Design和jQuery UI的表单设计器

基于Ant DesignjQuery UI 的表单设计器 GitHub 地址 在这里插入图片描述

概念

  • Comonent 组件
  • Layout 布局,一种特殊的Component
  • Component Editor 组件属性编辑器
  • Component Factory 组件工厂,创建Component 和 Component Editor

扩展组件

创建一个组件

组件的定义是通过<b>this.props.definition</b>来传递的。definition的格式如下:

{
  type: 'Checkbox', // 必须存在
  title: '多选框',  // 必须存在
  props:{
    'columnNum':2
  },
  children:[// 只有在Layout 中出现该属性
    componentDefinition,
    ...
  ]
}

在this.props 中还提供了<b>renderCounter</b>属性,每次组件的刷新该属性值会自增。definition的结构可能是深层次的,然而在Ant Design提供的React 组件中的<a href="https://reactjs.org/docs/react-component.html#shouldcomponentupdate" target="_blank" rel="noopener noreferrer">shouldComponentUpdate(nextProps,nextState)</a>方法的实现是对象的浅层比较,在某些情况下Component Editor对组件属性的改变不会导致组件的刷新。可以通过renderCounter来解决组件不刷新的问题。

/**
 * 组件
 */
@ComponentWrapper
class CheckboxComponent extends Component{

  render(){
    const { definition :{props}, renderCounter} = this.props;
    const defaultValue = props.options.filter(item=>{
      return item.checked;
    }).map(item=>{
      return item.value;
    });

    return (
      <CheckboxGroup options={props.options} value={defaultValue} renderCounter={renderCounter} />
    )
  }
}


/**
 * 组件编辑器
 */
@ComponentEditor
class InputComponentEditor extends PureComponent{

   /**
    * 当编辑器改变时,此方法被调用
    */
  onChange(_, allValues){
    if(!isNull(allValues.minLength, allValues.maxLength) && allValues.minLength > allValues.maxLength){
      message.warn(`最小长度${allValues.minLength}应该小于最大长度${allValues.maxLength}`);
      return false;// 阻止组件的刷新
    }

    const { definition:{props}, definition } = this.props;
    definition.name = getErasure(allValues, 'name');
    definition.title = getErasure(allValues, 'title');
    Object.assign(props, allValues);
    props.minLength = guaranteeNumber(props.minLength, 0, Number.MAX_VALUE);
    props.maxLength = guaranteeNumber(props.maxLength, 0, Number.MAX_VALUE);

    return true;
  }

  render(){
   return (
     <PropsEditor {...this.props} lengthLimit placeholder/>
   );
  }
}

/**
 * 组件工厂
 */
@FactoryRegister(InputComponent,InputComponentEditor)
class InputFactory {
  type="Input"      // 必须存在

  title="单行输入框"    // 必须存在

  /**
   * 初始化一个组件定义
   * @returns {{type: string, title: string}}
   */
  createComponentDefinition(){
    return {
      type: this.type,
      title: this.title,
      props:{
        placeholder: '请输入'
      },
    }
  }
}

export default InputFactory;

在compoents/index.js 或 layout/index.js 引入自己组件

转载于:https://my.oschina.net/noCry/blog/3044071

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值