@bitrise/bitkit 13.160.1-alpha.2 → 13.161.0
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
|
@@ -64,7 +64,7 @@ const baseStyle = defineStyle(({ variant, size, isExpanded }): SystemStyleObject
|
|
|
64
64
|
borderBottomRightRadius: variant === 'single' ? '0' : '2',
|
|
65
65
|
cursor: 'pointer',
|
|
66
66
|
div: {
|
|
67
|
-
overflow: 'auto !important',
|
|
67
|
+
overflow: variant !== 'multi-breakwords' && 'auto !important',
|
|
68
68
|
},
|
|
69
69
|
minHeight: variant === 'inline' ? '24' : '40',
|
|
70
70
|
overflow: 'auto',
|
|
@@ -1,18 +1,92 @@
|
|
|
1
|
+
import React, { isValidElement, ReactElement, useEffect, useState } from 'react';
|
|
1
2
|
import { forwardRef, Tabs as ChakraTabs, TabsProps as ChakraTabsProps } from '@chakra-ui/react';
|
|
3
|
+
import { useHistory } from '../../hooks';
|
|
4
|
+
import { TabProps } from './Tab';
|
|
2
5
|
|
|
3
6
|
export interface TabsProps extends ChakraTabsProps {
|
|
4
7
|
defaultTab?: string;
|
|
5
8
|
onChange?: (index: number, tabId?: string) => void;
|
|
6
9
|
tabId?: string;
|
|
7
10
|
variant?: 'contained' | 'line';
|
|
11
|
+
withHistory?: boolean;
|
|
8
12
|
}
|
|
9
13
|
|
|
14
|
+
const getTabIds = (props: TabsProps): string[] => {
|
|
15
|
+
const tabList = React.Children.toArray(props.children)[0] as ReactElement;
|
|
16
|
+
const tabs = React.Children.toArray(tabList.props.children) as ReactElement[];
|
|
17
|
+
|
|
18
|
+
return tabs.filter((item) => isValidElement(item)).map((item) => (item.props as TabProps).id || '');
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const getTabIndexFromSearchParams = (tabIds: string[], tabIndex?: number) => {
|
|
22
|
+
if (typeof window === 'undefined') {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
27
|
+
const tabName = searchParams.get('tab');
|
|
28
|
+
if (!tabName) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const index = tabIds.indexOf(tabName);
|
|
33
|
+
if (index === -1) {
|
|
34
|
+
return tabIndex;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return index;
|
|
38
|
+
};
|
|
39
|
+
|
|
10
40
|
/**
|
|
11
41
|
* An accessible tabs component that provides keyboard interactions and ARIA attributes described in the WAI-ARIA Tabs Design Pattern.
|
|
12
42
|
*/
|
|
13
43
|
const Tabs = forwardRef<TabsProps, 'div'>((props, ref) => {
|
|
14
|
-
const { variant = 'line', ...rest } = props;
|
|
15
|
-
|
|
44
|
+
const { defaultTab = '', onChange, tabId, variant = 'line', withHistory, ...rest } = props;
|
|
45
|
+
const { replace } = useHistory();
|
|
46
|
+
|
|
47
|
+
const tabIds = getTabIds(props);
|
|
48
|
+
|
|
49
|
+
let tabIndex = 0;
|
|
50
|
+
|
|
51
|
+
if (defaultTab) {
|
|
52
|
+
tabIndex = tabIds.includes(defaultTab) ? tabIds.indexOf(defaultTab) : 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (tabId) {
|
|
56
|
+
tabIndex = tabIds.includes(tabId) ? tabIds.indexOf(tabId) : 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const [actualIndex, setActualIndex] = useState(
|
|
60
|
+
withHistory ? getTabIndexFromSearchParams(tabIds, tabIndex) : tabIndex,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const onTabChange = (index: number) => {
|
|
64
|
+
const newTabId = tabIds[index];
|
|
65
|
+
|
|
66
|
+
if (withHistory) {
|
|
67
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
68
|
+
if (newTabId) {
|
|
69
|
+
searchParams.set('tab', newTabId);
|
|
70
|
+
} else {
|
|
71
|
+
searchParams.delete('tab');
|
|
72
|
+
}
|
|
73
|
+
replace(`?${searchParams.toString()}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (onChange) {
|
|
77
|
+
onChange(index, newTabId);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setActualIndex(index);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (tabId) {
|
|
85
|
+
setActualIndex(tabIds.includes(tabId) ? tabIds.indexOf(tabId) : 0);
|
|
86
|
+
}
|
|
87
|
+
}, [tabId]);
|
|
88
|
+
|
|
89
|
+
return <ChakraTabs {...rest} ref={ref} index={actualIndex} onChange={onTabChange} variant={variant} />;
|
|
16
90
|
});
|
|
17
91
|
|
|
18
92
|
export default Tabs;
|
package/src/index.ts
CHANGED
|
@@ -58,8 +58,6 @@ export { default as TabPanels } from './Components/Tabs/TabPanels';
|
|
|
58
58
|
export type { TabPanelProps } from './Components/Tabs/TabPanel';
|
|
59
59
|
export { default as TabPanel } from './Components/Tabs/TabPanel';
|
|
60
60
|
|
|
61
|
-
export { default as useTabs } from './Components/Tabs/useTabs';
|
|
62
|
-
|
|
63
61
|
export type { BadgeProps } from './Components/Badge/Badge';
|
|
64
62
|
export { default as Badge } from './Components/Badge/Badge';
|
|
65
63
|
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
type UseTabsProps<T> = {
|
|
4
|
-
defaultId?: T;
|
|
5
|
-
queryKey?: string;
|
|
6
|
-
tabIds: T[];
|
|
7
|
-
withHistory?: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const useTabs = <T extends string>(props: UseTabsProps<T>) => {
|
|
11
|
-
const { defaultId, queryKey = 'tab', tabIds, withHistory } = props;
|
|
12
|
-
|
|
13
|
-
const searchParams = new URLSearchParams(window.location.search);
|
|
14
|
-
|
|
15
|
-
const defaultIndex = useMemo(() => {
|
|
16
|
-
if (withHistory && searchParams.get(queryKey)) {
|
|
17
|
-
return tabIds.indexOf(searchParams.get(queryKey) as T);
|
|
18
|
-
}
|
|
19
|
-
if (defaultId) {
|
|
20
|
-
return tabIds.indexOf(defaultId);
|
|
21
|
-
}
|
|
22
|
-
return 0;
|
|
23
|
-
}, []);
|
|
24
|
-
|
|
25
|
-
const [tabIndex, setTabIndex] = useState(defaultIndex);
|
|
26
|
-
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
if (withHistory) {
|
|
29
|
-
searchParams.set(queryKey, tabIds[tabIndex]);
|
|
30
|
-
window.parent.history.replaceState(null, '', `?${decodeURIComponent(searchParams.toString())}`);
|
|
31
|
-
}
|
|
32
|
-
}, [tabIndex]);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
setTabIndex,
|
|
36
|
-
tabIndex,
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export default useTabs;
|