-
Notifications
You must be signed in to change notification settings - Fork 377
feat(Tabs): allow dynamic close/add #7297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a37f05e
feat(Tabs): allow dynamic close/add
kmcfaul df266d4
add button names
kmcfaul 1da8f7c
change to buttons, add disabled for close button, update demo
kmcfaul 2c9e98c
focus added tabs, update snap
kmcfaul 6b8b371
update default aria
kmcfaul ff2b5c3
update new tab naming
kmcfaul 37155d4
update aria names, fix logic for empty tabs
kmcfaul b8fa56b
update test with new prop names
kmcfaul 8923eaf
update prop name in desc, prevent single tab close
kmcfaul aafc4dc
update aria label, reabase
kmcfaul 478bba6
add update check for children
kmcfaul 9fd0a5d
add beta flags
kmcfaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
packages/react-core/src/components/Tabs/examples/TabsDynamic.tsx
This file contains hidden or 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,64 @@ | ||
| import React from 'react'; | ||
| import { Tabs, Tab, TabTitleText } from '@patternfly/react-core'; | ||
|
|
||
| export const TabsDynamic: React.FunctionComponent = () => { | ||
| const [activeTabKey, setActiveTabKey] = React.useState<number>(0); | ||
| const [tabs, setTabs] = React.useState<string[]>(['Terminal 1', 'Terminal 2', 'Terminal 3']); | ||
| const [newTabNumber, setNewTabNumber] = React.useState<number>(4); | ||
| const tabComponentRef = React.useRef<any>(); | ||
| const firstMount = React.useRef(true); | ||
|
|
||
| const onClose = (event: any, tabIndex: string | number) => { | ||
| const tabIndexNum = tabIndex as number; | ||
| let nextTabIndex = activeTabKey; | ||
| if (tabIndexNum < activeTabKey) { | ||
| // if a preceding tab is closing, keep focus on the new index of the current tab | ||
| nextTabIndex = activeTabKey - 1 > 0 ? activeTabKey - 1 : 0; | ||
| } else if (activeTabKey === tabs.length - 1) { | ||
| // if the closing tab is the last tab, focus the preceding tab | ||
| nextTabIndex = tabs.length - 2 > 0 ? tabs.length - 2 : 0; | ||
| } | ||
| setActiveTabKey(nextTabIndex); | ||
| setTabs(tabs.filter((tab, index) => index !== tabIndex)); | ||
| }; | ||
|
|
||
| const onAdd = () => { | ||
| setTabs([...tabs, `Terminal ${newTabNumber}`]); | ||
| setActiveTabKey(tabs.length); | ||
| setNewTabNumber(newTabNumber + 1); | ||
| }; | ||
|
|
||
| React.useEffect(() => { | ||
| if (firstMount.current) { | ||
| firstMount.current = false; | ||
| return; | ||
| } else { | ||
| const first = tabComponentRef.current.tabList.current.childNodes[activeTabKey]; | ||
| first && first.firstChild.focus(); | ||
| } | ||
| }, [tabs]); | ||
|
|
||
| return ( | ||
| <Tabs | ||
| activeKey={activeTabKey} | ||
| onSelect={(event: any, tabIndex: string | number) => setActiveTabKey(tabIndex as number)} | ||
| onClose={onClose} | ||
| onAdd={onAdd} | ||
| aria-label="Tabs in the addable/closeable example" | ||
| addButtonAriaLabel="Add new tab" | ||
| ref={tabComponentRef} | ||
| > | ||
| {tabs.map((tab, index) => ( | ||
| <Tab | ||
| key={index} | ||
| eventKey={index} | ||
| title={<TabTitleText>{tab}</TabTitleText>} | ||
| closeButtonAriaLabel={`Close ${tab}`} | ||
| isCloseDisabled={tabs.length === 1} | ||
| > | ||
| {tab} | ||
| </Tab> | ||
| ))} | ||
| </Tabs> | ||
| ); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.