1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _react = require('react');
|
8 |
|
9 | var _react2 = _interopRequireDefault(_react);
|
10 |
|
11 | var _propTypes = require('prop-types');
|
12 |
|
13 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
14 |
|
15 | var _style = require('./style.scss');
|
16 |
|
17 | var _style2 = _interopRequireDefault(_style);
|
18 |
|
19 | var _bind = require('classnames/bind');
|
20 |
|
21 | var _bind2 = _interopRequireDefault(_bind);
|
22 |
|
23 | var _Popover = require('../Popover/Popover');
|
24 |
|
25 | var _Popover2 = _interopRequireDefault(_Popover);
|
26 |
|
27 | var _Spinner = require('../Spinner');
|
28 |
|
29 | var _Spinner2 = _interopRequireDefault(_Spinner);
|
30 |
|
31 | var _ConfirmationOverlay = require('../internal/ConfirmationOverlay');
|
32 |
|
33 | var _ConfirmationOverlay2 = _interopRequireDefault(_ConfirmationOverlay);
|
34 |
|
35 | var _colors = require('../internal/colors');
|
36 |
|
37 | var _colors2 = _interopRequireDefault(_colors);
|
38 |
|
39 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
40 |
|
41 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
42 |
|
43 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
44 |
|
45 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
46 |
|
47 | var Toggle = function (_PureComponent) {
|
48 | _inherits(Toggle, _PureComponent);
|
49 |
|
50 | function Toggle(props) {
|
51 | _classCallCheck(this, Toggle);
|
52 |
|
53 | var _this = _possibleConstructorReturn(this, (Toggle.__proto__ || Object.getPrototypeOf(Toggle)).call(this, props));
|
54 |
|
55 | _this.state = {
|
56 | value: _this.props.value,
|
57 | text: ['Yes', 'No'],
|
58 | confirmIsOpen: false
|
59 | };
|
60 |
|
61 | _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
|
62 | if (nextProps.value !== _this.props.value || _this.props.confirm) {
|
63 | _this.setState({ value: nextProps.value });
|
64 | }
|
65 | };
|
66 |
|
67 | _this.handleChange = function () {
|
68 | if (_this.props.disabled) return;
|
69 |
|
70 | if (_this.props.confirm === 'both' || _this.props.confirm === 'on' && !_this.state.value || _this.props.confirm === 'off' && _this.state.value) {
|
71 | _this.setState({ confirmIsOpen: true });
|
72 | } else {
|
73 | _this.toggleValue();
|
74 | _this.setState({ confirmIsOpen: false });
|
75 | }
|
76 | };
|
77 |
|
78 | _this.handleConfirmation = function (confirm, e) {
|
79 | e.stopPropagation();
|
80 |
|
81 | if (confirm) _this.toggleValue();
|
82 |
|
83 | _this.setState({ confirmIsOpen: false });
|
84 | };
|
85 |
|
86 | _this.toggleValue = function () {
|
87 | _this.setState({ value: !_this.state.value }, function () {
|
88 | _this.props.changeCallback && _this.props.changeCallback({
|
89 | target: { name: _this.props.name, value: _this.state.value }
|
90 | });
|
91 | });
|
92 | };
|
93 |
|
94 | _this.getToggleText = function (isOn) {
|
95 | if (!_this.props.hasText) return '';
|
96 |
|
97 | return isOn ? _this.state.text[0] : _this.state.text[1];
|
98 | };
|
99 |
|
100 | _this.render = function () {
|
101 | var cx = _bind2.default.bind(_style2.default);
|
102 | var onClass = _this.state.value ? _style2.default.on : '';
|
103 | var loadingClass = _this.props.loading ? _style2.default['toggle-loading'] : '';
|
104 | var innerLoading = _this.props.loading ? 'loading' : '';
|
105 | var outerClasses = cx(_style2.default.outer, onClass);
|
106 | var innerClasses = cx(_style2.default.inner, onClass, innerLoading);
|
107 | var textClasses = cx(_style2.default.text, onClass);
|
108 | var hasTextClass = _this.props.hasText ? _style2.default['has-text'] : _style2.default['no-text'];
|
109 | var disabledClass = _this.props.disabled || _this.state.confirmIsOpen ? _style2.default['toggle-disabled'] : '';
|
110 | var toggleWrapper = cx(_style2.default['toggle-wrapper'], disabledClass, hasTextClass);
|
111 | var toggleClass = cx(_style2.default['toggle-component'], loadingClass, _this.props.optClass);
|
112 | var toggleText = _this.getToggleText(onClass);
|
113 | var toggle = _react2.default.createElement(
|
114 | 'div',
|
115 | { className: toggleWrapper },
|
116 | _react2.default.createElement('div', { className: outerClasses }),
|
117 | _this.props.hasText && _react2.default.createElement(
|
118 | 'span',
|
119 | { className: textClasses },
|
120 | toggleText
|
121 | ),
|
122 | _react2.default.createElement(
|
123 | 'div',
|
124 | { className: innerClasses },
|
125 | _this.props.loading && _react2.default.createElement(_Spinner2.default, { loading: true, type: 'spinner-circular', backgroundColor: 'transparent', color: _colors2.default.white, size: '16', className: _style2.default['toggle-loader'] })
|
126 | )
|
127 | );
|
128 |
|
129 | return _react2.default.createElement(
|
130 | 'div',
|
131 | { className: toggleClass, onClick: _this.handleChange },
|
132 | _this.props.label && _react2.default.createElement(
|
133 | 'label',
|
134 | { className: disabledClass },
|
135 | _this.props.label
|
136 | ),
|
137 | _this.props.confirm ? _react2.default.createElement(
|
138 | _Popover2.default,
|
139 | {
|
140 | showing: _this.state.confirmIsOpen,
|
141 | defaultPosition: 'bottom',
|
142 | content: _react2.default.createElement(_ConfirmationOverlay2.default, {
|
143 | handleConfirmation: _this.handleConfirmation,
|
144 | prompt: _this.props.confirmText }),
|
145 | width: _this.props.confirmWidth + 'px',
|
146 | onRequestClose: function onRequestClose() {
|
147 | return _this.setState({ confirmIsOpen: false });
|
148 | } },
|
149 | toggle
|
150 | ) : toggle
|
151 | );
|
152 | };
|
153 |
|
154 | return _this;
|
155 | }
|
156 |
|
157 | return Toggle;
|
158 | }(_react.PureComponent);
|
159 |
|
160 | Toggle.defaultProps = {
|
161 | disabled: false,
|
162 | value: false,
|
163 | hasText: false,
|
164 | loading: false,
|
165 | confirmWidth: '184'
|
166 | };
|
167 | Toggle.propTypes = {
|
168 | |
169 |
|
170 |
|
171 | name: _propTypes2.default.string,
|
172 | |
173 |
|
174 |
|
175 | value: _propTypes2.default.bool,
|
176 | |
177 |
|
178 |
|
179 | label: _propTypes2.default.string,
|
180 | |
181 |
|
182 |
|
183 | disabled: _propTypes2.default.bool,
|
184 | |
185 |
|
186 |
|
187 | optClass: _propTypes2.default.string,
|
188 | |
189 |
|
190 |
|
191 | changeCallback: _propTypes2.default.func,
|
192 | |
193 |
|
194 |
|
195 | hasText: _propTypes2.default.bool,
|
196 | |
197 |
|
198 |
|
199 | loading: _propTypes2.default.bool,
|
200 | |
201 |
|
202 |
|
203 | confirm: _propTypes2.default.oneOf(['on', 'off', 'both']),
|
204 | |
205 |
|
206 |
|
207 | confirmText: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]),
|
208 | |
209 |
|
210 |
|
211 | confirmWidth: _propTypes2.default.string
|
212 | };
|
213 | exports.default = Toggle; |
\ | No newline at end of file |