UNPKG

@navinc/base-react-components

Version:
46 lines 3.97 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.InputField = exports.capitalizeString = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styled_components_1 = __importDefault(require("styled-components")); const shared_js_1 = require("./form-elements/shared.js"); const copy_1 = __importDefault(require("./copy")); const utils_1 = require("@navinc/utils"); const password_strength_meter_js_1 = require("./password-strength-meter.js"); const capitalizeString = (string = '') => string.replace(/^\w/, (c) => c.toUpperCase()); exports.capitalizeString = capitalizeString; const StyledPasswordStrengthMeter = (0, styled_components_1.default)(password_strength_meter_js_1.PasswordStrengthMeter) ` margin: 0 ${({ theme }) => theme.gu(2)}; text-align: 'left'; `; const InputField = (_a) => { var { autoFocus, children, className, label = '', hasSpaceForErrors, helperIcon, helperLinkAction, helperText, isInvalid, isStatic, value, required, type, errors = [], lede = '', touched, placeholder, passwordStrengthScore, requiredPasswordScore } = _a, props = __rest(_a, ["autoFocus", "children", "className", "label", "hasSpaceForErrors", "helperIcon", "helperLinkAction", "helperText", "isInvalid", "isStatic", "value", "required", "type", "errors", "lede", "touched", "placeholder", "passwordStrengthScore", "requiredPasswordScore"]); /* isStatic to be used to overcome auto-populated fields that do not read as values or placeholders */ const isVisited = touched || value || placeholder || isStatic; const inputRef = (0, react_1.useRef)(null); (0, react_1.useEffect)(() => { if (autoFocus) { (0, utils_1.focusWithoutScroll)(inputRef.current); } }, [autoFocus]); return ((0, jsx_runtime_1.jsxs)(shared_js_1.FieldWrapper, Object.assign({ className: className }, { children: [lede && (0, jsx_runtime_1.jsx)(copy_1.default, Object.assign({ bold: true }, { children: lede }), void 0), (0, jsx_runtime_1.jsxs)(shared_js_1.Field, Object.assign({ isVisited: isVisited, isInvalid: isInvalid, required: required, type: type }, { children: [(0, jsx_runtime_1.jsx)(shared_js_1.Input, Object.assign({ isInvalid: isInvalid, placeholder: placeholder, ref: inputRef, required: required, type: type, value: value }, props), void 0), children, (0, jsx_runtime_1.jsx)(shared_js_1.Label, Object.assign({ required: required, value: value }, { children: (0, exports.capitalizeString)(label) }), void 0)] }), void 0), helperText && ((0, jsx_runtime_1.jsx)(shared_js_1.Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, hasSpaceForHelper: hasSpaceForErrors, helperText: helperText }, void 0)), passwordStrengthScore >= 0 && ((0, jsx_runtime_1.jsx)(StyledPasswordStrengthMeter, { passwordStrengthScore: passwordStrengthScore, requiredPasswordScore: requiredPasswordScore }, void 0)), (0, jsx_runtime_1.jsx)(shared_js_1.Errors, Object.assign({ hasSpaceForErrors: hasSpaceForErrors }, { children: !!errors.length && errors.map((err, i) => (0, jsx_runtime_1.jsx)(shared_js_1.Err, { children: err }, `err-${i}`)) }), void 0)] }), void 0)); }; exports.InputField = InputField; const StyledInput = (0, styled_components_1.default)(exports.InputField) ``; exports.default = StyledInput; //# sourceMappingURL=input.js.map