rekit / antd-form-builder

Dynamic meta driven React forms based on antd.
https://rekit.github.io/antd-form-builder
MIT License
422 stars 88 forks source link

Getting a error when defining FormBuilder.defineWidget in my component when a custom component is being rendered...The error is "Widget is already defined" #77

Open krinashah0493 opened 3 years ago

krinashah0493 commented 3 years ago

Here is a code snippet of what i am trying to implement

import React from 'react'; import "antd/dist/antd.css"; import { Form,Select,Upload, message, Button, Icon, } from 'antd' import FormBuilder from 'antd-form-builder' import MultipleSelect2 from './multipleInput'; // import MultipleSelect from './multipleInput'; const {Option}=Select

function FormBuild(props){ let formRef = React.createRef() const [form] = Form.useForm() const forceUpdate = FormBuilder.useForceUpdate();

const data=props.data
const meta = {
  fields: data
}

const imageProps = {
  name: 'file',
  action: '//jsonplaceholder.typicode.com/posts/',
  headers: {
    authorization: 'authorization-text',
  },
  onChange(info) {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      message.error(`${info.file.name} file upload failed.`);
    }
  },
};

const formStyle={
  marginTop:"20px",
  marginLeft:"-30px"
}

const handleFinish = evt => {
  const resp=formRef.current.getFieldsValue()
  props.onFinish(resp)
  // form.resetFields()
}
FormBuilder.defineWidget("multipleSelect",MultipleSelect2)

return (
  <Form form={form} style={formStyle} onFinish={handleFinish}>
    <FormBuilder meta={meta} form={form} />
    <Form.Item name="header" label="Header">
      {/* <MultipleSelect2 /> */}
    </Form.Item>
    <Form.Item wrapperCol={{ span: 8, offset: 8 }} >
      <Button type="primary" onClick={(e)=>{handleFinish()}} disabled={props.disable?props.disable:false}>Submit</Button>
      <Button type="primary" onClick={(e)=>{handleFinish()}} disabled={props.disable?false:true}>Add More Images/Data</Button>
    </Form.Item>  

  </Form>
)

}

export default FormBuild

mohaphez commented 2 years ago

i have same issue

has any solution ?

berakoc commented 1 year ago

The issue here is sometimes the line of code is run twice. So to solve the problem either convert it to singleton or far better use the widget itself in the meta field instead of defining it.