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