Skip to content

Commit 97a493e

Browse files
authoredApr 18, 2018
Merge pull request #6 from coreui/dev-switch
feat: add switch component
2 parents e60bcf9 + ac9d2f3 commit 97a493e

File tree

12 files changed

+709
-801
lines changed

12 files changed

+709
-801
lines changed
 

‎demo/src/scss/_custom.scss

-1
This file was deleted.

‎demo/src/scss/_variables.scss

-1
This file was deleted.

‎demo/src/scss/style.css

+571-678
Large diffs are not rendered by default.

‎demo/src/scss/style.scss

-10
This file was deleted.

‎demo/src/scss/vendors/_variables.scss

-7
This file was deleted.

‎demo/src/scss/vendors/chart.js/chart.css

-44
This file was deleted.

‎demo/src/scss/vendors/chart.js/chart.scss

-48
This file was deleted.

‎package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coreui/react",
3-
"version": "2.0.0-beta",
3+
"version": "2.0.0-beta.1",
44
"description": "CoreUI React Bootstrap 4 components",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -20,13 +20,13 @@
2020
"lint": "eslint src"
2121
},
2222
"dependencies": {
23-
"@coreui/coreui": "^2.0.0-beta.6",
24-
"bootstrap": "4.0.0",
23+
"@coreui/coreui": "^2.0.0-beta.9",
24+
"bootstrap": "^4.1.0",
2525
"classnames": "^2.2.5",
2626
"prop-types": "^15.6.1",
2727
"flag-icon-css": "^3.0.0",
2828
"font-awesome": "^4.7.0",
29-
"react-perfect-scrollbar": "^1.0.5",
29+
"react-perfect-scrollbar": "^1.1.1",
3030
"react-router-dom": "^4.2.2",
3131
"reactstrap": "^5.0.0",
3232
"simple-line-icons": "^2.4.1"
@@ -35,15 +35,15 @@
3535
"react": "16.x"
3636
},
3737
"devDependencies": {
38-
"babel-eslint": "^8.2.2",
38+
"babel-eslint": "^8.2.3",
3939
"enzyme": "^3.3.0",
4040
"enzyme-adapter-react-16": "^1.1.1",
4141
"eslint": "^4.19.1",
42-
"eslint-plugin-import": "^2.10.0",
42+
"eslint-plugin-import": "^2.11.0",
4343
"eslint-plugin-react": "^7.7.0",
44-
"nwb": "0.21.x",
45-
"react": "^16.3.1",
46-
"react-dom": "^16.3.1",
44+
"nwb": "^0.21.5",
45+
"react": "^16.3.2",
46+
"react-dom": "^16.3.2",
4747
"sinon": "^4.5.0"
4848
},
4949
"author": "Łukasz Holeczek",

‎src/Header.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class AppHeader extends Component {
2323
if (fixed) { document.body.classList.add('header-fixed'); }
2424
}
2525

26-
breakpoint(breakpoint) {
27-
return breakpoint || '';
28-
}
26+
// breakpoint(breakpoint) {
27+
// return breakpoint || '';
28+
// }
2929

