Skip to content

Commit fe98d6e

Browse files
committed
Merge pull request element-hq#3 from matrix-org/editable_text
Add an editable text atom
2 parents e0fae13 + 5cd9222 commit fe98d6e

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var EditableTextController = require("../../../../src/controllers/atoms/EditableText");
22+
23+
module.exports = React.createClass({
24+
displayName: 'EditableText',
25+
mixins: [EditableTextController],
26+
27+
onKeyUp: function(ev) {
28+
if (ev.key == "Enter") {
29+
this.onFinish(ev);
30+
} else if (ev.key == "Escape") {
31+
this.cancelEdit();
32+
}
33+
},
34+
35+
onClickDiv: function() {
36+
this.setState({
37+
phase: this.Phases.Edit,
38+
})
39+
},
40+
41+
onFocus: function(ev) {
42+
ev.target.setSelectionRange(0, ev.target.value.length);
43+
},
44+
45+
onFinish: function(ev) {
46+
this.setValue(ev.target.value);
47+
},
48+
49+
render: function() {
50+
var editable_el;
51+
52+
if (this.state.phase == this.Phases.Display) {
53+
editable_el = <div ref="display_div" onClick={this.onClickDiv}>{this.state.value}</div>;
54+
} else if (this.state.phase == this.Phases.Edit) {
55+
editable_el = (
56+
<div>
57+
<input type="text" defaultValue={this.state.value} onKeyUp={this.onKeyUp} onFocus={this.onFocus} onBlur={this.onFinish} autoFocus/>
58+
</div>
59+
);
60+
}
61+
62+
return (
63+
<div className="mx_EditableText">
64+
{editable_el}
65+
</div>
66+
);
67+
}
68+
});

‎src/ComponentBroker.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require('../skins/base/views/atoms/MessageTimestamp');
6464
require('../skins/base/views/atoms/create_room/CreateRoomButton');
6565
require('../skins/base/views/atoms/create_room/RoomNameTextbox');
6666
require('../skins/base/views/atoms/create_room/Presets');
67+
require('../skins/base/views/atoms/EditableText');
6768
require('../skins/base/views/molecules/MatrixToolbar');
6869
require('../skins/base/views/molecules/RoomTile');
6970
require('../skins/base/views/molecules/MessageTile');
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
module.exports = {
22+
propTypes: {
23+
onValueChanged: React.PropTypes.func,
24+
initalValue: React.PropTypes.string,
25+
},
26+
27+
Phases: {
28+
Display: "display",
29+
Edit: "edit",
30+
},
31+
32+
getDefaultProps: function() {
33+
return {
34+
onValueChanged: function() {},
35+
initalValue: '',
36+
};
37+
},
38+
39+
getInitialState: function() {
40+
return {
41+
value: this.props.initalValue,
42+
phase: this.Phases.Display,
43+
}
44+
},
45+
46+
getValue: function() {
47+
return this.state.value;
48+
},
49+
50+
setValue: function(val) {
51+
this.setState({
52+
value: val,
53+
phase: this.Phases.Display,
54+
});
55+
56+
this.onValueChanged();
57+
},
58+
59+
cancelEdit: function() {
60+
this.setState({
61+
phase: this.Phases.Display,
62+
});
63+
},
64+
65+
onValueChanged: function() {
66+
this.props.onValueChanged(this.state.value);
67+
},
68+
};

0 commit comments

Comments
 (0)