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:
Sam Zhou 2020-03-15 01:47:52 -04:00 committed by GitHub
parent da0a61d1f0
commit 55a50d37dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 268 additions and 30 deletions

View file

@ -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>
))}