3030
render() {
3131
const { className, children, fixed, tag: Tag, ...attributes } = this.props;

‎src/Switch.js

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import classNames from 'classnames';
4+
5+
const propTypes = {
6+
color: PropTypes.string,
7+
label: PropTypes.bool,
8+
outline: PropTypes.bool,
9+
outlineAlt: PropTypes.bool,
10+
pill: PropTypes.bool,
11+
size: PropTypes.string,
12+
checked: PropTypes.bool,
13+
defaultChecked: PropTypes.bool,
14+
defaultValue: PropTypes.any,
15+
disabled: PropTypes.bool,
16+
form: PropTypes.any,
17+
indeterminate: PropTypes.any,
18+
name: PropTypes.string,
19+
required: PropTypes.bool,
20+
type: PropTypes.string,
21+
value: PropTypes.string,
22+
className: PropTypes.string,
23+
dataOn: PropTypes.string,
24+
dataOff: PropTypes.string,
25+
};
26+
27+
const defaultProps = {
28+
color: 'secondary',
29+
label: false,
30+
outline: false,
31+
outlineAlt: false,
32+
pill: false,
33+
size: '',
34+
checked: false,
35+
defaultChecked: false,
36+
disabled: false,
37+
required: false,
38+
type: 'checkbox',
39+
dataOn: 'On',
40+
dataOff: 'Off',
41+
};
42+
43+
class AppSwitch extends Component {
44+
constructor(props) {
45+
super(props);
46+
this.toggle = this.toggle.bind(this);
47+
this.state = {
48+
checked: this.props.defaultChecked || this.props.checked,
49+
};
50+
}
51+
52+
toggle(event) {
53+
const target = event.target;
54+
this.setState({
55+
checked: target.checked,
56+
});
57+
}
58+
59+
render() {
60+
const { className, checked, disabled, color, defaultChecked, name, label, outline, outlineAlt, pill, size, required, type, value, dataOn, dataOff, ...attributes } = this.props;
61+
62+
const outlined = outline || outlineAlt;
63+
64+
const variant = `switch${outlined ? '-outline' : ''}-${color}${outlineAlt ? '-alt' : ''}`;
65+
66+
const classes = classNames(
67+
className,
68+
'switch',
69+
label ? 'switch-label' : false,
70+
pill ? 'switch-pill' : false,
71+
size ? `switch-${size}` : false,
72+
variant,
73+
'form-check-label',
74+
);
75+
76+
const inputClasses = classNames(
77+
'switch-input',
78+
'form-check-input',
79+
);
80+
81+
const sliderClasses = classNames(
82+
'switch-slider',
83+
);
84+
85+
return (
86+
<label className={classes}>
87+
<input type={type} className={inputClasses} onChange={this.toggle} checked={this.state.checked} name={name} required={required} disabled={disabled} value={value} />
88+
<span className={sliderClasses} data-checked={dataOn} data-unchecked={dataOff}></span>
89+
</label>
90+
);
91+
}
92+
}
93+
94+
AppSwitch.propTypes = propTypes;
95+
AppSwitch.defaultProps = defaultProps;
96+
97+
export default AppSwitch;

‎src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export AppSidebarHeader from './SidebarHeader';
1212
export AppSidebarMinimizer from './SidebarMinimizer';
1313
export AppSidebarNav from './SidebarNav';
1414
export AppSidebarToggler from './SidebarToggler';
15+
export AppSwitch from './Switch';
1516
export AppLayout from './Shared';

‎tests/Switch.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import expect from 'expect'
2+
import React from 'react'
3+
import {renderToStaticMarkup as render} from 'react-dom/server'
4+
5+
import { configure, mount } from 'enzyme'
6+
import Adapter from 'enzyme-adapter-react-16'
7+
import { spy } from 'sinon'
8+
9+
import AppSwitch from 'src/Switch'
10+
11+
configure({ adapter: new Adapter() });
12+
13+
describe('AppSwitch', () => {
14+
it('renders label with class="switch"', () => {
15+
expect(render(<AppSwitch />))
16+
.toContain('<label class="switch')
17+
});
18+
it('should call toggle', () => {
19+
const onChange = spy(AppSwitch.prototype, 'toggle');
20+
const event = {target: { checked: true }};
21+
const wrapper = mount(<AppSwitch outlineAlt label pill size="lg" />);
22+
expect(wrapper.find('input').props().checked).toBe(false);
23+
wrapper.find('input').simulate('change', event)
24+
expect(onChange.called).toBe(true);
25+
expect(wrapper.find('input').props().checked).toBe(true);
26+
27+
})
28+
})

0 commit comments

Comments
 (0)
Please sign in to comment.