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';
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-20 01:19:56 +00:00
|
|
|
import * as sdk from '../../../index';
|
2019-01-29 14:34:58 +00:00
|
|
|
import { _t } from '../../../languageHandler';
|
2021-03-09 02:59:41 +00:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2016-09-23 12:48:24 +00:00
|
|
|
|
2021-03-09 02:59:41 +00:00
|
|
|
@replaceableComponent("views.elements.DirectorySearchBox")
|
2016-09-23 12:48:24 +00:00
|
|
|
export default class DirectorySearchBox extends React.Component {
|
2020-11-11 13:36:17 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
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 = {
|
2020-11-11 13:36:17 +00:00
|
|
|
value: this.props.initialText || '',
|
2016-09-23 15:34:47 +00:00
|
|
|
};
|
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) {
|
2017-03-28 12:02:13 +00:00
|
|
|
if (ev.key == 'Enter' && this.props.showJoinButton) {
|
2016-09-23 12:48:24 +00:00
|
|
|
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() {
|
2019-01-29 14:34:58 +00:00
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
|
|
|
|
2020-10-30 18:18:17 +00:00
|
|
|
const searchboxClasses = {
|
2016-09-23 12:48:24 +00:00
|
|
|
mx_DirectorySearchBox: true,
|
|
|
|
};
|
2020-10-30 18:18:17 +00:00
|
|
|
searchboxClasses[this.props.className] = true;
|
2016-09-23 12:48:24 +00:00
|
|
|
|
2020-10-30 18:18:17 +00:00
|
|
|
let joinButton;
|
2016-09-29 14:05:15 +00:00
|
|
|
if (this.props.showJoinButton) {
|
2020-10-30 18:18:17 +00:00
|
|
|
joinButton = <AccessibleButton className="mx_DirectorySearchBox_joinButton"
|
2016-09-26 13:29:12 +00:00
|
|
|
onClick={this._onJoinButtonClick}
|
2019-01-29 14:34:58 +00:00
|
|
|
>{_t("Join")}</AccessibleButton>;
|
2016-09-23 14:25:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 14:34:58 +00:00
|
|
|
return <div className={`mx_DirectorySearchBox ${this.props.className} mx_textinput`}>
|
2020-11-11 13:36:17 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
name="dirsearch"
|
|
|
|
value={this.state.value}
|
|
|
|
className="mx_textinput_icon mx_textinput_search"
|
|
|
|
ref={this._collectInput}
|
|
|
|
onChange={this._onChange}
|
|
|
|
onKeyUp={this._onKeyUp}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
{ joinButton }
|
|
|
|
<AccessibleButton className="mx_DirectorySearchBox_clear" onClick={this._onClearClick} />
|
|
|
|
</div>;
|
2016-09-23 12:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectorySearchBox.propTypes = {
|
2017-12-26 01:03:18 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onClear: PropTypes.func,
|
|
|
|
onJoinClick: PropTypes.func,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
showJoinButton: PropTypes.bool,
|
2020-11-11 13:36:17 +00:00
|
|
|
initialText: PropTypes.string,
|
2016-09-23 12:48:24 +00:00
|
|
|
};
|