2019-01-18 23:43:17 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Travis Ralston
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-09-26 15:52:09 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-01-18 23:43:17 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as React from "react";
|
2019-01-19 01:07:11 +03:00
|
|
|
import {_t} from '../../languageHandler';
|
2019-09-26 15:52:09 +03:00
|
|
|
import {KeyCode} from "../../Keyboard";
|
2019-01-18 23:43:17 +03:00
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
|
|
|
/**
|
2019-01-19 01:07:11 +03:00
|
|
|
* Represents a tab for the TabbedView.
|
2019-01-18 23:43:17 +03:00
|
|
|
*/
|
|
|
|
export class Tab {
|
|
|
|
/**
|
2019-01-19 01:07:11 +03:00
|
|
|
* Creates a new tab.
|
|
|
|
* @param {string} tabLabel The untranslated tab label.
|
2019-01-22 19:54:35 +03:00
|
|
|
* @param {string} tabIconClass The class for the tab icon. This should be a simple mask.
|
2019-01-18 23:43:17 +03:00
|
|
|
* @param {string} tabJsx The JSX for the tab container.
|
|
|
|
*/
|
2019-01-22 19:54:35 +03:00
|
|
|
constructor(tabLabel, tabIconClass, tabJsx) {
|
2019-01-18 23:43:17 +03:00
|
|
|
this.label = tabLabel;
|
2019-01-22 19:54:35 +03:00
|
|
|
this.icon = tabIconClass;
|
2019-01-18 23:43:17 +03:00
|
|
|
this.body = tabJsx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TabbedView extends React.Component {
|
2019-01-19 01:07:11 +03:00
|
|
|
static propTypes = {
|
|
|
|
// The tabs to show
|
2019-01-22 03:27:43 +03:00
|
|
|
tabs: PropTypes.arrayOf(PropTypes.instanceOf(Tab)).isRequired,
|
2019-01-19 01:07:11 +03:00
|
|
|
};
|
|
|
|
|
2019-01-18 23:43:17 +03:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2019-01-19 01:07:11 +03:00
|
|
|
this.state = {
|
2019-01-18 23:43:17 +03:00
|
|
|
activeTabIndex: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_getActiveTabIndex() {
|
2019-01-19 01:07:11 +03:00
|
|
|
if (!this.state || !this.state.activeTabIndex) return 0;
|
|
|
|
return this.state.activeTabIndex;
|
2019-01-18 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the given tab
|
|
|
|
* @param {Tab} tab the tab to show
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_setActiveTab(tab) {
|
|
|
|
const idx = this.props.tabs.indexOf(tab);
|
|
|
|
if (idx !== -1) {
|
|
|
|
this.setState({activeTabIndex: idx});
|
2019-01-19 01:07:11 +03:00
|
|
|
} else {
|
|
|
|
console.error("Could not find tab " + tab.label + " in tabs");
|
2019-01-18 23:43:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 01:07:11 +03:00
|
|
|
_renderTabLabel(tab) {
|
2019-01-18 23:43:17 +03:00
|
|
|
let classes = "mx_TabbedView_tabLabel ";
|
|
|
|
|
|
|
|
const idx = this.props.tabs.indexOf(tab);
|
|
|
|
if (idx === this._getActiveTabIndex()) classes += "mx_TabbedView_tabLabel_active";
|
|
|
|
|
2019-01-19 05:40:21 +03:00
|
|
|
let tabIcon = null;
|
|
|
|
if (tab.icon) {
|
2019-01-22 19:54:35 +03:00
|
|
|
tabIcon = <span className={`mx_TabbedView_maskedIcon ${tab.icon}`} />;
|
2019-01-19 05:40:21 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 03:49:48 +03:00
|
|
|
const onClickHandler = () => this._setActiveTab(tab);
|
2019-09-26 15:52:09 +03:00
|
|
|
const onKeyDownHandler = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
if (e.keyCode === KeyCode.ENTER || e.keyCode === KeyCode.SPACE) {
|
|
|
|
this._setActiveTab(tab);
|
|
|
|
}
|
|
|
|
};
|
2019-01-22 03:49:48 +03:00
|
|
|
|
2019-09-25 20:55:18 +03:00
|
|
|
const label = _t(tab.label);
|
2019-01-18 23:43:17 +03:00
|
|
|
return (
|
2019-09-25 20:55:18 +03:00
|
|
|
<span
|
|
|
|
className={classes}
|
|
|
|
key={"tab_label_" + tab.label}
|
|
|
|
onClick={onClickHandler}
|
2019-09-26 15:52:09 +03:00
|
|
|
onKeyDown={onKeyDownHandler}
|
2019-09-25 20:55:18 +03:00
|
|
|
role="button"
|
|
|
|
aria-label={label}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
2019-01-19 05:40:21 +03:00
|
|
|
{tabIcon}
|
|
|
|
<span className="mx_TabbedView_tabLabel_text">
|
2019-09-25 20:55:18 +03:00
|
|
|
{ label }
|
2019-01-19 05:40:21 +03:00
|
|
|
</span>
|
2019-01-18 23:43:17 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-19 01:07:11 +03:00
|
|
|
_renderTabPanel(tab) {
|
2019-01-18 23:43:17 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_TabbedView_tabPanel" key={"mx_tabpanel_" + tab.label}>
|
2019-01-30 07:45:15 +03:00
|
|
|
<div className='mx_TabbedView_tabPanelContent'>
|
|
|
|
{tab.body}
|
|
|
|
</div>
|
2019-01-18 23:43:17 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-01-19 01:07:11 +03:00
|
|
|
const labels = this.props.tabs.map(tab => this._renderTabLabel(tab));
|
|
|
|
const panel = this._renderTabPanel(this.props.tabs[this._getActiveTabIndex()]);
|
2019-01-18 23:43:17 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mx_TabbedView">
|
|
|
|
<div className="mx_TabbedView_tabLabels">
|
|
|
|
{labels}
|
|
|
|
</div>
|
2019-01-19 01:07:11 +03:00
|
|
|
{panel}
|
2019-01-18 23:43:17 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-01-22 03:49:48 +03:00
|
|
|
}
|