-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add tabs navigation inside landing page (#808)
* Add tabs component and logic * Update constants * Use tabs in RWS panel * Update styles * test: update and add test cases * Update e2e * Update style and text * font size changes, and tab styles changes
- Loading branch information
Showing
10 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import classNames from 'classnames'; | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
interface TabsProps { | ||
items: Array<{ | ||
title: string; | ||
content: { | ||
Element: (props: any) => React.JSX.Element; | ||
props?: Record<string, any>; | ||
}; | ||
}>; | ||
} | ||
|
||
const Tabs = ({ items }: TabsProps) => { | ||
const [activeTab, setActiveTab] = useState(0); | ||
|
||
const ActiveTabContent = items?.[activeTab].content?.Element; | ||
|
||
const handleKeyDown = useCallback( | ||
(event: React.KeyboardEvent<HTMLButtonElement>) => { | ||
event.preventDefault(); | ||
|
||
if (event.key === 'Tab') { | ||
const nextIndex = activeTab + 1; | ||
if (nextIndex < items.length) { | ||
setActiveTab(nextIndex); | ||
} else { | ||
setActiveTab(0); | ||
} | ||
} | ||
|
||
if (event.shiftKey && event.key === 'Tab') { | ||
const previousIndex = activeTab - 1; | ||
if (previousIndex >= 0) { | ||
setActiveTab(previousIndex); | ||
} else { | ||
setActiveTab(items.length - 1); | ||
} | ||
} | ||
}, | ||
[activeTab, items.length] | ||
); | ||
|
||
return ( | ||
<div className="max-w-2xl h-fit px-4"> | ||
<div className="flex gap-10 border-b border-gray-300 dark:border-quartz"> | ||
{items.map((item, index) => ( | ||
<button | ||
key={index} | ||
onClick={() => setActiveTab(index)} | ||
onKeyDown={handleKeyDown} | ||
autoFocus={index === 0} | ||
className={classNames( | ||
'pb-1.5 px-3 border-b-2 hover:opacity-80 outline-none text-sm', | ||
{ | ||
'border-bright-navy-blue dark:border-jordy-blue text-bright-navy-blue dark:text-jordy-blue': | ||
index === activeTab, | ||
}, | ||
{ | ||
'border-transparent text-raisin-black dark:text-bright-gray': | ||
index !== activeTab, | ||
} | ||
)} | ||
> | ||
{item.title} | ||
</button> | ||
))} | ||
</div> | ||
<div className="pt-4"> | ||
{ActiveTabContent && ( | ||
<ActiveTabContent {...items?.[activeTab].content?.props} /> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Tabs; |
71 changes: 71 additions & 0 deletions
71
packages/design-system/src/components/tabs/tests/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import React from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Tabs from '..'; | ||
|
||
describe('Tabs', () => { | ||
const props = { | ||
items: [ | ||
{ | ||
title: 'title1', | ||
content: { | ||
Element: () => <div>content1</div>, | ||
}, | ||
}, | ||
{ | ||
title: 'title2', | ||
content: { | ||
Element: () => <div>content2</div>, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
it('should render', () => { | ||
render(<Tabs {...props} />); | ||
|
||
expect(screen.getByText('title1')).toBeInTheDocument(); | ||
expect(screen.getByText('title1')).toHaveClass('border-b-2'); | ||
|
||
fireEvent.click(screen.getByText('title2')); | ||
|
||
expect(screen.getByText('content2')).toBeInTheDocument(); | ||
expect(screen.getByText('title2')).toHaveClass('border-b-2'); | ||
|
||
fireEvent.keyDown(screen.getByText('title2'), { key: 'Tab' }); | ||
|
||
expect(screen.getByText('content1')).toBeInTheDocument(); | ||
expect(screen.getByText('title1')).toHaveClass('border-b-2'); | ||
|
||
fireEvent.keyDown(screen.getByText('title1'), { | ||
key: 'Tab', | ||
shiftKey: true, | ||
}); | ||
|
||
expect(screen.getByText('content2')).toBeInTheDocument(); | ||
expect(screen.getByText('title2')).toHaveClass('border-b-2'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters