UNPKG

@geezee/react-ui

Version:

Modern and minimalist React UI library.

89 lines (78 loc) 6.56 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _JSXStyle from "styled-jsx/style"; import React, { useMemo } from 'react'; import withDefaults from '../utils/with-defaults'; import Check from '@zeit-ui/react-icons/check'; import useTheme from '../styles/use-theme'; import { useSelectContext } from './select-context'; import { getSizes, getOptionColors } from './styles'; import useWarning from '../utils/use-warning'; import Ellipsis from '../shared/ellipsis'; var defaultProps = { disabled: false, divider: false, label: false, className: '', preventAllEvents: false }; var SelectOption = function SelectOption(_ref) { var identValue = _ref.value, className = _ref.className, children = _ref.children, disabled = _ref.disabled, divider = _ref.divider, label = _ref.label, preventAllEvents = _ref.preventAllEvents, props = _objectWithoutProperties(_ref, ["value", "className", "children", "disabled", "divider", "label", "preventAllEvents"]); var theme = useTheme(); var _useSelectContext = useSelectContext(), updateValue = _useSelectContext.updateValue, value = _useSelectContext.value, disableAll = _useSelectContext.disableAll, variant = _useSelectContext.variant, size = _useSelectContext.size; var isDisabled = useMemo(function () { return disabled || disableAll; }, [disabled, disableAll]); var isLabel = useMemo(function () { return label || divider; }, [label, divider]); if (!isLabel && identValue === undefined) { useWarning('The props "value" is required.', 'Select Option'); } var sizes = useMemo(function () { return getSizes(theme, size); }, [theme, size]); var selected = useMemo(function () { if (!value) return false; if (typeof value === 'string') { return identValue === value; } return value.includes("".concat(identValue)); }, [identValue, value]); var colors = useMemo(function () { return getOptionColors(selected, isDisabled, theme.palette, isLabel, variant); }, [selected, isDisabled, theme.palette, isLabel]); var clickHandler = function clickHandler(event) { if (preventAllEvents) return; event.stopPropagation(); event.nativeEvent.stopImmediatePropagation(); event.preventDefault(); if (isDisabled || isLabel) return; updateValue && updateValue(identValue); }; return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _extends({ onClick: clickHandler }, props, { className: _JSXStyle.dynamic([["2243187574", [sizes.fontSize, theme.layout.gap, theme.layout.gap, colors.bgColor, colors.color, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, colors.border, isDisabled ? 'not-allowed' : 'pointer', colors.hoverBgColor, colors.hoverColor, colors.hoverBorder, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, theme.layout.gapHalf, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, sizes.labelFontSize, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border]]]) + " " + (props && props.className != null && props.className || "option ".concat(selected ? 'selected' : '', " ").concat(disabled ? 'disabled' : '', " ").concat(divider ? 'divider' : '', " ").concat(label ? 'label' : '', " ").concat(className)) }), /*#__PURE__*/React.createElement(Ellipsis, { height: "calc(2.25 * ".concat(theme.layout.gap, ")") }, children), selected && /*#__PURE__*/React.createElement(Check, { size: 18 })), /*#__PURE__*/React.createElement(_JSXStyle, { id: "2243187574", dynamic: [sizes.fontSize, theme.layout.gap, theme.layout.gap, colors.bgColor, colors.color, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, colors.border, isDisabled ? 'not-allowed' : 'pointer', colors.hoverBgColor, colors.hoverColor, colors.hoverBorder, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, theme.layout.gapHalf, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, sizes.labelFontSize, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border, theme.expressiveness.L1, theme.expressiveness.cLineStyle1, theme.palette.border] }, ".option.__jsx-style-dynamic-selector{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;max-width:100%;-webkit-box-pack:justify;-webkit-justify-content:space-between;-ms-flex-pack:justify;justify-content:space-between;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;font-weight:500;font-size:".concat(sizes.fontSize, ";height:calc(2.5 * ").concat(theme.layout.gap, ");box-sizing:border-box;padding:0 ").concat(theme.layout.gap, ";background-color:").concat(colors.bgColor, ";color:").concat(colors.color, ";-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;border:").concat(theme.expressiveness.L1, " ").concat(theme.expressiveness.cLineStyle1, " ").concat(colors.border, ";cursor:").concat(isDisabled ? 'not-allowed' : 'pointer', ";-webkit-transition:background 0.2s ease 0s,border-color 0.2s ease 0s;transition:background 0.2s ease 0s,border-color 0.2s ease 0s;}.option.__jsx-style-dynamic-selector:hover{background-color:").concat(colors.hoverBgColor, ";color:").concat(colors.hoverColor, ";border:1px solid ").concat(colors.hoverBorder, ";}.divider.__jsx-style-dynamic-selector{line-height:0;height:0;padding:0;overflow:hidden;border-top:").concat(theme.expressiveness.L1, " ").concat(theme.expressiveness.cLineStyle1, " ").concat(theme.palette.border, ";margin:").concat(theme.layout.gapHalf, " 0;width:100%;}.divider.__jsx-style-dynamic-selector:hover{border-top:").concat(theme.expressiveness.L1, " ").concat(theme.expressiveness.cLineStyle1, " ").concat(theme.palette.border, ";}.label.__jsx-style-dynamic-selector{font-size:").concat(sizes.labelFontSize, ";border-bottom:").concat(theme.expressiveness.L1, " ").concat(theme.expressiveness.cLineStyle1, " ").concat(theme.palette.border, ";text-transform:capitalize;cursor:default;}.label.__jsx-style-dynamic-selector:hover{border-bottom:").concat(theme.expressiveness.L1, " ").concat(theme.expressiveness.cLineStyle1, " ").concat(theme.palette.border, ";}"))); }; export default withDefaults(SelectOption, defaultProps);