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.

169 lines (166 loc) 5.69 kB
import React, { useCallback } from "react"; import styled from "styled-components"; import defaultTheme from "../defaultTheme"; import TOKENS from "./consts"; import Check from "../icons/Check"; import { StyledText } from "../Text"; import { rtlSpacing } from "../utils/rtl"; import getFieldDataState from "../common/getFieldDataState"; const getToken = name => ({ theme, hasError, disabled, checked }) => { const tokens = { [TOKENS.borderColor]: hasError && !disabled && !checked ? theme.orbit.borderColorCheckboxRadioError : theme.orbit.borderColorCheckboxRadio, [TOKENS.iconColor]: disabled ? theme.orbit.colorIconCheckboxRadioDisabled : theme.orbit.colorIconCheckboxRadio }; return tokens[name]; }; const IconContainer = styled.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;}"], ({ theme }) => theme.orbit.backgroundInput, ({ theme }) => theme.orbit.heightCheckbox, ({ theme }) => theme.orbit.widthCheckbox, ({ theme }) => theme.orbit.borderRadiusNormal, ({ theme }) => theme.orbit.durationFast); IconContainer.defaultProps = { theme: defaultTheme }; const TextContainer = styled.div.withConfig({ displayName: "Checkbox__TextContainer", componentId: "sc-1x6twh3-1" })(["display:flex;flex-direction:column;margin:", ";flex:1;"], ({ theme }) => rtlSpacing(`0 0 0 ${theme.orbit.spaceXSmall}`)); TextContainer.defaultProps = { theme: defaultTheme }; const Info = styled.span.withConfig({ displayName: "Checkbox__Info", componentId: "sc-1x6twh3-2" })(["font-size:", ";color:", ";line-height:", ";"], ({ theme }) => theme.orbit.fontSizeFormFeedback, ({ theme }) => theme.orbit.colorInfoCheckBoxRadio, ({ theme }) => theme.orbit.lineHeightText); Info.defaultProps = { theme: defaultTheme }; const LabelText = styled.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:", ";}"], ({ theme }) => theme.orbit.fontFamily, ({ theme }) => theme.orbit.fontWeightNormal, ({ theme }) => theme.orbit.fontSizeFormLabel, ({ theme }) => theme.orbit.colorFormLabel, ({ theme }) => theme.orbit.heightCheckbox, StyledText, ({ theme }) => theme.orbit.fontWeightNormal, ({ theme }) => theme.orbit.fontSizeFormLabel, ({ theme }) => theme.orbit.colorFormLabel, ({ theme }) => theme.orbit.heightCheckbox); LabelText.defaultProps = { theme: defaultTheme }; const Input = styled.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, ({ theme }) => theme.orbit.fontWeightMedium, StyledText, ({ theme }) => theme.orbit.fontWeightMedium, IconContainer, IconContainer, ({ theme }) => `2px ${theme.orbit.borderStyleInput} ${theme.orbit.borderColorCheckboxRadioFocus}`, IconContainer, ({ disabled, theme }) => disabled ? getToken(TOKENS.borderColor) : theme.orbit.borderColorCheckboxRadioActive, ({ disabled, theme }) => !disabled && `scale(${theme.orbit.modifierScaleCheckboxRadioActive})`); Input.defaultProps = { theme: defaultTheme }; export const Label = styled(({ className, children, dataTest }) => React.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:", ";}"], ({ theme }) => theme.orbit.fontFamily, ({ disabled, theme }) => disabled ? theme.orbit.opacityCheckboxDisabled : "1", ({ disabled }) => disabled ? "not-allowed" : "pointer", IconContainer, getToken(TOKENS.iconColor), getToken(TOKENS.borderColor), IconContainer, ({ disabled, theme }) => !disabled && theme.orbit.borderColorCheckboxRadioHover); Label.defaultProps = { theme: defaultTheme }; // $FlowExpected const Checkbox = React.forwardRef((props, ref) => { const { label, value, hasError = false, disabled = false, checked = false, name, onChange, dataTest, info, readOnly, tabIndex } = props; const preventOnClick = useCallback(ev => { ev.preventDefault(); }, []); return React.createElement(Label, { disabled: disabled, hasError: hasError, checked: checked }, React.createElement(Input, { "data-test": dataTest, "data-state": getFieldDataState(!!hasError), value: value, type: "checkbox", disabled: disabled, name: name, tabIndex: tabIndex, checked: checked, onChange: onChange, ref: ref, readOnly: readOnly }), React.createElement(IconContainer, { onClick: readOnly ? preventOnClick : null }, React.createElement(Check, null)), (label || info) && React.createElement(TextContainer, null, label && React.createElement(LabelText, null, label), info && React.createElement(Info, null, info))); }); Checkbox.displayName = "Checkbox"; export default Checkbox;