-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInput.tsx
153 lines (142 loc) · 4.19 KB
/
Input.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
import React from 'react';
import cx from 'classnames';
import FieldWrapper, { type FieldWrapperProps } from './FieldWrapper';
import useFocusByTab from '../../hooks/useFocusByTab';
type InputAffixProps = {
value: InputAffixContent | InputAffixContentWithCallback;
type: 'leading' | 'trailing';
disabled: boolean;
readOnly: boolean;
};
const InputAffix = ({ value, type, disabled, readOnly }: InputAffixProps) => {
const isContentWithCallback =
typeof value === 'object' && value !== null && 'onClickCallback' in value;
const spanContent = isContentWithCallback
? (value as InputAffixContentWithCallback).content
: (value as InputAffixContent);
const wrapperProps = isContentWithCallback
? { onClick: (value as InputAffixContentWithCallback).onClickCallback }
: {};
return (
<div
className={cx(`${type}-content-wrapper`, { disabled, 'read-only': readOnly })}
{...wrapperProps}
>
<span className={`${type}-content`}>{spanContent}</span>
</div>
);
};
type InputAffixContent = string | React.ReactNode;
type InputAffixContentWithCallback = {
content: string | React.ReactNode;
onClickCallback: () => void;
};
type IconConfig = {
icon: React.ReactNode;
action: () => void;
className?: string;
};
export type InputProps = React.InputHTMLAttributes<HTMLInputElement> &
Omit<FieldWrapperProps, 'children'> & {
leadingContent?: InputAffixContent | InputAffixContentWithCallback;
trailingContent?: InputAffixContent | InputAffixContentWithCallback;
inputFieldWrapperClassname?: string;
withIcons?: IconConfig[];
};
const Input = React.forwardRef<HTMLInputElement, InputProps>(
(
{
id,
label,
type,
hint,
leadingContent,
trailingContent,
required,
disabled = false,
readOnly = false,
statusWithMessage,
narrow,
inputFieldWrapperClassname = '',
small = false,
withIcons = [],
onKeyUp,
onBlur,
onCloseStatusMessage,
...rest
},
ref
) => {
const { handleKeyUp, handleBlur, isFocusByTab } = useFocusByTab({ onBlur, onKeyUp });
return (
<FieldWrapper
id={id}
label={label}
hint={hint}
statusWithMessage={statusWithMessage}
disabled={disabled}
required={required}
small={small}
statusIconPosition={
statusWithMessage?.tooltip || narrow ? 'before-status-message' : undefined
}
narrow={narrow}
className={cx('input-field-wrapper', inputFieldWrapperClassname)}
onCloseStatusMessage={onCloseStatusMessage}
>
{leadingContent && (
<InputAffix
value={leadingContent}
type="leading"
disabled={disabled}
readOnly={readOnly}
/>
)}
<div
className={cx('input-container', {
'focused-by-tab': isFocusByTab,
})}
>
<input
ref={ref}
className={cx('input', {
'with-leading-only': leadingContent && !trailingContent,
'with-trailing-only': trailingContent && !leadingContent,
'with-leading-and-trailing': leadingContent && trailingContent,
[`with-icons-${withIcons.length}`]: withIcons.length > 0,
[statusWithMessage?.status || '']: !!statusWithMessage,
})}
id={id}
type={type}
disabled={disabled}
readOnly={readOnly}
onKeyUp={handleKeyUp}
onBlur={handleBlur}
{...rest}
/>
<div
className={cx('input-icons', {
small,
})}
>
{withIcons.map((iconConfig, index) => (
<span key={index} className={iconConfig?.className} onClick={iconConfig.action}>
{iconConfig.icon}
</span>
))}
</div>
</div>
{trailingContent && (
<InputAffix
value={trailingContent}
type="trailing"
disabled={disabled}
readOnly={readOnly}
/>
)}
</FieldWrapper>
);
}
);
Input.displayName = 'Input';
export default Input;