@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
148 lines • 12.4 kB
JavaScript
"use strict";
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.TextInput = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const classnames_1 = __importDefault(require("classnames"));
const react_1 = require("react");
const messages_1 = require("../_common/messages");
const use_semi_controlled_input_1 = require("../_common/use-semi-controlled-input");
const utils_1 = require("../_common/utils");
const ExclamationCircleIconSolid_1 = require("../icons/generated/heroIcons/ExclamationCircleIconSolid");
const InformationCircleIconOutline_1 = require("../icons/generated/heroIcons/InformationCircleIconOutline");
const XMarkIconOutline_1 = require("../icons/generated/heroIcons/XMarkIconOutline");
const loading_spinner_1 = require("../loading-spinner");
const skeleton_1 = require("../skeleton");
const tooltip_1 = require("../tooltip");
const typography_1 = require("../typography");
/**
*
*
* TextInput Component
*
*
*/
const TextInput = (_a) => {
var { label, isFluid, errorText, helpText, leadingElement, trailingElement, showRequiredOrOptionalLabel = false, moreInformationText, size = 'medium', placeholder, value, tooltipProps, htmlAttributes, isDisabled, isReadOnly, isRequired, onChange, isClearable = false, className, style, isSkeletonLoading = false, isLoading = false, skeletonProps, ref } = _a, restProps = __rest(_a, ["label", "isFluid", "errorText", "helpText", "leadingElement", "trailingElement", "showRequiredOrOptionalLabel", "moreInformationText", "size", "placeholder", "value", "tooltipProps", "htmlAttributes", "isDisabled", "isReadOnly", "isRequired", "onChange", "isClearable", "className", "style", "isSkeletonLoading", "isLoading", "skeletonProps", "ref"]);
const [currentValue, handleChange] = (0, use_semi_controlled_input_1.useSemiControlledInput)({
inputType: 'text',
isControlled: value !== undefined,
onChange,
state: value !== null && value !== void 0 ? value : '',
});
const hintId = (0, react_1.useId)();
const helpTextId = (0, react_1.useId)();
const errorTextId = (0, react_1.useId)();
const containerWrappingClasses = (0, classnames_1.default)(`ndl-text-input`, className, {
'ndl-disabled': isDisabled,
'ndl-has-error': errorText,
'ndl-has-icon': leadingElement || trailingElement || errorText,
'ndl-has-leading-icon': leadingElement,
'ndl-has-trailing-icon': trailingElement || errorText,
'ndl-large': size === 'large',
'ndl-medium': size === 'medium',
'ndl-read-only': isReadOnly,
'ndl-small': size === 'small',
});
const hasEmptyLabelValue = label === undefined || label === null || label === '';
const labelWrappingClasses = (0, classnames_1.default)('ndl-form-item-label', {
'ndl-fluid': isFluid,
'ndl-form-item-no-label': hasEmptyLabelValue,
});
const combinedHtmlInputAttributes = Object.assign(Object.assign({}, htmlAttributes), { className: (0, classnames_1.default)('ndl-input', htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.className) });
const ariaLabel = combinedHtmlInputAttributes['aria-label'];
const isCustomLabel = Boolean(label) && typeof label !== 'string';
const hasCustomLabelAndNoAriaLabel = isCustomLabel && (ariaLabel === undefined || ariaLabel === '');
const shouldRenderHint = isClearable || isLoading;
const handleInputKeyDown = (event) => {
var _a;
if (isClearable && event.key === 'Escape' && Boolean(currentValue)) {
event.preventDefault();
event.stopPropagation();
handleChange === null || handleChange === void 0 ? void 0 : handleChange({
target: { value: '' },
});
}
(_a = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(htmlAttributes, event);
};
(0, react_1.useMemo)(() => {
if (!label && !ariaLabel) {
(0, utils_1.needleWarningMessage)('A TextInput without a label does not have an aria label, be sure to include an aria label for screen readers. Link: https://dequeuniversity.com/rules/axe/4.2/label?application=axeAPI');
}
if (hasCustomLabelAndNoAriaLabel) {
(0, utils_1.needleWarningMessage)(messages_1.customLabelAndNoAriaLabelWarningMessage);
}
}, [label, ariaLabel, hasCustomLabelAndNoAriaLabel]);
const informationIconClasses = (0, classnames_1.default)({
'ndl-information-icon-large': size === 'large',
'ndl-information-icon-small': size === 'small' || size === 'medium',
});
const combinedAriaDescribedby = (0, react_1.useMemo)(() => {
const ariaDescribedby = [];
if (hintId && shouldRenderHint) {
ariaDescribedby.push(hintId);
}
if (helpText && !errorText) {
ariaDescribedby.push(helpTextId);
}
else if (errorText) {
ariaDescribedby.push(errorTextId);
}
return ariaDescribedby.join(' ');
}, [hintId, shouldRenderHint, helpText, errorText, helpTextId, errorTextId]);
return ((0, jsx_runtime_1.jsxs)("div", { className: containerWrappingClasses, style: style, children: [(0, jsx_runtime_1.jsxs)("label", { className: labelWrappingClasses, children: [!hasEmptyLabelValue && ((0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", shape: "rectangular" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-label-text-wrapper", children: [(0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-large' : 'body-medium', className: "ndl-label-text", children: label }), Boolean(moreInformationText) && ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.root, { type: "simple", children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.trigger, { className: informationIconClasses, hasButtonWrapper: true, children: (0, jsx_runtime_1.jsx)("div", { tabIndex: 0, role: "button", "aria-label": "Information icon", children: (0, jsx_runtime_1.jsx)(InformationCircleIconOutline_1.InformationCircleIconOutline, {}) }) })), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Content, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.content, { children: moreInformationText }))] }))), showRequiredOrOptionalLabel && ((0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-large' : 'body-medium', className: "ndl-form-item-optional", children: isRequired === true ? 'Required' : 'Optional' }))] }) }))), (0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", shape: "rectangular" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-input-wrapper", children: [(leadingElement || (isLoading && !trailingElement)) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-element-leading ndl-element", children: isLoading ? ((0, jsx_runtime_1.jsx)(loading_spinner_1.LoadingSpinner, { size: size === 'large' ? 'medium' : 'small', className: size === 'large'
? 'ndl-medium-spinner'
: 'ndl-small-spinner' })) : (leadingElement) })), (0, jsx_runtime_1.jsxs)("div", { className: (0, classnames_1.default)('ndl-input-container', {
'ndl-clearable': isClearable,
}), children: [(0, jsx_runtime_1.jsx)("input", Object.assign({ ref: ref, readOnly: isReadOnly, disabled: isDisabled, required: isRequired, value: currentValue, placeholder: placeholder, type: "text", onChange: handleChange, "aria-describedby": combinedAriaDescribedby, "aria-invalid": Boolean(errorText) }, combinedHtmlInputAttributes, { onKeyDown: handleInputKeyDown }, restProps)), shouldRenderHint && ((0, jsx_runtime_1.jsxs)("span", { id: hintId, className: "ndl-text-input-hint", "aria-hidden": true, children: [isLoading && 'Loading ', isClearable && 'Press Escape to clear input.'] })), isClearable && Boolean(currentValue) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-element-clear ndl-element", children: (0, jsx_runtime_1.jsx)("button", { tabIndex: -1, "aria-hidden": true, type: "button", title: "Clear input (Esc)", onClick: () => {
handleChange === null || handleChange === void 0 ? void 0 : handleChange({
target: { value: '' },
});
}, children: (0, jsx_runtime_1.jsx)(XMarkIconOutline_1.XMarkIconOutline, { className: "n-size-4" }) }) }))] }), trailingElement && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-element-trailing ndl-element", children: isLoading && !leadingElement ? ((0, jsx_runtime_1.jsx)(loading_spinner_1.LoadingSpinner, { size: size === 'large' ? 'medium' : 'small', className: size === 'large'
? 'ndl-medium-spinner'
: 'ndl-small-spinner' })) : (trailingElement) }))] }) }))] }), Boolean(helpText) && !errorText && ((0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, { onBackground: "weak", shape: "rectangular", isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-medium' : 'body-small', className: "ndl-form-message", htmlAttributes: {
'aria-live': 'polite',
id: helpTextId,
}, children: helpText }) })), Boolean(errorText) && (
// TODO v4: We might want to have a min width for the container for the messages to help skeleton loading.
// Currently the message fills 100% of the width while the rest of the text input has a set width.
(0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", shape: "rectangular", width: "fit-content" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-form-message", children: [(0, jsx_runtime_1.jsx)("div", { className: "ndl-error-icon", children: (0, jsx_runtime_1.jsx)(ExclamationCircleIconSolid_1.ExclamationCircleIconSolid, {}) }), (0, jsx_runtime_1.jsx)(typography_1.Typography, { className: "ndl-error-text", variant: size === 'large' ? 'body-medium' : 'body-small', htmlAttributes: {
'aria-live': 'polite',
id: errorTextId,
}, children: errorText })] }) })))] }));
};
exports.TextInput = TextInput;
//# sourceMappingURL=TextInput.js.map