@bitrise/bitkit 10.17.0 → 10.17.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,7 +1,7 @@
1
1
  {
2
2
  "name": "@bitrise/bitkit",
3
3
  "description": "Bitrise React component library",
4
- "version": "10.17.0",
4
+ "version": "10.17.1",
5
5
  "repository": "git@github.com:bitrise-io/bitkit.git",
6
6
  "main": "src/index.ts",
7
7
  "license": "UNLICENSED",
@@ -1,4 +1,4 @@
1
- import React, { isValidElement, ReactElement, useEffect, useState } from 'react';
1
+ import React, { isValidElement, ReactElement, 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
 
@@ -15,6 +15,25 @@ const getTabIds = (props: TabsProps): string[] => {
15
15
  return tabs.filter((item) => isValidElement(item)).map((item) => item.props.id);
16
16
  };
17
17
 
18
+ const getTabIndexFromSearchParams = (tabIds: string[], defaultIndex?: number) => {
19
+ if (typeof window === 'undefined') {
20
+ return undefined;
21
+ }
22
+
23
+ const searchParams = new URLSearchParams(window.location.search);
24
+ const tabName = searchParams.get('tab');
25
+ if (!tabName) {
26
+ return undefined;
27
+ }
28
+
29
+ const index = tabIds.indexOf(tabName);
30
+ if (index === -1) {
31
+ return defaultIndex;
32
+ }
33
+
34
+ return index;
35
+ };
36
+
18
37
  /**
19
38
  * An accessible tabs component that provides keyboard interactions and ARIA attributes described in the WAI-ARIA Tabs Design Pattern.
20
39
  */
@@ -23,21 +42,16 @@ const Tabs = forwardRef<TabsProps, 'div'>((props, ref) => {
23
42
  const tabIds = getTabIds(props);
24
43
  const defaultIndex = tabIds.indexOf(defaultTab) > -1 ? tabIds.indexOf(defaultTab) : 0;
25
44
 
26
- const { searchParams, replace } = useHistory();
27
- const [actualIndex, setActualIndex] = useState(defaultIndex);
28
-
29
- useEffect(() => {
30
- if (withHistory && searchParams.get('tab')) {
31
- const tabName = searchParams.get('tab') || '';
32
- const index = tabIds.indexOf(tabName) > -1 ? tabIds.indexOf(tabName) : defaultIndex;
33
- setActualIndex(index);
34
- }
35
- }, []);
45
+ const { replace } = useHistory();
46
+ const [actualIndex, setActualIndex] = useState(
47
+ withHistory ? getTabIndexFromSearchParams(tabIds, defaultIndex) : defaultIndex,
48
+ );
36
49
 
37
50
  const onTabChange = (index: number) => {
38
51
  const tabId = tabIds[index];
39
52
  setActualIndex(index);
40
53
  if (withHistory) {
54
+ const searchParams = new URLSearchParams(window.location.search);
41
55
  if (tabId) {
42
56
  searchParams.set('tab', tabId);
43
57
  } else {