UNPKG

@navinc/base-react-components

Version:
51 lines 3.9 kB
"use strict"; 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.Input = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const utils_1 = require("@navinc/utils"); const react_1 = require("react"); const styled_components_1 = __importDefault(require("styled-components")); const copy_js_1 = require("./copy.js"); const shared_js_1 = require("./form-elements/shared.js"); const tooltip_1 = require("./tooltip"); // copied from https://github.com/gregberge/react-merge-refs const combineRefs = (...refs) => (value) => { refs.forEach((ref) => { if (typeof ref === 'function') { ref(value); } else if (ref != null) { ; ref.current = value; } }); }; const _Input = (_a) => { var { autoFocus, children, className, label = '', hasSpaceForErrors, helperIcon, helperLinkAction, helperText, isInvalid, isStatic, value, required, type, errors = [], lede = '', touched, placeholder, childrenBeforeErrors, innerRef, tooltipText, isPrivate } = _a, props = __rest(_a, ["autoFocus", "children", "className", "label", "hasSpaceForErrors", "helperIcon", "helperLinkAction", "helperText", "isInvalid", "isStatic", "value", "required", "type", "errors", "lede", "touched", "placeholder", "childrenBeforeErrors", "innerRef", "tooltipText", "isPrivate"]); /* isStatic to be used to overcome auto-populated fields that do not read as values or placeholders */ const isVisited = touched || !(0, utils_1.isEmpty)(value) || typeof value === 'number' || !!placeholder || isStatic; const inputRef = (0, react_1.useRef)(); const combinedRefs = innerRef ? combineRefs(inputRef, innerRef) : inputRef; (0, react_1.useEffect)(() => { if (autoFocus && inputRef.current) { (0, utils_1.focusWithoutScroll)(inputRef.current); } }, [autoFocus]); return ((0, jsx_runtime_1.jsxs)(shared_js_1.FieldWrapper, { className: className, children: [lede && (0, jsx_runtime_1.jsx)(copy_js_1.Copy, { bold: true, children: lede }), (0, jsx_runtime_1.jsxs)(shared_js_1.Field, { isVisited: isVisited, children: [(0, jsx_runtime_1.jsx)(shared_js_1.Input, Object.assign({ isInvalid: isInvalid, placeholder: placeholder, ref: combinedRefs, required: required, type: type, value: value, "data-testid": `input:${props.name}` }, (isPrivate && { className: 'js-private', 'data-heap-redact-text': 'true' }), props)), children, (0, jsx_runtime_1.jsxs)(shared_js_1.Label, { required: required, children: [(0, utils_1.capitalize)(label), tooltipText && ((0, jsx_runtime_1.jsx)(tooltip_1.Tooltip, { isDark: true, placement: "top", size: "18px", children: tooltipText }))] })] }), helperText && ((0, jsx_runtime_1.jsx)(shared_js_1.Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, hasSpaceForHelper: hasSpaceForErrors, helperText: helperText })), childrenBeforeErrors, (0, jsx_runtime_1.jsx)(shared_js_1.Errors, { hasSpaceForErrors: hasSpaceForErrors, children: !!errors.length && errors.map((err, i) => (0, jsx_runtime_1.jsx)(shared_js_1.Err, { children: err }, `err-${i}`)) })] })); }; exports.Input = (0, styled_components_1.default)(_Input).withConfig({ displayName: "brc-sc-Input", componentId: "brc-sc-kyfu9n" }) ``; //# sourceMappingURL=input.js.map