@navinc/base-react-components
Version:
Nav's Pattern Library
120 lines (116 loc) • 4.68 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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Radio = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styled_components_1 = __importDefault(require("styled-components"));
const copy_1 = __importDefault(require("./copy"));
const utils_1 = require("@navinc/utils");
const getLabelStyling = ({ isInvalid, theme }) => `
margin: 0;
padding: 0;
color: ${isInvalid ? theme.error : 'inherit'};
`;
const CopyLabel = (0, styled_components_1.default)((_a) => {
var { isInvalid } = _a, props = __rest(_a, ["isInvalid"]);
return (0, jsx_runtime_1.jsx)(copy_1.default, Object.assign({}, props), void 0);
}) `
${getLabelStyling}
`;
const DivLabel = styled_components_1.default.div `
${getLabelStyling}
`;
const RadioItem = styled_components_1.default.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_components_1.default.div `
display: flex;
flex: 1;
justify-content: flex-end;
margin-left: 8px;
`;
const RadioIndicator = styled_components_1.default.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_components_1.default.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;
}
}
`;
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 = (0, react_1.useRef)(null);
(0, react_1.useEffect)(() => {
if (autoFocus) {
(0, utils_1.focusWithoutScroll)(inputRef.current);
}
}, [autoFocus]);
const Label = typeof label === 'string' ? CopyLabel : DivLabel;
return ((0, jsx_runtime_1.jsxs)(RadioItem, Object.assign({ asideContent: asideContent, checked: checked, className: className, hasBorder: hasBorder, checkedBackgroundColor: checkedBackgroundColor }, { children: [(0, jsx_runtime_1.jsx)(RadioSpot, Object.assign({}, { checked, disabled }, { children: (0, jsx_runtime_1.jsx)(RadioIndicator, Object.assign({ ref: inputRef }, { checked, disabled }, props), void 0) }), void 0), (0, jsx_runtime_1.jsx)(Label, Object.assign({ isInvalid: isInvalid }, { children: label }), void 0), asideContent && (0, jsx_runtime_1.jsx)(AsideContainer, { children: asideContent }, void 0)] }), void 0));
};
exports.Radio = Radio;
const StyledRadio = (0, styled_components_1.default)(exports.Radio) ``;
exports.default = StyledRadio;
//# sourceMappingURL=radio.js.map