Skip to content

Commit 070504b

Browse files
committed
Merge pull request #87 from ngsru/fix-module-checkbox
Fix module checkbox
2 parents 2b24fc8 + a5006f0 commit 070504b

File tree

8 files changed

+352
-56
lines changed

8 files changed

+352
-56
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ root = true
44
end_of_line = lf
55
insert_final_newline = true
66

7-
[*.{js,jsx,es6}]
7+
[*.{js,jsx,es6,md}]
88
charset = utf-8
99
indent_style = space
1010
indent_size = 4

index.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,12 +1333,10 @@ export interface CheckboxProps extends BaseProps<Checkbox> {
13331333
checked?: boolean;
13341334
fitted?: boolean;
13351335
disabled?: boolean;
1336-
indeterminate?: boolean;
13371336
name?: string;
1338-
radio?: boolean;
1337+
onClick?: React.MouseEventHandler;
13391338
readOnly?: boolean;
1340-
slider?: boolean;
1341-
toggle?: boolean;
1339+
type?: "default" | "radio" | "toggle" | "slider";
13421340
}
13431341
export class Checkbox extends React.Component<CheckboxProps, any> {
13441342
}
@@ -1485,4 +1483,4 @@ export interface ModalProps extends BaseProps<Modal> {
14851483
*/
14861484
onRequestClose: () => void;
14871485
}
1488-
export class Modal extends React.Component<ModalProps, any> { }
1486+
export class Modal extends React.Component<ModalProps, any> { }

src/components/modules/checkbox/checkbox.jsx

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,82 @@
11
import React, { Component } from 'react';
22
import classNames from 'classnames';
3+
import DefaultProps from '../../defaultProps';
34

