@navinc/base-react-components
Version:
Nav's Pattern Library
53 lines • 4 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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputField = 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 = __importDefault(require("./copy.js"));
const shared_js_1 = require("./form-elements/shared.js");
const tooltip_1 = __importDefault(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 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 || !(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, Object.assign({ className: className }, { children: [lede && (0, jsx_runtime_1.jsx)(copy_js_1.default, Object.assign({ bold: true }, { children: lede })), (0, jsx_runtime_1.jsxs)(shared_js_1.Field, Object.assign({ 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 }, props)), children, (0, jsx_runtime_1.jsxs)(shared_js_1.Label, Object.assign({ required: required }, { children: [(0, utils_1.capitalize)(label), tooltipText && ((0, jsx_runtime_1.jsx)(tooltip_1.default, Object.assign({ isDark: true, placement: "top" }, { 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, Object.assign({ hasSpaceForErrors: hasSpaceForErrors }, { children: !!errors.length && errors.map((err, i) => (0, jsx_runtime_1.jsx)(shared_js_1.Err, { children: err }, `err-${i}`)) }))] })));
};
exports.InputField = InputField;
const StyledInput = (0, styled_components_1.default)(exports.InputField).withConfig({ displayName: "brc-sc-StyledInput", componentId: "brc-sc-zrjsp4" }) ``;
exports.default = StyledInput;
//# sourceMappingURL=input.js.map