mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
feat(v2): support syncing tab choices (#2366)
* feat(v2): Support syncing tab choices * Move docs changes to website/docs * Do not import entire React in TabGroupChoiceContext * Store only one tab choice according to discussion in PR * Remove leftover logging code during debugging * Put storage value in separate const outside the hook-level * Use an array to keep track of different tab groups * Revert back to using `groupId` * Update markdown-features.mdx * Update markdown-features.mdx Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
This commit is contained in:
parent
da0a61d1f0
commit
55a50d37dc
8 changed files with 268 additions and 30 deletions
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import React, {useState, Children} from 'react';
|
||||
import useTabGroupChoiceContext from '@theme/hooks/useTabGroupChoiceContext';
|
||||
|
||||
import classnames from 'classnames';
|
||||
|
||||
|
@ -17,8 +18,27 @@ const keys = {
|
|||
};
|
||||
|
||||
function Tabs(props) {
|
||||
const {block, children, defaultValue, values} = props;
|
||||
const {block, children, defaultValue, values, groupId} = props;
|
||||
const {tabGroupChoices, setTabGroupChoices} = useTabGroupChoiceContext();
|
||||
const [selectedValue, setSelectedValue] = useState(defaultValue);
|
||||
|
||||
if (groupId != null) {
|
||||
const relevantTabGroupChoice = tabGroupChoices[groupId];
|
||||
if (
|
||||
relevantTabGroupChoice != null &&
|
||||
relevantTabGroupChoice !== selectedValue
|
||||
) {
|
||||
setSelectedValue(relevantTabGroupChoice);
|
||||
}
|
||||
}
|
||||
|
||||
const changeSelectedValue = newValue => {
|
||||
setSelectedValue(newValue);
|
||||
if (groupId != null) {
|
||||
setTabGroupChoices(groupId, newValue);
|
||||
}
|
||||
};
|
||||
|
||||
const tabRefs = [];
|
||||
|
||||
const focusNextTab = (tabs, target) => {
|
||||
|
@ -73,8 +93,8 @@ function Tabs(props) {
|
|||
key={value}
|
||||
ref={tabControl => tabRefs.push(tabControl)}
|
||||
onKeyDown={event => handleKeydown(tabRefs, event.target, event)}
|
||||
onFocus={() => setSelectedValue(value)}
|
||||
onClick={() => setSelectedValue(value)}>
|
||||
onFocus={() => changeSelectedValue(value)}
|
||||
onClick={() => changeSelectedValue(value)}>
|
||||
{label}
|
||||
</li>
|
||||
))}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue