@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
79 lines • 7.02 kB
JavaScript
"use strict";
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 react_1 = require("react");
const defaultImports_1 = require("../_common/defaultImports");
const messages_1 = require("../_common/messages");
const utils_1 = require("../_common/utils");
const icons_1 = require("../icons");
const skeleton_1 = require("../skeleton");
const tooltip_1 = require("../tooltip");
const typography_1 = require("../typography");
/**
*
*
* TextInput Component
*
*
*/
exports.TextInput = (0, react_1.forwardRef)(function TextInput(props, ref) {
const { label, isFluid, errorText, helpText, leftElement, rightElement, isOptional = false, informationIconText, size = 'medium', placeholder, value, tooltipProps, htmlAttributes, isDisabled, isReadOnly, isRequired, onChange, className, style, isSkeletonLoading = false, skeletonProps, } = props;
const containerWrappingClasses = (0, defaultImports_1.classNames)(`ndl-text-input`, className, {
'ndl-disabled': isDisabled,
'ndl-read-only': isReadOnly,
'ndl-small': size === 'small',
'ndl-medium': size === 'medium',
'ndl-large': size === 'large',
'ndl-has-error': errorText,
'ndl-has-left-icon': leftElement,
'ndl-has-right-icon': rightElement || errorText,
'ndl-has-icon': leftElement || rightElement || errorText,
});
const hasEmptyLabelValue = !label || label === '';
const labelWrappingClasses = (0, defaultImports_1.classNames)('ndl-form-item-label', {
'ndl-fluid': isFluid,
'ndl-form-item-no-label': hasEmptyLabelValue,
});
const combinedHtmlInputAttributes = Object.assign(Object.assign({}, htmlAttributes), { className: (0, defaultImports_1.classNames)('ndl-input', htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.className) });
const ariaLabel = combinedHtmlInputAttributes['aria-label'];
const isCustomLabel = label && typeof label !== 'string';
const hasCustomLabelAndNoAriaLabel = isCustomLabel && !ariaLabel;
(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, defaultImports_1.classNames)({
'ndl-information-icon-small': size === 'small' || size === 'medium',
'ndl-information-icon-large': size === 'large',
});
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", variant: "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 }), informationIconText && ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.tooltipRoot, { type: "simple", children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.tooltipTrigger, { className: informationIconClasses, children: (0, jsx_runtime_1.jsx)(icons_1.InformationCircleIconOutline, {}) })), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Content, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.tooltipContent, { children: informationIconText }))] }))), isOptional && ((0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-large' : 'body-medium', className: "ndl-form-item-optional", children: "Optional" }))] }) }))), (0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", variant: "rectangular" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-input-wrapper", children: [leftElement && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-element-left ndl-element", children: leftElement })), (0, jsx_runtime_1.jsx)("input", Object.assign({ readOnly: isReadOnly, disabled: isDisabled, required: isRequired, value: value, placeholder: placeholder, type: "text", onChange: onChange }, combinedHtmlInputAttributes, { ref: ref })), rightElement && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-element-right ndl-element", children: rightElement }))] }) }))] }), helpText && !errorText && ((0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, { onBackground: "weak", variant: "rectangular", isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-medium' : 'body-small', className: "ndl-form-message", children: helpText }) })), 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", variant: "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)(icons_1.ExclamationCircleIconSolid, {}) }), (0, jsx_runtime_1.jsx)(typography_1.Typography, { className: "ndl-error-text", variant: size === 'large' ? 'body-medium' : 'body-small', children: errorText })] }) })))] }));
});
//# sourceMappingURL=TextInput.js.map