UNPKG

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