Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

How to build a autosize textarea custom factory #274

Open
gcanti opened this issue Dec 17, 2015 · 0 comments
Open

How to build a autosize textarea custom factory #274

gcanti opened this issue Dec 17, 2015 · 0 comments

Comments

@gcanti
Copy link
Owner

gcanti commented Dec 17, 2015

Another example of a custom factory: an autosize textarea using @buildo's react-autosize-textarea

import classnames from 'classnames'
import ReactAutosizeTextarea from 'react-autosize-textarea'

// override just the renderTextarea partial of the default template
function renderTextarea(locals) {
  const attrs = {
    ...locals.attrs,
    className: classnames(locals.attrs.className)
  }
  const onChange = (evt) => locals.onChange(evt.target.value)
  return <ReactAutosizeTextarea rows="3" {...attrs} value={locals.value} onChange={onChange} />
}

const textboxTemplate = t.form.Form.templates.textbox.clone({ renderTextarea })

// here we are: the factory
class AutosizeTextareaFactory extends t.form.Textbox {

  getTemplate() {
    return textboxTemplate
  }

}

// example
const Type = t.struct({
  text: t.String
})

const options = {
  fields: {
    text: {
      type: 'textarea',
      factory: AutosizeTextareaFactory
    }
  }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant