UNPKG

@elastic/eui

Version:

Elastic UI Component Library

138 lines (137 loc) 6.27 kB
var _excluded = ["label", "id", "checked", "disabled", "compressed", "mini", "onChange", "className", "showLabel", "type", "labelProps"]; 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 n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.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.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { useCallback } from 'react'; import PropTypes from "prop-types"; import classNames from 'classnames'; import { useGeneratedHtmlId, useEuiMemoizedStyles } from '../../../services'; import { EuiIcon } from '../../icon'; import { euiSwitchStyles } from './switch.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiSwitch = function EuiSwitch(_ref) { var label = _ref.label, id = _ref.id, checked = _ref.checked, disabled = _ref.disabled, compressed = _ref.compressed, mini = _ref.mini, onChange = _ref.onChange, className = _ref.className, _ref$showLabel = _ref.showLabel, showLabel = _ref$showLabel === void 0 ? true : _ref$showLabel, _ref$type = _ref.type, type = _ref$type === void 0 ? 'button' : _ref$type, labelProps = _ref.labelProps, rest = _objectWithoutProperties(_ref, _excluded); var switchId = useGeneratedHtmlId({ conditionalId: id }); var labelId = useGeneratedHtmlId({ conditionalId: labelProps === null || labelProps === void 0 ? void 0 : labelProps.id }); var onClick = useCallback(function (e) { if (disabled) { return; } var event = e; event.target.checked = !checked; onChange(event); }, [checked, disabled, onChange]); var classes = classNames('euiSwitch', className); var labelClasses = classNames('euiSwitch__label', labelProps === null || labelProps === void 0 ? void 0 : labelProps.className); if (showLabel === false && typeof label !== 'string') { console.warn('EuiSwitch `label` must be a string when `showLabel` is false.'); } var size = mini ? 'mini' : compressed ? 'compressed' : 'uncompressed'; var styles = useEuiMemoizedStyles(euiSwitchStyles); var cssStyles = [styles.euiSwitch, disabled ? styles.disabled : styles.enabled]; var buttonStyles = [styles.button.euiSwitch__button, styles.button[size]]; var bodyStyles = [styles.body.euiSwitch__body, disabled ? styles.body.disabled[size] : checked ? styles.body.on : styles.body.off]; var iconsStyles = [styles.icons.euiSwitch__icons, checked ? styles.icons.on : styles.icons.off, disabled ? styles.icons.disabled : styles.icons.enabled]; var thumbStyles = [styles.thumb.euiSwitch__thumb, !disabled && [styles.thumb.enabled.enabled, styles.thumb.enabled[size]], // keep checked styles after enabled styles to ensure checked overrides enabled.off state checked ? styles.thumb.on[size] : styles.thumb.off, disabled && [styles.thumb.disabled.disabled, styles.thumb.disabled[size]]]; var labelStyles = [styles.label.euiSwitch__label, styles.label[size], disabled && styles.label.disabled, labelProps === null || labelProps === void 0 ? void 0 : labelProps.css]; return ___EmotionJSX("div", { css: cssStyles, className: classes }, ___EmotionJSX("button", _extends({ id: switchId, "aria-checked": checked || false, css: buttonStyles, className: "euiSwitch__button", role: "switch", type: type, disabled: disabled, onClick: onClick, "aria-label": showLabel ? undefined : label, "aria-labelledby": showLabel ? labelId : undefined }, rest), ___EmotionJSX("span", { css: bodyStyles, className: "euiSwitch__body" }, !(compressed || mini) && ___EmotionJSX("span", { css: iconsStyles, className: "euiSwitch__icons" }, ___EmotionJSX(EuiIcon, { type: "check", size: "m" }), ___EmotionJSX(EuiIcon, { type: "cross", size: "m" }))), ___EmotionJSX("span", { css: thumbStyles, className: "euiSwitch__thumb" })), showLabel && // <button> + <label> has poor screen reader support. // Click handler added to simulate natural, secondary <label> interactivity. // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions ___EmotionJSX("span", _extends({}, labelProps, { css: labelStyles, className: labelClasses, id: labelId, onClick: onClick }), label)); }; EuiSwitch.propTypes = { className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any, /** * Whether to render the text label */ showLabel: PropTypes.bool, /** * Must be a string if `showLabel` prop is false */ label: PropTypes.oneOfType([PropTypes.node.isRequired, PropTypes.string.isRequired]).isRequired, checked: PropTypes.bool.isRequired, onChange: PropTypes.func.isRequired, disabled: PropTypes.bool, /** * Compressed switches are smaller and contain no icon signifiers */ compressed: PropTypes.bool, /** * Object of props passed to the label's `<span />` */ labelProps: PropTypes.shape({ className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any }), /** * Mini styling is similar to compressed, but even smaller. * It's undocumented because it has very specific uses. */ mini: PropTypes.bool };