1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 |
|
7 | var _class, _temp;
|
8 |
|
9 | import classNames from 'classnames';
|
10 | import React from 'react';
|
11 | import PropTypes from 'prop-types';
|
12 | import { polyfill } from 'react-lifecycles-compat';
|
13 | import { KEYCODE } from '../util';
|
14 | import Icon from '../icon';
|
15 | import ConfigProvider from '../config-provider';
|
16 | import zhCN from '../locale/zh-cn';
|
17 |
|
18 |
|
19 | var Switch = (_temp = _class = function (_React$Component) {
|
20 | _inherits(Switch, _React$Component);
|
21 |
|
22 | function Switch(props, context) {
|
23 | _classCallCheck(this, Switch);
|
24 |
|
25 | var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
|
26 |
|
27 | var checked = props.checked || props.defaultChecked;
|
28 | _this.onChange = _this.onChange.bind(_this);
|
29 | _this.onKeyDown = _this.onKeyDown.bind(_this);
|
30 | _this.state = {
|
31 | checked: checked
|
32 | };
|
33 | return _this;
|
34 | }
|
35 |
|
36 | Switch.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
|
37 | if ('checked' in props && props.checked !== state.checked) {
|
38 | return {
|
39 | checked: !!props.checked
|
40 | };
|
41 | }
|
42 |
|
43 | return null;
|
44 | };
|
45 |
|
46 | Switch.prototype.onChange = function onChange(ev) {
|
47 | var checked = !this.state.checked;
|
48 |
|
49 | if (!('checked' in this.props)) {
|
50 | this.setState({
|
51 | checked: checked
|
52 | });
|
53 | }
|
54 | this.props.onChange(checked, ev);
|
55 | this.props.onClick && this.props.onClick(ev);
|
56 | };
|
57 |
|
58 | Switch.prototype.onKeyDown = function onKeyDown(e) {
|
59 | if (e.keyCode === KEYCODE.ENTER || e.keyCode === KEYCODE.SPACE) {
|
60 | this.onChange(e);
|
61 | }
|
62 | this.props.onKeyDown && this.props.onKeyDown(e);
|
63 | };
|
64 |
|
65 | Switch.prototype.render = function render() {
|
66 | var _classNames;
|
67 |
|
68 | var _props = this.props,
|
69 | prefix = _props.prefix,
|
70 | className = _props.className,
|
71 | disabled = _props.disabled,
|
72 | readOnly = _props.readOnly,
|
73 | size = _props.size,
|
74 | loading = _props.loading,
|
75 | autoWidth = _props.autoWidth,
|
76 | checkedChildren = _props.checkedChildren,
|
77 | unCheckedChildren = _props.unCheckedChildren,
|
78 | rtl = _props.rtl,
|
79 | isPreview = _props.isPreview,
|
80 | renderPreview = _props.renderPreview,
|
81 | locale = _props.locale,
|
82 | others = _objectWithoutProperties(_props, ['prefix', 'className', 'disabled', 'readOnly', 'size', 'loading', 'autoWidth', 'checkedChildren', 'unCheckedChildren', 'rtl', 'isPreview', 'renderPreview', 'locale']);
|
83 |
|
84 | var checked = this.state.checked;
|
85 |
|
86 | var status = checked ? 'on' : 'off';
|
87 | var children = checked ? checkedChildren : unCheckedChildren;
|
88 |
|
89 | var _size = size;
|
90 | if (_size !== 'small' && _size !== 'medium') {
|
91 | _size = 'medium';
|
92 | }
|
93 |
|
94 | var classes = classNames((_classNames = {}, _classNames[prefix + 'switch'] = true, _classNames[prefix + 'switch-loading'] = loading, _classNames[prefix + 'switch-' + status] = true, _classNames[prefix + 'switch-' + _size] = true, _classNames[prefix + 'switch-auto-width'] = autoWidth, _classNames[className] = className, _classNames));
|
95 | var attrs = void 0;
|
96 | var isDisabled = disabled || readOnly;
|
97 |
|
98 | if (!isDisabled) {
|
99 | attrs = {
|
100 | onClick: this.onChange,
|
101 | tabIndex: 0,
|
102 | onKeyDown: this.onKeyDown,
|
103 | disabled: false
|
104 | };
|
105 | } else {
|
106 | attrs = {
|
107 | disabled: true
|
108 | };
|
109 | }
|
110 |
|
111 | if (isPreview) {
|
112 | var _classNames2;
|
113 |
|
114 | var previewCls = classNames(className, (_classNames2 = {}, _classNames2[prefix + 'form-preview'] = true, _classNames2));
|
115 |
|
116 | if ('renderPreview' in this.props) {
|
117 | return React.createElement(
|
118 | 'div',
|
119 | _extends({ className: previewCls }, others),
|
120 | renderPreview(checked, this.props)
|
121 | );
|
122 | }
|
123 |
|
124 | return React.createElement(
|
125 | 'p',
|
126 | _extends({ className: previewCls }, others),
|
127 | children || locale[status]
|
128 | );
|
129 | }
|
130 |
|
131 | return React.createElement(
|
132 | 'div',
|
133 | _extends({
|
134 | role: 'switch',
|
135 | dir: rtl ? 'rtl' : undefined,
|
136 | tabIndex: '0'
|
137 | }, others, {
|
138 | className: classes
|
139 | }, attrs, {
|
140 | 'aria-checked': checked
|
141 | }),
|
142 | React.createElement(
|
143 | 'div',
|
144 | { className: prefix + 'switch-btn' },
|
145 | loading && React.createElement(Icon, { type: 'loading', className: prefix + 'switch-inner-icon' })
|
146 | ),
|
147 | React.createElement(
|
148 | 'div',
|
149 | { className: prefix + 'switch-children' },
|
150 | children
|
151 | )
|
152 | );
|
153 | };
|
154 |
|
155 | return Switch;
|
156 | }(React.Component), _class.contextTypes = {
|
157 | prefix: PropTypes.string
|
158 | }, _class.propTypes = {
|
159 | prefix: PropTypes.string,
|
160 | rtl: PropTypes.bool,
|
161 | pure: PropTypes.bool,
|
162 | |
163 |
|
164 |
|
165 | className: PropTypes.string,
|
166 | |
167 |
|
168 |
|
169 | style: PropTypes.object,
|
170 | |
171 |
|
172 |
|
173 | checkedChildren: PropTypes.any,
|
174 | |
175 |
|
176 |
|
177 | unCheckedChildren: PropTypes.any,
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 | onChange: PropTypes.func,
|
184 | |
185 |
|
186 |
|
187 | checked: PropTypes.bool,
|
188 | |
189 |
|
190 |
|
191 | defaultChecked: PropTypes.bool,
|
192 | |
193 |
|
194 |
|
195 | disabled: PropTypes.bool,
|
196 | |
197 |
|
198 |
|
199 | loading: PropTypes.bool,
|
200 | |
201 |
|
202 |
|
203 |
|
204 | size: PropTypes.oneOf(['medium', 'small']),
|
205 | |
206 |
|
207 |
|
208 |
|
209 | onClick: PropTypes.func,
|
210 | |
211 |
|
212 |
|
213 |
|
214 | onKeyDown: PropTypes.func,
|
215 | |
216 |
|
217 |
|
218 | isPreview: PropTypes.bool,
|
219 | |
220 |
|
221 |
|
222 |
|
223 | renderPreview: PropTypes.func,
|
224 | |
225 |
|
226 |
|
227 |
|
228 | autoWidth: PropTypes.bool,
|
229 | |
230 |
|
231 |
|
232 | locale: PropTypes.object
|
233 | }, _class.defaultProps = {
|
234 | prefix: 'next-',
|
235 | size: 'medium',
|
236 | disabled: false,
|
237 | defaultChecked: false,
|
238 | isPreview: false,
|
239 | loading: false,
|
240 | readOnly: false,
|
241 | autoWidth: false,
|
242 | onChange: function onChange() {},
|
243 | locale: zhCN.Switch
|
244 | }, _temp);
|
245 | Switch.displayName = 'Switch';
|
246 |
|
247 |
|
248 | export default ConfigProvider.config(polyfill(Switch)); |
\ | No newline at end of file |