UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

88 lines (79 loc) 3.87 kB
import _keys from "lodash/keys"; import _noop from "lodash/noop"; function _extends() { _extends = Object.assign || 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React, { createRef } from 'react'; import PropTypes from 'react-peek/prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { omitProps } from '../../util/component-types'; var cx = lucidClassNames.bind('&-Switch'); var bool = PropTypes.bool, func = PropTypes.func, object = PropTypes.object, string = PropTypes.string; var defaultProps = { isDisabled: false, isSelected: false, onSelect: _noop, isIncludeExclude: false }; export var Switch = function Switch(props) { var className = props.className, isDisabled = props.isDisabled, isSelected = props.isSelected, style = props.style, isIncludeExclude = props.isIncludeExclude, onSelect = props.onSelect, passThroughs = _objectWithoutProperties(props, ["className", "isDisabled", "isSelected", "style", "isIncludeExclude", "onSelect"]); var nativeElement = /*#__PURE__*/createRef(); function handleClicked(event) { event.preventDefault(); if (!isDisabled) { onSelect(!isSelected, { event: event, props: props }); if (nativeElement.current) { nativeElement.current.focus(); } } } return /*#__PURE__*/React.createElement("span", { className: cx('&', { '&-is-disabled': isDisabled, '&-is-selected': isSelected, '&-is-include-exclude': isIncludeExclude }, className), onClick: handleClicked, onTouchEnd: handleClicked, style: style }, /*#__PURE__*/React.createElement("input", _extends({ onChange: _noop }, omitProps(passThroughs, undefined, ['children'].concat(_keys(Switch.propTypes))), { checked: isSelected, className: cx('&-native'), disabled: isDisabled, ref: nativeElement, type: "checkbox" })), /*#__PURE__*/React.createElement("span", { className: cx('&-visualization-container') }), /*#__PURE__*/React.createElement("span", { className: cx('&-visualization-handle') })); }; Switch.defaultProps = defaultProps; Switch.displayName = 'Switch'; Switch.peek = { description: "\n\t\t\t\tThis is a toggle -- a component that is in one of two particular states\n\t\t\t\tat any given moment in time -- that uses a visualization of a physical\n\t\t\t\ton/off switch made popular by smartphone OSes to reflect its current\n\t\t\t\tstate.\n\n\t\t\t\tIt uses a hidden native check box control under the hood but leverages\n\t\t\t\tother HTML elements to visualize its state.\n\t\t\t", categories: ['controls', 'toggles'] }; Switch.propTypes = { className: string, isDisabled: bool, isSelected: bool, onSelect: func, style: object, isIncludeExclude: bool }; export default Switch;