@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
98 lines • 6.74 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.TextArea = 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 utils_1 = require("../_common/utils");
const ExclamationCircleIconSolid_1 = require("../icons/generated/heroIcons/ExclamationCircleIconSolid");
const InformationCircleIconOutline_1 = require("../icons/generated/heroIcons/InformationCircleIconOutline");
const tooltip_1 = require("../tooltip");
/**
*
*
* TextArea Component
*
*
*/
const TextArea = (_a) => {
var { label, isFluid = false, errorText, helpText, isOptional = false, moreInformationText, size = 'medium', ariaLabel, tooltipProps, isDisabled = false, className, isReadOnly = false, style, value, placeholder, htmlAttributes, ref } = _a, restProps = __rest(_a, ["label", "isFluid", "errorText", "helpText", "isOptional", "moreInformationText", "size", "ariaLabel", "tooltipProps", "isDisabled", "className", "isReadOnly", "style", "value", "placeholder", "htmlAttributes", "ref"]);
const helpTextId = (0, react_1.useId)();
const errorTextId = (0, react_1.useId)();
const containerWrappingClasses = (0, classnames_1.default)(`ndl-text-area ndl-type-text`, className, {
'ndl-disabled': isDisabled,
'ndl-has-error': errorText,
'ndl-has-icon': errorText,
'ndl-has-trailing-icon': errorText,
'ndl-large': size === 'large',
'ndl-medium': size === 'medium',
'ndl-small': size === 'small',
});
const hasEmptyLabelValue = !label || label === '';
const labelWrappingClasses = (0, classnames_1.default)('ndl-text-area-label', {
'ndl-fluid': isFluid,
'ndl-text-area-no-label': hasEmptyLabelValue,
});
const isCustomLabel = label && typeof label !== 'string';
const hasCustomLabelAndNoAriaLabel = isCustomLabel && !ariaLabel;
(0, react_1.useMemo)(() => {
if (!label && !ariaLabel) {
(0, utils_1.needleWarningMessage)('A TextArea 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 (helpText && !errorText) {
ariaDescribedby.push(helpTextId);
}
else if (errorText) {
ariaDescribedby.push(errorTextId);
}
return ariaDescribedby.join(' ');
}, [helpText, errorText, helpTextId, errorTextId]);
return ((0, jsx_runtime_1.jsxs)("div", { className: containerWrappingClasses, children: [(0, jsx_runtime_1.jsxs)("label", { className: labelWrappingClasses, children: [(0, jsx_runtime_1.jsx)("div", { className: "ndl-text-area-wrapper", children: (0, jsx_runtime_1.jsx)("textarea", Object.assign({ disabled: isDisabled, ref: ref, "aria-label": ariaLabel, "aria-invalid": Boolean(errorText), readOnly: isReadOnly, value: value, placeholder: placeholder, style: style, "aria-describedby": combinedAriaDescribedby }, restProps, htmlAttributes)) }), !hasEmptyLabelValue && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-text-area-wrapper", children: [(0, jsx_runtime_1.jsx)("span", { className: "ndl-text-area-label-text", children: label }), Boolean(moreInformationText) && ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, Object.assign({ type: "simple" }, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.root, { children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.trigger, { children: (0, jsx_runtime_1.jsx)("div", { className: informationIconClasses, "data-testid": "ndl-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 }))] }))), isOptional && ((0, jsx_runtime_1.jsx)("span", { className: "ndl-text-area-optional", children: "Optional" }))] }))] }), helpText && !errorText && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-text-area-msg", "aria-live": "polite", id: helpTextId, children: helpText })), errorText && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-text-area-msg", 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)("div", { className: "ndl-error-text", id: errorTextId, "aria-live": "polite", children: errorText })] }))] }));
};
exports.TextArea = TextArea;
//# sourceMappingURL=TextArea.js.map