UNPKG

@txdfe/at

Version:

一个设计体系组件库

176 lines (175 loc) 8.42 kB
var _excluded = ["prefix", "className", "loading", "disabled", "size", "rtl"]; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import classNames from 'classnames'; import React from 'react'; import PropTypes from 'prop-types'; import Icon from '../icon'; import { KEYCODE } from '../util'; import ConfigProvider from '../config-provider'; /** Switch */ var Switch = /*#__PURE__*/function (_React$Component) { function Switch(props, context) { var _this; _classCallCheck(this, Switch); _this = _callSuper(this, Switch, [props, context]); _defineProperty(_this, "onChange", function (ev) { var checked = !_this.state.checked; if (!('checked' in _this.props)) { _this.setState({ checked: checked }); } _this.props.onChange(checked, ev); _this.props.onClick && _this.props.onClick(ev); }); _defineProperty(_this, "onKeyDown", function (e) { if (e.keyCode === KEYCODE.ENTER || e.keyCode === KEYCODE.SPACE) { _this.onChange(e); } _this.props.onKeyDown && _this.props.onKeyDown(e); }); var _checked = props.checked || props.defaultChecked; _this.state = { checked: _checked }; return _this; } _inherits(Switch, _React$Component); return _createClass(Switch, [{ key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if ('checked' in nextProps) { var checked = nextProps.checked; if (checked === undefined) { checked = false; } this.setState({ checked: checked }); } } }, { key: "render", value: function render() { /* eslint-disable no-unused-vars */ var _this$props = this.props, prefix = _this$props.prefix, className = _this$props.className, loading = _this$props.loading, disabled = _this$props.disabled, size = _this$props.size, rtl = _this$props.rtl, others = _objectWithoutProperties(_this$props, _excluded); var status = this.state.checked ? 'on' : 'off'; var _size = size; if (_size !== 'small' && _size !== 'medium') { _size = 'medium'; } var classes = classNames(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "switch"), true), "".concat(prefix, "switch-").concat(status), true), "".concat(prefix, "switch-loading"), loading), "".concat(prefix, "switch-").concat(_size), true), className, className)); var attrs; if (!disabled && !loading) { attrs = { onClick: this.onChange, tabIndex: 0, onKeyDown: this.onKeyDown, disabled: false }; } else { attrs = { disabled: true }; } return /*#__PURE__*/React.createElement("div", _extends({ role: "switch", dir: rtl ? 'rtl' : undefined, tabIndex: "0" }, others, { className: classes }, attrs, { "aria-checked": this.state.checked }), loading ? /*#__PURE__*/React.createElement(Icon, { className: "loading-icon", type: "loading" }) : null); } }]); }(React.Component); _defineProperty(Switch, "displayName", 'Switch'); _defineProperty(Switch, "contextTypes", { prefix: PropTypes.string }); _defineProperty(Switch, "propTypes", { prefix: PropTypes.string, rtl: PropTypes.bool, pure: PropTypes.bool, /** * 自定义类名 */ className: PropTypes.string, /** * 自定义内敛样式 */ style: PropTypes.object, /** * 是否在加载中 */ loading: PropTypes.bool, /** * 开关状态改变是触发此事件 * @param {Boolean} checked 是否为打开状态 * @param {Event} e DOM事件对象 */ onChange: PropTypes.func, /** * 开关当前的值(针对受控组件) */ checked: PropTypes.bool, /** * 开关默认值 (针对非受控组件) */ defaultChecked: PropTypes.bool, /** * 表示开关被禁用 */ disabled: PropTypes.bool, /** * switch的尺寸 * @enumdesc 正常大小, 缩小版大小 */ size: PropTypes.oneOf(['medium', 'small']), /** * 鼠标点击事件 * @param {Event} e DOM事件对象 */ onClick: PropTypes.func, /** * 键盘按键事件 * @param {Event} e DOM事件对象 */ onKeyDown: PropTypes.func }); _defineProperty(Switch, "defaultProps", { prefix: 'next-', loading: false, disabled: false, size: 'medium', defaultChecked: false, onChange: function onChange() {} }); export default ConfigProvider.config(Switch);