forked from openshift-assisted/assisted-installer-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRichInputField.tsx
185 lines (176 loc) · 5.88 KB
/
RichInputField.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as React from 'react';
import { useField } from 'formik';
import classNames from 'classnames';
import {
FormGroup,
HelperTextItem,
TextInput,
HelperText as PFHelperText,
HelperTextItemProps,
Popover,
PopoverPosition,
InputGroup,
Button,
InputGroupItem,
FormHelperText,
HelperText,
} from '@patternfly/react-core';
import { CheckCircleIcon } from '@patternfly/react-icons/dist/js/icons/check-circle-icon';
import { CheckIcon } from '@patternfly/react-icons/dist/js/icons/check-icon';
import { ExclamationCircleIcon } from '@patternfly/react-icons/dist/js/icons/exclamation-circle-icon';
import { InfoCircleIcon } from '@patternfly/react-icons/dist/js/icons/info-circle-icon';
import { TimesIcon } from '@patternfly/react-icons/dist/js/icons/times-icon';
import { global_palette_green_500 as okColor } from '@patternfly/react-tokens/dist/js/global_palette_green_500';
import { global_danger_color_100 as dangerColor } from '@patternfly/react-tokens/dist/js/global_danger_color_100';
import { global_palette_blue_300 as blueInfoColor } from '@patternfly/react-tokens/dist/js/global_palette_blue_300';
import { InputFieldProps as BaseInputProps } from './types';
import { getFieldId } from './utils';
import './RichInputField.css';
const getHelperTextVariant = (
validationMessage: string,
value: RichValidationProps['value'],
errors: RichValidationProps['error'],
): {
variant: HelperTextItemProps['variant'];
icon?: HelperTextItemProps['icon'];
} => {
if (!value) {
return { variant: 'indeterminate' };
} else if (errors?.includes(validationMessage)) {
return { variant: 'error', icon: <TimesIcon /> };
}
return { variant: 'success', icon: <CheckIcon /> };
};
type RichValidationProps = {
// eslint-disable-next-line
value: any;
error: string | undefined;
richValidationMessages: { [key: string]: string };
};
export const RichValidation: React.FC<RichValidationProps> = ({
value,
error,
richValidationMessages,
}) => {
return (
<PFHelperText component="ul" className="rich-input__rules">
{Object.keys(richValidationMessages).map((key) => {
const variant = getHelperTextVariant(richValidationMessages[key], value, error);
return (
<HelperTextItem key={key} isDynamic component="li" {...variant}>
{richValidationMessages[key]}
</HelperTextItem>
);
})}
</PFHelperText>
);
};
export type RichInputFieldPropsProps = BaseInputProps & {
richValidationMessages: RichValidationProps['richValidationMessages'];
placeholder?: string;
};
// eslint-disable-next-line react/display-name
const RichInputField: React.FC<RichInputFieldPropsProps> = React.forwardRef(
(
{
label,
labelIcon,
helperText,
isRequired,
validate,
idPostfix,
richValidationMessages,
noDefaultOnChange,
onChange,
...props
},
ref: React.Ref<HTMLInputElement>,
) => {
const [popoverOpen, setPopoverOpen] = React.useState(false);
const [field, { error, value, touched }, { setTouched }] = useField<string>({
name: props.name,
validate,
});
const fieldId = getFieldId(props.name, 'input', idPostfix);
const isValid = !(touched && error?.length);
const errorMessage = isValid ? '' : error[0];
return (
<FormGroup
id={`form-control__${fieldId}`}
fieldId={fieldId}
label={label}
isRequired={isRequired}
labelIcon={labelIcon}
>
<InputGroup
className={classNames('rich-input__group', { 'rich_input__group--invalid': !isValid })}
>
<InputGroupItem isFill>
<TextInput
{...field}
{...props}
ref={ref}
id={fieldId}
isRequired={isRequired}
aria-describedby={`${fieldId}-helper`}
onChange={(event, val) => {
!popoverOpen && setPopoverOpen(true);
!noDefaultOnChange && field.onChange(event);
onChange && onChange(event);
if (!touched && val?.length) {
setTouched(true);
}
}}
className="rich-input__text"
onBlur={() => {
setPopoverOpen(false);
setTouched(true);
}}
/>
</InputGroupItem>
<InputGroupItem>
<Popover
isVisible={popoverOpen}
shouldClose={() => setPopoverOpen(false)}
shouldOpen={() => setPopoverOpen(true)}
aria-label="validation popover"
position={PopoverPosition.top}
bodyContent={
<RichValidation
value={value}
error={error}
richValidationMessages={richValidationMessages as Record<string, string>}
/>
}
withFocusTrap={false}
>
<Button variant="plain" aria-label="Validation">
{!isValid ? (
<ExclamationCircleIcon color={dangerColor.value} />
) : value ? (
<CheckCircleIcon color={okColor.value} />
) : (
<InfoCircleIcon color={blueInfoColor.value} />
)}
</Button>
</Popover>
</InputGroupItem>
</InputGroup>
{(errorMessage || helperText) && (
<FormHelperText>
<HelperText>
<HelperTextItem
icon={errorMessage && <ExclamationCircleIcon />}
variant={isValid ? 'default' : 'error'}
id={isValid ? `${fieldId}-helper` : `${fieldId}-helper-error`}
>
{errorMessage ? errorMessage : helperText}
</HelperTextItem>
</HelperText>
</FormHelperText>
)}
</FormGroup>
);
},
);
export default RichInputField;