Skip to content

Commit 9ce7457

Browse files
committed
chore(TextInputGroup): Added default as an option for validated
1 parent 911223a commit 9ce7457

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: Content
33
section: components
44
cssPrefix: pf-v6-c-content
5-
propComponents: ['Content']
5+
propComponents: ['Content', 'ContentVariants']
66
---
77

88
The `<Content>` component allows you to establish a block of HTML content and apply simple, built-in styling. `<Content>` can be used for any element supported by the `component` property (including `h1` through `h6`, `hr`, `p`, `a`, `small`, `blockquote`, and `pre`).

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createContext, useRef } from 'react';
22
import styles from '@patternfly/react-styles/css/components/TextInputGroup/text-input-group';
33
import { css } from '@patternfly/react-styles';
4+
import { ValidatedOptions } from '../../helpers/constants';
45

56
export interface TextInputGroupProps extends React.HTMLProps<HTMLDivElement> {
67
/** Content rendered inside the text input group */
@@ -11,8 +12,11 @@ export interface TextInputGroupProps extends React.HTMLProps<HTMLDivElement> {
1112
isDisabled?: boolean;
1213
/** Flag to indicate the toggle has no border or background */
1314
isPlain?: boolean;
14-
/** Status variant of the text input group. */
15-
validated?: 'success' | 'warning' | 'error';
15+
/** Value to indicate if the text input group is modified to show that validation state.
16+
* If set to success, warning, or error, the group will show that state.
17+
* If set to default, no validation styling is applied (use to clear a prior validation state).
18+
*/
19+
validated?: 'success' | 'warning' | 'error' | 'default' | ValidatedOptions;
1620
/** @hide A reference object to attach to the input box */
1721
innerRef?: React.RefObject<any>;
1822
}
@@ -32,6 +36,7 @@ export const TextInputGroup: React.FunctionComponent<TextInputGroupProps> = ({
3236
}: TextInputGroupProps) => {
3337
const ref = useRef(null);
3438
const textInputGroupRef = innerRef || ref;
39+
const hasValidatedModifier = ['success', 'error', 'warning'].includes(validated);
3540

3641
return (
3742
<TextInputGroupContext.Provider value={{ isDisabled, validated }}>
@@ -41,7 +46,7 @@ export const TextInputGroup: React.FunctionComponent<TextInputGroupProps> = ({
4146
styles.textInputGroup,
4247
isDisabled && styles.modifiers.disabled,
4348
isPlain && styles.modifiers.plain,
44-
validated && styles.modifiers[validated],
49+
hasValidatedModifier && styles.modifiers[validated as 'success' | 'warning' | 'error'],
4550
className
4651
)}
4752
{...props}

packages/react-core/src/components/TextInputGroup/TextInputGroupMain.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styles from '@patternfly/react-styles/css/components/TextInputGroup/text-
33
import { css } from '@patternfly/react-styles';
44
import { TextInputGroupContext } from './TextInputGroup';
55
import { TextInputGroupIcon } from './TextInputGroupIcon';
6-
import { statusIcons, ValidatedOptions } from '../../helpers';
6+
import { statusIcons } from '../../helpers';
77

88
export interface TextInputGroupMainProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
99
/** Content rendered inside the text input group main div */
@@ -86,7 +86,19 @@ const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> =
8686
const { isDisabled, validated } = useContext(TextInputGroupContext);
8787
const ref = useRef(null);
8888
const textInputGroupInputInputRef = innerRef || ref;
89-
const StatusIcon = statusIcons[validated === ValidatedOptions.error ? 'danger' : validated];
89+
const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
90+
const StatusIcon = (() => {
91+
if (!hasStatusIcon) {
92+
return undefined;
93+
}
94+
if (validated === 'error') {
95+
return statusIcons.danger;
96+
}
97+
if (validated === 'success') {
98+
return statusIcons.success;
99+
}
100+
return statusIcons.warning;
101+
})();
90102

91103
const handleChange = (event: React.FormEvent<HTMLInputElement>) => {
92104
onChange(event, event.currentTarget.value);
@@ -126,7 +138,7 @@ const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> =
126138
{...(ariaControls && { 'aria-controls': ariaControls })}
127139
{...inputProps}
128140
/>
129-
{validated && <TextInputGroupIcon isStatus>{<StatusIcon />}</TextInputGroupIcon>}
141+
{hasStatusIcon && <TextInputGroupIcon isStatus>{<StatusIcon />}</TextInputGroupIcon>}
130142
</span>
131143
</div>
132144
);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ describe('TextInputGroup', () => {
7272
expect(inputGroup).toHaveClass(styles.modifiers.error);
7373
});
7474

75+
it('does not apply validation modifiers when validated="default"', () => {
76+
render(<TextInputGroup validated="default">Test</TextInputGroup>);
77+
78+
const inputGroup = screen.getByText('Test');
79+
80+
expect(inputGroup).not.toHaveClass(styles.modifiers.success);
81+
expect(inputGroup).not.toHaveClass(styles.modifiers.warning);
82+
expect(inputGroup).not.toHaveClass(styles.modifiers.error);
83+
});
84+
7585
it('passes isDisabled=false to children via a context when isDisabled prop is not passed', () => {
7686
const TestComponent: React.FunctionComponent = () => {
7787
const context = useContext(TextInputGroupContext);

packages/react-core/src/components/TextInputGroup/__tests__/TextInputGroupMain.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ describe('TextInputGroupMain', () => {
251251
expect(screen.getByRole('textbox').nextElementSibling).toHaveClass(styles.modifiers.status);
252252
});
253253

254+
it('does not render a status icon when validated is default', () => {
255+
render(
256+
<TextInputGroupContext.Provider value={{ validated: 'default' }}>
257+
<TextInputGroupMain />
258+
</TextInputGroupContext.Provider>
259+
);
260+
261+
expect(screen.queryByRole('textbox').nextElementSibling).toBeNull();
262+
});
263+
254264
it('does not call onChange callback when the input does not change', () => {
255265
const onChangeMock = jest.fn();
256266

0 commit comments

Comments
 (0)