Skip to content

Commit

Permalink
Merge pull request #233 from M03M/checkbox-revamp
Browse files Browse the repository at this point in the history
Checkbox spread functionality and style changes
  • Loading branch information
Hai Nguyen committed Jan 16, 2015
2 parents 67c241e + c457f36 commit a57b9cd
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 114 deletions.
73 changes: 47 additions & 26 deletions docs/src/app/components/pages/components/switches.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ var SwitchesPage = React.createClass({
'//Checkboxes\n' +
'<Checkbox\n' +
' name="checkboxName1"\n' +
' value="checkboxValue1" />\n' +
' value="checkboxValue1"\n' +
' label="went for a run today" />\n' +
'<Checkbox\n' +
' name="checkboxName2"\n' +
' value="checkboxValue2"\n' +
' label="went for a run today" />\n' +
' value="checkboxValue2"\n' +
' label="fed the dog"\n' +
' defaultChecked={true} />\n' +
'<Checkbox\n' +
' name="checkboxName3"\n' +
' value="checkboxValue3"\n' +
' label="fed the dog"\n' +
' checked={true} />\n\n' +
' label="built a house on the moon"\n' +
' disabled={true} />\n\n' +
'//Radio Buttons\n' +
'<RadioButton\n' +
' name="shipSpeed1"\n' +
Expand All @@ -42,11 +44,12 @@ var SwitchesPage = React.createClass({
'<Toggle />\n';

var desc = 'This component generates a switches element and all props except for the custom ' +
'props below will be passed down to the switch element.';
'props below will be passed down to the switch element. Checkboxes can now accept input ' +
'attributes of type "checkbox" as properties. See checkbox 3 for an example of this.';

var componentInfo = [
{
name: 'Checkbox',
name: 'Checkbox Props',
infoArray: [
{
name: 'name',
Expand All @@ -67,10 +70,32 @@ var SwitchesPage = React.createClass({
desc: 'The text that is displayed to the right of the checkbox.'
},
{
name: 'checked',
name: 'defaultChecked',
type: 'boolean',
header: 'default:false',
desc: 'The current state of our checkbox component.'
desc: 'The default state of our checkbox component.'
},
{
name: 'onCheck',
type: 'function',
header: 'optional',
desc: 'Callback function that is called when the checkbox is checked.'
}
]
},
{
name: 'Checkbox Methods',
infoArray: [
{
name: 'isChecked',
header: 'Checkbox.isChecked()',
desc: 'Returns true if the checkbox is currently checked. Returns false otherwise'
},
{
name: 'setChecked',
header: 'Checkbox.setChecked(newCheckedValue)',
desc: 'Sets the checkbox to the value of newCheckedValue. This method cannot be used ' +
'while "checked" is defined as a property.'
}
]
},
Expand Down Expand Up @@ -131,11 +156,9 @@ var SwitchesPage = React.createClass({
componentInfo={componentInfo}>

<div className="switches-examples">
<form>
{this._getCheckboxExample()}
{this._getRadioButtonExample()}
{this._getToggleExample()}
</form>
{this._getCheckboxExample()}
{this._getRadioButtonExample()}
{this._getToggleExample()}
</div>

</ComponentDoc>
Expand All @@ -153,26 +176,24 @@ var SwitchesPage = React.createClass({

<div className="switches-example-container">
<Checkbox
name="checkboxName1"
name="checkboxName1"
value="checkboxValue1"
onClick={this._onCheck} />
label="went for a run today"/>
</div>
<div className="switches-example-container">
<Checkbox
name="checkboxName2"
<Checkbox
name="checkboxName2"
value="checkboxValue2"
label="went for a run today"
onClick={this._onCheck} />
label="fed the dog"
defaultChecked={true}/>
</div>
<div className="switches-example-container">
<Checkbox
name="checkboxName3"
<Checkbox
name="checkboxName3"
value="checkboxValue3"
label="fed the dog"
checked={true}
onClick={this._onCheck} />
label="built a house on the moon"
disabled={true}/>
</div>

</div>
);
},
Expand Down
98 changes: 67 additions & 31 deletions src/js/checkbox.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,104 @@
var React = require('react'),
Classable = require('./mixins/classable.js');
var React = require('react');
var Classable = require('./mixins/classable.js');
var Icon = require('./icon.jsx');

var Checkbox = React.createClass({

propTypes: {
label: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
onClick: React.PropTypes.func,
onCheck: React.PropTypes.func,
value: React.PropTypes.string.isRequired,
checked: React.PropTypes.bool
label: React.PropTypes.string,
onCheck: React.PropTypes.func,
required: React.PropTypes.bool,
disabled: React.PropTypes.bool,
defaultChecked: React.PropTypes.bool
},

mixins: [Classable],
mixins: [Classable, React.addons.LinkedStateMixin],

getInitialState: function() {
return {
checked: this.props.checked || false
checked: this.props.defaultChecked || false
}
},

componentWillReceiveProps: function(nextProps) {
if (nextProps.hasOwnProperty('checked')) this.setState({checked: nextProps.checked});
componentDidMount: function() {
var inputNode = this.refs.checkbox.getDOMNode();
this.setState({checked: inputNode.checked});
},

check: function() {
this.setState({ checked: !this.state.checked });
this.refs.checkbox.getDOMNode().checked = !this.refs.checkbox.getDOMNode().checked;
componentWillReceiveProps: function() {
var inputNode = this.refs.checkbox.getDOMNode();
this.setState({checked: inputNode.checked});
},

handleChange: function(e) {
var isInputChecked = this.refs.checkbox.getDOMNode().checked;

if (!this.props.checked) this.setState({checked: isInputChecked});
if (this.props.onCheck) this.props.onCheck(e, isInputChecked);
},

render: function() {

render: function() {
var classes = this.getClasses('mui-checkbox');

var componentclasses = React.addons.classSet({
'mui-checkbox-component': true,
'mui-checked': this.state.checked === true
'mui-is-checked': this.state.checked,
'mui-is-disabled': this.props.disabled,
'mui-is-required': this.props.required
});

var {
type,
name,
value,
onCheck,
...other
} = this.props;

return (
<div className={classes}>
<div className={componentclasses} onClick={this._onClick}>
<input
ref="checkbox"
type="checkbox"
name={this.props.name}
value={this.props.value} />
<span className="mui-checkbox-box" />
<span className="mui-checkbox-check" />

<input
{...other}
ref="checkbox"
type="checkbox"
name={this.props.name}
value={this.props.value}
onChange={this.handleChange}/>

<div className={componentclasses}>
<div className="mui-checkbox-box">
<Icon icon="toggle-check-box-outline-blank" />
</div>
<div className="mui-checkbox-check">
<Icon icon="toggle-check-box" />
</div>
</div>

<div className="mui-checkbox-label">
{this.props.label}
</div>
<span className="mui-checkbox-label">{this.props.label}</span>
</div>
);
},

_onClick: function(e) {
var checkedState = this.state.checked;

this.check();
isChecked: function() {
return this.refs.checkbox.getDOMNode().checked;
},

if (this.props.onClick) this.props.onClick(e, !checkedState);
// no callback here because there is no event
setChecked: function(newCheckedValue) {
if (!this.props.hasOwnProperty('checked')) {
this.setState({checked: newCheckedValue});
this.refs.checkbox.getDOMNode().checked = newCheckedValue;
} else {
var message = 'Cannot call setChecked() while checked is defined as a property.';
console.error(message);
}
}

});

module.exports = Checkbox;
module.exports = Checkbox;
Loading

0 comments on commit a57b9cd

Please sign in to comment.