UNPKG

@evg-b/evg-ui

Version:

EVG-UI library inspired by Material Design.

159 lines (145 loc) 5.58 kB
import _extends from '@babel/runtime/helpers/extends'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import React, { useRef, useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import Elevation from '../styles/Elevation.js'; import withStyles from '../styles/withStyles.js'; import '@babel/runtime/helpers/construct'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/classCallCheck'; import '@babel/runtime/helpers/createClass'; import IconButton from '../IconButton/IconButton.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var styles = { base: { position: 'relative', display: 'inline-flex', justifyContent: 'center', alignItems: 'center', '--evg-switch-thumb-color': function evgSwitchThumbColor(props) { return props.color === 'default' ? '#FFFFFF' : props.Color.Base(); }, '--evg-switch-track-color': function evgSwitchTrackColor(props) { return props.color === 'default' ? '#000000' : props.Color.Base(); }, '&[disabled] > $track, &[disabled] > $thumb > $thumbIcon': { cursor: 'default', pointerEvents: ' none', backgroundColor: '#bdbdbd' } }, input: { opacity: 0, cursor: 'inherit', position: 'absolute', top: 0, left: 0, margin: 0, padding: 0, '&:checked ~ $thumbIcon': { backgroundColor: 'var(--evg-switch-thumb-color)' } }, thumb: { transform: 'translateX(-10px)', transition: 'transform 100ms cubic-bezier(0.4, 0, 0.2, 1) 20ms' }, thumbChecked: { transform: 'translateX(10px)' }, thumbIcon: _objectSpread({ width: '20px', height: '20px', backgroundColor: '#fff', borderRadius: '50%', boxSizing: 'border-box' }, Elevation(1)), track: { position: 'absolute', width: '32px', height: '14px', backgroundColor: '#000000', borderRadius: '7px', opacity: '.38', boxSizing: 'border-box' }, active: { backgroundColor: 'var(--evg-switch-track-color)' } }; /** * Включает или выключает состояние отдельного параметра настроек. */ var Switch = /*#__PURE__*/React.forwardRef(function Switch(props, ref) { var classes = props.classes, className = props.className; props.children; var onChange = props.onChange, color = props.color, otherProps = _objectWithoutProperties(props, ["classes", "className", "children", "onChange", "color"]); var Switch_ref = useRef(); Switch_ref = ref || Switch_ref; var _useState = useState(otherProps.defaultChecked || otherProps.checked || false), _useState2 = _slicedToArray(_useState, 2), isActive = _useState2[0], SetIsActive = _useState2[1]; useEffect(function () { SetIsActive(Switch_ref.current.checked); }, []); var onChangeChecked = function onChangeChecked(e) { onChange && onChange(e); SetIsActive(Switch_ref.current.checked); }; return /*#__PURE__*/React.createElement("div", { className: classNames(classes.base, className), disabled: otherProps.disabled }, /*#__PURE__*/React.createElement("span", { className: classNames(classes.track, _defineProperty({}, classes.active, isActive)) }), /*#__PURE__*/React.createElement(IconButton, { tabIndex: -1, component: "label", color: color, disabled: otherProps.disabled, className: classNames(classes.thumb, _defineProperty({}, classes.thumbChecked, isActive)) }, /*#__PURE__*/React.createElement("input", _extends({ type: "checkbox", className: classNames(classes.input), ref: Switch_ref, onChange: onChangeChecked, readOnly: true }, otherProps)), /*#__PURE__*/React.createElement("span", { className: classes.thumbIcon }))); }); Switch.propTypes = { /** * Объект содержит jss стили компонента. */ classes: PropTypes.object, /** * Чтобы указать CSS классы, используйте этот атрибут. */ className: PropTypes.string, /** * Это свойство не реализуется. */ children: PropTypes.any, /** * Название цвета в разных форматах. */ color: PropTypes.string, /** * Вызывается при изменении состояния. */ onChange: PropTypes.func }; Switch.defaultProps = { color: 'default' }; Switch.displayName = 'SwitchEVG'; var Switch$1 = withStyles(styles)(Switch); export default Switch$1;