-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/components/Documentation/Markdown/ToggleProvider/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,48 @@ | ||
import React, { createContext, useState } from 'react' | ||
|
||
interface ITogglesData { | ||
[key: string]: { texts: string[]; checkedInd: number } | ||
} | ||
|
||
interface ITogglesContext { | ||
addNewToggle?: (id: string, texts: string[]) => void | ||
updateToggleInd?: (id: string, newInd: number) => void | ||
togglesData?: ITogglesData | ||
} | ||
|
||
export const TogglesContext = createContext<ITogglesContext>({}) | ||
|
||
export const TogglesProvider: React.FC = ({ children }) => { | ||
const [togglesData, setTogglesData] = useState({}) | ||
|
||
const addNewToggle = (id: string, texts: string[]): void => { | ||
const togglesDataCopy: ITogglesData = { ...togglesData } | ||
togglesDataCopy[id] = { texts, checkedInd: 0 } | ||
setTogglesData(togglesDataCopy) | ||
} | ||
|
||
const updateToggleInd = (id: string, newInd: number): void => { | ||
const togglesDataCopy: ITogglesData = { ...togglesData } | ||
const selectedTabText = togglesDataCopy[id].texts[newInd] | ||
togglesDataCopy[id] = { ...togglesDataCopy[id], checkedInd: newInd } | ||
|
||
for (const [toggleId, { texts }] of Object.entries(togglesDataCopy)) { | ||
if (texts.includes(selectedTabText)) { | ||
togglesDataCopy[toggleId] = { | ||
...togglesDataCopy[toggleId], | ||
checkedInd: togglesDataCopy[id].texts.indexOf(selectedTabText) | ||
} | ||
} | ||
} | ||
|
||
setTogglesData(togglesDataCopy) | ||
} | ||
|
||
return ( | ||
<TogglesContext.Provider | ||
value={{ addNewToggle, updateToggleInd, togglesData }} | ||
> | ||
{children} | ||
</TogglesContext.Provider> | ||
) | ||
} |
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