MCPcopy Create free account
hub / github.com/DTStack/molecule / Tab

Function Tab

src/components/tabs/tab.tsx:73–165  ·  view source on GitHub ↗
({ tab, active, ...restEvents }: ITabComponent)

Source from the content-addressed store, hash-verified

71export type ITabComponent = { tab: ITabProps; active?: boolean } & ITabEvent;
72
73export function Tab({ tab, active, ...restEvents }: ITabComponent) {
74 const { name, closable, id, icon, status } = tab;
75 const { onCloseTab, onSelectTab, onContextMenu, onDrag } = restEvents;
76
77 const ref = useRef<HTMLDivElement>(null);
78
79 const handleOnContextMenu = (event: React.MouseEvent) => {
80 event.preventDefault();
81 onContextMenu?.(event, tab);
82 };
83
84 const [, drag] = useDrag({
85 collect: (monitor) => ({
86 isDragging: monitor.isDragging(),
87 }),
88 type: 'DND_NODE',
89 item: tab,
90 } as any);
91
92 const [, drop] = useDrop({
93 accept: 'DND_NODE',
94 hover(item: ITabProps, monitor) {
95 if (!ref.current) return;
96 const component = ref.current;
97 const hoverBoundingRect = component?.getBoundingClientRect();
98 const hoverMiddleX =
99 (hoverBoundingRect.right - hoverBoundingRect.left) / 2;
100 const clientOffset = monitor.getClientOffset();
101 const hoverClientX =
102 (clientOffset as { x: number; y: number }).x -
103 hoverBoundingRect.left;
104
105 const dragInfo = {
106 hoverMiddleX,
107 hoverClientX,
108 };
109 onDrag?.(item, tab, dragInfo);
110 },
111 });
112
113 drag(drop(ref));
114
115 const renderIcon = (icon: string | JSX.Element) => {
116 return typeof icon === 'string' ? <Icon type={icon} /> : icon;
117 };
118
119 const renderStatus = (
120 status?: ITabStatus | ((tab: ITabProps) => JSX.Element),
121 isHover?: boolean
122 ) => {
123 if (status && !isHover) {
124 if (typeof status === 'function') {
125 return status(tab);
126 }
127 switch (status) {
128 case 'edited':
129 return <Icon type="primitive-dot" />;
130

Callers

nothing calls this directly

Calls 5

classNamesFunction · 0.90
dragFunction · 0.85
onSelectTabFunction · 0.85
renderStatusFunction · 0.85
renderIconFunction · 0.70

Tested by

no test coverage detected