| 1 |
import { createRef, Component, forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/FormControl/form-control'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { ValidatedOptions } from '../../helpers/constants'; |
| 5 |
import { trimLeft } from '../../helpers/util'; |
| 6 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 7 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 8 |
import { getResizeObserver } from '../../helpers/resizeObserver'; |
| 9 |
import { FormControlIcon } from '../FormControl/FormControlIcon'; |
| 10 |
|
| 11 |
export enum TextInputTypes { |
| 12 |
text = 'text', |
| 13 |
date = 'date', |
| 14 |
datetimeLocal = 'datetime-local', |
| 15 |
email = 'email', |
| 16 |
month = 'month', |
| 17 |
number = 'number', |
| 18 |
password = 'password', |
| 19 |
search = 'search', |
| 20 |
tel = 'tel', |
| 21 |
time = 'time', |
| 22 |
url = 'url' |
| 23 |
} |
| 24 |
|
| 25 |
export enum TextInputReadOnlyVariant { |
| 26 |
default = 'default', |
| 27 |
plain = 'plain' |
| 28 |
} |
| 29 |
|
| 30 |
export interface TextInputExpandedObj { |
| 31 |
/** Flag to apply expanded styling. */ |
| 32 |
isExpanded: boolean; |
| 33 |
/** Id of the element that the text input is controlling expansion of. */ |
| 34 |
ariaControls: string; |
| 35 |
} |
| 36 |
|
| 37 |
export interface TextInputProps |
| 38 |
extends Omit<React.HTMLProps<HTMLInputElement>, 'onChange' | 'onFocus' | 'onBlur' | 'disabled' | 'ref'>, OUIAProps { |
| 39 |
/** Additional classes added to the text input. */ |
| 40 |
className?: string; |
| 41 |
/** Flag to show if the text input is disabled. */ |
| 42 |
isDisabled?: boolean; |
| 43 |
/** @deprecated Flag to apply expanded styling. expandedProps should now be used instead. */ |
| 44 |
isExpanded?: boolean; |
| 45 |
/** Prop to apply expanded styling to the text input and link it to the element it is controlling. This should be used when the input controls a menu and that menu is expandable. */ |
| 46 |
expandedProps?: TextInputExpandedObj; |
| 47 |
/** Sets the input as readonly and determines the readonly styling. */ |
| 48 |
readOnlyVariant?: 'plain' | 'default'; |
| 49 |
/** Flag indicating whether the input is required */ |
| 50 |
isRequired?: boolean; |
| 51 |
/** Value to indicate if the text input is modified to show that validation state. |
| 52 |
* If set to success, text input will be modified to indicate valid state. |
| 53 |
* If set to error, text input will be modified to indicate error state. |
| 54 |
*/ |
| 55 |
validated?: 'success' | 'warning' | 'error' | 'default'; |
| 56 |
/** A callback for when the text input value changes. */ |
| 57 |
onChange?: (event: React.FormEvent<HTMLInputElement>, value: string) => void; |
| 58 |
/** Type that the text input accepts. */ |
| 59 |
type?: |
| 60 |
| 'text' |
| 61 |
| 'date' |
| 62 |
| 'datetime-local' |
| 63 |
| 'email' |
| 64 |
| 'month' |
| 65 |
| 'number' |
| 66 |
| 'password' |
| 67 |
| 'search' |
| 68 |
| 'tel' |
| 69 |
| 'time' |
| 70 |
| 'url'; |
| 71 |
/** Value of the text input. */ |
| 72 |
value?: string | number; |
| 73 |
/** Placeholder of the text input when there is no value */ |
| 74 |
placeholder?: string; |
| 75 |
/** Aria-label. The text input requires an associated id or aria-label. */ |
| 76 |
'aria-label'?: string; |
| 77 |
/** @hide A reference object to attach to the text input box. */ |
| 78 |
innerRef?: React.RefObject<any>; |
| 79 |
/** @deprecated Use isStartTruncated instead. Trim text at start */ |
| 80 |
isLeftTruncated?: boolean; |
| 81 |
/** Trim text at start */ |
| 82 |
isStartTruncated?: boolean; |
| 83 |
/** Callback function when text input is focused */ |
| 84 |
onFocus?: (event?: any) => void; |
| 85 |
/** Callback function when text input is blurred (focus leaves) */ |
| 86 |
onBlur?: (event?: any) => void; |
| 87 |
/** Custom icon to render. If the validated prop is also passed, this will render an icon in addition to a validated icon. */ |
| 88 |
customIcon?: React.ReactNode; |
| 89 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 90 |
ouiaId?: number | string; |
| 91 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 92 |
ouiaSafe?: boolean; |
| 93 |
} |
| 94 |
|
| 95 |
export class TextInputBase extends Component<TextInputProps> { |
| 96 |
static displayName = 'TextInputBase'; |
| 97 |
static defaultProps: TextInputProps = { |
| 98 |
'aria-label': null, |
| 99 |
isRequired: false, |
| 100 |
validated: 'default' as 'success' | 'warning' | 'error' | 'default', |
| 101 |
isDisabled: false, |
| 102 |
isExpanded: false, |
| 103 |
type: TextInputTypes.text, |
| 104 |
isLeftTruncated: false, |
| 105 |
isStartTruncated: false, |
| 106 |
onChange: (): any => undefined, |
| 107 |
ouiaSafe: true |
| 108 |
}; |
| 109 |
inputRef = createRef<HTMLInputElement>(); |
| 110 |
observer: any = () => {}; |
| 111 |
|
| 112 |
constructor(props: TextInputProps) { |
| 113 |
super(props); |
| 114 |
if (!props.id && !props['aria-label'] && !props['aria-labelledby']) { |
| 115 |
// eslint-disable-next-line no-console |
| 116 |
console.error('Text input:', 'Text input requires either an id or aria-label to be specified'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
handleChange = (event: React.FormEvent<HTMLInputElement>) => { |
| 121 |
if (this.props.onChange) { |
| 122 |
this.props.onChange(event, event.currentTarget.value); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
componentDidMount() { |
| 127 |
if (this.props.isLeftTruncated || this.props.isStartTruncated) { |
| 128 |
const inputRef = this.props.innerRef || this.inputRef; |
| 129 |
this.observer = getResizeObserver(inputRef.current, this.handleResize, true); |
| 130 |
this.handleResize(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
componentWillUnmount() { |
| 135 |
if (this.props.isLeftTruncated || this.props.isStartTruncated) { |
| 136 |
this.observer(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
handleResize = () => { |
| 141 |
const inputRef = this.props.innerRef || this.inputRef; |
| 142 |
if (inputRef && inputRef.current) { |
| 143 |
trimLeft(inputRef.current, String(this.props.value)); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
restoreText = () => { |
| 148 |
const inputRef = this.props.innerRef || this.inputRef; |
| 149 |
// restore the value |
| 150 |
(inputRef.current as HTMLInputElement).value = String(this.props.value); |
| 151 |
// make sure we still see the rightmost value to preserve cursor click position |
| 152 |
inputRef.current.scrollLeft = inputRef.current.scrollWidth; |
| 153 |
}; |
| 154 |
|
| 155 |
onFocus = (event?: any) => { |
| 156 |
const { isLeftTruncated, isStartTruncated, onFocus } = this.props; |
| 157 |
if (isLeftTruncated || isStartTruncated) { |
| 158 |
this.restoreText(); |
| 159 |
} |
| 160 |
onFocus && onFocus(event); |
| 161 |
}; |
| 162 |
|
| 163 |
onBlur = (event?: any) => { |
| 164 |
const { isLeftTruncated, isStartTruncated, onBlur } = this.props; |
| 165 |
if (isLeftTruncated || isStartTruncated) { |
| 166 |
this.handleResize(); |
| 167 |
} |
| 168 |
onBlur && onBlur(event); |
| 169 |
}; |
| 170 |
|
| 171 |
render() { |
| 172 |
const { |
| 173 |
innerRef, |
| 174 |
className, |
| 175 |
type, |
| 176 |
value, |
| 177 |
placeholder, |
| 178 |
validated, |
| 179 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 180 |
onChange, |
| 181 |
onFocus, |
| 182 |
onBlur, |
| 183 |
isLeftTruncated, |
| 184 |
isStartTruncated, |
| 185 |
isExpanded, |
| 186 |
expandedProps, |
| 187 |
readOnly, |
| 188 |
readOnlyVariant, |
| 189 |
isRequired, |
| 190 |
isDisabled, |
| 191 |
customIcon, |
| 192 |
ouiaId, |
| 193 |
ouiaSafe, |
| 194 |
...props |
| 195 |
} = this.props; |
| 196 |
|
| 197 |
const hasStatusIcon = ['success', 'error', 'warning'].includes(validated); |
| 198 |
const ariaExpandedProps = expandedProps |
| 199 |
? { 'aria-expanded': expandedProps?.isExpanded, 'aria-controls': expandedProps?.ariaControls, role: 'combobox' } |
| 200 |
: {}; |
| 201 |
|
| 202 |
return ( |
| 203 |
<SSRSafeIds prefix="pf-" ouiaComponentType={TextInputBase.displayName}> |
| 204 |
{(_, generatedOuiaId) => ( |
| 205 |
<span |
| 206 |
className={css( |
| 207 |
styles.formControl, |
| 208 |
readOnlyVariant && styles.modifiers.readonly, |
| 209 |
readOnlyVariant === 'plain' && styles.modifiers.plain, |
| 210 |
isDisabled && styles.modifiers.disabled, |
| 211 |
(isExpanded || expandedProps?.isExpanded) && styles.modifiers.expanded, |
| 212 |
customIcon && styles.modifiers.icon, |
| 213 |
hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'], |
| 214 |
className |
| 215 |
)} |
| 216 |
> |
| 217 |
<input |
| 218 |
{...props} |
| 219 |
onFocus={this.onFocus} |
| 220 |
onBlur={this.onBlur} |
| 221 |
onChange={this.handleChange} |
| 222 |
type={type} |
| 223 |
value={this.sanitizeInputValue(value)} |
| 224 |
aria-invalid={props['aria-invalid'] ? props['aria-invalid'] : validated === ValidatedOptions.error} |
| 225 |
{...ariaExpandedProps} |
| 226 |
required={isRequired} |
| 227 |
disabled={isDisabled} |
| 228 |
readOnly={!!readOnlyVariant || readOnly} |
| 229 |
ref={innerRef || this.inputRef} |
| 230 |
placeholder={placeholder} |
| 231 |
{...getOUIAProps(TextInput.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)} |
| 232 |
/> |
| 233 |
{(customIcon || hasStatusIcon) && ( |
| 234 |
<span className={css(styles.formControlUtilities)}> |
| 235 |
{customIcon && <FormControlIcon customIcon={customIcon} />} |
| 236 |
{hasStatusIcon && <FormControlIcon status={validated as 'success' | 'error' | 'warning'} />} |
| 237 |
</span> |
| 238 |
)} |
| 239 |
</span> |
| 240 |
)} |
| 241 |
</SSRSafeIds> |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
private sanitizeInputValue = (value: string | number) => |
| 246 |
typeof value === 'string' ? value.replace(/\n/g, ' ') : value; |
| 247 |
} |
| 248 |
|
| 249 |
export const TextInput = forwardRef((props: TextInputProps, ref: React.Ref<HTMLInputElement>) => ( |
| 250 |
<TextInputBase {...props} innerRef={ref as React.MutableRefObject<any>} /> |
| 251 |
)); |
| 252 |
TextInput.displayName = 'TextInput'; |