fix: issues
This commit is contained in:
parent
05c93e0301
commit
c977f366ea
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue