UNPKG

@navinc/base-react-components

Version:
45 lines 3.38 kB
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 { capitalize, focusWithoutScroll, isEmpty } from '@navinc/utils'; import { useEffect, useRef } from 'react'; import styled from 'styled-components'; import { Copy } from './copy.js'; import { Err, Errors, Field, FieldWrapper, Helper, Input as FormInput, Label } from './form-elements/shared.js'; import { Tooltip } from './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 || !isEmpty(value) || typeof value === 'number' || !!placeholder || isStatic; const inputRef = useRef(); const combinedRefs = innerRef ? combineRefs(inputRef, innerRef) : inputRef; useEffect(() => { if (autoFocus && inputRef.current) { focusWithoutScroll(inputRef.current); } }, [autoFocus]); return (_jsxs(FieldWrapper, { className: className, children: [lede && _jsx(Copy, { bold: true, children: lede }), _jsxs(Field, { isVisited: isVisited, children: [_jsx(FormInput, 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, _jsxs(Label, { required: required, children: [capitalize(label), tooltipText && (_jsx(Tooltip, { isDark: true, placement: "top", size: "18px", children: tooltipText }))] })] }), helperText && (_jsx(Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, hasSpaceForHelper: hasSpaceForErrors, helperText: helperText })), childrenBeforeErrors, _jsx(Errors, { hasSpaceForErrors: hasSpaceForErrors, children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) })] })); }; export const Input = styled(_Input).withConfig({ displayName: "brc-sc-Input", componentId: "brc-sc-kyfu9n" }) ``; //# sourceMappingURL=input.js.map