2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2020-09-17 02:23:37 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
import React, {createRef, RefObject} from 'react';
|
2019-12-21 00:41:07 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import classNames from "classnames";
|
2018-04-13 02:43:44 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-16 20:14:03 +03:00
|
|
|
import {Key} from "../../../Keyboard";
|
2020-09-17 02:23:37 +03:00
|
|
|
import DesktopBuildsNotice, {WarningKind} from "../elements/DesktopBuildsNotice";
|
2021-03-09 06:12:00 +03:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
interface IProps {
|
|
|
|
onCancelClick: () => void;
|
|
|
|
onSearch: (query: string, scope: string) => void;
|
|
|
|
searchInProgress?: boolean;
|
|
|
|
isRoomEncrypted?: boolean;
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
interface IState {
|
|
|
|
scope: SearchScope;
|
|
|
|
}
|
2019-12-08 15:16:17 +03:00
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
export enum SearchScope {
|
|
|
|
Room = "Room",
|
|
|
|
All = "All",
|
|
|
|
}
|
|
|
|
|
|
|
|
@replaceableComponent("views.rooms.SearchBar")
|
|
|
|
export default class SearchBar extends React.Component<IProps, IState> {
|
|
|
|
private searchTerm: RefObject<HTMLInputElement> = createRef();
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2021-06-21 16:50:21 +03:00
|
|
|
scope: SearchScope.Room,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
public onThisRoomClick = () => {
|
|
|
|
this.setState({ scope: SearchScope.Room }, () => this._searchIfQuery());
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
public onAllRoomsClick = () => {
|
|
|
|
this.setState({ scope: SearchScope.All }, () => this._searchIfQuery());
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2021-06-21 16:50:21 +03:00
|
|
|
public onSearchChange = (e: React.KeyboardEvent) => {
|
2019-12-16 20:14:03 +03:00
|
|
|
switch (e.key) {
|
|
|
|
case Key.ENTER:
|
|
|
|
this.onSearch();
|
|
|
|
break;
|
|
|
|
case Key.ESCAPE:
|
|
|
|
this.props.onCancelClick();
|
|
|
|
break;
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_searchIfQuery() {
|
2021-06-21 16:50:21 +03:00
|
|
|
if (this.searchTerm.current.value) {
|
2019-01-16 13:13:35 +03:00
|
|
|
this.onSearch();
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-01-16 13:13:35 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
onSearch = () => {
|
2021-06-21 16:50:21 +03:00
|
|
|
this.props.onSearch(this.searchTerm.current.value, this.state.scope);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2019-12-07 15:45:28 +03:00
|
|
|
const searchButtonClasses = classNames("mx_SearchBar_searchButton", {
|
|
|
|
mx_SearchBar_searching: this.props.searchInProgress,
|
|
|
|
});
|
|
|
|
const thisRoomClasses = classNames("mx_SearchBar_button", {
|
2021-06-21 16:50:21 +03:00
|
|
|
mx_SearchBar_unselected: this.state.scope !== SearchScope.Room,
|
2019-12-07 15:45:28 +03:00
|
|
|
});
|
|
|
|
const allRoomsClasses = classNames("mx_SearchBar_button", {
|
2021-06-21 16:50:21 +03:00
|
|
|
mx_SearchBar_unselected: this.state.scope !== SearchScope.All,
|
2019-12-07 15:45:28 +03:00
|
|
|
});
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
return (
|
2020-09-17 02:23:37 +03:00
|
|
|
<>
|
|
|
|
<div className="mx_SearchBar">
|
|
|
|
<div className="mx_SearchBar_buttons" role="radiogroup">
|
2021-06-21 16:50:21 +03:00
|
|
|
<AccessibleButton
|
|
|
|
className={ thisRoomClasses }
|
|
|
|
onClick={this.onThisRoomClick}
|
|
|
|
aria-checked={this.state.scope === SearchScope.Room}
|
|
|
|
role="radio"
|
|
|
|
>
|
2020-09-17 02:23:37 +03:00
|
|
|
{_t("This Room")}
|
|
|
|
</AccessibleButton>
|
2021-06-21 16:50:21 +03:00
|
|
|
<AccessibleButton
|
|
|
|
className={ allRoomsClasses }
|
|
|
|
onClick={this.onAllRoomsClick}
|
|
|
|
aria-checked={this.state.scope === SearchScope.All}
|
|
|
|
role="radio"
|
|
|
|
>
|
2020-09-17 02:23:37 +03:00
|
|
|
{_t("All Rooms")}
|
|
|
|
</AccessibleButton>
|
|
|
|
</div>
|
|
|
|
<div className="mx_SearchBar_input mx_textinput">
|
2021-06-21 16:50:21 +03:00
|
|
|
<input
|
|
|
|
ref={this.searchTerm}
|
|
|
|
type="text"
|
|
|
|
autoFocus={true}
|
|
|
|
placeholder={_t("Search…")}
|
|
|
|
onKeyDown={this.onSearchChange}
|
|
|
|
/>
|
2020-09-17 02:23:37 +03:00
|
|
|
<AccessibleButton className={ searchButtonClasses } onClick={this.onSearch} />
|
|
|
|
</div>
|
|
|
|
<AccessibleButton className="mx_SearchBar_cancel" onClick={this.props.onCancelClick} />
|
2019-12-07 15:45:28 +03:00
|
|
|
</div>
|
2020-09-17 02:23:37 +03:00
|
|
|
<DesktopBuildsNotice isRoomEncrypted={this.props.isRoomEncrypted} kind={WarningKind.Search} />
|
|
|
|
</>
|
2018-04-12 01:58:04 +03:00
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|