-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
95 lines (87 loc) · 2.06 KB
/
index.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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {Row} from 'react-bootstrap'
import {TextInput} from '../../components'
/**
* Text input that can switch between text field and label
* by using attribute `editing`
*/
class EditableText extends Component {
static propTypes = {
/**
* String value for this text field
*/
children: PropTypes.string,
/**
* Toggle whether the text field is in editable or not. Default is 'false'
*/
editable: PropTypes.bool,
/**
* Toggle whether the text field is in editing mode or not.
* Default is 'false'
*/
editing: PropTypes.bool,
/**
* Placeholder
*/
placeholder: PropTypes.string,
/**
* String to display if it is editable and children is
* empty and there is not placeholder
*/
emptyReadOnlyText: PropTypes.string,
/**
* Tooltip
*/
title: PropTypes.string
}
constructor () {
super()
this.state = {
focus: false
}
}
handleClick = () => {
this.setState({
focus: true
})
}
handleBlur = () => {
this.setState({
focus: false
})
}
render () {
const {
children = '',
editable = false,
editing = false,
emptyReadOnlyText = '',
placeholder = '',
title,
...props
} = this.props
if (editable && editing) {
const cssClass = 'textInput textState ' +
(editable ? 'editable' : 'text') + (children ? '' : ' u-textMuted')
return (
<TextInput className={cssClass}
{...props}
autoFocus={this.state.focus}
onBlur={this.handleBlur}
placeholder={placeholder}
value={children}
/>
)
}
const emptyText = editable ? placeholder : emptyReadOnlyText
const content = children ||
<span className='u-textMuted'>{emptyText}</span>
return (
<Row className='textInput textState text'
onClick={this.handleClick} title={title}>
{content}
</Row>
) }
}
export default EditableText