-
Notifications
You must be signed in to change notification settings - Fork 46
/
InputSearch.tsx
131 lines (108 loc) · 3.52 KB
/
InputSearch.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
import {
ChangeEvent,
FocusEvent,
forwardRef,
ReactNode,
useImperativeHandle,
useState,
} from 'react';
import classNames from 'classnames';
import scss from './InputSearch.module.scss';
import alertIconSvg from './assets/alertIcon.svg';
import leftIcon from './assets/leftIcon.svg';
import rightIconSvg from './assets/rightIcon.svg';
import { TInputComponentRef, TInputProps } from './InputSearch.types';
function InputSearch(props: TInputProps, ref: TInputComponentRef): ReactNode {
const [value, setValue] = useState('');
const labelClass = [scss.label];
const inputClass = [scss.input];
const legendClass = [scss.legend];
const fieldsetClass = [scss.fieldset];
const containerClass = [scss.container];
const [isFocused, setIsFocused] = useState(false);
const rightIcon = <img alt="right icon" src={rightIconSvg} />;
const alertIcon = <img alt="alert icon" src={alertIconSvg} />;
const handleClear = (): void => {
if (props.onChange) props.onChange('');
setValue('');
};
const handleIcons = (): ReactNode => (
<div
className={scss.iconRight}
data-testid="clear-button"
onClick={handleClear}
>
{props.error ? alertIcon : rightIcon}
</div>
);
const handleValue = (e: ChangeEvent<HTMLInputElement>): void => {
if (props.onChange) props.onChange(e.target.value);
setValue(e.target.value);
};
const renderErrorMessage = (): ReactNode => (
<div className={scss.errorWrapper}>
<span className={scss.errorMessage}>{props.errorMessage}</span>
</div>
);
const onInputFocus = (event: FocusEvent<HTMLInputElement>): void => {
if (props.onFocus) props.onFocus(event);
setIsFocused(true);
};
const onInputBlur = (e: FocusEvent<HTMLInputElement>): void => {
setIsFocused(e.target.value.length > 0);
};
const handleFocusedStyles = (): void => {
fieldsetClass.push(scss.fieldsetFocus);
legendClass.push(scss.legendFocus);
labelClass.push(scss.labelAnimate);
containerClass.push(scss.containerFocus);
};
const handleErrorStyles = (): void => {
fieldsetClass.push(scss.fieldsetError);
containerClass.push(scss.containerError);
labelClass.push(scss.labelError);
};
if (isFocused || (typeof value === 'string' && value)) {
handleFocusedStyles();
}
if (props.error) {
handleErrorStyles();
}
useImperativeHandle(ref, () => ({
clearInput: handleClear,
}));
return (
<div className={classNames(containerClass)}>
<div className={scss.inputWrapper}>
<label className={classNames(labelClass)} htmlFor={props.name}>
{props.placeholder}
</label>
<div className={scss.iconLeft}>
<img alt="left icon" src={leftIcon} />
</div>
<input
className={classNames(inputClass)}
id={props.name}
name={props.name}
onBlur={(e) => onInputBlur(e)}
onChange={handleValue}
onFocus={onInputFocus}
placeholder={props.placeholder}
readOnly={props.readOnly}
required
type={props.type}
value={value}
/>
{value && handleIcons()}
<fieldset aria-hidden="true" className={classNames(fieldsetClass)}>
<legend className={classNames(legendClass)}>
<span className={scss.legendTitle}>{props.placeholder}</span>
</legend>
</fieldset>
</div>
{props.error && renderErrorMessage()}
</div>
);
}
const ForwardedInputSearch = forwardRef(InputSearch);
export default ForwardedInputSearch;