@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.
266 lines (259 loc) • 9.19 kB
JavaScript
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 * as React from "react";
import styled from "styled-components";
import defaultTheme from "../defaultTheme";
import FormLabel from "../FormLabel";
import ChevronDown from "../icons/ChevronDown";
import FormFeedback from "../FormFeedback";
import TYPE_OPTIONS from "../FormFeedback/consts";
import SIZE_OPTIONS from "./consts";
import { right, left, rtlSpacing } from "../utils/rtl";
import getSpacingToken from "../common/getSpacingToken";
import getFieldDataState from "../common/getFieldDataState";
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-6agypz-0"
})(["position:relative;display:block;width:100%;margin-bottom:", ";"], getSpacingToken);
Label.defaultProps = {
theme: defaultTheme
};
const StyledSelect = styled( // $FlowExpected
React.forwardRef(({
className,
dataTest,
children,
value,
disabled,
name,
error,
tabIndex,
onChange,
onFocus,
onBlur,
id
}, ref) => React.createElement("select", {
id: id,
"data-test": dataTest,
"data-state": getFieldDataState(error),
value: value,
className: className,
onChange: onChange,
onFocus: onFocus,
onBlur: onBlur,
disabled: disabled,
name: name,
tabIndex: tabIndex,
ref: ref
}, children))).withConfig({
displayName: "Select__StyledSelect",
componentId: "sc-6agypz-1"
})(["appearance:none;background:", ";border-radius:", ";cursor:pointer;color:", ";font-family:", ";font-size:", ";height:", ";padding:", ";outline:none;width:100%;transition:box-shadow ", " ease-in-out;z-index:2;color:", ";> 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{box-shadow:inset 0 0 0 ", ";}&:disabled{color:", ";background:", ";cursor:not-allowed;&:hover{box-shadow:inset 0 0 0 1px ", ";}}"], ({
theme
}) => theme.orbit.backgroundInput, ({
theme
}) => theme.orbit.borderRadiusNormal, ({
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}`), ({
theme
}) => theme.orbit.durationFast, ({
customValueText
}) => customValueText && "transparent", ({
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}`, ({
theme
}) => `${theme.orbit.borderWidthInputFocus} ${theme.orbit.borderColorInputFocus}`, ({
theme
}) => theme.orbit.colorTextInputDisabled, ({
theme
}) => theme.orbit.backgroundInputDisabled, ({
theme
}) => theme.orbit.borderColorInput);
StyledSelect.defaultProps = {
theme: defaultTheme
};
export const SelectContainer = styled(({
className,
children
}) => React.createElement("div", {
className: className
}, children)).withConfig({
displayName: "Select__SelectContainer",
componentId: "sc-6agypz-2"
})(["position:relative;display:flex;align-items:center;background:", ";width:100%;box-sizing:border-box;cursor:pointer;"], ({
theme
}) => theme.orbit.backgroundInput);
SelectContainer.defaultProps = {
theme: defaultTheme
};
const SelectPrefix = styled(({
className,
children
}) => React.createElement("div", {
className: className
}, children)).withConfig({
displayName: "Select__SelectPrefix",
componentId: "sc-6agypz-3"
})(["display:flex;align-items:center;position:absolute;padding:", ";pointer-events:none;z-index:3;top:0;height:", ";"], ({
theme
}) => `0 ${theme.orbit.spaceSmall}`, getSelectSize);
SelectPrefix.defaultProps = {
theme: defaultTheme
};
const SelectSuffix = styled(({
children,
className
}) => React.createElement("div", {
className: className
}, children)).withConfig({
displayName: "Select__SelectSuffix",
componentId: "sc-6agypz-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);
SelectSuffix.defaultProps = {
theme: defaultTheme
};
const StyledCustomValue = styled((_ref) => {
let {
prefix,
theme,
size,
filled
} = _ref,
props = _objectWithoutProperties(_ref, ["prefix", "theme", "size", "filled"]);
return React.createElement("div", props);
}).withConfig({
displayName: "Select__StyledCustomValue",
componentId: "sc-6agypz-5"
})(["color:", ";font-family:", ";font-size:", ";z-index:3;position:absolute;height:100%;line-height:", ";top:0;", ":", ";bottom:0;"], ({
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, left, ({
prefix,
theme
}) => prefix ? "48px" : theme.orbit.spaceSmall);
StyledCustomValue.defaultProps = {
theme: defaultTheme
}; // $FlowExpected
const Select = React.forwardRef((props, ref) => {
const {
size = SIZE_OPTIONS.NORMAL,
label,
placeholder,
value,
disabled = false,
error,
help,
name,
onChange,
onBlur,
onFocus,
options,
tabIndex,
id,
required,
dataTest,
prefix,
spaceAfter,
customValueText
} = props;
const filled = !(value == null || value === "");
return React.createElement(Label, {
spaceAfter: spaceAfter
}, label && React.createElement(FormLabel, {
filled: filled,
disabled: disabled,
required: required
}, label), React.createElement(SelectContainer, {
disabled: disabled
}, prefix && React.createElement(SelectPrefix, {
prefix: prefix,
size: size
}, prefix), customValueText && React.createElement(StyledCustomValue, {
filled: filled,
size: size,
prefix: prefix
}, customValueText), React.createElement(StyledSelect, {
dataTest: dataTest,
size: size,
disabled: disabled,
error: error,
value: value == null ? "" : value,
prefix: prefix,
name: name,
onFocus: onFocus,
onBlur: onBlur,
onChange: onChange,
filled: filled,
customValueText: customValueText,
tabIndex: tabIndex,
id: id,
required: required,
ref: ref
}, placeholder && React.createElement("option", {
label: placeholder,
value: ""
}, placeholder), options.map(option => React.createElement("option", {
key: `option-${option.value}`,
value: option.value,
disabled: option.disabled
}, option.label))), React.createElement(SelectSuffix, {
size: size,
disabled: disabled
}, React.createElement(ChevronDown, null))), help && !error && React.createElement(FormFeedback, {
type: TYPE_OPTIONS.HELP
}, help), error && React.createElement(FormFeedback, {
type: TYPE_OPTIONS.ERROR
}, error));
}); // otherwise Unknown in storybook
Select.displayName = "Select";
export default Select;