0.2.0 • Published 7 years ago

snabbdom-form-helpers v0.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

Snabbdom form helpers

CircleCI

Demo

npm install snabbdom-form-helpers

Provides helpers for the following form elements:

  • checkbox (handles setting unique id for input and for attr for label)
  • credit card input (uses creditcards package to format number and set card type to data-credit-card attribute)
  • phone number input (formats 123456780 to 123 456 7890)
  • radios (handles setting unique id for input and for attr for label)
  • select

checkBox

  • handles setting unique id for input and for attr for label

takes an object with the following properties:

keytypeexplanationrequired
valueStringvalue of input when checkbox is checkedtrue
labelStringlabel texttrue
nameStringvalue of name property of the inputfalse
checkedBooleanwhether the checkbox is checkedfalse
classesStringstring of class names ('.mt-2.color-red')false
cbFunctioncallback function that gets called on change eventfalse
var fh = require('snabbdom-form-helpers')

h('div', [
  h('label', 'Checkbox')
, fh.checkBox({
    name: 'anonymous'
  , value: 't'
  , label: 'Donate anonymously?'
  })
])

cardInput

takes an object with the following properties:

keytypeexplanationrequired
nameStringvalue of name property of the inputfalse
valueStringvalue of inputfalse
placeholderStringplaceholder textfalse
classesStringstring of class names ('.mt-2.color-red')false
cbFunctioncallback function that gets called on input eventfalse
var fh = require('snabbdom-form-helpers')

h('div', [
  h('label', 'Credit card')
  , fh.cardInput({
      name: 'card'
    , value: '4242424242424242'
    , placeholder: 'Credit card number'
    })
])

phoneInput

  • simply formats a 10 digit number into three parts (formats 123456780 to 123 456 7890)
  • does not do any validation
  • meant only for US phone numbers

takes an object with the following properties:

keytypeexplanationrequired
nameStringvalue of name property of the inputfalse
placeholderStringplaceholder textfalse
valueStringvalue of inputfalse
classesStringstring of class names ('.mt-2.color-red')false
cbFunctioncallback function that gets called on input eventfalse
var fh = require('snabbdom-form-helpers')

h('div', [
  h('label', 'Phone number')
  , fh.phoneInput({
      name: 'phone'
    , value: '1234567890'
    , placeholder: 'Credit card number'
    })
])

radios

  • handles setting unique id for input and for attr for label

takes an object with the following properties:

keytypeexplanationrequired
nameStringvalue of name property of the inputtrue
optionsArray of objects{label: 'cash', value: 'money'} the value key is optionaltrue
selectedStringwill check an input if the selected string matches the input's valuefalse
classesStringstring of class names ('.mt-2.color-red')false
cbFunctioncallback function that gets called on change eventfalse
var fh = require('snabbdom-form-helpers')

h('div', [
  h('label', 'Radios')
, fh.radios({cb: cb, selected: 'check', name: 'payment-method', options: [
    {label: 'check'}
  , {label: 'credit card', value: 'card'}
  , {label: 'cash', value: 'money'}
  ]})

select

takes an object with the following properties:

keytypeexplanationrequired
nameStringvalue of name property of the selectfalse
optionsArray of strings or objectsa select option will be created for each of these option strings or objectstrue
selectedStringwill select an option if the selected string matches the option's valuefalse
placeholderStringplaceholder optionfalse
classesStringstring of class names ('.mt-2.color-red')false
disabledFunctionfunction that disables an option if the return value is truthyfalse
cbFunctioncallback function that gets called on change eventfalse
var fh = require('snabbdom-form-helpers')

var disabled = function(option) {return option ==='mail'}

h('div', [
  h('label', 'Select')
, fh.select({cb: cb, disabled: disabled, selected: 'phone', placeholder: 'Contact preference', name: 'contact-preference', options: [
  'SMS', {value: 'phone', label: 'Phone number'}, 'email', 'mail']})
])
0.2.0

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago