@navinc/base-react-components
Version:
Nav's Pattern Library
67 lines (61 loc) • 4.5 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 { 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 { combineRefs } from './combine-refs.js';
import { Tooltip } from './tooltip.js';
import { redactProps } from './redact/redact.js';
const UnstyledInput = (_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(null);
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, { children: lede }), _jsxs(Field, { isVisited: isVisited, children: [_jsx(FormInput, Object.assign({ isInvalid: isInvalid, placeholder: placeholder, required: required, type: type, value: value, "data-testid": `input:${props.name}` }, (isPrivate && redactProps), props, { ref: combinedRefs })), children, _jsxs(Label, { required: required, children: [capitalize(label), tooltipText && (_jsx(Tooltip, { placement: "top", size: "24", className: "z-10", children: _jsx("p", { children: tooltipText }) }))] })] }), helperText && _jsx(Helper, { iconName: helperIcon, helperLinkAction: helperLinkAction, helperText: helperText }), childrenBeforeErrors, _jsx(Errors, { hasSpaceForErrors: hasSpaceForErrors, hasHelperText: !!helperText, children: !!errors.length && errors.map((err, i) => _jsx(Err, { children: err }, `err-${i}`)) })] }));
};
/**
* All form-element components expect to be controlled components. This makes them
* immediately compatible with form managers like formik or redux-form, as well as
* within any stateful component.
* Use the `<Input />` component just as you would for any `input` type that
* accepts textual input (`radio`, `checkbox` have their own respective
* components). To enable label dynamicism, be sure to pass `touched` as a property based on the Formik detection of the field having been visited. All props get passed on to the underlying `input`.
* In addition to native form element attributes, all form-element components
* implement these props.
* Add a line of helper text below the input using `helperText` and `helperIcon`, and optionally convert the helper
* into a button using the `helperAction` to perform some work, such as showing and hiding a password.
* isInvalid: Boolean -- Indicates whether the value of the field is valid
* (`false`) or invalid (`true`)
* ```
* <Input
* name="my-input"
* required
* isInvalid={this.state['my-input'].isInvalid}
* onChange={this.handleChange}
* value={this.state['my-input'].value}
* />
* ```
* TODO: Add some standardized onClick tracking?
*
* @deprecated This component is deprecated and will be removed in a future release. Avoid using it in new code.
*/
export const Input = styled(UnstyledInput).withConfig({ displayName: "brc-sc-Input", componentId: "brc-sc-2ei8sh" }) ``;
//# sourceMappingURL=input.js.map