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.

230 lines (204 loc) 9.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.Label = void 0; var _react = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _consts = _interopRequireDefault(require("./consts")); var _Check = _interopRequireDefault(require("../icons/Check")); var _Text = require("../Text"); var _rtl = require("../utils/rtl"); var _getFieldDataState = _interopRequireDefault(require("../common/getFieldDataState")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var getToken = function getToken(name) { return function (_ref) { var _tokens; var theme = _ref.theme, hasError = _ref.hasError, disabled = _ref.disabled, checked = _ref.checked; var tokens = (_tokens = {}, _defineProperty(_tokens, _consts.default.borderColor, hasError && !disabled && !checked ? theme.orbit.borderColorCheckboxRadioError : theme.orbit.borderColorCheckboxRadio), _defineProperty(_tokens, _consts.default.iconColor, disabled ? theme.orbit.colorIconCheckboxRadioDisabled : theme.orbit.colorIconCheckboxRadio), _tokens); return tokens[name]; }; }; var IconContainer = _styledComponents.default.div.withConfig({ displayName: "Checkbox__IconContainer", componentId: "sc-1x6twh3-0" })(["position:relative;box-sizing:border-box;flex:0 0 auto;display:flex;align-items:center;justify-content:center;background-color:", ";height:", ";width:", ";border-radius:", ";transform:scale(1);transition:all ", " ease-in-out;& > svg{visibility:hidden;display:flex;align-items:center;justify-content:center;width:16px;height:16px;}"], function (_ref2) { var theme = _ref2.theme; return theme.orbit.backgroundInput; }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.heightCheckbox; }, function (_ref4) { var theme = _ref4.theme; return theme.orbit.widthCheckbox; }, function (_ref5) { var theme = _ref5.theme; return theme.orbit.borderRadiusNormal; }, function (_ref6) { var theme = _ref6.theme; return theme.orbit.durationFast; }); IconContainer.defaultProps = { theme: _defaultTheme.default }; var TextContainer = _styledComponents.default.div.withConfig({ displayName: "Checkbox__TextContainer", componentId: "sc-1x6twh3-1" })(["display:flex;flex-direction:column;margin:", ";flex:1;"], function (_ref7) { var theme = _ref7.theme; return (0, _rtl.rtlSpacing)("0 0 0 ".concat(theme.orbit.spaceXSmall)); }); TextContainer.defaultProps = { theme: _defaultTheme.default }; var Info = _styledComponents.default.span.withConfig({ displayName: "Checkbox__Info", componentId: "sc-1x6twh3-2" })(["font-size:", ";color:", ";line-height:", ";"], function (_ref8) { var theme = _ref8.theme; return theme.orbit.fontSizeFormFeedback; }, function (_ref9) { var theme = _ref9.theme; return theme.orbit.colorInfoCheckBoxRadio; }, function (_ref10) { var theme = _ref10.theme; return theme.orbit.lineHeightText; }); Info.defaultProps = { theme: _defaultTheme.default }; var LabelText = _styledComponents.default.span.withConfig({ displayName: "Checkbox__LabelText", componentId: "sc-1x6twh3-3" })(["font-family:", ";font-weight:", ";font-size:", ";color:", ";line-height:", ";", "{font-weight:", ";font-size:", ";color:", ";line-height:", ";}"], function (_ref11) { var theme = _ref11.theme; return theme.orbit.fontFamily; }, function (_ref12) { var theme = _ref12.theme; return theme.orbit.fontWeightNormal; }, function (_ref13) { var theme = _ref13.theme; return theme.orbit.fontSizeFormLabel; }, function (_ref14) { var theme = _ref14.theme; return theme.orbit.colorFormLabel; }, function (_ref15) { var theme = _ref15.theme; return theme.orbit.heightCheckbox; }, _Text.StyledText, function (_ref16) { var theme = _ref16.theme; return theme.orbit.fontWeightNormal; }, function (_ref17) { var theme = _ref17.theme; return theme.orbit.fontSizeFormLabel; }, function (_ref18) { var theme = _ref18.theme; return theme.orbit.colorFormLabel; }, function (_ref19) { var theme = _ref19.theme; return theme.orbit.heightCheckbox; }); LabelText.defaultProps = { theme: _defaultTheme.default }; var Input = _styledComponents.default.input.withConfig({ displayName: "Checkbox__Input", componentId: "sc-1x6twh3-4" })(["opacity:0;z-index:-1;position:absolute;&:checked ~ ", " > ", "{font-weight:", ";& > ", "{font-weight:", ";}}&:checked + ", " > svg{visibility:visible;}&:focus + ", "{border:", ";}&:active + ", "{border-color:", ";transform:", ";}"], TextContainer, LabelText, function (_ref20) { var theme = _ref20.theme; return theme.orbit.fontWeightMedium; }, _Text.StyledText, function (_ref21) { var theme = _ref21.theme; return theme.orbit.fontWeightMedium; }, IconContainer, IconContainer, function (_ref22) { var theme = _ref22.theme; return "2px ".concat(theme.orbit.borderStyleInput, " ").concat(theme.orbit.borderColorCheckboxRadioFocus); }, IconContainer, function (_ref23) { var disabled = _ref23.disabled, theme = _ref23.theme; return disabled ? getToken(_consts.default.borderColor) : theme.orbit.borderColorCheckboxRadioActive; }, function (_ref24) { var disabled = _ref24.disabled, theme = _ref24.theme; return !disabled && "scale(".concat(theme.orbit.modifierScaleCheckboxRadioActive, ")"); }); Input.defaultProps = { theme: _defaultTheme.default }; var Label = (0, _styledComponents.default)(function (_ref25) { var className = _ref25.className, children = _ref25.children, dataTest = _ref25.dataTest; return _react.default.createElement("label", { className: className, "data-test": dataTest }, children); }).withConfig({ displayName: "Checkbox__Label", componentId: "sc-1x6twh3-5" })(["font-family:", ";display:flex;width:100%;flex-direction:row;align-items:self-start;opacity:", ";cursor:", ";position:relative;", "{color:", ";border:1px solid ", ";}&:hover ", "{border-color:", ";}"], function (_ref26) { var theme = _ref26.theme; return theme.orbit.fontFamily; }, function (_ref27) { var disabled = _ref27.disabled, theme = _ref27.theme; return disabled ? theme.orbit.opacityCheckboxDisabled : "1"; }, function (_ref28) { var disabled = _ref28.disabled; return disabled ? "not-allowed" : "pointer"; }, IconContainer, getToken(_consts.default.iconColor), getToken(_consts.default.borderColor), IconContainer, function (_ref29) { var disabled = _ref29.disabled, theme = _ref29.theme; return !disabled && theme.orbit.borderColorCheckboxRadioHover; }); exports.Label = Label; Label.defaultProps = { theme: _defaultTheme.default }; // $FlowExpected var Checkbox = _react.default.forwardRef(function (props, ref) { var label = props.label, value = props.value, _props$hasError = props.hasError, hasError = _props$hasError === void 0 ? false : _props$hasError, _props$disabled = props.disabled, disabled = _props$disabled === void 0 ? false : _props$disabled, _props$checked = props.checked, checked = _props$checked === void 0 ? false : _props$checked, name = props.name, onChange = props.onChange, dataTest = props.dataTest, info = props.info, readOnly = props.readOnly, tabIndex = props.tabIndex; var preventOnClick = (0, _react.useCallback)(function (ev) { ev.preventDefault(); }, []); return _react.default.createElement(Label, { disabled: disabled, hasError: hasError, checked: checked }, _react.default.createElement(Input, { "data-test": dataTest, "data-state": (0, _getFieldDataState.default)(!!hasError), value: value, type: "checkbox", disabled: disabled, name: name, tabIndex: tabIndex, checked: checked, onChange: onChange, ref: ref, readOnly: readOnly }), _react.default.createElement(IconContainer, { onClick: readOnly ? preventOnClick : null }, _react.default.createElement(_Check.default, null)), (label || info) && _react.default.createElement(TextContainer, null, label && _react.default.createElement(LabelText, null, label), info && _react.default.createElement(Info, null, info))); }); Checkbox.displayName = "Checkbox"; var _default = Checkbox; exports.default = _default;