UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TextArea = exports.TextAreaBase = exports.TextAreResizeOrientation = void 0;
4const tslib_1 = require("tslib");
5const React = tslib_1.__importStar(require("react"));
6const form_control_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/FormControl/form-control"));
7const c_form_control_textarea_Height_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_form_control_textarea_Height'));
8const react_styles_1 = require("@patternfly/react-styles");
9const helpers_1 = require("../../helpers");
10var TextAreResizeOrientation;
11(function (TextAreResizeOrientation) {
12 TextAreResizeOrientation["horizontal"] = "horizontal";
13 TextAreResizeOrientation["vertical"] = "vertical";
14 TextAreResizeOrientation["both"] = "both";
15})(TextAreResizeOrientation = exports.TextAreResizeOrientation || (exports.TextAreResizeOrientation = {}));
16class TextAreaBase extends React.Component {
17 constructor(props) {
18 super(props);
19 this.handleChange = (event) => {
20 // https://gomakethings.com/automatically-expand-a-textarea-as-the-user-types-using-vanilla-javascript/
21 const field = event.currentTarget;
22 if (this.props.autoResize && helpers_1.canUseDOM) {
23 field.style.setProperty(c_form_control_textarea_Height_1.default.name, 'inherit');
24 const computed = window.getComputedStyle(field);
25 // Calculate the height
26 const height = parseInt(computed.getPropertyValue('border-top-width')) +
27 parseInt(computed.getPropertyValue('padding-top')) +
28 field.scrollHeight +
29 parseInt(computed.getPropertyValue('padding-bottom')) +
30 parseInt(computed.getPropertyValue('border-bottom-width'));
31 field.style.setProperty(c_form_control_textarea_Height_1.default.name, `${height}px`);
32 }
33 if (this.props.onChange) {
34 this.props.onChange(field.value, event);
35 }
36 };
37 if (!props.id && !props['aria-label']) {
38 // eslint-disable-next-line no-console
39 console.error('TextArea: TextArea requires either an id or aria-label to be specified');
40 }
41 }
42 render() {
43 const _a = this.props, { className, value, validated, isRequired, isDisabled, isIconSprite, isReadOnly, resizeOrientation, innerRef, readOnly, disabled,
44 /* eslint-disable @typescript-eslint/no-unused-vars */
45 autoResize, onChange } = _a,
46 /* eslint-enable @typescript-eslint/no-unused-vars */
47 props = tslib_1.__rest(_a, ["className", "value", "validated", "isRequired", "isDisabled", "isIconSprite", "isReadOnly", "resizeOrientation", "innerRef", "readOnly", "disabled", "autoResize", "onChange"]);
48 const orientation = `resize${helpers_1.capitalize(resizeOrientation)}`;
49 return (React.createElement("textarea", Object.assign({ className: react_styles_1.css(form_control_1.default.formControl, isIconSprite && form_control_1.default.modifiers.iconSprite, className, resizeOrientation !== TextAreResizeOrientation.both && form_control_1.default.modifiers[orientation], validated === helpers_1.ValidatedOptions.success && form_control_1.default.modifiers.success, validated === helpers_1.ValidatedOptions.warning && form_control_1.default.modifiers.warning), onChange: this.handleChange }, (typeof this.props.defaultValue !== 'string' && { value }), { "aria-invalid": validated === helpers_1.ValidatedOptions.error, required: isRequired, disabled: isDisabled || disabled, readOnly: isReadOnly || readOnly, ref: innerRef }, props)));
50 }
51}
52exports.TextAreaBase = TextAreaBase;
53TextAreaBase.displayName = 'TextArea';
54TextAreaBase.defaultProps = {
55 innerRef: React.createRef(),
56 className: '',
57 isRequired: false,
58 isDisabled: false,
59 isIconSprite: false,
60 validated: 'default',
61 resizeOrientation: 'both',
62 'aria-label': null
63};
64exports.TextArea = React.forwardRef((props, ref) => (React.createElement(TextAreaBase, Object.assign({}, props, { innerRef: ref }))));
65exports.TextArea.displayName = 'TextArea';
66//# sourceMappingURL=TextArea.js.map
\No newline at end of file