-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from M03M/checkbox-revamp
Checkbox spread functionality and style changes
- Loading branch information
Showing
4 changed files
with
169 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.