-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathindex.jsx
159 lines (143 loc) · 4.96 KB
/
index.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
function selectInputText(element) {
element.setSelectionRange(0, element.value.length);
}
export default class InlineEdit extends React.Component {
static propTypes = {
text: PropTypes.string.isRequired,
paramName: PropTypes.string.isRequired,
change: PropTypes.func.isRequired,
placeholder: PropTypes.string,
className: PropTypes.string,
activeClassName: PropTypes.string,
minLength: PropTypes.number,
maxLength: PropTypes.number,
validate: PropTypes.func,
style: PropTypes.object,
editingElement: PropTypes.string,
staticElement: PropTypes.string,
tabIndex: PropTypes.number,
isDisabled: PropTypes.bool,
editing: PropTypes.bool
};
static defaultProps = {
minLength: 1,
maxLength: 256,
editingElement: 'input',
staticElement: 'span',
tabIndex: 0,
isDisabled: false,
editing: false
};
state = {
editing: this.props.editing,
text: this.props.text,
minLength: this.props.minLength,
maxLength: this.props.maxLength,
};
componentWillMount() {
this.isInputValid = this.props.validate || this.isInputValid;
// Warn about deprecated elements
if (this.props.element) {
console.warn('`element` prop is deprecated: instead pass editingElement or staticElement to InlineEdit component');
}
}
componentWillReceiveProps(nextProps) {
const isTextChanged = (nextProps.text !== this.props.text);
const isEditingChanged = (nextProps.editing !== this.props.editing);
let nextState = {};
if (isTextChanged) {
nextState.text = nextProps.text;
}
if (isEditingChanged) {
nextState.editing = nextProps.editing;
}
if (isTextChanged || isEditingChanged) {
this.setState(nextState);
}
}
componentDidUpdate(prevProps, prevState) {
let inputElem = ReactDOM.findDOMNode(this.refs.input);
if (this.state.editing && !prevState.editing) {
inputElem.focus();
selectInputText(inputElem);
} else if (this.state.editing && prevProps.text != this.props.text) {
this.finishEditing();
}
}
startEditing = (e) => {
if (this.props.stopPropagation) {
e.stopPropagation()
}
this.setState({editing: true, text: this.props.text});
};
finishEditing = () => {
if (this.isInputValid(this.state.text) && this.props.text != this.state.text){
this.commitEditing();
} else if (this.props.text === this.state.text || !this.isInputValid(this.state.text)) {
this.cancelEditing();
}
};
cancelEditing = () => {
this.setState({editing: false, text: this.props.text});
};
commitEditing = () => {
this.setState({editing: false, text: this.state.text});
let newProp = {};
newProp[this.props.paramName] = this.state.text;
this.props.change(newProp);
};
clickWhenEditing = (e) => {
if (this.props.stopPropagation) {
e.stopPropagation();
}
};
isInputValid = (text) => {
return (text.length >= this.state.minLength && text.length <= this.state.maxLength);
};
keyDown = (event) => {
if (event.keyCode === 13) {
this.finishEditing();
} else if (event.keyCode === 27) {
this.cancelEditing();
}
};
textChanged = (event) => {
this.setState({
text: event.target.value.trim()
});
};
render() {
if (this.props.isDisabled) {
const Element = this.props.element || this.props.staticElement;
return <Element
className={this.props.className}
style={this.props.style} >
{this.state.text || this.props.placeholder}
</Element>;
} else if (!this.state.editing) {
const Element = this.props.element || this.props.staticElement;
return <Element
className={this.props.className}
onClick={this.startEditing}
tabIndex={this.props.tabIndex}
style={this.props.style} >
{this.state.text || this.props.placeholder}
</Element>;
} else {
const Element = this.props.element || this.props.editingElement;
return <Element
onClick={this.clickWhenEditing}
onKeyDown={this.keyDown}
onBlur={this.finishEditing}
className={this.props.activeClassName}
placeholder={this.props.placeholder}
defaultValue={this.state.text}
onChange={this.textChanged}
style={this.props.style}
ref="input" />;
}
}
}