gcanti / tcomb-form

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

Factories inside custom templates? #186

Closed heyheyjp closed 9 years ago

heyheyjp commented 9 years ago

Hey there. Fascinated with the tcomb* libs. Really appreciate the work you're doing. :smiley: :+1:

Our Situation

Let's say our data model looks something like this...

const Parent = t.struct({
  id: t.Num,
  child: T.Str
});

...and we want to render the form with a custom template which might look like this...

let parentTemplate = function(locals) {
  return (
    <div>
      <p>Mah Form</p>
      <div>{locals.inputs.child}</div>
    </div>
  );
};

...so far so good. Now, let's say we want to render our child input using not a custom template, but a custom factory:

class Child extends React.Component {
  onChange() {
    this.props.onChange(...);
  }

  validate() {
    return t.validate(...)
  }

  render() {
    return (...);
  }
}

So our form options might then look something like this...

let options = {
  template: parentTemplate,
  fields: {
    child: {
      factory: Child
    }
  }
};
The Problem

It doesn't seem that we're able to get our custom factory to be used to render the input for the child. Nested templates work well of course, but when inside a custom template and rendering a child input for which you want to use a factory instead, locals.inputs[blah] doesn't seem to connect us to our specified custom factory for that input.

So far, we see the only viable options to make this work as being:

let parentTemplate = function(locals) {
  return (
    <div>
      <p>Mah Form</p>
      <div><Child/></div>
    </div>
  );
};

Are we missing something? Is our use case supported or is this not currently possible? If not, is this by design and is there any other workaround you would suggest?

Thank you!

heyheyjp commented 9 years ago

In the end, this turned out to actually be due to a simple (silly) user error. Things do in fact work the way we'd originally hoped. Closing this puppy. Thanks! :)