UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

303 lines (293 loc) 10.3 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _extends from "@babel/runtime/helpers/esm/extends"; const _excluded = ["prefix", "theme", "size", "filled", "disabled"]; import * as React from "react"; import styled, { css } from "styled-components"; import defaultTheme from "../defaultTheme"; import FormLabel from "../FormLabel"; import ChevronDown from "../icons/ChevronDown"; import ErrorFormTooltip from "../ErrorFormTooltip"; import SIZE_OPTIONS from "./consts"; import { right, left, rtlSpacing } from "../utils/rtl"; import getSpacingToken from "../common/getSpacingToken"; import getFieldDataState from "../common/getFieldDataState"; import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip"; import formElementFocus from "../InputField/helpers/formElementFocus"; import mq from "../utils/mediaQuery"; const getSelectSize = ({ theme, size }) => { const tokens = { [SIZE_OPTIONS.SMALL]: theme.orbit.heightInputSmall, [SIZE_OPTIONS.NORMAL]: theme.orbit.heightInputNormal }; return tokens[size]; }; const Label = styled.label.withConfig({ displayName: "Select__Label", componentId: "sc-19y5dzg-0" })(["", ""], ({ $width }) => css(["position:relative;display:block;width:", ";margin-bottom:", ";"], $width, getSpacingToken)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Label.defaultProps = { theme: defaultTheme }; const StyledSelect = styled( /*#__PURE__*/React.forwardRef(({ className, dataTest, children, value, disabled, name, error, tabIndex, onChange, onFocus, onBlur, id, dataAttrs, readOnly }, ref) => /*#__PURE__*/React.createElement("select", _extends({ id: id, "data-test": dataTest, "data-state": getFieldDataState(error), value: value, className: className, onChange: onChange, onFocus: onFocus, readOnly: readOnly, onBlur: onBlur, disabled: disabled, name: name, tabIndex: tabIndex, ref: ref }, dataAttrs), children))).withConfig({ displayName: "Select__StyledSelect", componentId: "sc-19y5dzg-1" })(["appearance:none;background:", ";cursor:pointer;color:", ";font-family:", ";font-size:", ";height:", ";padding:", ";outline:none;width:100%;color:", ";transition:box-shadow ", " ease-in-out;z-index:2;border-radius:6px;", ";> option{color:", ";}&::-ms-expand{display:none;}&::-ms-value{background:transparent;color:", ";color:", ";}border:0;box-shadow:inset 0 0 0 ", ";&:hover{box-shadow:inset 0 0 0 ", ";}&:focus{", "}&:disabled{color:", ";background:", ";cursor:not-allowed;&:hover{box-shadow:inset 0 0 0 1px ", ";}}", ""], ({ theme }) => theme.orbit.backgroundInput, ({ theme, filled }) => filled ? theme.orbit.colorTextInput : theme.orbit.colorPlaceholderInput, ({ theme }) => theme.orbit.fontFamily, ({ theme, size }) => size === SIZE_OPTIONS.SMALL ? theme.orbit.fontSizeInputSmall : theme.orbit.fontSizeInputNormal, getSelectSize, ({ theme, size, prefix }) => rtlSpacing(size === SIZE_OPTIONS.SMALL && !prefix && `0 ${theme.orbit.spaceXLarge} 0 ${theme.orbit.spaceSmall}` || size === SIZE_OPTIONS.SMALL && prefix && `0 ${theme.orbit.spaceXLarge} 0 ${theme.orbit.paddingLeftSelectPrefix}` || prefix && `0 ${theme.orbit.spaceXXLarge} 0 ${theme.orbit.paddingLeftSelectPrefix}` || `0 ${theme.orbit.spaceXXLarge} 0 ${theme.orbit.spaceSmall}`), ({ customValueText }) => customValueText && "transparent !important", ({ theme }) => theme.orbit.durationFast, mq.tablet(css(["border-radius:", ";"], ({ theme }) => theme.orbit.borderRadiusNormal)), ({ theme }) => theme.orbit.colorTextInput, ({ theme, filled }) => filled ? theme.orbit.colorTextInput : theme.orbit.colorPlaceholderInput, ({ customValueText }) => customValueText && "rgba(255, 255, 255, 0)", ({ theme, error }) => `${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput}`, ({ theme, error }) => `${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover}`, formElementFocus, ({ theme }) => theme.orbit.colorTextInputDisabled, ({ theme }) => theme.orbit.backgroundInputDisabled, ({ theme }) => theme.orbit.borderColorInput, ({ customValueText }) => customValueText && ` &:-webkit-autofill, &:-internal-autofill-selected { -webkit-text-fill-color: transparent; } `); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledSelect.defaultProps = { theme: defaultTheme }; export const SelectContainer = styled.div.withConfig({ displayName: "Select__SelectContainer", componentId: "sc-19y5dzg-2" })(["position:relative;display:flex;align-items:center;background:", ";width:100%;box-sizing:border-box;cursor:pointer;"], ({ theme }) => theme.orbit.backgroundInput); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 SelectContainer.defaultProps = { theme: defaultTheme }; const SelectPrefix = styled.div.withConfig({ displayName: "Select__SelectPrefix", componentId: "sc-19y5dzg-3" })(["display:flex;align-items:center;position:absolute;padding:", ";pointer-events:none;z-index:3;top:0;height:", ";"], ({ theme }) => `0 ${theme.orbit.spaceSmall}`, getSelectSize); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 SelectPrefix.defaultProps = { theme: defaultTheme }; const SelectSuffix = styled.div.withConfig({ displayName: "Select__SelectSuffix", componentId: "sc-19y5dzg-4" })(["position:absolute;display:flex;justify-content:center;align-items:center;top:0;", ":", ";color:", ";pointer-events:none;z-index:3;height:100%;& > *{width:", ";height:", ";margin-bottom:", ";}"], right, ({ theme }) => theme.orbit.spaceXSmall, ({ theme, disabled }) => disabled ? theme.orbit.colorTextInputDisabled : theme.orbit.colorTextInput, ({ theme, size }) => size === SIZE_OPTIONS.SMALL && theme.orbit.widthIconSmall, ({ theme, size }) => size === SIZE_OPTIONS.SMALL && theme.orbit.heightIconSmall, ({ size, theme }) => size === SIZE_OPTIONS.SMALL && theme.orbit.spaceXXXSmall); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 SelectSuffix.defaultProps = { theme: defaultTheme }; const StyledCustomValue = styled(_ref => { let { prefix, theme, size, filled, disabled } = _ref, props = _objectWithoutProperties(_ref, _excluded); return /*#__PURE__*/React.createElement("div", props); }).withConfig({ displayName: "Select__StyledCustomValue", componentId: "sc-19y5dzg-5" })(["color:", ";font-family:", ";font-size:", ";z-index:3;position:absolute;height:100%;line-height:", ";top:0;", ":", ";bottom:0;pointer-events:none;"], ({ theme, filled, disabled }) => disabled && theme.orbit.paletteInkLighter || (filled ? theme.orbit.colorTextInput : theme.orbit.colorPlaceholderInput), ({ theme }) => theme.orbit.fontFamily, ({ theme, size }) => size === SIZE_OPTIONS.SMALL ? theme.orbit.fontSizeInputSmall : theme.orbit.fontSizeInputNormal, getSelectSize, left, ({ prefix, theme }) => prefix ? "48px" : theme.orbit.spaceSmall); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledCustomValue.defaultProps = { theme: defaultTheme }; const Select = /*#__PURE__*/React.forwardRef((props, ref) => { const { size = SIZE_OPTIONS.NORMAL, label, placeholder, value, disabled = false, error, help, name, onChange, onBlur, onFocus, width = "100%", options, tabIndex, id, required, helpClosable = true, dataTest, prefix, spaceAfter, customValueText, insideInputGroup, dataAttrs, readOnly } = props; const filled = !(value == null || value === ""); const { tooltipShown, tooltipShownHover, setTooltipShownHover, labelRef, iconRef, setTooltipShown, handleFocus } = useErrorTooltip({ onFocus }); const inputRef = React.useRef(null); const shown = tooltipShown || tooltipShownHover; return /*#__PURE__*/React.createElement(Label, { spaceAfter: spaceAfter, ref: inputRef, $width: width }, label && /*#__PURE__*/React.createElement(FormLabel, { filled: !!filled, error: !!error, help: !!help, disabled: disabled, labelRef: labelRef, iconRef: iconRef, onMouseEnter: () => setTooltipShownHover(true), onMouseLeave: () => setTooltipShownHover(false), required: required }, label), /*#__PURE__*/React.createElement(SelectContainer, { ref: label ? null : labelRef }, prefix && /*#__PURE__*/React.createElement(SelectPrefix, { prefix: prefix, size: size }, prefix), customValueText && /*#__PURE__*/React.createElement(StyledCustomValue, { disabled: disabled, filled: filled, size: size, prefix: prefix }, customValueText), /*#__PURE__*/React.createElement(StyledSelect, { dataTest: dataTest, size: size, disabled: disabled, error: error, value: value == null ? "" : value, prefix: prefix, name: name, onFocus: handleFocus, onBlur: onBlur, onChange: onChange, filled: filled, customValueText: customValueText, tabIndex: tabIndex, id: id, readOnly: readOnly, required: required, ref: ref, dataAttrs: dataAttrs }, placeholder && /*#__PURE__*/React.createElement("option", { label: placeholder, value: "" }, placeholder), options.map(option => /*#__PURE__*/React.createElement("option", { key: `option-${option.key || option.value}`, value: option.value, disabled: option.disabled }, option.label))), /*#__PURE__*/React.createElement(SelectSuffix, { size: size, disabled: disabled }, /*#__PURE__*/React.createElement(ChevronDown, { color: "secondary" }))), !insideInputGroup && /*#__PURE__*/React.createElement(ErrorFormTooltip, { help: help, error: error, inputSize: size, helpClosable: helpClosable, shown: shown, onShown: setTooltipShown, referenceElement: inputRef })); }); // otherwise Unknown in storybook Select.displayName = "Select"; export default Select;