@navinc/base-react-components
Version:
Nav's Pattern Library
113 lines (109 loc) • 4.07 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useRef, useEffect } from 'react';
import styled from 'styled-components';
import Copy from './copy';
import { focusWithoutScroll } from '@navinc/utils';
const getLabelStyling = ({ isInvalid, theme }) => `
margin: 0;
padding: 0;
color: ${isInvalid ? theme.error : 'inherit'};
`;
const CopyLabel = styled((_a) => {
var { isInvalid } = _a, props = __rest(_a, ["isInvalid"]);
return _jsx(Copy, Object.assign({}, props), void 0);
}) `
${getLabelStyling}
`;
const DivLabel = styled.div `
${getLabelStyling}
`;
const RadioItem = styled.label `
display: flex;
flex-flow: row nowrap;
align-items: center;
flex: 1 1 auto;
margin-bottom: 0;
cursor: pointer;
text-align: left;
${({ asideContent, hasBorder, theme }) => (asideContent || hasBorder) &&
`
border-radius: 12px;
border: 1px solid ${theme.border};
padding: 8px;
padding-left: 16px;
padding-right: 16px;
min-height: 62px;
`}
${({ checked, checkedBackgroundColor }) => checked &&
checkedBackgroundColor &&
`
background-color: ${checkedBackgroundColor};
border-color: ${checkedBackgroundColor};
`}
& > :first-child {
margin-right: 16px;
}
`;
const AsideContainer = styled.div `
display: flex;
flex: 1;
justify-content: flex-end;
margin-left: 8px;
`;
const RadioIndicator = styled.input.attrs(() => ({ type: 'radio' })) `
box-sizing: border-box;
border-radius: 50%;
background-color: ${({ checked, theme }) => (checked ? theme.navBlue : 'transparent')};
height: 12px;
width: 12px;
appearance: none;
margin: 0;
font-size: inherit;
transition: background-color 300ms ease;
`;
const RadioSpot = styled.div `
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
border-radius: 50%;
border-style: solid;
border-width: ${({ checked }) => (checked ? '2px' : '1px')};
border-color: ${({ disabled, theme }) => (disabled ? theme.scuttleGray300 : theme.bubbleBlue200)};
background: ${({ disabled, theme }) => (disabled ? theme.neutral100 : theme.bubbleBlue100)};
height: 24px;
flex: 0 0 auto;
width: 24px;
transition: box-shadow 300ms ease;
&:focus-within {
box-shadow: 0 0 0 8px ${({ theme }) => theme.bubbleBlue300};
& > ${RadioIndicator} {
outline: none;
}
}
`;
export const Radio = (_a) => {
var { asideContent, autoFocus, checkedBackgroundColor, children, label, className, checked, disabled, hasBorder, isInvalid } = _a, props = __rest(_a, ["asideContent", "autoFocus", "checkedBackgroundColor", "children", "label", "className", "checked", "disabled", "hasBorder", "isInvalid"]);
const inputRef = useRef(null);
useEffect(() => {
if (autoFocus) {
focusWithoutScroll(inputRef.current);
}
}, [autoFocus]);
const Label = typeof label === 'string' ? CopyLabel : DivLabel;
return (_jsxs(RadioItem, Object.assign({ asideContent: asideContent, checked: checked, className: className, hasBorder: hasBorder, checkedBackgroundColor: checkedBackgroundColor }, { children: [_jsx(RadioSpot, Object.assign({}, { checked, disabled }, { children: _jsx(RadioIndicator, Object.assign({ ref: inputRef }, { checked, disabled }, props), void 0) }), void 0), _jsx(Label, Object.assign({ isInvalid: isInvalid }, { children: label }), void 0), asideContent && _jsx(AsideContainer, { children: asideContent }, void 0)] }), void 0));
};
const StyledRadio = styled(Radio) ``;
export default StyledRadio;
//# sourceMappingURL=radio.js.map