fix: issues

This commit is contained in:
hypercross 2026-03-01 11:28:23 +08:00
parent 05c93e0301
commit c977f366ea
2 changed files with 30 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import {type Component, For, Show} from "solid-js"; import {type Component, For, Show, createEffect, on} from "solid-js";
import type { CompletionItem } from "./types"; import type { CompletionItem } from "./types";
export interface CommanderInputProps { export interface CommanderInputProps {
@ -17,6 +17,25 @@ export interface CommanderInputProps {
} }
export const CommanderInput: Component<CommanderInputProps> = (props) => { export const CommanderInput: Component<CommanderInputProps> = (props) => {
let containerRef: HTMLDivElement | undefined;
// 当选中项变化时自动滚动到可见区域
createEffect(
on(
() => props.selectedCompletion(),
() => {
if (containerRef) {
const selectedItem = containerRef.querySelector('[data-selected="true"]');
if (selectedItem) {
selectedItem.scrollIntoView({ block: "nearest" });
}
}
},
),
);
const maxVisible = 3;
return ( return (
<div class="flex items-center border-b border-gray-300 bg-gray-50 px-3 py-2"> <div class="flex items-center border-b border-gray-300 bg-gray-50 px-3 py-2">
<span class="text-gray-500 mr-2"></span> <span class="text-gray-500 mr-2"></span>
@ -39,9 +58,14 @@ export const CommanderInput: Component<CommanderInputProps> = (props) => {
{/* 自动补全下拉框 - 向上弹出 */} {/* 自动补全下拉框 - 向上弹出 */}
<Show when={props.showCompletions() && props.completions().length > 0}> <Show when={props.showCompletions() && props.completions().length > 0}>
<div class="absolute left-0 bottom-full w-full bg-white border border-gray-300 shadow-lg mb-1"> <div
<For each={props.completions().slice(0, 3)}>{(comp, idx) => ( ref={containerRef}
class="absolute left-0 bottom-full w-full bg-white border border-gray-300 shadow-lg mb-1 overflow-auto"
style={{ "max-height": `${maxVisible * 40}px` }}
>
<For each={props.completions()}>{(comp, idx) => (
<div <div
data-selected={idx() === props.selectedCompletion()}
class={`px-3 py-2 cursor-pointer flex justify-between items-center ${ class={`px-3 py-2 cursor-pointer flex justify-between items-center ${
idx() === props.selectedCompletion() idx() === props.selectedCompletion()
? "bg-blue-100" ? "bg-blue-100"
@ -71,11 +95,6 @@ export const CommanderInput: Component<CommanderInputProps> = (props) => {
</Show> </Show>
</div> </div>
)}</For> )}</For>
<Show when={props.completions().length > 3}>
<div class="px-3 py-1 text-xs text-gray-500 bg-gray-50 border-t border-gray-200">
{props.completions().length - 3} ...
</div>
</Show>
</div> </div>
</Show> </Show>
</div> </div>

View File

@ -383,9 +383,8 @@ export function useCommander(
const setSelectedCompletion = (v: number | ((prev: number) => number)) => { const setSelectedCompletion = (v: number | ((prev: number) => number)) => {
const comps = completions(); const comps = completions();
const maxVisible = 3; const maxIdx = comps.length - 1;
const maxIdx = Math.min(comps.length - 1, maxVisible - 1);
if (typeof v === 'function') { if (typeof v === 'function') {
setSelectedCompletionState(prev => { setSelectedCompletionState(prev => {
const next = v(prev); const next = v(prev);
@ -399,8 +398,7 @@ export function useCommander(
const acceptCompletion = () => { const acceptCompletion = () => {
const idx = selectedCompletion(); const idx = selectedCompletion();
const comps = completions(); const comps = completions();
// 确保索引在有效范围内 const validIdx = Math.min(idx, comps.length - 1);
const validIdx = Math.min(idx, Math.min(comps.length - 1, 2));
const comp = comps[validIdx]; const comp = comps[validIdx];
if (!comp) return; if (!comp) return;