@bitrise/bitkit 13.262.0 → 13.264.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 +1 -1
- package/src/Components/Dialog/Dialog.context.tsx +22 -0
- package/src/Components/Dialog/Dialog.tsx +38 -50
- package/src/Components/Dialog/DialogBody.tsx +95 -2
- package/src/Components/Dialog/DialogProps.ts +16 -0
- package/src/Components/TreeView/TreeView.context.tsx +64 -20
- package/src/Components/TreeView/TreeView.theme.ts +2 -1
- package/src/Components/TreeView/TreeView.tsx +38 -32
- package/src/Components/TreeView/TreeView.types.ts +34 -8
- package/src/Components/TreeView/TreeViewGroup.tsx +11 -3
- package/src/Components/TreeView/TreeViewNode.tsx +90 -49
- package/src/index.ts +2 -1
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createContext, ReactNode, useContext } from 'react';
|
|
2
|
+
import { DialogProps } from './DialogProps';
|
|
3
|
+
|
|
4
|
+
export type DialogContextValueType = {
|
|
5
|
+
scrollBehavior: DialogProps['scrollBehavior'];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const DialogContext = createContext<DialogContextValueType>({ scrollBehavior: 'outside' });
|
|
9
|
+
|
|
10
|
+
export const DialogContextProvider = ({ children, value }: { children: ReactNode; value: DialogContextValueType }) => {
|
|
11
|
+
return <DialogContext.Provider value={value}>{children}</DialogContext.Provider>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const useDialogContext = () => {
|
|
15
|
+
const context = useContext(DialogContext);
|
|
16
|
+
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error('DialogContext is not provided');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return context;
|
|
22
|
+
};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { useId } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
ComponentWithAs,
|
|
4
|
-
HTMLChakraProps,
|
|
5
4
|
Modal,
|
|
6
5
|
ModalCloseButton,
|
|
7
6
|
ModalContent,
|
|
8
7
|
ModalHeader,
|
|
9
8
|
ModalOverlay,
|
|
10
|
-
ModalProps,
|
|
11
9
|
useBreakpointValue,
|
|
12
10
|
usePrefersReducedMotion,
|
|
13
11
|
} from '@chakra-ui/react';
|
|
@@ -15,21 +13,8 @@ import { BREAKPOINTS } from '../../types/bitkit';
|
|
|
15
13
|
import Icon from '../Icon/Icon';
|
|
16
14
|
import Text from '../Text/Text';
|
|
17
15
|
import Tooltip from '../Tooltip/Tooltip';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
extends Omit<HTMLChakraProps<'section'>, 'scrollBehavior'>,
|
|
21
|
-
Pick<ModalProps, 'returnFocusOnClose'> {
|
|
22
|
-
isClosable?: boolean;
|
|
23
|
-
isOpen: boolean;
|
|
24
|
-
onClose(): void;
|
|
25
|
-
onCloseComplete?: () => void;
|
|
26
|
-
size?: 'small' | 'medium' | 'large' | 'full';
|
|
27
|
-
scrollBehavior?: 'inside' | 'outside';
|
|
28
|
-
title: string;
|
|
29
|
-
trapFocus?: boolean;
|
|
30
|
-
variant?: 'default' | 'empty';
|
|
31
|
-
closeNotice?: string;
|
|
32
|
-
}
|
|
16
|
+
import { DialogContextProvider } from './Dialog.context';
|
|
17
|
+
import { DialogProps } from './DialogProps';
|
|
33
18
|
|
|
34
19
|
const Dialog: ComponentWithAs<'section', DialogProps> = ({
|
|
35
20
|
children,
|
|
@@ -57,39 +42,42 @@ const Dialog: ComponentWithAs<'section', DialogProps> = ({
|
|
|
57
42
|
);
|
|
58
43
|
|
|
59
44
|
return (
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
45
|
+
<DialogContextProvider value={{ scrollBehavior }}>
|
|
46
|
+
<Modal
|
|
47
|
+
closeOnEsc={isClosable}
|
|
48
|
+
closeOnOverlayClick={false}
|
|
49
|
+
isOpen={isOpen}
|
|
50
|
+
motionPreset={prefersReducedMotion ? 'none' : 'scale'}
|
|
51
|
+
onClose={isClosable ? onClose : () => {}}
|
|
52
|
+
onCloseComplete={onCloseComplete}
|
|
53
|
+
returnFocusOnClose={returnFocusOnClose}
|
|
54
|
+
scrollBehavior={scrollBehavior}
|
|
55
|
+
size={dialogSize}
|
|
56
|
+
trapFocus={trapFocus}
|
|
57
|
+
>
|
|
58
|
+
<ModalOverlay />
|
|
59
|
+
|
|
60
|
+
<ModalContent aria-labelledby={variant !== 'empty' ? headerId : undefined} {...rest}>
|
|
61
|
+
{variant !== 'empty' && (
|
|
62
|
+
<>
|
|
63
|
+
<ModalHeader marginRight="48">
|
|
64
|
+
<Text as="h1" id={headerId} textStyle="comp/dialog/title">
|
|
65
|
+
{title}
|
|
66
|
+
</Text>
|
|
67
|
+
</ModalHeader>
|
|
68
|
+
{closeNotice ? (
|
|
69
|
+
<Tooltip label={closeNotice} placement="top">
|
|
70
|
+
{closeBtn}
|
|
71
|
+
</Tooltip>
|
|
72
|
+
) : (
|
|
73
|
+
closeBtn
|
|
74
|
+
)}
|
|
75
|
+
</>
|
|
76
|
+
)}
|
|
77
|
+
{children}
|
|
78
|
+
</ModalContent>
|
|
79
|
+
</Modal>
|
|
80
|
+
</DialogContextProvider>
|
|
93
81
|
);
|
|
94
82
|
};
|
|
95
83
|
|
|
@@ -1,9 +1,102 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { ModalBody, ModalBodyProps, useModalContext } from '@chakra-ui/react';
|
|
3
|
+
import Box, { BoxProps } from '../Box/Box';
|
|
4
|
+
import Icon from '../Icon/Icon';
|
|
5
|
+
import { useDialogContext } from './Dialog.context';
|
|
6
|
+
|
|
7
|
+
const ScrollButton = (props: BoxProps) => {
|
|
8
|
+
return (
|
|
9
|
+
<Box
|
|
10
|
+
{...props}
|
|
11
|
+
as="button"
|
|
12
|
+
bg="background/primary"
|
|
13
|
+
borderColor="border/minimal"
|
|
14
|
+
borderRadius={16}
|
|
15
|
+
borderWidth={1}
|
|
16
|
+
boxShadow="large"
|
|
17
|
+
h={32}
|
|
18
|
+
w={32}
|
|
19
|
+
>
|
|
20
|
+
<Icon color="icon/tertiary" name="ArrowDown" size="16" />
|
|
21
|
+
</Box>
|
|
22
|
+
);
|
|
23
|
+
};
|
|
2
24
|
|
|
3
25
|
export type DialogBodyProps = ModalBodyProps;
|
|
4
26
|
|
|
27
|
+
const ScrollableDialogBody = (props: DialogBodyProps) => {
|
|
28
|
+
const { isOpen } = useModalContext();
|
|
29
|
+
|
|
30
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
31
|
+
|
|
32
|
+
const [isScrollButtonVisible, setIsScrollButtonVisible] = useState(false);
|
|
33
|
+
|
|
34
|
+
const updateScrollButtonVisibility = useCallback(() => {
|
|
35
|
+
const content = contentRef.current;
|
|
36
|
+
|
|
37
|
+
if (!isOpen || !content) {
|
|
38
|
+
setIsScrollButtonVisible(false);
|
|
39
|
+
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const didScrollToBottom = content.scrollTop >= content.scrollHeight - content.offsetHeight - 1;
|
|
44
|
+
setIsScrollButtonVisible(!didScrollToBottom);
|
|
45
|
+
}, []);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const content = contentRef.current;
|
|
49
|
+
|
|
50
|
+
if (!isOpen || !content) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
updateScrollButtonVisibility();
|
|
55
|
+
|
|
56
|
+
content.addEventListener('scroll', updateScrollButtonVisibility);
|
|
57
|
+
const resizeObserver = new ResizeObserver(updateScrollButtonVisibility);
|
|
58
|
+
resizeObserver.observe(content);
|
|
59
|
+
|
|
60
|
+
return () => {
|
|
61
|
+
content.removeEventListener('scroll', updateScrollButtonVisibility);
|
|
62
|
+
resizeObserver.unobserve(content);
|
|
63
|
+
resizeObserver.disconnect();
|
|
64
|
+
};
|
|
65
|
+
}, [isOpen]);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Box display="flex" flexDir="column" minH={0} position="relative">
|
|
69
|
+
<ModalBody ref={contentRef} {...props} />
|
|
70
|
+
{isScrollButtonVisible && (
|
|
71
|
+
<ScrollButton
|
|
72
|
+
alignSelf="center"
|
|
73
|
+
bottom={8}
|
|
74
|
+
flexShrink={0}
|
|
75
|
+
onClick={() => {
|
|
76
|
+
if (!contentRef.current) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
contentRef.current.scrollTo({
|
|
81
|
+
top: contentRef.current.scrollHeight,
|
|
82
|
+
behavior: 'smooth',
|
|
83
|
+
});
|
|
84
|
+
}}
|
|
85
|
+
position="absolute"
|
|
86
|
+
/>
|
|
87
|
+
)}
|
|
88
|
+
</Box>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
5
92
|
const DialogBody = (props: DialogBodyProps) => {
|
|
6
|
-
|
|
93
|
+
const { scrollBehavior } = useDialogContext();
|
|
94
|
+
|
|
95
|
+
if (scrollBehavior === 'outside') {
|
|
96
|
+
return <ModalBody {...props} />;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return <ScrollableDialogBody {...props} />;
|
|
7
100
|
};
|
|
8
101
|
|
|
9
102
|
export default DialogBody;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { HTMLChakraProps, ModalProps } from '@chakra-ui/react';
|
|
2
|
+
|
|
3
|
+
export interface DialogProps
|
|
4
|
+
extends Omit<HTMLChakraProps<'section'>, 'scrollBehavior'>,
|
|
5
|
+
Pick<ModalProps, 'returnFocusOnClose'> {
|
|
6
|
+
isClosable?: boolean;
|
|
7
|
+
isOpen: boolean;
|
|
8
|
+
onClose(): void;
|
|
9
|
+
onCloseComplete?: () => void;
|
|
10
|
+
size?: 'small' | 'medium' | 'large' | 'full';
|
|
11
|
+
scrollBehavior?: 'inside' | 'outside';
|
|
12
|
+
title: string;
|
|
13
|
+
trapFocus?: boolean;
|
|
14
|
+
variant?: 'default' | 'empty';
|
|
15
|
+
closeNotice?: string;
|
|
16
|
+
}
|
|
@@ -1,35 +1,79 @@
|
|
|
1
|
-
import { createContext, PropsWithChildren, useCallback, useContext, useMemo
|
|
1
|
+
import { createContext, PropsWithChildren, useCallback, useContext, useMemo } from 'react';
|
|
2
|
+
import { useControllableState } from '@chakra-ui/react';
|
|
3
|
+
import { NodeCallback, TreeViewAction, TreeViewState } from './TreeView.types';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
selectedId?: string;
|
|
5
|
-
selectNode: (id: string) => void;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
const TreeViewContext = createContext<TreeViewState>({
|
|
5
|
+
const TreeViewContext = createContext<TreeViewState & TreeViewAction>({
|
|
9
6
|
selectedId: undefined,
|
|
10
|
-
|
|
7
|
+
expandedIds: new Set<string>(),
|
|
8
|
+
disabledIds: new Set<string>(),
|
|
9
|
+
onSelectionChange: () => undefined,
|
|
10
|
+
onExpandedChange: () => undefined,
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
type TreeViewContenxtProviderProps = PropsWithChildren<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
export type TreeViewContenxtProviderProps = PropsWithChildren<
|
|
14
|
+
Partial<TreeViewState> &
|
|
15
|
+
Partial<TreeViewAction> & {
|
|
16
|
+
defaultSelectedId?: string;
|
|
17
|
+
defaultExpandedIds?: Set<string>;
|
|
18
|
+
defaultDisabledIds?: Set<string>;
|
|
19
|
+
}
|
|
20
|
+
>;
|
|
17
21
|
export const TreeViewContextProvider = ({
|
|
22
|
+
selectedId: controlledSelectedId,
|
|
18
23
|
defaultSelectedId,
|
|
19
|
-
|
|
24
|
+
expandedIds: controlledExpandedIds,
|
|
25
|
+
defaultExpandedIds = new Set<string>(),
|
|
26
|
+
disabledIds: controlledDisabledIds,
|
|
27
|
+
defaultDisabledIds = new Set<string>(),
|
|
28
|
+
onSelectionChange,
|
|
29
|
+
onExpandedChange,
|
|
20
30
|
children,
|
|
21
31
|
}: TreeViewContenxtProviderProps) => {
|
|
22
|
-
const [selectedId, setSelectedId] =
|
|
32
|
+
const [selectedId, setSelectedId] = useControllableState({
|
|
33
|
+
value: controlledSelectedId,
|
|
34
|
+
defaultValue: defaultSelectedId,
|
|
35
|
+
});
|
|
36
|
+
const [expandedIds, setExpandedIds] = useControllableState({
|
|
37
|
+
value: controlledExpandedIds,
|
|
38
|
+
defaultValue: defaultExpandedIds,
|
|
39
|
+
});
|
|
40
|
+
const [disabledIds] = useControllableState({
|
|
41
|
+
value: controlledDisabledIds,
|
|
42
|
+
defaultValue: defaultDisabledIds,
|
|
43
|
+
});
|
|
23
44
|
|
|
24
|
-
const
|
|
25
|
-
(
|
|
26
|
-
setSelectedId(
|
|
27
|
-
|
|
45
|
+
const handleSelect = useCallback<NodeCallback>(
|
|
46
|
+
({ node }) => {
|
|
47
|
+
setSelectedId(node.id);
|
|
48
|
+
onSelectionChange?.({ node: { ...node, selected: true } });
|
|
28
49
|
},
|
|
29
|
-
[
|
|
50
|
+
[onSelectionChange],
|
|
30
51
|
);
|
|
31
52
|
|
|
32
|
-
const
|
|
53
|
+
const handleExpand = useCallback<NodeCallback>(
|
|
54
|
+
({ node }) => {
|
|
55
|
+
if (node.type === 'leaf') {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setExpandedIds((prev) => {
|
|
60
|
+
const newIds = new Set(prev);
|
|
61
|
+
if (newIds.has(node.id)) {
|
|
62
|
+
newIds.delete(node.id);
|
|
63
|
+
} else {
|
|
64
|
+
newIds.add(node.id);
|
|
65
|
+
}
|
|
66
|
+
onExpandedChange?.({ node: { ...node, expanded: newIds.has(node.id) } });
|
|
67
|
+
return newIds;
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
[onExpandedChange],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const contextValue = useMemo(
|
|
74
|
+
() => ({ selectedId, expandedIds, disabledIds, onSelectionChange: handleSelect, onExpandedChange: handleExpand }),
|
|
75
|
+
[selectedId, expandedIds, disabledIds, handleSelect, handleExpand],
|
|
76
|
+
);
|
|
33
77
|
|
|
34
78
|
return <TreeViewContext.Provider value={contextValue}>{children}</TreeViewContext.Provider>;
|
|
35
79
|
};
|
|
@@ -1,68 +1,74 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
|
-
import { TreeViewContextProvider } from './TreeView.context';
|
|
2
|
+
import { TreeViewContenxtProviderProps, TreeViewContextProvider } from './TreeView.context';
|
|
3
3
|
import TreeViewGroup from './TreeViewGroup';
|
|
4
|
-
import TreeViewNode
|
|
5
|
-
import {
|
|
4
|
+
import TreeViewNode from './TreeViewNode';
|
|
5
|
+
import { NodeData } from './TreeView.types';
|
|
6
6
|
|
|
7
|
-
const TreeViewLeaf = (props: Omit<
|
|
8
|
-
|
|
7
|
+
const TreeViewLeaf = (props: Omit<NodeData, 'type' | 'children'>) => (
|
|
8
|
+
<TreeViewNode type="leaf" level={-1} indexPath={[]} titlePath={[]} {...props} />
|
|
9
|
+
);
|
|
10
|
+
const TreeViewBranch = (props: PropsWithChildren<Omit<NodeData, 'type' | 'children'>>) => (
|
|
11
|
+
<TreeViewNode type="branch" level={-1} indexPath={[]} titlePath={[]} {...props} />
|
|
12
|
+
);
|
|
9
13
|
|
|
10
|
-
const renderTreeNodes = (nodes:
|
|
14
|
+
const renderTreeNodes = (nodes: NodeData[]) => {
|
|
11
15
|
return nodes.map((node) => {
|
|
12
|
-
const { id, title, description, iconName, iconColor,
|
|
13
|
-
node;
|
|
16
|
+
const { id, type, title, description, iconName, iconColor, label, labelIconName, labelIconColor, children } = node;
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
const isBranch = nodeType === 'branch';
|
|
17
|
-
|
|
18
|
-
if (isBranch) {
|
|
18
|
+
if (type === 'branch') {
|
|
19
19
|
return (
|
|
20
|
-
<
|
|
20
|
+
<TreeViewNode
|
|
21
21
|
key={id}
|
|
22
22
|
id={id}
|
|
23
|
-
|
|
23
|
+
type="branch"
|
|
24
24
|
title={title}
|
|
25
25
|
description={description}
|
|
26
26
|
iconName={iconName}
|
|
27
27
|
iconColor={iconColor}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
label={label}
|
|
29
|
+
labelIconName={labelIconName}
|
|
30
|
+
labelIconColor={labelIconColor}
|
|
31
|
+
// Thes props will be overridden by the TreeViewGroup
|
|
32
|
+
level={-1}
|
|
33
|
+
indexPath={[]}
|
|
34
|
+
titlePath={[]}
|
|
31
35
|
>
|
|
32
|
-
{children && renderTreeNodes(children
|
|
33
|
-
</
|
|
36
|
+
{children && renderTreeNodes(children)}
|
|
37
|
+
</TreeViewNode>
|
|
34
38
|
);
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
return (
|
|
38
|
-
<
|
|
42
|
+
<TreeViewNode
|
|
39
43
|
key={id}
|
|
40
44
|
id={id}
|
|
41
|
-
|
|
45
|
+
type="leaf"
|
|
42
46
|
title={title}
|
|
43
47
|
description={description}
|
|
44
48
|
iconName={iconName}
|
|
45
49
|
iconColor={iconColor}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
label={label}
|
|
51
|
+
labelIconName={labelIconName}
|
|
52
|
+
labelIconColor={labelIconColor}
|
|
53
|
+
// Thes props will be overridden by the TreeViewGroup
|
|
54
|
+
level={-1}
|
|
55
|
+
indexPath={[]}
|
|
56
|
+
titlePath={[]}
|
|
49
57
|
/>
|
|
50
58
|
);
|
|
51
59
|
});
|
|
52
60
|
};
|
|
53
61
|
|
|
54
|
-
export type TreeViewProps = {
|
|
62
|
+
export type TreeViewProps = TreeViewContenxtProviderProps & {
|
|
55
63
|
label: string;
|
|
56
|
-
data?:
|
|
57
|
-
defaultSelectedId?: string;
|
|
58
|
-
onNodeSelect?: (id: string) => void;
|
|
64
|
+
data?: NodeData[];
|
|
59
65
|
};
|
|
60
66
|
|
|
61
|
-
const TreeView = ({
|
|
67
|
+
const TreeView = ({ data, label, children, ...rest }: PropsWithChildren<TreeViewProps>) => {
|
|
62
68
|
return (
|
|
63
|
-
<TreeViewContextProvider
|
|
64
|
-
<TreeViewGroup level={1}
|
|
65
|
-
{data ? renderTreeNodes(data
|
|
69
|
+
<TreeViewContextProvider {...rest}>
|
|
70
|
+
<TreeViewGroup role="tree" label={label} level={1} indexPath={[]} titlePath={[]}>
|
|
71
|
+
{data ? renderTreeNodes(data) : children}
|
|
66
72
|
</TreeViewGroup>
|
|
67
73
|
</TreeViewContextProvider>
|
|
68
74
|
);
|
|
@@ -1,18 +1,44 @@
|
|
|
1
1
|
import { TypeIconName } from '../Icon/Icon';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type NodeType = 'branch' | 'leaf';
|
|
4
|
+
|
|
5
|
+
export type NodeData = {
|
|
4
6
|
id: string;
|
|
5
|
-
type
|
|
7
|
+
type: NodeType;
|
|
6
8
|
title: string;
|
|
7
9
|
description?: string;
|
|
8
10
|
iconName?: TypeIconName;
|
|
9
11
|
iconColor?: string;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
label?: string;
|
|
13
|
+
labelIconName?: TypeIconName;
|
|
14
|
+
labelIconColor?: string;
|
|
15
|
+
children?: NodeData[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type NodePath = {
|
|
19
|
+
level: number;
|
|
20
|
+
indexPath: number[];
|
|
21
|
+
titlePath: string[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type NodeState = {
|
|
25
|
+
id: string;
|
|
26
|
+
type: NodeType;
|
|
27
|
+
title: string;
|
|
28
|
+
disabled: boolean;
|
|
29
|
+
selected: boolean;
|
|
14
30
|
expanded?: boolean;
|
|
15
|
-
children?: TreeNodeData[];
|
|
16
31
|
};
|
|
17
32
|
|
|
18
|
-
export type
|
|
33
|
+
export type TreeViewState = {
|
|
34
|
+
selectedId?: string;
|
|
35
|
+
expandedIds: Set<string>;
|
|
36
|
+
disabledIds: Set<string>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type NodeCallback = ({ node }: { node: NodeState & NodePath }) => void;
|
|
40
|
+
|
|
41
|
+
export type TreeViewAction = {
|
|
42
|
+
onSelectionChange: NodeCallback;
|
|
43
|
+
onExpandedChange: NodeCallback;
|
|
44
|
+
};
|
|
@@ -3,15 +3,23 @@ import Box from '../Box/Box';
|
|
|
3
3
|
|
|
4
4
|
type TreeViewGroupProps = PropsWithChildren<{
|
|
5
5
|
level: number;
|
|
6
|
+
indexPath: number[];
|
|
7
|
+
titlePath: string[];
|
|
6
8
|
label?: string;
|
|
7
9
|
role?: string;
|
|
8
10
|
}>;
|
|
9
11
|
|
|
10
|
-
const TreeViewGroup = ({ label, role, level, children }: TreeViewGroupProps) => {
|
|
12
|
+
const TreeViewGroup = ({ label, role, level, indexPath, titlePath, children }: TreeViewGroupProps) => {
|
|
11
13
|
return (
|
|
12
14
|
<Box as="ul" role={role} aria-label={label} listStyleType="none" textStyle="body/md/regular">
|
|
13
|
-
{Children.map(children, (child) =>
|
|
14
|
-
isValidElement(child)
|
|
15
|
+
{Children.map(children, (child, childIndex) =>
|
|
16
|
+
isValidElement(child)
|
|
17
|
+
? cloneElement(child, {
|
|
18
|
+
level,
|
|
19
|
+
indexPath: [...indexPath, childIndex],
|
|
20
|
+
titlePath: [...titlePath, child.props.title],
|
|
21
|
+
} as { level: number; indexPath: number[]; titlePath: string[] })
|
|
22
|
+
: child,
|
|
15
23
|
)}
|
|
16
24
|
</Box>
|
|
17
25
|
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { memo, PropsWithChildren, useCallback, useMemo
|
|
1
|
+
import { memo, PropsWithChildren, useCallback, useMemo } from 'react';
|
|
2
2
|
import { useMultiStyleConfig } from '@chakra-ui/react';
|
|
3
3
|
import Icon from '../Icon/Icon';
|
|
4
4
|
import Box from '../Box/Box';
|
|
@@ -6,42 +6,38 @@ import Text from '../Text/Text';
|
|
|
6
6
|
import Collapse from '../Collapse/Collapse';
|
|
7
7
|
import TreeViewGroup from './TreeViewGroup';
|
|
8
8
|
import { useTreeViewContext } from './TreeView.context';
|
|
9
|
-
import {
|
|
9
|
+
import { NodeData, NodePath } from './TreeView.types';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
defaultExpanded?: boolean;
|
|
16
|
-
}
|
|
17
|
-
>;
|
|
18
|
-
|
|
19
|
-
type TreeViewNodeContentProps = Omit<TreeViewNodeProps, 'defaultDisabled' | 'defaultExpanded' | 'level'> & {
|
|
20
|
-
level: number;
|
|
21
|
-
isBranch?: boolean;
|
|
22
|
-
isSelected?: boolean;
|
|
23
|
-
isDisabled?: boolean;
|
|
11
|
+
type TreeViewNodeContentProps = TreeViewNodeProps & {
|
|
12
|
+
isBranch: boolean;
|
|
13
|
+
isSelected: boolean;
|
|
14
|
+
isDisabled: boolean;
|
|
24
15
|
isExpanded?: boolean;
|
|
25
16
|
onClick: VoidFunction;
|
|
26
17
|
};
|
|
27
18
|
|
|
28
19
|
const TreeViewNodeContent = memo(
|
|
29
20
|
({
|
|
21
|
+
// NodeData
|
|
30
22
|
id,
|
|
31
|
-
children,
|
|
32
|
-
level,
|
|
33
|
-
iconName,
|
|
34
|
-
iconColor,
|
|
35
23
|
title,
|
|
36
24
|
description,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
iconName,
|
|
26
|
+
iconColor,
|
|
27
|
+
label,
|
|
28
|
+
labelIconName,
|
|
29
|
+
labelIconColor,
|
|
30
|
+
// NodePath
|
|
31
|
+
level,
|
|
32
|
+
indexPath,
|
|
33
|
+
titlePath,
|
|
34
|
+
// Content props
|
|
40
35
|
isBranch,
|
|
36
|
+
isSelected,
|
|
41
37
|
isDisabled,
|
|
42
38
|
isExpanded,
|
|
43
|
-
isSelected,
|
|
44
39
|
onClick,
|
|
40
|
+
children,
|
|
45
41
|
}: TreeViewNodeContentProps) => {
|
|
46
42
|
const styles = useMultiStyleConfig('TreeView', {});
|
|
47
43
|
|
|
@@ -79,12 +75,12 @@ const TreeViewNodeContent = memo(
|
|
|
79
75
|
</Text>
|
|
80
76
|
)}
|
|
81
77
|
</Box>
|
|
82
|
-
{(
|
|
78
|
+
{(label || labelIconName) && (
|
|
83
79
|
<Box sx={styles.suffixBlock} color={isSelected || isExpanded ? 'text/primary' : 'text/secondary'}>
|
|
84
|
-
{
|
|
85
|
-
{
|
|
80
|
+
{labelIconName && <Icon name={labelIconName} size="16" color={labelIconColor} />}
|
|
81
|
+
{label && (
|
|
86
82
|
<Text textStyle="body/md/regular" textAlign="right">
|
|
87
|
-
{
|
|
83
|
+
{label}
|
|
88
84
|
</Text>
|
|
89
85
|
)}
|
|
90
86
|
</Box>
|
|
@@ -94,7 +90,7 @@ const TreeViewNodeContent = memo(
|
|
|
94
90
|
</Box>
|
|
95
91
|
{isBranch && children && (
|
|
96
92
|
<Collapse in={isExpanded} unmountOnExit>
|
|
97
|
-
<TreeViewGroup level={level + 1}
|
|
93
|
+
<TreeViewGroup role="group" level={level + 1} indexPath={indexPath} titlePath={titlePath}>
|
|
98
94
|
{children}
|
|
99
95
|
</TreeViewGroup>
|
|
100
96
|
</Collapse>
|
|
@@ -104,26 +100,31 @@ const TreeViewNodeContent = memo(
|
|
|
104
100
|
},
|
|
105
101
|
);
|
|
106
102
|
|
|
103
|
+
export type TreeViewNodeProps = PropsWithChildren<Omit<NodeData, 'children'> & NodePath>;
|
|
104
|
+
|
|
107
105
|
const TreeViewNode = ({
|
|
106
|
+
// NodeData
|
|
108
107
|
id,
|
|
109
108
|
type,
|
|
110
|
-
level = 1,
|
|
111
|
-
iconName,
|
|
112
|
-
iconColor,
|
|
113
109
|
title,
|
|
114
110
|
description,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
111
|
+
iconName,
|
|
112
|
+
iconColor,
|
|
113
|
+
label,
|
|
114
|
+
labelIconName,
|
|
115
|
+
labelIconColor,
|
|
116
|
+
// NodePath
|
|
117
|
+
level,
|
|
118
|
+
indexPath,
|
|
119
|
+
titlePath,
|
|
120
|
+
// Children
|
|
120
121
|
children,
|
|
121
122
|
}: TreeViewNodeProps) => {
|
|
122
|
-
const { selectedId,
|
|
123
|
+
const { selectedId, disabledIds, expandedIds, onSelectionChange, onExpandedChange } = useTreeViewContext();
|
|
123
124
|
|
|
124
|
-
const isBranch =
|
|
125
|
-
const
|
|
126
|
-
const
|
|
125
|
+
const isBranch = type === 'branch';
|
|
126
|
+
const isDisabled = useMemo(() => disabledIds.has(id), [id, disabledIds]);
|
|
127
|
+
const isExpanded = useMemo(() => (isBranch ? expandedIds.has(id) : undefined), [id, isBranch, expandedIds]);
|
|
127
128
|
const isSelected = useMemo(() => id === selectedId, [id, selectedId]);
|
|
128
129
|
|
|
129
130
|
const handleClick = useCallback(() => {
|
|
@@ -132,25 +133,65 @@ const TreeViewNode = ({
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
if (!isSelected) {
|
|
135
|
-
|
|
136
|
+
onSelectionChange({
|
|
137
|
+
node: {
|
|
138
|
+
id,
|
|
139
|
+
type,
|
|
140
|
+
title,
|
|
141
|
+
disabled: isDisabled,
|
|
142
|
+
selected: true,
|
|
143
|
+
expanded: isExpanded,
|
|
144
|
+
level,
|
|
145
|
+
indexPath,
|
|
146
|
+
titlePath,
|
|
147
|
+
},
|
|
148
|
+
});
|
|
136
149
|
}
|
|
137
150
|
|
|
138
151
|
if (isBranch) {
|
|
139
|
-
|
|
152
|
+
onExpandedChange({
|
|
153
|
+
node: {
|
|
154
|
+
id,
|
|
155
|
+
type,
|
|
156
|
+
title,
|
|
157
|
+
disabled: isDisabled,
|
|
158
|
+
selected: true,
|
|
159
|
+
expanded: !isExpanded,
|
|
160
|
+
level,
|
|
161
|
+
indexPath,
|
|
162
|
+
titlePath,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
140
165
|
}
|
|
141
|
-
}, [
|
|
166
|
+
}, [
|
|
167
|
+
id,
|
|
168
|
+
type,
|
|
169
|
+
title,
|
|
170
|
+
isDisabled,
|
|
171
|
+
isSelected,
|
|
172
|
+
isBranch,
|
|
173
|
+
isExpanded,
|
|
174
|
+
level,
|
|
175
|
+
indexPath,
|
|
176
|
+
titlePath,
|
|
177
|
+
onSelectionChange,
|
|
178
|
+
onExpandedChange,
|
|
179
|
+
]);
|
|
142
180
|
|
|
143
181
|
return (
|
|
144
182
|
<TreeViewNodeContent
|
|
145
183
|
id={id}
|
|
146
|
-
|
|
147
|
-
iconName={iconName}
|
|
148
|
-
iconColor={iconColor}
|
|
184
|
+
type={type}
|
|
149
185
|
title={title}
|
|
150
186
|
description={description}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
187
|
+
iconName={iconName}
|
|
188
|
+
iconColor={iconColor}
|
|
189
|
+
label={label}
|
|
190
|
+
labelIconName={labelIconName}
|
|
191
|
+
labelIconColor={labelIconColor}
|
|
192
|
+
level={level}
|
|
193
|
+
indexPath={indexPath}
|
|
194
|
+
titlePath={titlePath}
|
|
154
195
|
isBranch={isBranch}
|
|
155
196
|
isDisabled={isDisabled}
|
|
156
197
|
isExpanded={isExpanded}
|
package/src/index.ts
CHANGED
|
@@ -95,7 +95,7 @@ export { default as useToast } from './Components/Toast/Toast';
|
|
|
95
95
|
export type { EmptyStateProps } from './Components/EmptyState/EmptyState';
|
|
96
96
|
export { default as EmptyState } from './Components/EmptyState/EmptyState';
|
|
97
97
|
|
|
98
|
-
export type { DialogProps } from './Components/Dialog/
|
|
98
|
+
export type { DialogProps } from './Components/Dialog/DialogProps';
|
|
99
99
|
export { default as Dialog } from './Components/Dialog/Dialog';
|
|
100
100
|
|
|
101
101
|
export type { DialogBodyProps } from './Components/Dialog/DialogBody';
|
|
@@ -372,6 +372,7 @@ export { default as ToggleButton } from './Components/ToggleButton/ToggleButton'
|
|
|
372
372
|
|
|
373
373
|
export type { TreeViewProps } from './Components/TreeView/TreeView';
|
|
374
374
|
export { default as TreeView } from './Components/TreeView/TreeView';
|
|
375
|
+
export * from './Components/TreeView/TreeView.types';
|
|
375
376
|
|
|
376
377
|
export {
|
|
377
378
|
default as SettingsCard,
|