diff --git a/src/component-index.js b/src/component-index.js
index 4cf2ba4016..c35b287257 100644
--- a/src/component-index.js
+++ b/src/component-index.js
@@ -54,6 +54,7 @@ module.exports.components['views.dialogs.NeedToRegisterDialog'] = require('./com
module.exports.components['views.dialogs.QuestionDialog'] = require('./components/views/dialogs/QuestionDialog');
module.exports.components['views.dialogs.SetDisplayNameDialog'] = require('./components/views/dialogs/SetDisplayNameDialog');
module.exports.components['views.dialogs.TextInputDialog'] = require('./components/views/dialogs/TextInputDialog');
+module.exports.components['views.elements.AddressSelector'] = require('./components/views/elements/AddressSelector');
module.exports.components['views.elements.AddressTile'] = require('./components/views/elements/AddressTile');
module.exports.components['views.elements.EditableText'] = require('./components/views/elements/EditableText');
module.exports.components['views.elements.EditableTextContainer'] = require('./components/views/elements/EditableTextContainer');
diff --git a/src/components/views/dialogs/ChatInviteDialog.js b/src/components/views/dialogs/ChatInviteDialog.js
index e322127135..f0eb1c4e63 100644
--- a/src/components/views/dialogs/ChatInviteDialog.js
+++ b/src/components/views/dialogs/ChatInviteDialog.js
@@ -56,10 +56,8 @@ module.exports = React.createClass({
getInitialState: function() {
return {
user: null,
- queryList: [],
addressSelected: false,
- selected: 0,
- hover: false,
+ queryList: [],
};
},
@@ -71,15 +69,6 @@ module.exports = React.createClass({
this._updateUserList();
},
- componentDidUpdate: function() {
- // As the user scrolls with the arrow keys keep the selected item
- // at the top of the window.
- if (this.scrollElement && !this.state.hover) {
- var elementHeight = this.queryListElement.getBoundingClientRect().height;
- this.scrollElement.scrollTop = (this.state.selected * elementHeight) - elementHeight;
- }
- },
-
onStartChat: function() {
var addr;
@@ -124,32 +113,15 @@ module.exports = React.createClass({
} else if (e.keyCode === 38) { // up arrow
e.stopPropagation();
e.preventDefault();
- if (this.state.selected > 0) {
- this.setState({
- selected: this.state.selected - 1,
- hover : false,
- });
- }
+ this.addressSelector.onKeyUpArrow();
} else if (e.keyCode === 40) { // down arrow
e.stopPropagation();
e.preventDefault();
- if (this.state.selected < this._maxSelected(this.state.queryList)) {
- this.setState({
- selected: this.state.selected + 1,
- hover : false,
- });
- }
+ this.addressSelector.onKeyDownArrow();
} else if (e.keyCode === 13) { // enter
e.stopPropagation();
e.preventDefault();
- if (this.state.queryList.length > 0) {
- this.setState({
- user: this.state.queryList[this.state.selected],
- addressSelected: true,
- queryList: [],
- hover : false,
- });
- }
+ this.addressSelector.onKeyReturn();
}
},
@@ -164,24 +136,13 @@ module.exports = React.createClass({
});
}
- // Make sure the selected item isn't outside the list bounds
- var selected = this.state.selected;
- var maxSelected = this._maxSelected(queryList);
- if (selected > maxSelected) {
- selected = maxSelected;
- }
-
- this.setState({
- queryList: queryList,
- selected: selected,
- });
+ this.setState({ queryList: queryList });
},
onDismissed: function() {
this.setState({
user: null,
addressSelected: false,
- selected: 0,
queryList: [],
});
},
@@ -193,51 +154,16 @@ module.exports = React.createClass({
user: self.state.queryList[index],
addressSelected: true,
queryList: [],
- hover: false,
});
};
},
- onMouseEnter: function(index) {
- var self = this;
- return function() {
- self.setState({
- selected: index,
- hover: true,
- });
- };
- },
-
- onMouseLeave: function() {
- this.setState({ hover : false });
- },
-
- createQueryListTiles: function() {
- var self = this;
- var TintableSvg = sdk.getComponent("elements.TintableSvg");
- var AddressTile = sdk.getComponent("elements.AddressTile");
- var maxSelected = this._maxSelected(this.state.queryList);
- var queryList = [];
-
- // Only create the query elements if there are queries
- if (this.state.queryList.length > 0) {
- for (var i = 0; i <= maxSelected; i++) {
- var classes = classNames({
- "mx_ChatInviteDialog_queryListElement": true,
- "mx_ChatInviteDialog_selected": this.state.selected === i,
- });
-
- // NOTE: Defaulting to "vector" as the network, until the network backend stuff is done.
- // Saving the queryListElement so we can use it to work out, in the componentDidUpdate
- // method, how far to scroll when using the arrow keys
- queryList.push(
-
{ this.queryListElement = ref; }} >
-
-
- );
- }
- }
- return queryList;
+ onSelected: function(index) {
+ this.setState({
+ user: this.state.queryList[index],
+ addressSelected: true,
+ queryList: [],
+ });
},
_getDirectMessageRoom: function(addr) {
@@ -279,12 +205,6 @@ module.exports = React.createClass({
this._userList = MatrixClientPeg.get().getUsers();
}, 500),
- _maxSelected: function(list) {
- var listSize = list.length === 0 ? 0 : list.length - 1;
- var maxSelected = listSize > (TRUNCATE_QUERY_LIST - 1) ? (TRUNCATE_QUERY_LIST - 1) : listSize
- return maxSelected;
- },
-
// This is the search algorithm for matching users
_matches: function(query, user) {
var name = user.displayName.toLowerCase();
@@ -313,6 +233,7 @@ module.exports = React.createClass({
render: function() {
var TintableSvg = sdk.getComponent("elements.TintableSvg");
+ var AddressSelector = sdk.getComponent("elements.AddressSelector");
this.scrollElement = null;
var query;
@@ -335,16 +256,6 @@ module.exports = React.createClass({
);
}
- var queryList;
- var queryListElements = this.createQueryListTiles();
- if (queryListElements.length > 0) {
- queryList = (
- {this.scrollElement = ref}}>
- { queryListElements }
-
- );
- }
-
return (
@@ -358,7 +269,10 @@ module.exports = React.createClass({
{ query }
- { queryList }
+
{this.addressSelector = ref}}
+ addressList={ this.state.queryList }
+ onSelected={ this.onSelected }
+ truncateAt={ TRUNCATE_QUERY_LIST } />