修复后端模型筛选框只剩一个结果时无法修改的问题
This commit is contained in:
@@ -19,6 +19,7 @@ const FuzzySearchSelect = ({
|
||||
const loadedOptionsRef = useRef([]);
|
||||
const hasLoadedRef = useRef(false);
|
||||
const lastLoadOptionsRef = useRef(null);
|
||||
const isUserTypingRef = useRef(false); // 标记用户是否正在输入
|
||||
|
||||
const fuzzySearch = (query, items) => {
|
||||
if (!query) return items;
|
||||
@@ -58,13 +59,22 @@ const FuzzySearchSelect = ({
|
||||
}
|
||||
}, [isOpen]); // 仅依赖 isOpen
|
||||
|
||||
// 当 value prop 改变时,同步到 searchTerm(用于初始化或外部更新,但不打开下拉框)
|
||||
// 当 value prop 改变时,同步到 searchTerm(用于初始化或外部更新)
|
||||
// 但只有在用户没有正在输入的情况下才同步,避免覆盖用户输入
|
||||
useEffect(() => {
|
||||
if (value !== undefined && value !== null && value !== searchTerm) {
|
||||
console.log('[FuzzySearchSelect] value useEffect 触发:', {
|
||||
value,
|
||||
searchTerm,
|
||||
isUserTyping: isUserTypingRef.current,
|
||||
condition: value && value !== searchTerm && !isUserTypingRef.current,
|
||||
willUpdate: value && value !== searchTerm && !isUserTypingRef.current
|
||||
});
|
||||
// 只有在用户没有正在输入时才接受外部的 value 更新
|
||||
if (value && value !== searchTerm && !isUserTypingRef.current) {
|
||||
console.log('[FuzzySearchSelect] 强制同步 searchTerm 为 value:', value);
|
||||
setSearchTerm(value);
|
||||
// 不打开下拉框,只更新搜索词显示
|
||||
}
|
||||
}, [value, searchTerm]);
|
||||
}, [value]);
|
||||
|
||||
// 当 loadOptions 函数引用改变时(提供商改变),重置加载状态
|
||||
useEffect(() => {
|
||||
@@ -83,10 +93,22 @@ const FuzzySearchSelect = ({
|
||||
if (loadOptions && hasLoadedRef.current) {
|
||||
// 异步加载模式:从已加载的选项中搜索
|
||||
const filtered = fuzzySearch(searchTerm, loadedOptionsRef.current);
|
||||
console.log('[FuzzySearchSelect] 搜索过滤结果 (异步模式):', {
|
||||
searchTerm,
|
||||
totalOptions: loadedOptionsRef.current.length,
|
||||
filteredCount: filtered.length,
|
||||
filtered
|
||||
});
|
||||
setFilteredOptions(filtered);
|
||||
} else if (!loadOptions && options.length > 0) {
|
||||
// 同步模式:从传入的 options 中搜索
|
||||
const filtered = fuzzySearch(searchTerm, options);
|
||||
console.log('[FuzzySearchSelect] 搜索过滤结果 (同步模式):', {
|
||||
searchTerm,
|
||||
totalOptions: options.length,
|
||||
filteredCount: filtered.length,
|
||||
filtered
|
||||
});
|
||||
setFilteredOptions(filtered);
|
||||
}
|
||||
}, [searchTerm]); // 仅依赖 searchTerm,不依赖 loadOptions 或 options
|
||||
@@ -104,6 +126,12 @@ const FuzzySearchSelect = ({
|
||||
}, []);
|
||||
|
||||
const handleSelect = (option) => {
|
||||
console.log('[FuzzySearchSelect] 用户选择选项:', {
|
||||
option,
|
||||
currentSearchTerm: searchTerm
|
||||
});
|
||||
// 用户选择后,清除输入标记,允许外部更新
|
||||
isUserTypingRef.current = false;
|
||||
onChange(option);
|
||||
setSearchTerm(option);
|
||||
setIsOpen(false);
|
||||
@@ -111,8 +139,22 @@ const FuzzySearchSelect = ({
|
||||
|
||||
const handleInputChange = (e) => {
|
||||
const value = e.target.value;
|
||||
console.log('[FuzzySearchSelect] 用户输入改变:', {
|
||||
newValue: value,
|
||||
currentSearchTerm: searchTerm,
|
||||
filteredOptionsCount: filteredOptions.length
|
||||
});
|
||||
|
||||
// 标记用户正在输入
|
||||
isUserTypingRef.current = true;
|
||||
setSearchTerm(value);
|
||||
setIsOpen(true);
|
||||
|
||||
// 使用防抖:300ms 后清除输入标记
|
||||
// 这样可以防止在用户输入过程中被外部更新覆盖
|
||||
setTimeout(() => {
|
||||
isUserTypingRef.current = false;
|
||||
}, 300);
|
||||
};
|
||||
|
||||
const handleInputFocus = () => {
|
||||
@@ -162,3 +204,4 @@ const FuzzySearchSelect = ({
|
||||
};
|
||||
|
||||
export default FuzzySearchSelect;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user