Skip to content

Instantly share code, notes, and snippets.

@ronny
Created August 17, 2014 05:04
Show Gist options
  • Save ronny/64776de35360f2960405 to your computer and use it in GitHub Desktop.
Save ronny/64776de35360f2960405 to your computer and use it in GitHub Desktop.
ReactJS CheckBox component that works like Rails' `f.check_box`
_ = require('underscore')
React = require('React')
{span, input} = React.DOM
# Check box that always submits a value, even if unchecked. This uses the same
# trick as Rails's `f.check_box`: a hidden input with the same name as the
# checkbox but with a value of 0 that appears before the actual checkbox. When
# the checkbox is checked, the checkbox's value will be submitted, when it's
# unchecked, then the hidden input value of 0 will be submitted.
window.CheckBox = React.createClass
render: ->
(span {},
(input {type: 'hidden', name: @props.name, value: 0})
(input _.extend({}, @props, {type: 'checkbox', value: 1})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment