Debian Codegraph @

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

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