gcanti / tcomb-form

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

Date pickers for tcomb-form #261

Open gcanti opened 8 years ago

gcanti commented 8 years ago

Hi all, I wrote a simple date picker for tcomb-form https://github.com/gcanti/simple-date-picker if you want to give it a spin

Usage

import SimpleDatePicker from 'simple-date-picker'

function renderDate(locals) {
  return (
    <SimpleDatePicker
      value={locals.value}
      onChange={locals.onChange}
    />
  )
}

const dateTemplate = t.form.Form.templates.date.clone({ renderDate })

class DatePickerFactory extends t.form.Component {

  getTemplate() {
    return dateTemplate
  }

}

t.Date.getTcombFormFactory = () => DatePickerFactory

const Type = t.struct({
  birthDate: t.Date
})
carbureted commented 8 years ago

You may want to mention that it also requires the style react-day-picker styles to be imported as well, but this is great! Huge improvement over the default.

grahamlyus commented 8 years ago

Before looking at tcomb-form, I've been using this one: http://pushtell.github.io/react-bootstrap-date-picker/, which uses a Popover, so works better in a modal.

It works with an ISO String, not a Date, so it needs a transformer. Is there any way to set a default transformer for the template/factory, rather than having to specify it in the Form options?

import DatePicker from 'react-bootstrap-date-picker';

const dateTransformer = {
  format: (value) => t.Date.is(value) ? value.toISOString() : value,
  parse: (str) => str ? new Date(str) : null
};

const renderDate = (locals) => {
  return (
    <DatePicker value={locals.value} onChange={locals.onChange}/>
  );
}

const datePickerTemplate = Form.templates.date.clone({ renderDate });

class DatePickerFactory extends t.form.Component {
  getTemplate() {
    return datePickerTemplate;
  }
}

t.Date.getTcombFormFactory = () => DatePickerFactory;
gcanti commented 8 years ago

Before looking at tcomb-form, I've been using this one: http://pushtell.github.io/react-bootstrap-date-picker/, which uses a Popover, so works better in a modal.

Nice one! Thanks for sharing @grahamlyus

Is there any way to set a default transformer for the template/factory, rather than having to specify it in the Form options?

class DatePickerFactory extends t.form.Component {
  static transformer = dateTransformer // <= yep!
  getTemplate() {
    return datePickerTemplate
  }
}
grahamlyus commented 8 years ago

Great, thanks!