This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Switch TabItems with identical labels together
#380
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { createContext, useState } from "react"; | ||
| import { createHash } from "crypto"; | ||
| import type { BinaryToTextEncoding } from "crypto"; | ||
| import { DataTab } from "./types"; | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export interface TabContextProps { | ||
| getSelectedLabel: (tabs: Array<string>) => string; | ||
| setSelectedLabel: (tabs: Array<string>, selected: string) => void; | ||
| } | ||
|
|
||
| export const TabContext = createContext<TabContextProps>({ | ||
| getSelectedLabel: (tabs: Array<string>): string => { | ||
| return ""; | ||
| }, | ||
| setSelectedLabel: (tabs: Array<string>, selected: string) => { | ||
| return; | ||
| }, | ||
| }); | ||
|
|
||
| // labelHashKey returns the key that TabContextProvider uses to get and set the | ||
| // selected tab label or dropdown menu item within a particular combination of | ||
| // lables/menu items. | ||
| const labelHashKey = (labels: Array<string>): string => { | ||
| const labelsCopy = [...labels]; | ||
| labelsCopy.sort(); | ||
| return labelsCopy.join(""); | ||
| }; | ||
|
|
||
| interface TabContextProviderProps { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| // TabContextProvider tracks the currently selected tab label and dropdown menu | ||
| // option in each Tabs component that has rendered. | ||
| export const TabContextProvider = ({ children }: TabContextProviderProps) => { | ||
| const [stateForAllLabels, setStateForAllLabels] = useState({}); | ||
|
|
||
| const context = { | ||
| getSelectedLabel: (tabs: Array<string>): string => { | ||
| return stateForAllLabels[labelHashKey(tabs)]; | ||
| }, | ||
|
|
||
| setSelectedLabel: (tabs: Array<string>, selected: string): void => { | ||
| const key = labelHashKey(tabs); | ||
| setStateForAllLabels((prevState) => { | ||
| return { ...prevState, [key]: selected }; | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| return <TabContext.Provider value={context}>{children}</TabContext.Provider>; | ||
| }; | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I haven't created a context with methods like this before. It looked so wrong to me but, the docs say this is how it goes so, 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this approach in a few places in the docs engine. I'm not super familiar with React best practices, so I'm happy to change this (or create a GitHub issue so we can change this everywhere later) if there's a better way!