gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Error - TagsInput & Custom Component [Cannot read property 'transformer' of undefined - components.js:177] #305

Closed rajeshps closed 8 years ago

rajeshps commented 8 years ago

Hi,

At first, have found the tcomb* to be a high quality of library and nicely written and organized too - Thanks!. Am trying to achieve a custom component or factory, simple ones as provided in guide or one of the issues - unable to get it working.

_Error_ is

components.js:177 Uncaught TypeError: Cannot read property 'transformer' of undefined

from chrome debugger this is where it fails and options is undefined

Component.prototype.getTransformer = function getTransformer() {
    return this.props.options.transformer || this.constructor.transformer;
  };

I tried both the examples provided in Guide and one another issue #280 example code in guide is:

import TagsInput from 'react-tagsinput';

class TagsComponent extends t.form.Component { // extend the base class

  getTemplate() {
    return (locals) => {
      return (
        <TagsInput value={locals.value} onChange={locals.onChange} />
      );
    };
  }

}

export default TagsComponent;

Assuming that the transformer was required to be provided within the custom / extended component thus included the same as below from #280 but still error remains same:

const template = t.form.Form.templates.textbox.clone({
  // override just the textbox rendering...
  renderTextbox: (locals) => <TagsInput value={locals.value} onChange={(tags) => locals.onChange(tags)} />
})

class TagsComponent extends t.form.Component {

  static transformer = {
    format: (value) => value || [],
    parse: (value) => value
  };

  getTemplate() {
    return template
  }

}

export default TagsComponent;

And the usage is as below (not sure if it could be different way to use)? - please help/hint on what could be wrong in this scenario - very important to crack a sample custom component / factory to move ahead.

Have also tried to attach a transformer in options which is passed to <Form options={options}/> - still no luck.

TCombSample = React.createClass({

onClick: function () {
    var value = this.refs.form.getValue();
    // getValue returns null if validation failed
    if (value) {
      console.log(value);
    }
  },
render: function() {

    var Person = t.struct({
      name: t.Str,
      surname: t.Str,
      tags: t.list(t.String)
    });

    var options = {
      fields: {
        tags: {
          factory: TagsComponent
        }
      }
    };  

  return (
                <div>
                <Form ref="form" type={Person} options={options} />
                <button onClick={this.onClick}>Click me</button>
                </div>

            )
    }
});

export default TCombSample

Thanks!

rajeshps commented 8 years ago

Please close this issue. It looks to be the order issue while trying things in a compact manner from a single file.

Thanks.