-
Notifications
You must be signed in to change notification settings - Fork 256
/
Input.tsx
174 lines (151 loc) · 3.88 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Libraries
import React, {
Component,
CSSProperties,
ChangeEvent,
KeyboardEvent,
} from 'react'
import classnames from 'classnames'
// Types
import {ComponentStatus, ComponentSize, IconFont} from 'src/reusable_ui/types'
export enum InputType {
Text = 'text',
Number = 'number',
Password = 'password',
Email = 'email',
}
interface Props {
value?: string
placeholder?: string
onChange?: (e: ChangeEvent<HTMLInputElement>) => void
onBlur?: () => void
onFocus?: () => void
onKeyPress?: (e: KeyboardEvent<HTMLInputElement>) => void
onKeyUp?: (e: KeyboardEvent<HTMLInputElement>) => void
onKeyDown?: (e: KeyboardEvent<HTMLInputElement>) => void
size?: ComponentSize
icon?: IconFont
status?: ComponentStatus
autoFocus?: boolean
spellCheck?: boolean
type?: InputType
widthPixels?: number
titleText?: string
disabledTitleText?: string
customClass?: string
}
class Input extends Component<Props> {
public static defaultProps: Partial<Props> = {
value: '',
placeholder: '',
titleText: '',
disabledTitleText: 'This input is disabled',
size: ComponentSize.Small,
status: ComponentStatus.Default,
autoFocus: false,
spellCheck: false,
type: InputType.Text,
}
public render() {
const {
status,
type,
value,
placeholder,
autoFocus,
spellCheck,
onChange,
onBlur,
onFocus,
onKeyPress,
onKeyUp,
onKeyDown,
} = this.props
return (
<div className={this.className} style={this.containerStyle}>
<input
title={this.title}
type={type}
value={value}
placeholder={placeholder}
autoFocus={autoFocus}
spellCheck={spellCheck}
onChange={onChange}
onBlur={onBlur}
onFocus={onFocus}
onKeyPress={onKeyPress}
onKeyUp={onKeyUp}
onKeyDown={onKeyDown}
className="input-field"
disabled={status === ComponentStatus.Disabled}
/>
{this.icon}
{this.statusIndicator}
</div>
)
}
private get icon(): JSX.Element {
const {icon} = this.props
if (icon) {
return <span className={`input-icon icon ${icon}`} />
}
return null
}
private get title(): string {
const {titleText, disabledTitleText, status} = this.props
if (status === ComponentStatus.Disabled) {
return disabledTitleText
}
return titleText
}
private get statusIndicator(): JSX.Element {
const {status} = this.props
if (status === ComponentStatus.Loading) {
return (
<>
<div className="input-status">
<div className="input-spinner" />
</div>
<div className="input-shadow" />
</>
)
}
if (status === ComponentStatus.Error) {
return (
<>
<span className={`input-status icon ${IconFont.AlertTriangle}`} />
<div className="input-shadow" />
</>
)
}
if (status === ComponentStatus.Valid) {
return (
<>
<span className={`input-status icon ${IconFont.Checkmark}`} />
<div className="input-shadow" />
</>
)
}
return <div className="input-shadow" />
}
private get className(): string {
const {size, status, icon, customClass} = this.props
return classnames('input', {
[`input-${size}`]: size,
'input--has-icon': icon,
'input--valid': status === ComponentStatus.Valid,
'input--error': status === ComponentStatus.Error,
'input--loading': status === ComponentStatus.Loading,
'input--disabled': status === ComponentStatus.Disabled,
[`${customClass}`]: customClass,
})
}
private get containerStyle(): CSSProperties {
const {widthPixels} = this.props
if (widthPixels) {
return {width: `${widthPixels}px`}
}
return {width: '100%'}
}
}
export default Input