@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
277 lines (273 loc) • 9.41 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React__default, { useContext, useState, useRef, useEffect } from 'react';
import cx from 'classnames';
import deprecate from '../../prop-types/deprecate.js';
import { usePrefix } from '../../internal/usePrefix.js';
import '../FluidForm/FluidForm.js';
import { FormContext } from '../FluidForm/FormContext.js';
import { useAnnouncer } from '../../internal/useAnnouncer.js';
import useIsomorphicEffect from '../../internal/useIsomorphicEffect.js';
import { useMergedRefs } from '../../internal/useMergedRefs.js';
import setupGetInstanceId from '../../tools/setupGetInstanceId.js';
var _span, _span2;
const getInstanceId = setupGetInstanceId();
const TextArea = /*#__PURE__*/React__default.forwardRef((props, forwardRef) => {
const {
className,
id,
labelText,
hideLabel,
onChange = () => {},
onClick = () => {},
invalid = false,
invalidText = '',
helperText = '',
light,
placeholder = '',
enableCounter = false,
maxCount,
warn = false,
warnText = '',
rows = 4,
...other
} = props;
const prefix = usePrefix();
const {
isFluid
} = useContext(FormContext);
const {
defaultValue,
value,
disabled
} = other;
const [textCount, setTextCount] = useState(defaultValue?.toString()?.length || value?.toString()?.length || 0);
const {
current: textAreaInstanceId
} = useRef(getInstanceId());
useEffect(() => {
setTextCount(defaultValue?.toString()?.length || value?.toString()?.length || 0);
}, [value, defaultValue]);
const textareaProps = {
id,
onChange: evt => {
if (!other.disabled && onChange) {
// delay textCount assignation to give the textarea element value time to catch up if is a controlled input
setTimeout(() => {
setTextCount(evt.target?.value?.length);
}, 0);
onChange(evt);
}
},
onClick: evt => {
if (!other.disabled && onClick) {
onClick(evt);
}
}
};
if (enableCounter) {
textareaProps.maxLength = maxCount;
}
const ariaAnnouncement = useAnnouncer(textCount, maxCount);
const labelClasses = cx(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel && !isFluid,
[`${prefix}--label--disabled`]: disabled
});
const label = labelText ? /*#__PURE__*/React__default.createElement("label", {
htmlFor: id,
className: labelClasses
}, labelText) : null;
const counterClasses = cx(`${prefix}--label`, {
[`${prefix}--label--disabled`]: disabled
});
const counter = enableCounter && maxCount ? /*#__PURE__*/React__default.createElement("div", {
className: counterClasses
}, `${textCount}/${maxCount}`) : null;
const helperTextClasses = cx(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: other.disabled
});
const helperId = !helperText ? undefined : `text-area-helper-text-${textAreaInstanceId}`;
const helper = helperText ? /*#__PURE__*/React__default.createElement("div", {
id: helperId,
className: helperTextClasses
}, helperText) : null;
const errorId = id + '-error-msg';
const error = invalid ?
/*#__PURE__*/
// <div role='alert' className={`${prefix}--form-requirement`} id={errorId}>
// {invalidText}
// {isFluid && (
// <WarningFilled className={`${prefix}--text-area__invalid-icon`} />
// )}
// </div>
React__default.createElement("div", {
role: "alert",
className: `msk-validation-msg`
}, _span || (_span = /*#__PURE__*/React__default.createElement("span", {
className: `msk-validation-msg--icon msk-icon`
}, "error")), /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, invalidText)) : null;
const warning = warn ?
/*#__PURE__*/
// <div role="alert" className={`${prefix}--form-requirement`}>
// {warnText}
// {isFluid && (
// <WarningAltFilled
// className={`${prefix}--text-area__invalid-icon ${prefix}--text-area__invalid-icon--warning`}
// />
// )}
// </div>
React__default.createElement("div", {
role: "alert",
className: `msk-validation-msg`
}, _span2 || (_span2 = /*#__PURE__*/React__default.createElement("span", {
className: `msk-validation-msg--icon msk-validation-msg--icon--warning msk-icon`
}, "warning")), /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, warnText)) : null;
const textareaClasses = cx(`${prefix}--text-area`, {
[`${prefix}--text-area--light`]: light,
[`${prefix}--text-area--invalid`]: invalid,
[`${prefix}--text-area--warn`]: warn
});
const textareaRef = useRef(null);
const ref = useMergedRefs([forwardRef, textareaRef]);
useIsomorphicEffect(() => {
if (other.cols && textareaRef.current) {
textareaRef.current.style.width = '';
textareaRef.current.style.resize = 'none';
} else if (textareaRef.current) {
textareaRef.current.style.width = `100%`;
}
}, [other.cols]);
let ariaDescribedBy;
if (invalid) {
ariaDescribedBy = errorId;
} else if (!invalid && !warn && !isFluid && helperText) {
ariaDescribedBy = helperId;
}
const input = /*#__PURE__*/React__default.createElement("textarea", _extends({
rows: rows
}, other, textareaProps, {
placeholder: placeholder,
className: textareaClasses,
"aria-invalid": invalid,
"aria-describedby": ariaDescribedBy,
disabled: other.disabled,
readOnly: other.readOnly,
ref: ref
}));
return /*#__PURE__*/React__default.createElement("div", {
className: cx(`${prefix}--form-item`, className)
}, /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--text-area__label-wrapper`
}, label, counter), /*#__PURE__*/React__default.createElement("div", {
className: cx(`${prefix}--text-area__wrapper`, {
[`${prefix}--text-area__wrapper--readonly`]: other.readOnly,
[`${prefix}--text-area__wrapper--warn`]: warn
}),
"data-invalid": invalid || null
}, input, /*#__PURE__*/React__default.createElement("span", {
className: `${prefix}--text-area__counter-alert`,
role: "alert"
}, ariaAnnouncement), isFluid && /*#__PURE__*/React__default.createElement("hr", {
className: `${prefix}--text-area__divider`
}), isFluid && invalid ? error : null, isFluid && warn && !invalid ? warning : null), !invalid && !warn && !isFluid ? helper : null, invalid && !isFluid ? error : null, warn && !invalid && !isFluid ? warning : null);
});
TextArea.displayName = 'TextArea';
TextArea.propTypes = {
/**
* Provide a custom className that is applied directly to the underlying
* `<textarea>` node
*/
className: PropTypes.string,
/**
* Specify the `cols` attribute for the underlying `<textarea>` node
*/
cols: PropTypes.number,
/**
* Optionally provide the default value of the `<textarea>`
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the control is disabled
*/
disabled: PropTypes.bool,
/**
* Specify whether to display the character counter
*/
enableCounter: PropTypes.bool,
/**
* Provide text that is used alongside the control label for additional help
*/
helperText: PropTypes.node,
/**
* Specify whether you want the underlying label to be visually hidden
*/
hideLabel: PropTypes.bool,
/**
* Provide a unique identifier for the control
*/
id: PropTypes.string,
/**
* Specify whether the control is currently invalid
*/
invalid: PropTypes.bool,
/**
* Provide the text that is displayed when the control is in an invalid state
*/
invalidText: PropTypes.node,
/**
* Provide the text that will be read by a screen reader when visiting this
* control
*/
labelText: PropTypes.node.isRequired,
/**
* `true` to use the light version. For use on $ui-01 backgrounds only.
* Don't use this to make tile background color same as container background color.
*/
light: deprecate(PropTypes.bool, 'The `light` prop for `TextArea` has ' + 'been deprecated in favor of the new `Layer` component. It will be removed in the next major release.'),
/**
* Max character count allowed for the textarea. This is needed in order for enableCounter to display
*/
maxCount: PropTypes.number,
/**
* Optionally provide an `onChange` handler that is called whenever `<textarea>`
* is updated
*/
onChange: PropTypes.func,
/**
* Optionally provide an `onClick` handler that is called whenever the
* `<textarea>` is clicked
*/
onClick: PropTypes.func,
/**
* Specify the placeholder attribute for the `<textarea>`
*/
placeholder: PropTypes.string,
/**
* Whether the textarea should be read-only
*/
readOnly: PropTypes.bool,
/**
* Specify the rows attribute for the `<textarea>`
*/
rows: PropTypes.number,
/**
* Provide the current value of the `<textarea>`
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the control is currently in warning state
*/
warn: PropTypes.bool,
/**
* Provide the text that is displayed when the control is in warning state
*/
warnText: PropTypes.node
};
export { TextArea as default };