Merge pull request #10041 from yousifalraheem/feat/extra-user-settings
Enable adding extra setting tabs
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* 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 { renderWithEffects, wrapInTestApp } from '@backstage/test-utils';
|
||||
import { SettingsPage } from './SettingsPage';
|
||||
import { UserSettingsTab } from './UserSettingsTab';
|
||||
|
||||
jest.mock('react-router', () => ({
|
||||
...jest.requireActual('react-router'),
|
||||
useOutlet: jest.fn().mockReturnValue(undefined),
|
||||
}));
|
||||
|
||||
import { useOutlet } from 'react-router';
|
||||
|
||||
describe('<SettingsPage />', () => {
|
||||
beforeEach(() => {
|
||||
(useOutlet as jest.Mock).mockReset();
|
||||
});
|
||||
|
||||
it('should render the settings page with 3 tabs', async () => {
|
||||
const { container } = await renderWithEffects(
|
||||
wrapInTestApp(<SettingsPage />),
|
||||
);
|
||||
|
||||
const tabs = container.querySelectorAll('[class*=MuiTabs-root] button');
|
||||
expect(tabs).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('should render the settings page with 4 tabs when extra tabs are provided', async () => {
|
||||
const advancedTabRoute = (
|
||||
<UserSettingsTab path="/advanced" title="Advanced">
|
||||
<div>Advanced settings</div>
|
||||
</UserSettingsTab>
|
||||
);
|
||||
(useOutlet as jest.Mock).mockReturnValue(advancedTabRoute);
|
||||
const { container } = await renderWithEffects(
|
||||
wrapInTestApp(<SettingsPage />),
|
||||
);
|
||||
|
||||
const tabs = container.querySelectorAll('[class*=MuiTabs-root] button');
|
||||
expect(tabs).toHaveLength(4);
|
||||
expect(tabs[3].textContent).toEqual('Advanced');
|
||||
});
|
||||
});
|
||||
@@ -21,9 +21,12 @@ import {
|
||||
TabbedLayout,
|
||||
} from '@backstage/core-components';
|
||||
import React, { useContext } from 'react';
|
||||
import { useOutlet } from 'react-router';
|
||||
import { useElementFilter } from '@backstage/core-plugin-api';
|
||||
import { UserSettingsAuthProviders } from './AuthProviders';
|
||||
import { UserSettingsFeatureFlags } from './FeatureFlags';
|
||||
import { UserSettingsGeneral } from './General';
|
||||
import { USER_SETTINGS_TAB_KEY, UserSettingsTabProps } from './UserSettingsTab';
|
||||
|
||||
type Props = {
|
||||
providerSettings?: JSX.Element;
|
||||
@@ -31,6 +34,15 @@ type Props = {
|
||||
|
||||
export const SettingsPage = ({ providerSettings }: Props) => {
|
||||
const { isMobile } = useContext(SidebarPinStateContext);
|
||||
const outlet = useOutlet();
|
||||
|
||||
const tabs = useElementFilter(outlet, elements =>
|
||||
elements
|
||||
.selectByComponentData({
|
||||
key: USER_SETTINGS_TAB_KEY,
|
||||
})
|
||||
.getElements<UserSettingsTabProps>(),
|
||||
);
|
||||
|
||||
return (
|
||||
<Page themeId="home">
|
||||
@@ -48,6 +60,12 @@ export const SettingsPage = ({ providerSettings }: Props) => {
|
||||
<TabbedLayout.Route path="feature-flags" title="Feature Flags">
|
||||
<UserSettingsFeatureFlags />
|
||||
</TabbedLayout.Route>
|
||||
|
||||
{tabs.map((child, i) => (
|
||||
<TabbedLayout.Route key={i} {...child.props}>
|
||||
{child}
|
||||
</TabbedLayout.Route>
|
||||
))}
|
||||
</TabbedLayout>
|
||||
</Page>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* 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, { PropsWithChildren } from 'react';
|
||||
import { attachComponentData } from '@backstage/core-plugin-api';
|
||||
|
||||
/** @public */
|
||||
export const USER_SETTINGS_TAB_KEY = 'user-settings.tab';
|
||||
|
||||
/** @public */
|
||||
export type UserSettingsTabProps = PropsWithChildren<{
|
||||
/**
|
||||
* The path to the tab in the settings route
|
||||
* @example `/settings/advanced`
|
||||
*/
|
||||
path: string;
|
||||
/** The title of the tab. It will also reflect in the document title when the tab is active */
|
||||
title: string;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Renders a tab inside the settings page
|
||||
* @param props - Component props
|
||||
* @public
|
||||
*/
|
||||
export const UserSettingsTab = (props: UserSettingsTabProps) => {
|
||||
return <>{props.children}</>;
|
||||
};
|
||||
|
||||
attachComponentData(UserSettingsTab, USER_SETTINGS_TAB_KEY, 'UserSettingsTab');
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
export * from './UserSettingsTab';
|
||||
@@ -19,3 +19,4 @@ export * from './AuthProviders';
|
||||
export * from './General';
|
||||
export * from './FeatureFlags';
|
||||
export { useUserProfile } from './useUserProfileInfo';
|
||||
export * from './UserSettingsTab';
|
||||
|
||||
Reference in New Issue
Block a user