From 3f187fd65506ad2dbedb22f61645806a08baab6f Mon Sep 17 00:00:00 2001 From: lfbn Date: Sat, 7 Oct 2017 23:26:30 +0100 Subject: [PATCH 1/2] Add loading: can be set an loading state displaying an loading icon --- spec/component.spec.js | 21 +++++++++++++ src/component/index.js | 14 +++++++++ src/component/loading.js | 20 ++++++++++++ src/docs/index.js | 66 +++++++++++++++++++++++++++++++++++++++- src/docs/style.css | 1 + style.css | 4 +++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/component/loading.js diff --git a/spec/component.spec.js b/spec/component.spec.js index 804caaf..5a1f21f 100644 --- a/spec/component.spec.js +++ b/spec/component.spec.js @@ -53,6 +53,20 @@ describe('Component', () => { expect(wrapper.find('input').hasClass(className)).to.be.false }) + it('allows to be set as loading', () => { + wrapper = shallow() + + expect(wrapper.state('loading')).to.be.true + expect(wrapper.find('.react-toggle--loading')).to.not.be.empty + }) + + it('defaults the value of `loading` to false', () => { + wrapper = shallow() + + expect(wrapper.state('loading')).to.be.false + expect(wrapper.find('.react-toggle--loading')).to.be.empty + }) + it('defaults to the value of `defaultChecked`', () => { wrapper = shallow() @@ -86,6 +100,13 @@ describe('Component', () => { expect(wrapper.find('.react-toggle-track-x')).to.be.contain(unchecked) }) + it('takes loading custom icons', () => { + const loading =
loading
+ wrapper = shallow() + + expect(wrapper.find('.react-toggle--loading')).to.be.contain(loading) + }) + it('defaults to the regular icon if only one is supplied', () => { const checked =
checked
const unchecked =
unchecked
diff --git a/src/component/index.js b/src/component/index.js index 859de19..b61daf1 100644 --- a/src/component/index.js +++ b/src/component/index.js @@ -3,6 +3,7 @@ import classNames from 'classnames' import PropTypes from 'prop-types' import Check from './check' import X from './x' +import Loading from './loading' import { pointerCoord } from './util' export default class Toggle extends PureComponent { @@ -18,6 +19,7 @@ export default class Toggle extends PureComponent { this.state = { checked: !!(props.checked || props.defaultChecked), hasFocus: false, + loading: !!(props.loading || false), } } @@ -125,11 +127,20 @@ export default class Toggle extends PureComponent { render () { const { className, icons: _icons, ...inputProps } = this.props const classes = classNames('react-toggle', { + 'react-toggle--loading react-toggle--default-cursor': this.state.loading, 'react-toggle--checked': this.state.checked, 'react-toggle--focus': this.state.hasFocus, 'react-toggle--disabled': this.props.disabled, }, className) + if (this.state.loading) { + return ( +
+ {this.getIcon('loading')} +
+ ) + } + return (
, unchecked: , + loading: , }, } Toggle.propTypes = { + loading: PropTypes.bool, checked: PropTypes.bool, disabled: PropTypes.bool, defaultChecked: PropTypes.bool, @@ -185,6 +198,7 @@ Toggle.propTypes = { PropTypes.shape({ checked: PropTypes.node, unchecked: PropTypes.node, + loading: PropTypes.node, }), ]), } diff --git a/src/component/loading.js b/src/component/loading.js new file mode 100644 index 0000000..9e9a125 --- /dev/null +++ b/src/component/loading.js @@ -0,0 +1,20 @@ +import React from 'react' + +export default () => ( + + + + + + + + + + +) diff --git a/src/docs/index.js b/src/docs/index.js index e142eb0..8a9a7a2 100644 --- a/src/docs/index.js +++ b/src/docs/index.js @@ -19,6 +19,25 @@ const Heart = () => (
) +const LoadingText = () => ( +
+ + + + + + + + + + + +
+) + class App extends Component { constructor (props) { super(props) @@ -286,6 +305,27 @@ class App extends Component { + {/* Loading */} + +
+
+ +
+ +
+            {``}
+          
+
+ {/* Custom className */}
@@ -336,7 +376,31 @@ class App extends Component { unchecked: null, }} onChange={this.handleSoupChange} /> - Custom icons + Custom icons +`} + +
+ + {/* Loading Custom icon */} + +
+ + +
+            {``}
           
diff --git a/src/docs/style.css b/src/docs/style.css index 09a9dc8..df3a1d3 100644 --- a/src/docs/style.css +++ b/src/docs/style.css @@ -24,3 +24,4 @@ pre { .custom-classname.react-toggle--checked .react-toggle-track { background-color: #ab199f; } + diff --git a/style.css b/style.css index 9e06dcd..263ecc4 100644 --- a/style.css +++ b/style.css @@ -139,3 +139,7 @@ -moz-box-shadow: 0px 0px 5px 5px #0099E0; box-shadow: 0px 0px 5px 5px #0099E0; } + +.react-toggle--default-cursor { + cursor: default; +} \ No newline at end of file From 178024369482871810311f94945c283cbaa0811a Mon Sep 17 00:00:00 2001 From: lfbn Date: Sat, 7 Oct 2017 23:30:19 +0100 Subject: [PATCH 2/2] Add missing line break at the end of file --- style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/style.css b/style.css index 263ecc4..0698b5b 100644 --- a/style.css +++ b/style.css @@ -142,4 +142,4 @@ .react-toggle--default-cursor { cursor: default; -} \ No newline at end of file +}