javascript - Wrapping an ant-design form component -


i've been trying figure out how wrap ant-design form component. have several selects have same options, wanted create selectwrapper (see snippet below). unfortunately, antd's form doesn't seem , error with

createbaseform.js:98 uncaught typeerror: cannot read property 'onchange' of undefined

despite me passing through form props select.

function reusablecountryselect({countries, ...props}) {   return (     <select       {...props}     >       {         countries.map(c => (           <select.option             value={c.id}             key={c.id}           >{c.name}</select.option>         ))       }     </select>   ); } 

full example (requires babel spread)

import react 'react'; import reactdom 'react-dom'; import './index.css'; const mountnode = document.getelementbyid('root');  import {   form, select, button } 'antd'; const formitem = form.item; const option = select.option;  function reusablecountryselect({countries, ...props}) {   return (     <select       {...props}     >       {         countries.map(c => (           <select.option             value={c.id}             key={c.id}           >{c.name}</select.option>         ))       }     </select>   ); }  class demo extends react.component {   handlesubmit = (e) => {     e.preventdefault();     this.props.form.validatefields((err, values) => {       if (!err) {         console.log('received values of form: ', values);       }     });   }    render() {     const { getfielddecorator } = this.props.form;      return (       <form onsubmit={this.handlesubmit}>          <formitem           label="select country"           hasfeedback         >           {getfielddecorator('select', {             rules: [               { required: true, message: 'please select country!' },             ],           })(             <reusablecountryselect               countries={[                 {name: 'china', id: 'china'},                 {name: 'india', id: 'india'},                 {name: 'britain', id: 'britain'}               ]}               placeholder="please select country"             />           )}         </formitem>          <formitem           wrappercol={{ span: 12, offset: 6 }}         >           <button type="primary" htmltype="submit">submit</button>         </formitem>       </form>     );   } }  const wrappeddemo = form.create()(demo);  reactdom.render(<wrappeddemo />, mountnode); 

clone https://github.com/megawac/antd-form-issue , npm start see issue

solved in https://github.com/ant-design/ant-design/issues/5700

form need controls's ref, functional component don't have ref.

the solution use class based wrapper component in place of functional based one.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -