gcanti / tcomb-form

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

Placeholders not passed through to component templates #136

Closed jimmed closed 9 years ago

jimmed commented 9 years ago

I'm still fairly new to the ins and outs of tcomb-form, so please bear with me if I'm being silly.

I am using custom templates to generate a form, which has manually set placeholders on some fields.

const FormModel = t.struct({
  // ...
  bio: t.Str,
  // ...
});

const formOptions = {
  fields: {
    // ...
    bio: {
      placeholder: 'A short description of yourself'
    },
    // ...
  }
};

In my custom form templates, I am logging the locals passed to the template, and it does not contain a placeholder property.

I manually traced through the tcomb-form code, and it seems that the placeholder is never passed down to the template, despite examples to the contrary on the documentation. Looking at Component.prototype.getLocals, it seems that the placeholder never gets passed! By altering the given lines to the following, placeholders begin to appear in my form:

  return {
    path: this.props.ctx.path,
    error: this.getError(),
    hasError: this.hasError(),
    label: this.getLabel(),
    onChange: this.onChange.bind(this),
    config: this.getConfig(),
    value: value,
    disabled: options.disabled,
    help: options.help,
    placeholder: options.placeholder
  };

Have I misunderstood tcomb-form, or is this a genuine bug?

gcanti commented 9 years ago

Hi, I think you are referring to v0.4 docs.

Here the v0.5 docs: https://github.com/gcanti/tcomb-form/blob/master/GUIDE.md#templates.

Try with:

const formOptions = {
  fields: {
    // ...
    bio: {
      attrs: { // <-- this is the new v0.5 API to handle attributes and events
        placeholder: 'A short description of yourself'
      }
    },
    // ...
  }
};
gcanti commented 9 years ago

attrs are provided to input components by a decorator. Since you are tracing the source code, if it can be helpful:

Decorator definition:

https://github.com/gcanti/tcomb-form/blob/master/src/components.js#L80

Decorator usage:

https://github.com/gcanti/tcomb-form/blob/master/src/components.js#L249

Retrieving the attrs:

https://github.com/gcanti/tcomb-form/blob/master/src/components.js#L273

jimmed commented 9 years ago

Thanks for your quick response! I must admit I didn't check the version of the guide with respect to my local version of tcomb.

Not using attrs was indeed my mistake! Consider this issue closed :+1: