@bitrise/bitkit 12.1.1 → 12.2.0-alpha-chakra.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Button, TabProps as ChakraTabProps, forwardRef, useTab, useTabsStyles } from '@chakra-ui/react';
|
|
2
|
-
import { TypeColors } from '
|
|
2
|
+
import { TypeColors } from '../../Foundations/Colors/Colors';
|
|
3
3
|
import Box from '../Box/Box';
|
|
4
4
|
import Icon, { TypeIconName } from '../Icon/Icon';
|
|
5
5
|
import Text from '../Text/Text';
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import React, { isValidElement, ReactElement, useState } from 'react';
|
|
1
|
+
import React, { isValidElement, ReactElement, useEffect, useState } from 'react';
|
|
2
2
|
import { Tabs as ChakraTabs, TabsProps as ChakraTabsProps, forwardRef } from '@chakra-ui/react';
|
|
3
3
|
import { useHistory } from '../../hooks';
|
|
4
4
|
|
|
5
5
|
export interface TabsProps extends ChakraTabsProps {
|
|
6
6
|
defaultTab?: string;
|
|
7
7
|
onChange?: (index: number, tabId?: string) => void;
|
|
8
|
+
tabId?: string;
|
|
8
9
|
variant?: 'contained' | 'line';
|
|
9
10
|
withHistory?: boolean;
|
|
10
11
|
}
|
|
@@ -16,7 +17,7 @@ const getTabIds = (props: TabsProps): string[] => {
|
|
|
16
17
|
return tabs.filter((item) => isValidElement(item)).map((item) => item.props.id);
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
const getTabIndexFromSearchParams = (tabIds: string[],
|
|
20
|
+
const getTabIndexFromSearchParams = (tabIds: string[], tabIndex?: number) => {
|
|
20
21
|
if (typeof window === 'undefined') {
|
|
21
22
|
return undefined;
|
|
22
23
|
}
|
|
@@ -29,7 +30,7 @@ const getTabIndexFromSearchParams = (tabIds: string[], defaultIndex?: number) =>
|
|
|
29
30
|
|
|
30
31
|
const index = tabIds.indexOf(tabName);
|
|
31
32
|
if (index === -1) {
|
|
32
|
-
return
|
|
33
|
+
return tabIndex;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
return index;
|
|
@@ -39,31 +40,51 @@ const getTabIndexFromSearchParams = (tabIds: string[], defaultIndex?: number) =>
|
|
|
39
40
|
* An accessible tabs component that provides keyboard interactions and ARIA attributes described in the WAI-ARIA Tabs Design Pattern.
|
|
40
41
|
*/
|
|
41
42
|
const Tabs = forwardRef<TabsProps, 'div'>((props, ref) => {
|
|
42
|
-
const { defaultTab = '', onChange, withHistory, ...rest } = props;
|
|
43
|
+
const { defaultTab = '', onChange, tabId, withHistory, ...rest } = props;
|
|
44
|
+
const { replace } = useHistory();
|
|
45
|
+
|
|
43
46
|
const tabIds = getTabIds(props);
|
|
44
|
-
const defaultIndex = tabIds.indexOf(defaultTab) > -1 ? tabIds.indexOf(defaultTab) : 0;
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
let tabIndex = 0;
|
|
49
|
+
|
|
50
|
+
if (defaultTab) {
|
|
51
|
+
tabIndex = tabIds.includes(defaultTab) ? tabIds.indexOf(defaultTab) : 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (tabId) {
|
|
55
|
+
tabIndex = tabIds.includes(tabId) ? tabIds.indexOf(tabId) : 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
47
58
|
const [actualIndex, setActualIndex] = useState(
|
|
48
|
-
withHistory ? getTabIndexFromSearchParams(tabIds,
|
|
59
|
+
withHistory ? getTabIndexFromSearchParams(tabIds, tabIndex) : tabIndex,
|
|
49
60
|
);
|
|
50
61
|
|
|
51
62
|
const onTabChange = (index: number) => {
|
|
52
|
-
const
|
|
53
|
-
|
|
63
|
+
const newTabId = tabIds[index];
|
|
64
|
+
|
|
54
65
|
if (withHistory) {
|
|
55
66
|
const searchParams = new URLSearchParams(window.location.search);
|
|
56
|
-
if (
|
|
57
|
-
searchParams.set('tab',
|
|
67
|
+
if (newTabId) {
|
|
68
|
+
searchParams.set('tab', newTabId);
|
|
58
69
|
} else {
|
|
59
70
|
searchParams.delete('tab');
|
|
60
71
|
}
|
|
61
72
|
replace(`?${searchParams.toString()}`);
|
|
62
73
|
}
|
|
74
|
+
|
|
63
75
|
if (onChange) {
|
|
64
|
-
onChange(index,
|
|
76
|
+
onChange(index, newTabId);
|
|
65
77
|
}
|
|
78
|
+
|
|
79
|
+
setActualIndex(index);
|
|
66
80
|
};
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
if (tabId) {
|
|
84
|
+
setActualIndex(tabIds.includes(tabId) ? tabIds.indexOf(tabId) : 0);
|
|
85
|
+
}
|
|
86
|
+
}, [tabId]);
|
|
87
|
+
|
|
67
88
|
return <ChakraTabs {...rest} ref={ref} onChange={onTabChange} index={actualIndex} />;
|
|
68
89
|
});
|
|
69
90
|
|