Skip to content

Commit c105008

Browse files
committed
Added babel-compiled .js version
Changed `package.json` to point `main` to the `.js` version Added `prepublish` hook to compile the `.jsx` to `.js`
1 parent c732fbe commit c105008

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "react-checkbox-group",
3-
"version": "0.1.5",
3+
"version": "0.1.7",
44
"description": "Sensible checkbox groups manipulation for DOM.",
5-
"main": "react-checkbox-group.jsx",
5+
"main": "react-checkbox-group.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"prepublish": "node_modules/.bin/babel --stage 1 ./react-checkbox-group.jsx --out-file ./react-checkbox-group.js"
89
},
910
"repository": {
1011
"type": "git",
@@ -25,7 +26,10 @@
2526
"url": "https://github.com/ziad-saab/react-checkbox-group/issues"
2627
},
2728
"homepage": "https://github.com/ziad-saab/react-checkbox-group",
28-
"dependencies": {
29-
"react": "~0.11.1"
29+
"peerDependencies": {
30+
"react": ">= 0.11.1 < 1"
31+
},
32+
"devDependencies": {
33+
"babel": "^5.6.7"
3034
}
3135
}

react-checkbox-group.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @jsx React.DOM
3+
*/
4+
'use strict';
5+
6+
var React = require('react');
7+
8+
module.exports = React.createClass({
9+
displayName: 'CheckboxGroup',
10+
getInitialState: function getInitialState() {
11+
return { defaultValue: this.props.defaultValue || [] };
12+
},
13+
14+
componentDidMount: function componentDidMount() {
15+
this.setCheckboxNames();
16+
this.setCheckedBoxes();
17+
},
18+
19+
componentDidUpdate: function componentDidUpdate() {
20+
this.setCheckboxNames();
21+
this.setCheckedBoxes();
22+
},
23+
24+
render: function render() {
25+
return React.createElement(
26+
'div',
27+
this.props,
28+
this.props.children
29+
);
30+
},
31+
32+
setCheckboxNames: function setCheckboxNames() {
33+
// stay DRY and don't put the same `name` on all checkboxes manually. Put it on
34+
// the tag and it'll be done here
35+
var $checkboxes = this.getCheckboxes();
36+
for (var i = 0, _length = $checkboxes.length; i < _length; i++) {
37+
$checkboxes[i].setAttribute('name', this.props.name);
38+
}
39+
},
40+
41+
getCheckboxes: function getCheckboxes() {
42+
return this.getDOMNode().querySelectorAll('input[type="checkbox"]');
43+
},
44+
45+
setCheckedBoxes: function setCheckedBoxes() {
46+
var $checkboxes = this.getCheckboxes();
47+
// if `value` is passed from parent, always use that value. This is similar
48+
// to React's controlled component. If `defaultValue` is used instead,
49+
// subsequent updates to defaultValue are ignored. Note: when `defaultValue`
50+
// and `value` are both passed, the latter takes precedence, just like in
51+
// a controlled component
52+
var destinationValue = this.props.value != null ? this.props.value : this.state.defaultValue;
53+
54+
for (var i = 0, _length2 = $checkboxes.length; i < _length2; i++) {
55+
var $checkbox = $checkboxes[i];
56+
57+
// intentionally use implicit conversion for those who accidentally used,
58+
// say, `valueToChange` of 1 (integer) to compare it with `value` of "1"
59+
// (auto conversion to valid html value from React)
60+
if (destinationValue.indexOf($checkbox.value) >= 0) {
61+
$checkbox.checked = true;
62+
}
63+
}
64+
},
65+
66+
getCheckedValues: function getCheckedValues() {
67+
var $checkboxes = this.getCheckboxes();
68+
69+
var checked = [];
70+
for (var i = 0, _length3 = $checkboxes.length; i < _length3; i++) {
71+
if ($checkboxes[i].checked) {
72+
checked.push($checkboxes[i].value);
73+
}
74+
}
75+
76+
return checked;
77+
}
78+
});

react-checkbox-group.jsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
*/
44
var React = require('react');
55

6-
var CheckboxGroup = module.exports = React.createClass({
6+
module.exports = React.createClass({
7+
displayName: 'CheckboxGroup',
78
getInitialState: function() {
89
return {defaultValue: this.props.defaultValue || []};
910
},
@@ -19,8 +20,8 @@ var CheckboxGroup = module.exports = React.createClass({
1920
},
2021

2122
render: function() {
22-
return this.transferPropsTo(
23-
<div onChange={this.props.onChange}>
23+
return (
24+
<div {...this.props}>
2425
{this.props.children}
2526
</div>
2627
);
@@ -29,8 +30,8 @@ var CheckboxGroup = module.exports = React.createClass({
2930
setCheckboxNames: function() {
3031
// stay DRY and don't put the same `name` on all checkboxes manually. Put it on
3132
// the tag and it'll be done here
32-
var $checkboxes = this.getCheckboxes();
33-
for (var i = 0, length = $checkboxes.length; i < length; i++) {
33+
let $checkboxes = this.getCheckboxes();
34+
for (let i = 0, length = $checkboxes.length; i < length; i++) {
3435
$checkboxes[i].setAttribute('name', this.props.name);
3536
}
3637
},
@@ -40,18 +41,18 @@ var CheckboxGroup = module.exports = React.createClass({
4041
},
4142

4243
setCheckedBoxes: function() {
43-
var $checkboxes = this.getCheckboxes();
44+
let $checkboxes = this.getCheckboxes();
4445
// if `value` is passed from parent, always use that value. This is similar
4546
// to React's controlled component. If `defaultValue` is used instead,
4647
// subsequent updates to defaultValue are ignored. Note: when `defaultValue`
4748
// and `value` are both passed, the latter takes precedence, just like in
4849
// a controlled component
49-
var destinationValue = this.props.value != null
50+
let destinationValue = this.props.value != null
5051
? this.props.value
5152
: this.state.defaultValue;
5253

53-
for (var i = 0, length = $checkboxes.length; i < length; i++) {
54-
var $checkbox = $checkboxes[i];
54+
for (let i = 0, length = $checkboxes.length; i < length; i++) {
55+
let $checkbox = $checkboxes[i];
5556

5657
// intentionally use implicit conversion for those who accidentally used,
5758
// say, `valueToChange` of 1 (integer) to compare it with `value` of "1"
@@ -63,10 +64,10 @@ var CheckboxGroup = module.exports = React.createClass({
6364
},
6465

6566
getCheckedValues: function() {
66-
var $checkboxes = this.getCheckboxes();
67+
let $checkboxes = this.getCheckboxes();
6768

68-
var checked = [];
69-
for (var i = 0, length = $checkboxes.length; i < length; i++) {
69+
let checked = [];
70+
for (let i = 0, length = $checkboxes.length; i < length; i++) {
7071
if ($checkboxes[i].checked) {
7172
checked.push($checkboxes[i].value);
7273
}

0 commit comments

Comments
 (0)