@navinc/base-react-components
Version:
Nav's Pattern Library
38 lines • 3.24 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 { Err, Errors, Field, FieldWrapper, Helper, Input, Label } from './form-elements/shared.js';
import Copy from './copy';
import { focusWithoutScroll } from '@navinc/utils';
import { PasswordStrengthMeter } from './password-strength-meter.js';
export const capitalizeString = (string = '') => string.replace(/^\w/, (c) => c.toUpperCase());
const StyledPasswordStrengthMeter = styled(PasswordStrengthMeter) `
margin: 0 ${({ theme }) => theme.gu(2)};
text-align: 'left';
`;
export 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 = useRef(null);
useEffect(() => {
if (autoFocus) {
focusWithoutScroll(inputRef.current);
}
}, [autoFocus]);
return (_jsxs(FieldWrapper, Object.assign({ className: className }, { children: [lede && _jsx(Copy, Object.assign({ bold: true }, { children: lede }), void 0), _jsxs(Field, Object.assign({ isVisited: isVisited, isInvalid: isInvalid, required: required, type: type }, { children: [_jsx(Input, Object.assign({ isInvalid: isInvalid, placeholder: placeholder, ref: inputRef, required: required, type: type, value: value }, props), void 0), children, _jsx(Label, Object.assign({ required: required, value: value }, { children: capitalizeString(label) }), void 0)] }), void 0), helperText && (_jsx(Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, hasSpaceForHelper: hasSpaceForErrors, helperText: helperText }, void 0)), passwordStrengthScore >= 0 && (_jsx(StyledPasswordStrengthMeter, { passwordStrengthScore: passwordStrengthScore, requiredPasswordScore: requiredPasswordScore }, void 0)), _jsx(Errors, Object.assign({ hasSpaceForErrors: hasSpaceForErrors }, { children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) }), void 0)] }), void 0));
};
const StyledInput = styled(InputField) ``;
export default StyledInput;
//# sourceMappingURL=input.js.map