Debian Codegraph @

node/@patternfly/react-core/src/components/TextInputGroup/TextInputGroupMain.tsx

raw
1 import { forwardRef, useContext, 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 { TextInputGroupContext } from './TextInputGroup';
5 import { TextInputGroupIcon } from './TextInputGroupIcon';
6 import { statusIcons } from '../../helpers';
7
8 export interface TextInputGroupMainProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
9 /** Content rendered inside the text input group main div */
10 children?: React.ReactNode;
11 /** Additional classes applied to the text input group main container */
12 className?: string;
13 /** Icon to be shown on the left side of the text input group main container */
14 icon?: React.ReactNode;
15 /** Type that the input accepts. */
16 type?:
17 | 'text'
18 | 'date'
19 | 'datetime-local'
20 | 'email'
21 | 'month'
22 | 'number'
23 | 'password'
24 | 'search'
25 | 'tel'
26 | 'time'
27 | 'url';
28 /** Suggestion that will show up like a placeholder even with text in the input */
29 hint?: string;
30 /** Callback for when there is a change in the input field*/
31 onChange?: (event: React.FormEvent<HTMLInputElement>, value: string) => void;
32 /** Callback for when the input field is focused*/
33 onFocus?: (event?: any) => void;
34 /** Callback for when focus is lost on the input field*/
35 onBlur?: (event?: any) => void;
36 /** Accessibility label for the input */
37 'aria-label'?: string;
38 /** Value for the input */
39 value?: string | number;
40 /** Placeholder value for the input */
41 placeholder?: string;
42 /** @hide A reference object to attach to the input box */
43 innerRef?: React.RefObject<any>;
44 /** Name for the input */
45 name?: string;
46 /** The id of the active element. Required if role has a value of "combobox", and focus
47 * should remain on the input.
48 */
49 'aria-activedescendant'?: string;
50 /** Determines the accessible role of the input. */
51 role?: string;
52 /** Flag for whether an associated element controlled by the input is visible. Required if
53 * role has a value of "combobox".
54 */
55 isExpanded?: boolean;
56 /** The id of the element(s) controlled by the input. Required if role has a value of "combobox". */
57 'aria-controls'?: string;
58 /** The id of the input element */
59 inputId?: string;
60 /** Additional props to spread to the input element. */
61 inputProps?: any;
62 }
63
64 const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> = ({
65 children,
66 className,
67 icon,
68 type = 'text',
69 hint,
70 onChange = (): any => undefined,
71 onFocus,
72 onBlur,
73 'aria-label': ariaLabel = 'Type to filter',
74 value: inputValue,
75 placeholder: inputPlaceHolder,
76 innerRef,
77 name,
78 'aria-activedescendant': ariaActivedescendant,
79 role,
80 isExpanded,
81 'aria-controls': ariaControls,
82 inputId,
83 inputProps,
84 ...props
85 }: TextInputGroupMainProps) => {
86 const { isDisabled, validated } = useContext(TextInputGroupContext);
87 const ref = useRef(null);
88 const textInputGroupInputInputRef = innerRef || ref;
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 })();
102
103 const handleChange = (event: React.FormEvent<HTMLInputElement>) => {
104 onChange(event, event.currentTarget.value);
105 };
106
107 return (
108 <div className={css(styles.textInputGroupMain, icon && styles.modifiers.icon, className)} {...props}>
109 {children}
110 <span className={css(styles.textInputGroupText)}>
111 {hint && (
112 <input
113 className={css(styles.textInputGroupTextInput, styles.modifiers.hint)}
114 type="text"
115 disabled
116 aria-hidden="true"
117 value={hint}
118 id={inputId}
119 />
120 )}
121 {icon && <TextInputGroupIcon>{icon}</TextInputGroupIcon>}
122 <input
123 ref={textInputGroupInputInputRef}
124 type={type}
125 className={css(styles.textInputGroupTextInput)}
126 aria-label={ariaLabel}
127 disabled={isDisabled}
128 onChange={handleChange}
129 onFocus={onFocus}
130 onBlur={onBlur}
131 value={inputValue || ''}
132 placeholder={inputPlaceHolder}
133 name={name}
134 aria-activedescendant={ariaActivedescendant}
135 id={inputId}
136 {...(role && { role })}
137 {...(isExpanded !== undefined && { 'aria-expanded': isExpanded })}
138 {...(ariaControls && { 'aria-controls': ariaControls })}
139 {...inputProps}
140 />
141 {hasStatusIcon && <TextInputGroupIcon isStatus>{<StatusIcon />}</TextInputGroupIcon>}
142 </span>
143 </div>
144 );
145 };
146
147 export const TextInputGroupMain = forwardRef((props: TextInputGroupMainProps, ref: React.Ref<HTMLInputElement>) => (
148 <TextInputGroupMainBase innerRef={ref as React.MutableRefObject<any>} {...props} />
149 ));
150
151 TextInputGroupMain.displayName = 'TextInputGroupMain';