45
export default class Checkbox extends Component {
56
static propTypes = {
7+
...DefaultProps.propTypes,
8+
9+
/**
10+
* State checked
11+
*/
612
checked: React.PropTypes.bool,
7-
children: React.PropTypes.node,
8-
className: React.PropTypes.any,
9-
component: React.PropTypes.oneOfType([
10-
React.PropTypes.element,
11-
React.PropTypes.string
12-
]),
13-
defaultClasses: React.PropTypes.bool,
13+
/**
14+
* Does not allow user interaction
15+
*/
1416
disabled: React.PropTypes.bool,
15-
fitted: React.PropTypes.bool,
16-
indeterminate: React.PropTypes.bool,
17-
name: React.PropTypes.string,
18-
onClick: React.PropTypes.func,
19-
radio: React.PropTypes.bool,
17+
/**
18+
* It does disabled, but does not allow user interaction
19+
*/
2020
readOnly: React.PropTypes.bool,
21-
slider: React.PropTypes.bool,
22-
toggle: React.PropTypes.bool
21+
/**
22+
* Callback handler to click checkbox
23+
*/
24+
onClick: React.PropTypes.func,
25+
/**
26+
* Attr name
27+
*/
28+
name: React.PropTypes.string,
29+
/**
30+
* Checkbox - appearance
31+
*/
32+
type: React.PropTypes.oneOf(['default', 'radio', 'toggle', 'slider']),
33+
/**
34+
* A fitted checkbox does not leave padding for a label
35+
*/
36+
fitted: React.PropTypes.bool
2337
};
2438

2539
static defaultProps = {
26-
component: 'div',
27-
defaultClasses: true
40+
...DefaultProps.defaultProps,
41+
type: 'default',
42+
onClick: () => { }
2843
};
2944

30-
constructor(props) {
31-
super(props);
45+
onClick = (event) => {
46+
if (this.props.disabled || this.props.readOnly) return;
3247

33-
this.state = {
34-
active: this.props.checked
35-
}
36-
}
37-
38-
onClick() {
39-
if (!this.state.active || (this.state.active && !this.props.radio)) {
40-
this.setState({
41-
active: !this.state.active
42-
});
43-
}
44-
}
48+
this.props.onClick(event);
49+
};
4550

4651
renderChildren() {
4752
/* eslint-disable no-use-before-define */
48-
let { children, defaultClasses, className, onClick,
49-
radio, slider, toggle, component, readOnly, checked,
50-
disabled, ...other } = this.props;
53+
let { children, defaultClasses, className, onClick, type,
54+
component, readOnly, checked, ...other } = this.props;
5155
/* eslint-enable no-use-before-define */
5256

5357
let childElements = [
54-
React.DOM.input({
55-
type: 'checkbox',
56-
key: 'input',
57-
className: 'hidden',
58-
checked: this.state.active || this.props.checked,
59-
readOnly: true,
60-
...other
61-
}),
62-
React.DOM.label({
63-
key: 'label'
64-
}, this.props.children)
58+
<input
59+
{ ...other }
60+
type="checkbox"
61+
key="input"
62+
className="hidden"
63+
readOnly
64+
checked={checked} />,
65+
<label key="label">
66+
{children}
67+
</label>
6568
];
6669

6770
return childElements;
6871
}
6972

7073
render() {
7174
/* eslint-disable no-use-before-define */
72-
let { component, defaultClasses, name, ...other } = this.props;
75+
let { component, defaultClasses, checked, type, onClick, name, ...other } = this.props;
7376
/* eslint-enable no-use-before-define */
7477

7578
other.className = classNames(this.props.className, this.getClasses());
76-
other.onClick = typeof this.props.onClick === 'function' ? this.props.onClick : this.onClick.bind(this);
79+
other.onClick = this.onClick;
7780

7881
return React.createElement(
7982
this.props.component,
@@ -91,16 +94,16 @@ export default class Checkbox extends Component {
9194
// positioning
9295

9396
// types
94-
radio: this.props.radio,
97+
radio: this.props.type == 'radio',
98+
slider: this.props.type == 'slider',
99+
toggle: this.props.type == 'toggle',
95100

96101
// variations
97102
fitted: this.props.fitted,
98-
slider: this.props.slider,
99-
toggle: this.props.toggle,
100103

101104
// state
102105
'read-only': this.props.readOnly,
103-
checked: this.state.active || this.props.checked,
106+
checked: this.props.checked,
104107
disabled: this.props.disabled,
105108
indeterminate: this.props.indeterminate
106109
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
A checkbox allows a user to select a value from a small set of options, often binary
2+
3+
## Types
4+
5+
```require
6+
./types.jsx
7+
```
8+
9+
### Radio - a lot of
10+
11+
```require
12+
./radios.jsx
13+
```
14+
15+
## State
16+
17+
### Read-only
18+
19+
```require
20+
./read-only.jsx
21+
```
22+
23+
### Disabled
24+
25+
```require
26+
./disabled.jsx
27+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react';
2+
import Checkbox from '../checkbox';
3+
import Form from '../../../collections/form/fields';
4+
import Field from './../../../collections/form/form';
5+
6+
class MyCheckboxes extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
10+
this.state = {
11+
isCheckbox: false,
12+
isRadio: false,
13+
isSlider: false,
14+
isToggle: false
15+
}
16+
}
17+
18+
onClickCheckbox(name) {
19+
this.setState({
20+
[name]: !this.state[name]
21+
});
22+
}
23+
24+
render() {
25+
let { isCheckbox, isRadio, isSlider, isToggle } = this.state;
26+
27+
return (
28+
<Form>
29+
<Field>
30+
<Checkbox
31+
disabled
32+
checked={isCheckbox}
33+
onClick={this.onClickCheckbox.bind(this, 'isCheckbox')}
34+
>
35+
Disabled, you can test
36+
</Checkbox>
37+
</Field>
38+
<Field>
39+
<Checkbox
40+
disabled
41+
type="radio"
42+
checked={isRadio}
43+
onClick={this.onClickCheckbox.bind(this, 'isRadio')}
44+
>
45+
Disabled, you can test
46+
</Checkbox>
47+
</Field>
48+
<Field>
49+
<Checkbox
50+
disabled
51+
type="slider"
52+
checked={isSlider}
53+
onClick={this.onClickCheckbox.bind(this, 'isSlider')}
54+
>
55+
Disabled, you can test
56+
</Checkbox>
57+
</Field>
58+
<Field>
59+
<Checkbox
60+
disabled
61+
type="toggle"
62+
checked={isToggle}
63+
onClick={this.onClickCheckbox.bind(this, 'isToggle')}
64+
>
65+
Disabled, you can test
66+
</Checkbox>
67+
</Field>
68+
</Form>
69+
);
70+
}
71+
}
72+
73+
<MyCheckboxes />
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react';
2+
import Checkbox from '../checkbox';
3+
import Form from '../../../collections/form/fields';
4+
import Fields from '../../../collections/form/Fields';
5+
import Field from './../../../collections/form/form';
6+
7+
class MyRadios extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
11+
this.state = {
12+
value: null
13+
};
14+
}
15+
16+
onClickRadio(value) {
17+
this.setState({
18+
value: value
19+
});
20+
}
21+
22+
render() {
23+
let { value } = this.state;
24+
25+
return (
26+
<Form>
27+
<Fields>
28+
<label>Omg bananas!</label>
29+
<Field>
30+
<Checkbox
31+
type="radio"
32+
checked={value === 'one'}
33+
onClick={this.onClickRadio.bind(this, 'one')}
34+
>
35+
One banana
36+
</Checkbox>
37+
</Field>
38+
<Field>
39+
<Checkbox
40+
type="radio"
41+
checked={value === 'two'}
42+
onClick={this.onClickRadio.bind(this, 'two')}
43+
>
44+
Two banana
45+
</Checkbox>
46+
</Field>
47+
</Fields>
48+
</Form>
49+
);
50+
}
51+
}
52+
53+
<MyRadios />

0 commit comments

Comments
 (0)