1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
|
4 | import heightToken from '@patternfly/react-tokens/dist/esm/c_form_control_textarea_Height';
|
5 | import { css } from '@patternfly/react-styles';
|
6 | import { capitalize, ValidatedOptions, canUseDOM } from '../../helpers';
|
7 | export var TextAreResizeOrientation;
|
8 | (function (TextAreResizeOrientation) {
|
9 | TextAreResizeOrientation["horizontal"] = "horizontal";
|
10 | TextAreResizeOrientation["vertical"] = "vertical";
|
11 | TextAreResizeOrientation["both"] = "both";
|
12 | })(TextAreResizeOrientation || (TextAreResizeOrientation = {}));
|
13 | export class TextAreaBase extends React.Component {
|
14 | constructor(props) {
|
15 | super(props);
|
16 | this.handleChange = (event) => {
|
17 |
|
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 |
|
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 |
|
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 |
|
42 | autoResize, onChange } = _a,
|
43 |
|
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 | }
|
49 | TextAreaBase.displayName = 'TextArea';
|
50 | TextAreaBase.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 | };
|
60 | export const TextArea = React.forwardRef((props, ref) => (React.createElement(TextAreaBase, Object.assign({}, props, { innerRef: ref }))));
|
61 | TextArea.displayName = 'TextArea';
|
62 |
|
\ | No newline at end of file |