2016-09-23 12:48:24 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
export default class DirectorySearchBox extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2016-09-26 13:29:12 +00:00
|
|
|
this._collectInput = this._collectInput.bind(this);
|
|
|
|
this._onClearClick = this._onClearClick.bind(this);
|
|
|
|
this._onChange = this._onChange.bind(this);
|
|
|
|
this._onKeyUp = this._onKeyUp.bind(this);
|
|
|
|
this._onJoinButtonClick = this._onJoinButtonClick.bind(this);
|
2016-09-23 12:48:24 +00:00
|
|
|
|
|
|
|
this.input = null;
|
2016-09-23 15:34:47 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
value: '',
|
|
|
|
};
|
2016-09-23 12:48:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 13:29:12 +00:00
|
|
|
_collectInput(e) {
|
2016-09-23 12:48:24 +00:00
|
|
|
this.input = e;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:29:12 +00:00
|
|
|
_onClearClick() {
|
2016-09-23 15:34:47 +00:00
|
|
|
this.setState({value: ''});
|
|
|
|
|
2016-09-23 12:48:24 +00:00
|
|
|
if (this.input) {
|
|
|
|
this.input.focus();
|
|
|
|
|
|
|
|
if (this.props.onClear) {
|
|
|
|
this.props.onClear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:29:12 +00:00
|
|
|
_onChange(ev) {
|
2016-09-23 12:48:24 +00:00
|
|
|
if (!this.input) return;
|
2016-09-23 15:34:47 +00:00
|
|
|
this.setState({value: ev.target.value});
|
2016-09-23 12:48:24 +00:00
|
|
|
|
|
|
|
if (this.props.onChange) {
|
2016-09-29 14:05:15 +00:00
|
|
|
this.props.onChange(ev.target.value);
|
2016-09-23 12:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:29:12 +00:00
|
|
|
_onKeyUp(ev) {
|
2016-09-23 12:48:24 +00:00
|
|
|
if (ev.key == 'Enter') {
|
|
|
|
if (this.props.onJoinClick) {
|
2016-09-23 15:34:47 +00:00
|
|
|
this.props.onJoinClick(this.state.value);
|
2016-09-23 12:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:29:12 +00:00
|
|
|
_onJoinButtonClick() {
|
2016-09-23 15:34:47 +00:00
|
|
|
if (this.props.onJoinClick) {
|
|
|
|
this.props.onJoinClick(this.state.value);
|
|
|
|
}
|
2016-09-23 14:25:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 12:48:24 +00:00
|
|
|
render() {
|
|
|
|
const searchbox_classes = {
|
|
|
|
mx_DirectorySearchBox: true,
|
|
|
|
};
|
|
|
|
searchbox_classes[this.props.className] = true;
|
|
|
|
|
2016-09-23 14:25:38 +00:00
|
|
|
let join_button;
|
2016-09-29 14:05:15 +00:00
|
|
|
if (this.props.showJoinButton) {
|
2016-09-23 14:25:38 +00:00
|
|
|
join_button = <span className="mx_DirectorySearchBox_joinButton"
|
2016-09-26 13:29:12 +00:00
|
|
|
onClick={this._onJoinButtonClick}
|
2016-09-23 14:25:38 +00:00
|
|
|
>
|
|
|
|
Join
|
|
|
|
</span>;
|
|
|
|
}
|
|
|
|
|
2016-09-23 12:48:24 +00:00
|
|
|
return <span className={classnames(searchbox_classes)}>
|
2016-09-23 14:58:11 +00:00
|
|
|
<div className="mx_DirectorySearchBox_container">
|
2016-12-06 14:40:09 +00:00
|
|
|
<input type="text" name="dirsearch" value={this.state.value}
|
2016-09-23 14:58:11 +00:00
|
|
|
className="mx_DirectorySearchBox_input"
|
2016-09-26 13:29:12 +00:00
|
|
|
ref={this._collectInput}
|
|
|
|
onChange={this._onChange} onKeyUp={this._onKeyUp}
|
2016-09-29 14:05:15 +00:00
|
|
|
placeholder={this.props.placeholder}
|
2016-09-23 14:58:11 +00:00
|
|
|
/>
|
|
|
|
{join_button}
|
|
|
|
<span className="mx_DirectorySearchBox_clear_wrapper">
|
2016-09-26 13:29:12 +00:00
|
|
|
<span className="mx_DirectorySearchBox_clear" onClick={this._onClearClick} />
|
2016-09-23 14:58:11 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
2016-09-23 12:48:24 +00:00
|
|
|
</span>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectorySearchBox.propTypes = {
|
|
|
|
className: React.PropTypes.string,
|
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
onClear: React.PropTypes.func,
|
|
|
|
onJoinClick: React.PropTypes.func,
|
2016-09-29 14:05:15 +00:00
|
|
|
placeholder: React.PropTypes.string,
|
|
|
|
showJoinButton: React.PropTypes.bool,
|
2016-09-23 12:48:24 +00:00
|
|
|
};
|