gcanti / tcomb-form

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

remove uvdom dependency from core #255

Closed gcanti closed 8 years ago

gcanti commented 8 years ago

remove uvdom dependency from core

This is a follow up of https://github.com/gcanti/tcomb-form/issues/31#issuecomment-157165597

I agree with @Xananax and I'm willing to remove the uvdom dependency from the core. However I'm trying to avoid unnecessary breaking changes so I came up with this proposal.

In order to be consumed by React a template must be a function with the following signature

template(locals) -> ReactElement

Obviously template can be factored out to

template = template' . toReactElement

where

template'(locale) -> MyCustomVDOM

toReactElement(MyCustomVDOM) -> ReactElement // <= template compiler

Making every tcomb-form factory handle an (optional) toReactElement static function attached to the template, I could drop the uvdom dependency from the core of tcomb-form without bracking changes (i.e. people using the uvdom returned by the current bootstrap templates). Also this open up the possibility to use UVDOM systems other than uvdom.

Example

// template textbox.js
import { compile } from 'uvdom/react';

function textbox(locals) {
  // return uvdom
  return {
    tag: 'input'
  };
}

textbox.toReactElement = compile;

// t.form.Component render() method
render() {

  ...

  const template = this.getTemplate();
  const result = template(locals);
  return t.Function.is(template.toReactElement) ? 
    template.toReactElement(result) : 
    result;

}

I'd love to hear your feedback

/cc @minedeljkovic

minedeljkovic commented 8 years ago

To be honest, this dependency was not bothering me a lot, but it is always nice to have dependencies more pluggable (and optional). I think your solution is very good.

Also this open up the possibility to use UVDOM systems other than uvdom.

Until some UVDOM specification gains traction (I wish you luck with uvdom) and community starts to produce good number of quality components for it, I consider React as UVDOM. :) But it is always nice to be prepared for that, as tcomb-form will be.