mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-17 19:07:39 +08:00
🐛 fix(data-grid): 修复快捷 WHERE 自动补全回车行为
- 调整快捷 WHERE 输入框 Enter 处理,避免抢占 AutoComplete 选中事件 - 方向键高亮建议项后回车优先选择字段 - 增加快捷 WHERE 回车行为回归测试
This commit is contained in:
@@ -77,6 +77,7 @@ import {
|
||||
normalizeQuickWhereCondition,
|
||||
resolveWhereConditionSelectedValue,
|
||||
resolveWhereConditionSuggestions,
|
||||
shouldApplyQuickWhereOnEnter,
|
||||
validateQuickWhereCondition,
|
||||
} from '../utils/dataGridWhereFilter';
|
||||
import {
|
||||
@@ -2418,6 +2419,7 @@ const DataGrid: React.FC<DataGridProps> = ({
|
||||
const [filterConditions, setFilterConditions] = useState<GridFilterCondition[]>([]);
|
||||
const [nextFilterId, setNextFilterId] = useState(1);
|
||||
const [quickWhereDraft, setQuickWhereDraft] = useState(() => normalizeQuickWhereCondition(quickWhereCondition));
|
||||
const [quickWhereSuggestionsOpen, setQuickWhereSuggestionsOpen] = useState(false);
|
||||
const filterPanelRef = useRef<HTMLDivElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -5638,6 +5640,21 @@ const DataGrid: React.FC<DataGridProps> = ({
|
||||
value={quickWhereDraft}
|
||||
options={quickWhereSuggestionOptions}
|
||||
onChange={setQuickWhereDraft}
|
||||
onOpenChange={setQuickWhereSuggestionsOpen}
|
||||
onInputKeyDown={(event) => {
|
||||
if (!shouldApplyQuickWhereOnEnter({
|
||||
key: event.key,
|
||||
shiftKey: event.shiftKey,
|
||||
isComposing: Boolean((event.nativeEvent as any)?.isComposing),
|
||||
suggestionsOpen: quickWhereSuggestionsOpen,
|
||||
suggestionCount: quickWhereSuggestionOptions.length,
|
||||
activeSuggestionId: event.currentTarget.getAttribute('aria-activedescendant'),
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
applyQuickWhereCondition();
|
||||
}}
|
||||
onSelect={(value, option) => {
|
||||
setQuickWhereDraft(resolveWhereConditionSelectedValue({
|
||||
selectedValue: value,
|
||||
@@ -5652,12 +5669,6 @@ const DataGrid: React.FC<DataGridProps> = ({
|
||||
{...noAutoCapInputProps}
|
||||
allowClear
|
||||
placeholder={dbType === 'mongodb' ? '输入 MongoDB JSON 查询对象,例如 {"status":"A"}' : '输入 WHERE 后面的条件,例如 status = 1 AND name LIKE \'A%\''}
|
||||
onPressEnter={(event) => {
|
||||
if (!event.shiftKey) {
|
||||
event.preventDefault();
|
||||
applyQuickWhereCondition();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</AutoComplete>
|
||||
<Button size="small" type="primary" onClick={() => applyQuickWhereCondition()}>
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
normalizeQuickWhereCondition,
|
||||
resolveWhereConditionSuggestions,
|
||||
resolveWhereConditionSelectedValue,
|
||||
shouldApplyQuickWhereOnEnter,
|
||||
validateQuickWhereCondition,
|
||||
} from './dataGridWhereFilter';
|
||||
|
||||
@@ -110,4 +111,30 @@ describe('dataGridWhereFilter', () => {
|
||||
}),
|
||||
).toBe('`username` = ');
|
||||
});
|
||||
|
||||
it('lets autocomplete consume enter while quick where suggestions are open', () => {
|
||||
expect(shouldApplyQuickWhereOnEnter({
|
||||
key: 'Enter',
|
||||
suggestionsOpen: true,
|
||||
suggestionCount: 1,
|
||||
activeSuggestionId: 'quick-where-list-0',
|
||||
})).toBe(false);
|
||||
expect(shouldApplyQuickWhereOnEnter({
|
||||
key: 'Enter',
|
||||
suggestionsOpen: true,
|
||||
suggestionCount: 1,
|
||||
})).toBe(true);
|
||||
expect(shouldApplyQuickWhereOnEnter({
|
||||
key: 'Enter',
|
||||
suggestionsOpen: false,
|
||||
suggestionCount: 1,
|
||||
activeSuggestionId: 'quick-where-list-0',
|
||||
})).toBe(true);
|
||||
expect(shouldApplyQuickWhereOnEnter({
|
||||
key: 'Enter',
|
||||
shiftKey: true,
|
||||
suggestionsOpen: false,
|
||||
suggestionCount: 0,
|
||||
})).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -182,6 +182,26 @@ export const resolveWhereConditionSelectedValue = ({
|
||||
return applyWhereConditionSuggestion(String(currentInput ?? ''), insertTextValue);
|
||||
};
|
||||
|
||||
export const shouldApplyQuickWhereOnEnter = ({
|
||||
key,
|
||||
shiftKey = false,
|
||||
isComposing = false,
|
||||
suggestionsOpen = false,
|
||||
suggestionCount = 0,
|
||||
activeSuggestionId = '',
|
||||
}: {
|
||||
key: unknown;
|
||||
shiftKey?: boolean;
|
||||
isComposing?: boolean;
|
||||
suggestionsOpen?: boolean;
|
||||
suggestionCount?: number;
|
||||
activeSuggestionId?: unknown;
|
||||
}): boolean => {
|
||||
if (String(key || '') !== 'Enter') return false;
|
||||
if (shiftKey || isComposing) return false;
|
||||
return !(suggestionsOpen && suggestionCount > 0 && String(activeSuggestionId ?? '').trim());
|
||||
};
|
||||
|
||||
export const resolveWhereConditionSuggestions = ({
|
||||
input,
|
||||
columnNames,
|
||||
|
||||
Reference in New Issue
Block a user