Debian Codegraph @

node/@patternfly/react-core/src/components/TextArea/TextArea.tsx

raw
1 import { Component, HTMLProps, createRef, forwardRef } from 'react';
2 import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
3 import { css } from '@patternfly/react-styles';
4 import { capitalize, ValidatedOptions, canUseDOM } from '../../helpers';
5 import { FormControlIcon } from '../FormControl/FormControlIcon';
6
7 export enum TextAreResizeOrientation {
8 horizontal = 'horizontal',
9 vertical = 'vertical',
10 both = 'both',
11 none = 'none'
12 }
13
14 export enum TextAreaReadOnlyVariant {
15 default = 'default',
16 plain = 'plain'
17 }
18
19 export interface TextAreaProps extends Omit<HTMLProps<HTMLTextAreaElement>, 'onChange' | 'ref'> {
20 /** Additional classes added to the text area. */
21 className?: string;
22 /** Flag to show if the text area is required. */
23 isRequired?: boolean;
24 /** Flag to show if the text area is disabled. */
25 isDisabled?: boolean;
26 /** Read only variant. */
27 readOnlyVariant?: 'default' | 'plain';
28 /** Flag to modify height based on contents. */
29 autoResize?: boolean;
30 /** Value to indicate if the text area is modified to show that validation state.
31 * If set to success, text area will be modified to indicate valid state.
32 * If set to error, text area will be modified to indicate error state.
33 */
34 validated?: 'success' | 'warning' | 'error' | 'default';
35 /** Value of the text area. */
36 value?: string | number;
37 /** A callback for when the text area value changes. */
38 onChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => void;
39 /** Sets the orientation to limit the resize to */
40 resizeOrientation?: 'horizontal' | 'vertical' | 'both' | 'none';
41 /** Custom flag to show that the text area requires an associated id or aria-label. */
42 'aria-label'?: string;
43 /** @hide A reference object to attach to the text area. */
44 innerRef?: React.RefObject<any>;
45 }
46
47 class TextAreaBase extends Component<TextAreaProps> {
48 static displayName = 'TextArea';
49 static defaultProps: TextAreaProps = {
50 innerRef: createRef<HTMLTextAreaElement>(),
51 className: '',
52 isRequired: false,
53 isDisabled: false,
54 validated: 'default',
55 resizeOrientation: 'both',
56 'aria-label': null as string
57 };
58
59 inputRef = createRef<HTMLTextAreaElement>();
60
61 private setAutoHeight = (field: HTMLTextAreaElement) => {
62 const parent = field.parentElement;
63 parent.style.setProperty('height', 'inherit');
64 const computed = window.getComputedStyle(field);
65 const parentComputed = window.getComputedStyle(parent);
66 // Calculate the height
67 const height =
68 parseInt(computed.getPropertyValue('border-top-width')) +
69 field.scrollHeight +
70 parseInt(computed.getPropertyValue('border-bottom-width')) +
71 parseInt(parentComputed.getPropertyValue('padding-top')) +
72 parseInt(parentComputed.getPropertyValue('padding-bottom'));
73
74 parent.style.setProperty('height', `${height}px`);
75 };
76
77 constructor(props: TextAreaProps) {
78 super(props);
79 if (!props.id && !props['aria-label']) {
80 // eslint-disable-next-line no-console
81 console.error('TextArea: TextArea requires either an id or aria-label to be specified');
82 }
83 }
84
85 componentDidMount(): void {
86 const inputRef = this.props.innerRef || this.inputRef;
87 if (this.props.autoResize && canUseDOM) {
88 const field = inputRef.current;
89 this.setAutoHeight(field);
90 }
91 }
92
93 private handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
94 // https://gomakethings.com/automatically-expand-a-textarea-as-the-user-types-using-vanilla-javascript/
95 const field = event.currentTarget;
96 if (this.props.autoResize && canUseDOM) {
97 this.setAutoHeight(field);
98 }
99 if (this.props.onChange) {
100 this.props.onChange(event, field.value);
101 }
102 };
103
104 render() {
105 const {
106 className,
107 value,
108 validated,
109 isRequired,
110 isDisabled,
111 readOnlyVariant,
112 resizeOrientation,
113 innerRef,
114 disabled,
115 /* eslint-disable @typescript-eslint/no-unused-vars */
116 autoResize,
117 onChange,
118 /* eslint-enable @typescript-eslint/no-unused-vars */
119 onBlur,
120 onFocus,
121 ...props
122 } = this.props;
123 const orientation =
124 resizeOrientation !== 'none'
125 ? (`resize${capitalize(resizeOrientation)}` as 'resizeVertical' | 'resizeHorizontal' | 'resizeBoth')
126 : undefined;
127 const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
128
129 return (
130 <span
131 className={css(
132 styles.formControl,
133 styles.modifiers.textarea,
134 readOnlyVariant && styles.modifiers.readonly,
135 readOnlyVariant === 'plain' && styles.modifiers.plain,
136 resizeOrientation !== 'none' && styles.modifiers[orientation],
137 isDisabled && styles.modifiers.disabled,
138 hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'],
139 className
140 )}
141 >
142 <textarea
143 onChange={this.handleChange}
144 onFocus={onFocus}
145 onBlur={onBlur}
146 {...(typeof this.props.defaultValue !== 'string' && { value })}
147 aria-invalid={validated === ValidatedOptions.error}
148 required={isRequired}
149 disabled={isDisabled || disabled}
150 readOnly={!!readOnlyVariant}
151 ref={innerRef || this.inputRef}
152 {...props}
153 />
154 {hasStatusIcon && (
155 <span className={css(styles.formControlUtilities)}>
156 <FormControlIcon status={validated as 'success' | 'error' | 'warning'} />
157 </span>
158 )}
159 </span>
160 );
161 }
162 }
163
164 export const TextArea = forwardRef((props: TextAreaProps, ref: React.Ref<HTMLTextAreaElement>) => (
165 <TextAreaBase {...props} innerRef={ref as React.MutableRefObject<any>} />
166 ));
167 TextArea.displayName = 'TextArea';