Debian Codegraph @

node/@patternfly/react-core/dist/js/components/TextArea/TextArea.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.TextArea = exports.TextAreaReadOnlyVariant = exports.TextAreResizeOrientation = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const form_control_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/FormControl/form-control"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const helpers_1 = require("../../helpers");
10 const FormControlIcon_1 = require("../FormControl/FormControlIcon");
11 var TextAreResizeOrientation;
12 (function (TextAreResizeOrientation) {
13 TextAreResizeOrientation["horizontal"] = "horizontal";
14 TextAreResizeOrientation["vertical"] = "vertical";
15 TextAreResizeOrientation["both"] = "both";
16 TextAreResizeOrientation["none"] = "none";
17 })(TextAreResizeOrientation || (exports.TextAreResizeOrientation = TextAreResizeOrientation = {}));
18 var TextAreaReadOnlyVariant;
19 (function (TextAreaReadOnlyVariant) {
20 TextAreaReadOnlyVariant["default"] = "default";
21 TextAreaReadOnlyVariant["plain"] = "plain";
22 })(TextAreaReadOnlyVariant || (exports.TextAreaReadOnlyVariant = TextAreaReadOnlyVariant = {}));
23 class TextAreaBase extends react_1.Component {
24 constructor(props) {
25 super(props);
26 this.inputRef = (0, react_1.createRef)();
27 this.setAutoHeight = (field) => {
28 const parent = field.parentElement;
29 parent.style.setProperty('height', 'inherit');
30 const computed = window.getComputedStyle(field);
31 const parentComputed = window.getComputedStyle(parent);
32 // Calculate the height
33 const height = parseInt(computed.getPropertyValue('border-top-width')) +
34 field.scrollHeight +
35 parseInt(computed.getPropertyValue('border-bottom-width')) +
36 parseInt(parentComputed.getPropertyValue('padding-top')) +
37 parseInt(parentComputed.getPropertyValue('padding-bottom'));
38 parent.style.setProperty('height', `${height}px`);
39 };
40 this.handleChange = (event) => {
41 // https://gomakethings.com/automatically-expand-a-textarea-as-the-user-types-using-vanilla-javascript/
42 const field = event.currentTarget;
43 if (this.props.autoResize && helpers_1.canUseDOM) {
44 this.setAutoHeight(field);
45 }
46 if (this.props.onChange) {
47 this.props.onChange(event, field.value);
48 }
49 };
50 if (!props.id && !props['aria-label']) {
51 // eslint-disable-next-line no-console
52 console.error('TextArea: TextArea requires either an id or aria-label to be specified');
53 }
54 }
55 componentDidMount() {
56 const inputRef = this.props.innerRef || this.inputRef;
57 if (this.props.autoResize && helpers_1.canUseDOM) {
58 const field = inputRef.current;
59 this.setAutoHeight(field);
60 }
61 }
62 render() {
63 const _a = this.props, { className, value, validated, isRequired, isDisabled, readOnlyVariant, resizeOrientation, innerRef, disabled,
64 /* eslint-disable @typescript-eslint/no-unused-vars */
65 autoResize, onChange,
66 /* eslint-enable @typescript-eslint/no-unused-vars */
67 onBlur, onFocus } = _a, props = tslib_1.__rest(_a, ["className", "value", "validated", "isRequired", "isDisabled", "readOnlyVariant", "resizeOrientation", "innerRef", "disabled", "autoResize", "onChange", "onBlur", "onFocus"]);
68 const orientation = resizeOrientation !== 'none'
69 ? `resize${(0, helpers_1.capitalize)(resizeOrientation)}`
70 : undefined;
71 const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
72 return ((0, jsx_runtime_1.jsxs)("span", { className: (0, react_styles_1.css)(form_control_1.default.formControl, form_control_1.default.modifiers.textarea, readOnlyVariant && form_control_1.default.modifiers.readonly, readOnlyVariant === 'plain' && form_control_1.default.modifiers.plain, resizeOrientation !== 'none' && form_control_1.default.modifiers[orientation], isDisabled && form_control_1.default.modifiers.disabled, hasStatusIcon && form_control_1.default.modifiers[validated], className), children: [(0, jsx_runtime_1.jsx)("textarea", Object.assign({ onChange: this.handleChange, onFocus: onFocus, onBlur: onBlur }, (typeof this.props.defaultValue !== 'string' && { value }), { "aria-invalid": validated === helpers_1.ValidatedOptions.error, required: isRequired, disabled: isDisabled || disabled, readOnly: !!readOnlyVariant, ref: innerRef || this.inputRef }, props)), hasStatusIcon && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(form_control_1.default.formControlUtilities), children: (0, jsx_runtime_1.jsx)(FormControlIcon_1.FormControlIcon, { status: validated }) }))] }));
73 }
74 }
75 TextAreaBase.displayName = 'TextArea';
76 TextAreaBase.defaultProps = {
77 innerRef: (0, react_1.createRef)(),
78 className: '',
79 isRequired: false,
80 isDisabled: false,
81 validated: 'default',
82 resizeOrientation: 'both',
83 'aria-label': null
84 };
85 exports.TextArea = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(TextAreaBase, Object.assign({}, props, { innerRef: ref }))));
86 exports.TextArea.displayName = 'TextArea';
87 //# sourceMappingURL=TextArea.js.map