| 1 |
import { createContext, useRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/TextInputGroup/text-input-group'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { ValidatedOptions } from '../../helpers/constants'; |
| 5 |
|
| 6 |
export interface TextInputGroupProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Content rendered inside the text input group */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** Additional classes applied to the text input group container */ |
| 10 |
className?: string; |
| 11 |
/** Adds disabled styling and a disabled context value which text input group main hooks into for the input itself */ |
| 12 |
isDisabled?: boolean; |
| 13 |
/** Flag to indicate the toggle has no border or background */ |
| 14 |
isPlain?: boolean; |
| 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; |
| 20 |
/** @hide A reference object to attach to the input box */ |
| 21 |
innerRef?: React.RefObject<any>; |
| 22 |
} |
| 23 |
|
| 24 |
export const TextInputGroupContext = createContext<Partial<TextInputGroupProps>>({ |
| 25 |
isDisabled: false |
| 26 |
}); |
| 27 |
|
| 28 |
export const TextInputGroup: React.FunctionComponent<TextInputGroupProps> = ({ |
| 29 |
children, |
| 30 |
className, |
| 31 |
isDisabled, |
| 32 |
isPlain, |
| 33 |
validated, |
| 34 |
innerRef, |
| 35 |
...props |
| 36 |
}: TextInputGroupProps) => { |
| 37 |
const ref = useRef(null); |
| 38 |
const textInputGroupRef = innerRef || ref; |
| 39 |
const hasValidatedModifier = ['success', 'error', 'warning'].includes(validated); |
| 40 |
|
| 41 |
return ( |
| 42 |
<TextInputGroupContext.Provider value={{ isDisabled, validated }}> |
| 43 |
<div |
| 44 |
ref={textInputGroupRef} |
| 45 |
className={css( |
| 46 |
styles.textInputGroup, |
| 47 |
isDisabled && styles.modifiers.disabled, |
| 48 |
isPlain && styles.modifiers.plain, |
| 49 |
hasValidatedModifier && styles.modifiers[validated as 'success' | 'warning' | 'error'], |
| 50 |
className |
| 51 |
)} |
| 52 |
{...props} |
| 53 |
> |
| 54 |
{children} |
| 55 |
</div> |
| 56 |
</TextInputGroupContext.Provider> |
| 57 |
); |
| 58 |
}; |
| 59 |
|
| 60 |
TextInputGroup.displayName = 'TextInputGroup'; |