UNPKG

@zohodesk/components

Version:

Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development

152 lines (124 loc) • 8.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _defaultProps = require("./props/defaultProps"); var _propTypes = require("./props/propTypes"); var _Layout = require("../Layout"); var _Label = _interopRequireDefault(require("../Label/Label")); var _SwitchModule = _interopRequireDefault(require("./Switch.module.css")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var Switch = /*#__PURE__*/function (_React$Component) { _inherits(Switch, _React$Component); var _super = _createSuper(Switch); function Switch(props) { var _this; _classCallCheck(this, Switch); _this = _super.call(this, props); _this.onChange = _this.onChange.bind(_assertThisInitialized(_this)); return _this; } _createClass(Switch, [{ key: "onChange", value: function onChange(e) { var _this$props = this.props, onChange = _this$props.onChange, checked = _this$props.checked; onChange && onChange(!checked, e); } }, { key: "render", value: function render() { var _this$props2 = this.props, id = _this$props2.id, name = _this$props2.name, value = _this$props2.value, checked = _this$props2.checked, disabled = _this$props2.disabled, isReadOnly = _this$props2.isReadOnly, size = _this$props2.size, text = _this$props2.text, labelSize = _this$props2.labelSize, labelPalette = _this$props2.labelPalette, title = _this$props2.title, disableTitle = _this$props2.disableTitle, dataId = _this$props2.dataId, dataSelectorId = _this$props2.dataSelectorId, customClass = _this$props2.customClass, customProps = _this$props2.customProps; var _customProps$SwitchPr = customProps.SwitchProps, SwitchProps = _customProps$SwitchPr === void 0 ? {} : _customProps$SwitchPr, _customProps$LabelPro = customProps.LabelProps, LabelProps = _customProps$LabelPro === void 0 ? {} : _customProps$LabelPro; var _customClass$customSw = customClass.customSwitchWrap, customSwitchWrap = _customClass$customSw === void 0 ? '' : _customClass$customSw, _customClass$customSw2 = customClass.customSwitch, customSwitch = _customClass$customSw2 === void 0 ? '' : _customClass$customSw2, _customClass$customSw3 = customClass.customSwitchSize, customSwitchSize = _customClass$customSw3 === void 0 ? '' : _customClass$customSw3, _customClass$customLa = customClass.customLabel, customLabel = _customClass$customLa === void 0 ? '' : _customClass$customLa; size !== 'small' ? size = 'switch_medium' : size = 'switch_small'; return /*#__PURE__*/_react["default"].createElement(_Layout.Container, _extends({ align: "vertical", isCover: false, alignBox: "row", isInline: true, className: "".concat(_SwitchModule["default"].container, " ").concat(disabled ? _SwitchModule["default"].disabled : isReadOnly ? _SwitchModule["default"].readonly : _SwitchModule["default"].effect, " ").concat(customSwitchWrap), "data-title": disabled ? disableTitle : title, "aria-checked": checked, role: "switch", dataSelectorId: dataSelectorId }, SwitchProps), /*#__PURE__*/_react["default"].createElement(_Layout.Box, { className: "".concat(_SwitchModule["default"][size], " ").concat(customSwitchSize) }, /*#__PURE__*/_react["default"].createElement("input", { type: "checkbox", id: id, className: "".concat(_SwitchModule["default"].input, " ").concat(checked ? _SwitchModule["default"].checked : ''), name: name, value: value, checked: checked, disabled: disabled, readOnly: isReadOnly, onClick: !disabled || !isReadOnly ? this.onChange : null }), /*#__PURE__*/_react["default"].createElement("label", { htmlFor: id, "data-id": dataId, "data-test-id": dataId, className: "".concat(_SwitchModule["default"].label, " ").concat(_SwitchModule["default"]["".concat(size, "Label")], " ").concat(customSwitch), tabIndex: isReadOnly || disabled ? '-1' : '0' })), text && /*#__PURE__*/_react["default"].createElement(_Label["default"], _extends({ text: text, palette: labelPalette, id: id, size: labelSize, type: "subtitle", onClick: !disabled || !isReadOnly ? this.onChange : null, customClass: customLabel }, LabelProps))); } }]); return Switch; }(_react["default"].Component); exports["default"] = Switch; Switch.defaultProps = _defaultProps.defaultProps; Switch.propTypes = _propTypes.propTypes; // if (__DOCS__) { // Switch.docs = { // componentGroup: 'Form Elements', // folderName: 'Style Guide' // }; // }