-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditableTitle.js
65 lines (64 loc) · 1.7 KB
/
EditableTitle.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
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import './EditableTitle.scss';
const EditableTitle = React.createClass({
propTypes: {
tag: PropTypes.string,
className: PropTypes.string,
children: PropTypes.node,
onInputChange: PropTypes.func,
value: PropTypes.string,
},
getDefaultProps() {
return { tag: 'label' };
},
getInitialState() {
return { editing: false };
},
handleEdit(e) {
this.setState({
editing: true,
value: this.props.value,
oldValue: this.props.value,
});
ReactDOM.findDOMNode(this.refs.input).focus();
},
handleChange(e) {
this.props.onInputChange(e);
},
handleBlur() {
this.replaceState(this.getInitialState());
},
render() {
const Tag = this.props.tag;
if (this.state.editing) {
return (
<Tag className={`EditableTitle EditableTitle--editing ${(this.props.className || '')}`}>
<input
className="EditableTitle-input"
key="input"
ref="input"
value={this.props.value}
onChange={this.handleChange}
onBlur={this.handleBlur}
maxLength={100} />
</Tag>
);
} else {
return (
<Tag className={`EditableTitle EditableTitle--set ${(this.props.className || '')}`} onClick={this.handleEdit}>
<input
className="EditableTitle-input"
key="input"
ref="input"
maxLength={100} />
{this.props.children}
<button type="button" className="EditableTitle-editButton" onClick={this.handleEdit}>
Edit
</button>
</Tag>
);
}
},
});
export default EditableTitle;