-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathDropdownMenu.tsx
More file actions
195 lines (181 loc) · 6.03 KB
/
DropdownMenu.tsx
File metadata and controls
195 lines (181 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, { useMemo } from "react";
import { Box, Text } from "ink";
/**
* Generic dropdown menu item structure
*/
export type DropdownMenuItem = {
/** Unique key for React list rendering */
key: string;
/** Main label text (can include status indicators) */
label: string;
/** Secondary description text (dimmed) */
description?: string;
/** Whether this item is currently selected */
selected?: boolean;
/** Whether to show a special status indicator (e.g., loaded checkmark) */
statusIndicator?: {
symbol: string;
color: string;
};
};
/**
* Props for the DropdownMenu component
*/
type DropdownMenuProps = {
/** List of items to display */
items: DropdownMenuItem[];
/** Index of the currently active/highlighted item */
activeIndex: number;
/** Maximum number of visible items before scrolling */
maxVisible?: number;
/** Container width in columns */
width: number;
/** Optional title displayed at the top */
title?: string;
/** Color for the title (default: "magenta") */
titleColor?: string;
/** Color for the active item indicator (default: "cyanBright") */
activeColor?: string;
/** Help text displayed at the bottom */
helpText?: string;
/** Text to display when items list is empty */
emptyText?: string;
/** Custom item renderer (overrides default rendering) */
renderItem?: (item: DropdownMenuItem, isActive: boolean) => React.ReactNode;
};
/**
* Calculate the visible window start position for scrolling
* Ensures the activeIndex is always visible within the window
*/
export function calculateVisibleStart(activeIndex: number, totalItems: number, maxVisible: number): number {
return Math.min(Math.max(0, activeIndex - Math.floor((maxVisible - 1) / 2)), Math.max(0, totalItems - maxVisible));
}
/**
* Generic dropdown menu component with scrolling support
* Used by Skills Dropdown, Model Dropdown, and other selection menus
*/
const DropdownMenu = React.memo(function DropdownMenu({
items,
activeIndex,
maxVisible = 8,
width,
title,
titleColor = "magenta",
activeColor = "cyanBright",
helpText,
emptyText = "No items found",
renderItem,
}: DropdownMenuProps): React.ReactElement | null {
// Calculate visible window
const visibleStart = calculateVisibleStart(activeIndex, items?.length, maxVisible);
const visibleItems = items?.slice(visibleStart, visibleStart + maxVisible);
// 计算标签列最佳宽度:包含所有可能的前缀和后缀
const labelColumnWidth = useMemo(() => {
if (visibleItems.length === 0) {
return 0;
}
// 计算每个 item 实际需要的最大宽度
const maxContentWidth = Math.max(
...visibleItems.map((item) => {
let width = 2; // prefix "> " or " "
if (item.selected !== undefined) {
width += 2; // "● " or "○ "
}
width += item.label.length;
if (item.statusIndicator) {
width += 2; // " ✓" or similar
}
return width;
})
);
const maxAllowed = Math.max(10, (width - 2) >> 1); // 容器50%宽度(减去gap),至少保留10列
return Math.min(maxContentWidth, maxAllowed);
}, [visibleItems, width]);
// Early return if no items
if (items?.length === 0) {
return (
<Box flexDirection="column" marginBottom={1} width={width}>
{title ? (
<Text color={titleColor} bold>
{title}
</Text>
) : null}
<Text dimColor>{emptyText}</Text>
{helpText ? <Text dimColor>{helpText}</Text> : null}
</Box>
);
}
return (
<Box flexDirection="column" marginBottom={1} borderStyle={"round"} borderDimColor width={width}>
{/* Title */}
{title ? (
<Box
borderStyle={"single"}
borderDimColor
borderBottom={true}
borderRight={false}
borderTop={false}
borderLeft={false}
paddingX={1}
>
<Text color={titleColor} bold>
{title}
</Text>
</Box>
) : null}
{/* Scroll indicator - top */}
{visibleStart > 0 ? (
<Box marginLeft={2}>
<Text dimColor>… {visibleStart} above</Text>
</Box>
) : null}
{/* Visible items */}
<Box flexDirection="column">
{visibleItems.map((item, idx) => {
const actualIndex = visibleStart + idx;
const isActive = actualIndex === activeIndex;
// Use custom renderer if provided
if (renderItem) {
return <React.Fragment key={item.key}>{renderItem(item, isActive)}</React.Fragment>;
}
// Default rendering with selection indicator and optional features
return (
<Box key={item.key} flexGrow={1} flexDirection="row" gap={2} paddingX={1}>
<Box width={labelColumnWidth} flexShrink={0}>
<Text color={isActive ? activeColor : undefined} wrap="truncate-end">
{isActive ? "> " : " "}
{item.selected !== undefined ? (item.selected ? "●" : "○") : null} <Text bold>{item.label}</Text>
{item.statusIndicator ? (
<Text color={item.statusIndicator.color}> {item.statusIndicator.symbol}</Text>
) : null}
</Text>
</Box>
<Box flexGrow={1}>{item.description ? <Text dimColor>{`${item.description}`}</Text> : null}</Box>
</Box>
);
})}
</Box>
{/* Scroll indicator - bottom */}
{visibleStart + visibleItems.length < items.length ? (
<Box marginLeft={2}>
<Text dimColor>… {items.length - visibleStart - visibleItems.length} more</Text>
</Box>
) : null}
{/* Help text */}
{helpText ? (
<Box
borderStyle={"single"}
borderDimColor
borderBottom={false}
borderRight={false}
borderTop={true}
borderLeft={false}
paddingX={1}
>
<Text dimColor>{helpText}</Text>
</Box>
) : null}
</Box>
);
});
export default DropdownMenu;