Skip to content

Commit 9868ab5

Browse files
authored
feat(Text area): Add resize disable feature (#11383)
* feat(Text area): Add reasize disable feature * change disabled to none * update tests * add resize none example
1 parent c8cbc6d commit 9868ab5

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

packages/react-core/src/components/TextArea/TextArea.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { FormControlIcon } from '../FormControl/FormControlIcon';
88
export enum TextAreResizeOrientation {
99
horizontal = 'horizontal',
1010
vertical = 'vertical',
11-
both = 'both'
11+
both = 'both',
12+
none = 'none'
1213
}
1314

1415
export enum TextAreaReadOnlyVariant {
@@ -37,7 +38,7 @@ export interface TextAreaProps extends Omit<HTMLProps<HTMLTextAreaElement>, 'onC
3738
/** A callback for when the text area value changes. */
3839
onChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, value: string) => void;
3940
/** Sets the orientation to limit the resize to */
40-
resizeOrientation?: 'horizontal' | 'vertical' | 'both';
41+
resizeOrientation?: 'horizontal' | 'vertical' | 'both' | 'none';
4142
/** Custom flag to show that the text area requires an associated id or aria-label. */
4243
'aria-label'?: string;
4344
/** @hide A reference object to attach to the text area. */
@@ -118,10 +119,10 @@ class TextAreaBase extends React.Component<TextAreaProps> {
118119
onFocus,
119120
...props
120121
} = this.props;
121-
const orientation = `resize${capitalize(resizeOrientation)}` as
122-
| 'resizeVertical'
123-
| 'resizeHorizontal'
124-
| 'resizeBoth';
122+
const orientation =
123+
resizeOrientation !== 'none'
124+
? (`resize${capitalize(resizeOrientation)}` as 'resizeVertical' | 'resizeHorizontal' | 'resizeBoth')
125+
: undefined;
125126
const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
126127

127128
return (
@@ -130,7 +131,7 @@ class TextAreaBase extends React.Component<TextAreaProps> {
130131
styles.formControl,
131132
readOnlyVariant && styles.modifiers.readonly,
132133
readOnlyVariant === 'plain' && styles.modifiers.plain,
133-
resizeOrientation && styles.modifiers[orientation],
134+
resizeOrientation !== 'none' && styles.modifiers[orientation],
134135
isDisabled && styles.modifiers.disabled,
135136
hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'],
136137
className

packages/react-core/src/components/TextArea/__tests__/TextArea.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import '@testing-library/jest-dom';
23

34
import { render, screen } from '@testing-library/react';
45
import userEvent from '@testing-library/user-event';
@@ -93,6 +94,16 @@ test('Text area is not invalid by default', () => {
9394
expect(screen.getByRole('textbox')).not.toBeInvalid();
9495
});
9596

97+
test('Renders vertically & resizable text area by default', () => {
98+
render(<TextArea aria-label="vertical resize textarea" />);
99+
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-both');
100+
});
101+
102+
test('Renders vertically & resizable text area when resizeOrientation is set to both', () => {
103+
render(<TextArea aria-label="vertical resize textarea" resizeOrientation="both" />);
104+
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-both');
105+
});
106+
96107
test('Renders vertically resizable text area', () => {
97108
render(<TextArea aria-label="vertical resize textarea" resizeOrientation="vertical" />);
98109
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-vertical');
@@ -103,6 +114,13 @@ test('Renders horizontally resizable text area', () => {
103114
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-horizontal');
104115
});
105116

117+
test('Disables resizable text area', () => {
118+
render(<TextArea aria-label="disabled resize textarea" resizeOrientation="none" />);
119+
expect(screen.getByRole('textbox').parentElement).not.toHaveClass('pf-m-resize-vertical');
120+
expect(screen.getByRole('textbox').parentElement).not.toHaveClass('pf-m-resize-horizontal');
121+
expect(screen.getByRole('textbox').parentElement).not.toHaveClass('pf-m-resize-both');
122+
});
123+
106124
test('Throws console error when no aria-label or id is given', () => {
107125
jest.spyOn(global.console, 'error');
108126

packages/react-core/src/components/TextArea/examples/TextArea.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ propComponents: ['TextArea']
3838

3939
```
4040

41+
### Not resizable
42+
43+
```ts file="./TextAreaResizableNone.tsx"
44+
45+
```
46+
4147
### Uncontrolled
4248

4349
```ts file="./TextAreaUncontrolled.tsx"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { TextArea } from '@patternfly/react-core';
3+
4+
export const TextAreaResizeNone: React.FunctionComponent = () => {
5+
const [value, setValue] = React.useState('');
6+
return (
7+
<TextArea
8+
value={value}
9+
onChange={(_event, value) => setValue(value)}
10+
resizeOrientation="none"
11+
aria-label="text area resize none example"
12+
/>
13+
);
14+
};

0 commit comments

Comments
 (0)