UNPKG

@navinc/base-react-components

Version:
219 lines (203 loc) 7.8 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import styled, { css } from 'styled-components'; import { Button } from '../button.js'; import { Copy } from '../copy.js'; import { Text } from '../text.js'; import { Icon } from '../icon.js'; import NumberFormat from 'react-number-format'; import CarrotDown from '../icons/actions/carrot-down.js'; import { Tooltip } from '../tooltip'; export const HelperIcon = styled(Icon).withConfig({ displayName: "brc-sc-HelperIcon", componentId: "brc-sc-152bxnk" }) ` max-width: 16px; max-height: 16px; fill: ${({ theme }) => theme.navNeutral500}; `; export const HelperDescription = styled(Copy).attrs(() => ({ size: 'sm', bold: true })).withConfig({ displayName: "brc-sc-HelperDescription", componentId: "brc-sc-1lfhxik" }) ` color: inherit; `; // TODO: hasSpaceForHelper is unused and might be a mistake export const HelperItem = styled.div.attrs(({ isLink }) => isLink && { as: Button, variation: 'buttonLink', type: 'button' }).withConfig({ displayName: "brc-sc-HelperItem", componentId: "brc-sc-1kmuylm" }) ` display: grid; padding-top: 4px; grid-template-columns: min-content 1fr; grid-gap: 4px; text-align: left; ${({ hasSpaceForErrors }) => (hasSpaceForErrors ? 'min-height: 18px;' : '')} ${({ theme, isLink }) => (!isLink ? `color: ${theme.navNeutral500}` : `color: ${theme.neutral400};`)} `; export const Helper = styled(({ className, hasSpaceForHelper, helperLinkAction, helperText, iconName }) => (_jsxs(HelperItem, { className: className, isLink: !!helperLinkAction, onClick: helperLinkAction, hasSpaceForHelper: hasSpaceForHelper, children: [iconName && _jsx(HelperIcon, { name: iconName }), _jsx(HelperDescription, { "data-testid": `input:helper-text:${helperText}`, light: true, children: helperText })] }))).withConfig({ displayName: "brc-sc-Helper", componentId: "brc-sc-1g2oo3r" }) ``; export const Label = styled(Text).withConfig({ displayName: "brc-sc-Label", componentId: "brc-sc-1w6t3tr" }) ` display: flex; align-items: center; height: 32px; transition: all 0.2s linear; cursor: text; transform-origin: 0 0; ${Tooltip} { margin-left: 4px; cursor: default; } &:focus { outline: none; } &::after { content: '${({ required }) => (required ? '*' : '')}'; font-size: 24px; } `; export const FieldWrapper = styled.div.withConfig({ displayName: "brc-sc-FieldWrapper", componentId: "brc-sc-11njtq" }) ` display: grid; & > ${Copy} { margin-bottom: ${({ theme }) => theme.gu(1)}; } & * { font-family: inherit; } & > ${Label} { font-size: 16px; } & > ${Helper} { margin-left: ${({ theme }) => theme.gu(2)}; } `; FieldWrapper.displayName = 'FieldWrapper'; const getBorderColor = (theme, isInvalid, disabled) => { if (disabled) { return theme.navNeutral300; } else if (isInvalid) { return theme.navStatusNegative; } return theme.navNeutral300; }; const getBackgroundColor = (theme, isInvalid, disabled, readOnly = false) => { if (disabled || readOnly) { return theme.navNeutral100; } else if (isInvalid) { return theme.navStatusNegative100; } return theme.navNeutralLight; }; export const Input = styled.input.withConfig({ shouldForwardProp: (prop) => !['isInvalid', 'hasSpaceForErrors'].includes(prop), }) ` width: 100%; border-width: 1px; border-style: solid; border-color: ${({ isInvalid, theme, disabled }) => getBorderColor(theme, isInvalid, disabled)}; border-radius: 8px; margin: 3px; font-weight: 800; line-height: 24px; background: ${({ isInvalid, theme, disabled, readOnly }) => getBackgroundColor(theme, isInvalid, disabled, readOnly)}; cursor: ${({ disabled }) => disabled && 'not-allowed'}; padding: ${({ theme }) => `${theme.gu(3)} ${theme.gu(2)} ${theme.gu(1)} ${theme.gu(2)}`}; &:focus, &:hover { outline: none; outline-offset: 0; } &:focus { margin: 0; border-width: 4px; border-color: ${({ theme }) => theme.navStatusPositive500}; } `; export const CurrencyInput = styled(Input).attrs(() => ({ as: NumberFormat, })).withConfig({ displayName: "brc-sc-CurrencyInput", componentId: "brc-sc-3u2kf0" }) ``; export const PercentInput = styled(Input).attrs(() => ({ as: NumberFormat })).withConfig({ displayName: "brc-sc-PercentInput", componentId: "brc-sc-ph72pd" }) ``; export const NumberInput = styled(Input).attrs(() => ({ as: NumberFormat })).withConfig({ displayName: "brc-sc-NumberInput", componentId: "brc-sc-l732kk" }) ``; const getIconFill = (disabled, theme) => { return disabled ? theme.navNeutral400 : theme.navNeutralDark; }; const ChevronWrapper = styled.div.withConfig({ displayName: "brc-sc-ChevronWrapper", componentId: "brc-sc-ozhbdy" }) ` pointer-events: none; & > ${Icon} { fill: ${({ isInvalid, disabled, theme }) => (isInvalid ? theme.navStatusNegative : getIconFill(disabled, theme))}; } `; export const Chevron = styled(({ className, disabled, isInvalid }) => (_jsx(ChevronWrapper, { className: className, disabled: disabled, isInvalid: isInvalid, children: _jsx(CarrotDown, {}) }))).withConfig({ displayName: "brc-sc-Chevron", componentId: "brc-sc-u78bxs" }) ``; const getLabelFontWeight = (theme, isVisited) => { return isVisited ? 400 : 800; }; export const Field = styled.label.withConfig({ displayName: "brc-sc-Field", componentId: "brc-sc-1mfvla" }) ` align-items: stretch; color: ${({ theme }) => theme.navNeutralDark}; cursor: pointer; display: flex; flex: 1 1 auto; margin-bottom: 0; overflow: hidden; position: relative; text-align: left; white-space: nowrap; & > * { width: 100%; border-radius: 8px; appearance: none; } & > ${Label} { will-change: transform; font-weight: ${({ theme, isVisited }) => getLabelFontWeight(theme, isVisited)}; line-height: 20px; top: ${({ isVisited, theme }) => (isVisited ? theme.gu(1) : theme.gu(2))}; transform: ${({ isVisited }) => (isVisited ? 'scale(0.75)' : 'scale(1)')}; left: ${({ theme }) => theme.gu(2)}; position: absolute; transition: all 0.2s ease; color: ${({ theme }) => theme.navNeutralDark}; } * { font-size: 16px; } ${Input}:focus ~ ${Label}, &:hover ${Label}, &:active ${Label} { transform: scale(0.75); font-weight: normal; top: ${({ theme }) => theme.gu(1)}; } ${CurrencyInput}:focus + ${Label}, &:hover ${Label}, &:active ${Label} { color: ${({ theme }) => theme.navNeutralDark}; } & > ${Chevron} { width: min-content; position: absolute; right: 16px; top: 22px; } `; export const Err = styled(Copy).attrs(() => ({ size: 'sm', bold: true })).withConfig({ displayName: "brc-sc-Err", componentId: "brc-sc-vze2v" }) ` color: ${({ theme }) => theme.navStatusNegative}; text-align: left; `; export const Errors = styled.div.withConfig({ displayName: "brc-sc-Errors", componentId: "brc-sc-1iayozj" }) ` ${({ hasSpaceForErrors }) => (hasSpaceForErrors ? 'min-height: 18px;' : '')} & > ${Err} { margin-left: ${({ theme }) => theme.gu(2)}; } `; export const visuallyHiddenStyles = css ` clip: rect(0 0 0 0); clip-path: inset(50%); height: 1px; overflow: hidden; position: absolute; white-space: nowrap; width: 1px; `; export const HiddenLegend = styled.legend.withConfig({ displayName: "brc-sc-HiddenLegend", componentId: "brc-sc-xik3we" }) ` ${visuallyHiddenStyles} `; export const Legend = styled.legend.withConfig({ displayName: "brc-sc-Legend", componentId: "brc-sc-g0at4n" }) ` padding: 0; width: 100%; `; export const Fieldset = styled.fieldset.withConfig({ displayName: "brc-sc-Fieldset", componentId: "brc-sc-3t31l8" }) ` border: none; padding: 0; margin: 0; `; //# sourceMappingURL=shared.js.map