Make TabbedView a controlled component (#12480)
* Convert tabbedview to functional component The 'Tab' is still a class, so now it's a functional component that has a supporting class, which is maybe a bit... jarring, but I think is actually perfectly logical. * put comment back * Fix bad tab ID behaviour * Make TabbedView a controlled component This does mean the logic of keeping what tab is active is now in each container component, but for a functional component, this is a single line. It makes TabbedView simpler and the container components always know exactly what tab is being displayed rather than having to effectively keep the state separately themselves if they wanted it. Based on https://github.com/matrix-org/matrix-react-sdk/pull/12478 * Fix some types & unused prop * Remove weird behaviour of using first tab is active isn't valid * Don't pass initialTabID here now it no longer has the prop * Fix test * bleh... id, not icon * Change to sub-components and use contitional call syntax * Comments * Fix element IDs * Fix merge * Test DesktopCapturerSourcePicker to make sonarcloud the right colour * Use custom hook for the fllback tab behaviour
This commit is contained in:
@@ -18,7 +18,6 @@ limitations under the License.
|
||||
|
||||
import * as React from "react";
|
||||
import classNames from "classnames";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import { _t, TranslationKey } from "../../languageHandler";
|
||||
import AutoHideScrollbar from "./AutoHideScrollbar";
|
||||
@@ -47,6 +46,18 @@ export class Tab<T extends string> {
|
||||
) {}
|
||||
}
|
||||
|
||||
export function useActiveTabWithDefault<T extends string>(
|
||||
tabs: NonEmptyArray<Tab<string>>,
|
||||
defaultTabID: T,
|
||||
initialTabID?: T,
|
||||
): [T, (tabId: T) => void] {
|
||||
const [activeTabId, setActiveTabId] = React.useState(
|
||||
initialTabID && tabs.some((t) => t.id === initialTabID) ? initialTabID : defaultTabID,
|
||||
);
|
||||
|
||||
return [activeTabId, setActiveTabId];
|
||||
}
|
||||
|
||||
export enum TabLocation {
|
||||
LEFT = "left",
|
||||
TOP = "top",
|
||||
@@ -113,12 +124,12 @@ function TabLabel<T extends string>({ tab, isActive, onClick }: ITabLabelProps<T
|
||||
interface IProps<T extends string> {
|
||||
// An array of objects representign tabs that the tabbed view will display.
|
||||
tabs: NonEmptyArray<Tab<T>>;
|
||||
// The ID of the tab to display initially.
|
||||
initialTabId?: T;
|
||||
// The ID of the tab to show
|
||||
activeTabId: T;
|
||||
// The location of the tabs, dictating the layout of the TabbedView.
|
||||
tabLocation?: TabLocation;
|
||||
// A callback that is called when the active tab changes.
|
||||
onChange?: (tabId: T) => void;
|
||||
// A callback that is called when the active tab should change
|
||||
onChange: (tabId: T) => void;
|
||||
// The screen name to report to Posthog.
|
||||
screenName?: ScreenName;
|
||||
}
|
||||
@@ -130,39 +141,19 @@ interface IProps<T extends string> {
|
||||
export default function TabbedView<T extends string>(props: IProps<T>): JSX.Element {
|
||||
const tabLocation = props.tabLocation ?? TabLocation.LEFT;
|
||||
|
||||
const [activeTabId, setActiveTabId] = React.useState<T>((): T => {
|
||||
const initialTabIdIsValid = props.tabs.find((tab) => tab.id === props.initialTabId);
|
||||
// unfortunately typescript doesn't infer the types coorectly if the null check is included above
|
||||
return initialTabIdIsValid && props.initialTabId ? props.initialTabId : props.tabs[0].id;
|
||||
});
|
||||
|
||||
const getTabById = (id: T): Tab<T> | undefined => {
|
||||
return props.tabs.find((tab) => tab.id === id);
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows the given tab
|
||||
* @param {Tab} tab the tab to show
|
||||
*/
|
||||
const setActiveTab = (tab: Tab<T>): void => {
|
||||
// make sure this tab is still in available tabs
|
||||
if (!!getTabById(tab.id)) {
|
||||
props.onChange?.(tab.id);
|
||||
setActiveTabId(tab.id);
|
||||
} else {
|
||||
logger.error("Could not find tab " + tab.label + " in tabs");
|
||||
}
|
||||
};
|
||||
|
||||
const labels = props.tabs.map((tab) => (
|
||||
<TabLabel
|
||||
key={"tab_label_" + tab.id}
|
||||
tab={tab}
|
||||
isActive={tab.id === activeTabId}
|
||||
onClick={() => setActiveTab(tab)}
|
||||
isActive={tab.id === props.activeTabId}
|
||||
onClick={() => props.onChange(tab.id)}
|
||||
/>
|
||||
));
|
||||
const tab = getTabById(activeTabId);
|
||||
const tab = getTabById(props.activeTabId);
|
||||
const panel = tab ? <TabPanel tab={tab} /> : null;
|
||||
|
||||
const tabbedViewClasses = classNames({
|
||||
|
||||
Reference in New Issue
Block a user