UNPKG

@navinc/base-react-components

Version:
46 lines 3.36 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, 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; } }); }; export const InputField = (_a) => { var { autoFocus, children, className, label = '', hasSpaceForErrors, helperIcon, helperLinkAction, helperText, isInvalid, isStatic, value, required, type, errors = [], lede = '', touched, placeholder, childrenBeforeErrors, innerRef, tooltipText } = _a, props = __rest(_a, ["autoFocus", "children", "className", "label", "hasSpaceForErrors", "helperIcon", "helperLinkAction", "helperText", "isInvalid", "isStatic", "value", "required", "type", "errors", "lede", "touched", "placeholder", "childrenBeforeErrors", "innerRef", "tooltipText"]); /* 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, Object.assign({ className: className }, { children: [lede && _jsx(Copy, Object.assign({ bold: true }, { children: lede })), _jsxs(Field, Object.assign({ isVisited: isVisited }, { children: [_jsx(Input, Object.assign({ isInvalid: isInvalid, placeholder: placeholder, ref: combinedRefs, required: required, type: type, value: value }, props)), children, _jsxs(Label, Object.assign({ required: required }, { children: [capitalize(label), tooltipText && (_jsx(Tooltip, Object.assign({ isDark: true, placement: "top" }, { children: tooltipText })))] }))] })), helperText && (_jsx(Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, hasSpaceForHelper: hasSpaceForErrors, helperText: helperText })), childrenBeforeErrors, _jsx(Errors, Object.assign({ hasSpaceForErrors: hasSpaceForErrors }, { children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) }))] }))); }; const StyledInput = styled(InputField).withConfig({ displayName: "brc-sc-StyledInput", componentId: "brc-sc-zrjsp4" }) ``; export default StyledInput; //# sourceMappingURL=input.js.map