forked from Clovis-team/clovis-open-code-extracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient:components:text-input.js
204 lines (166 loc) · 5 KB
/
client:components:text-input.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @fileOverview
* Text Input is used in places we need to edit them (like on project info)
* or profile
* To edit a text input and save it, user needs to click on "edit" field
* which toggles, then user can cancel edition or save edition
* when the user cancels it comes back to initial data
*/
import _ from 'lodash'
import { Icon } from '~/components/icon'
import { TooltipGenerator, Tooltip } from '~/components/tooltip'
const RightLabel = ({children, valid}) => (
<div className="text-input__right-label">
<Icon name="validated-tick" />
<span>{children}</span>
</div>
)
export class TextInput extends React.Component {
static propTypes = {
value: PropTypes.string,
validator: PropTypes.func,
rightLabel: PropTypes.node,
textarea: PropTypes.bool,
// Called with the value and the result of validate()
onChange: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
// Force to display a validation result
validationResult: PropTypes.object,
popperOptions: PropTypes.object.isRequired,
}
static defaultProps = {
validator: () => null,
onChange() {},
onBlur() {},
onFocus() {},
textarea: false,
popperOptions: {},
}
state = {
value: '', // Not updated if the component is controlled
rightLabel: '',
hasBeenEdited: false,
focused: false,
input: null,
}
// Can be used from exernal code using ref={...}
get value() {
return this.state.input.value
}
validate() {
if (!this.state.hasBeenEdited) {
return null
}
const { validator } = this.props
if (!validator) {
return null
}
return validator(this.value)
}
get controlled() {
return this.props.value !== undefined
}
onChange = event => {
const { value } = event.target
const propagate = () => {
const { validator } = this.props
const validationResult = validator && validator(value)
this.props.onChange(value, validationResult)
}
if (this.controlled) {
this.setState({
hasBeenEdited: true,
})
return propagate()
}
this.setState({
value,
hasBeenEdited: true,
}, propagate)
}
get className() {
const result = this.props.validationResult || this.validate()
return [
'text-input',
result && ('text-input--' + result.level),
].filter(v => v).join(' ')
}
get inputProps() {
return _.omit(
this.props,
[
'popperOptions',
'validator',
'rightLabel',
'textarea',
'validationResult',
this.props.readOnly && 'autoFocus', // Safari issue on iOS
].filter(v => v),
)
}
get validationMessage() {
const result = this.props.validationResult || this.validate()
return result ? result.message : ''
}
get validationTooltip() {
const { validationMessage: message } = this
return message && (
<Tooltip color="sunrise">
{message}
</Tooltip>
)
}
get tooltipVisible() {
if (this.state.focused) {
return false
}
if (this.props.validationResult) {
return true
}
return !!this.validate()
}
onFocus = () => {
this.setState({focused: true})
this.props.onFocus()
}
onBlur = () => {
this.setState({focused: false})
this.props.onBlur()
}
onInputMount = input => this.setState({ input })
componentWillReceiveProps(nextProps) {
if (nextProps.autoFocus &&
this.state.input &&
!nextProps.disabled &&
!nextProps.readOnly) {
this.state.input.focus()
}
}
renderInput = () =>
this.props.textarea ? (
<textarea {...this.inputProps}
onChange={this.onChange}
onFocus={this.onFocus}
onBlur={this.onBlur}
ref={this.onInputMount} />
) : (
<input {...this.inputProps}
onChange={this.onChange}
onFocus={this.onFocus}
onBlur={this.onBlur}
ref={this.onInputMount} />
)
render() {
return (
<div className={this.className}>
<TooltipGenerator tooltip={this.tooltipVisible &&
this.validationTooltip}
popperOptions={this.props.popperOptions}>
<RightLabel>{this.props.rightLabel}</RightLabel>
{this.renderInput()}
</TooltipGenerator>
</div>
)
}
}