Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Table): fix default horizontal scroll behavior #4904

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/_common
4 changes: 3 additions & 1 deletion src/table/base-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export default defineComponent({
clearHoverRow,
addRowHoverKeyboardListener,
removeRowHoverKeyboardListener,
tableRefTabIndex,
} = useHoverKeyboardEvent(props, tableRef);

watch(tableElmRef, () => {
Expand Down Expand Up @@ -394,6 +395,7 @@ export default defineComponent({
horizontalScrollAffixRef,
headerTopAffixRef,
footerBottomAffixRef,
tableRefTabIndex,
};
},

Expand Down Expand Up @@ -714,7 +716,7 @@ export default defineComponent({
return (
<div
ref="tableRef"
tabindex="0"
tabindex={this.tableRefTabIndex}
class={this.dynamicBaseTableClasses}
onFocus={this.onTableFocus}
onBlur={this.onTableBlur}
Expand Down
20 changes: 19 additions & 1 deletion src/table/hooks/useHoverKeyboardEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { toRefs, Ref, ref, computed } from 'vue';
import get from 'lodash/get';
import { BaseTableProps } from '../interface';
import { on, off } from '../../utils/dom';
import { ALL_REG, ARROW_DOWN_REG, ARROW_UP_REG, CLEAR_REG, ESCAPE_REG, SPACE_REG } from '../../_common/js/common';
import {
ALL_REG,
ARROW_DOWN_REG,
ARROW_UP_REG,
CLEAR_REG,
ESCAPE_REG,
SPACE_REG,
ARROW_LEFT_REG,
ARROW_RIGHT_REG,
} from '../../_common/js/common';
import { RowEventContext, TableRowData } from '../type';

/**
Expand All @@ -13,6 +22,7 @@ export function useHoverKeyboardEvent(props: BaseTableProps, tableRef: Ref<HTMLD
const { hover, data, activeRowType, keyboardRowHover, disableSpaceInactiveRow } = toRefs(props);
const hoverRow = ref<string | number>();
const currentHoverRowIndex = ref(-1);
const tableRefTabIndex = ref(0);

// 单行高亮场景,不需要键盘悬浮效果
const needKeyboardRowHover = computed(() => {
Expand Down Expand Up @@ -67,6 +77,13 @@ export function useHoverKeyboardEvent(props: BaseTableProps, tableRef: Ref<HTMLD
} else if (CLEAR_REG.test(code) && !props.activeRowType) {
props.onActiveRowAction?.({ action: 'clear', activeRowList: [] });
}

// 用于支持键盘默认的左右滚动,左右滚动时重置为undefined,其他情况下为0,支持键盘操作
if (ARROW_LEFT_REG.test(code) || ARROW_RIGHT_REG.test(code)) {
tableRefTabIndex.value = undefined;
} else {
tableRefTabIndex.value = 0;
}
};

const addRowHoverKeyboardListener = () => {
Expand All @@ -83,6 +100,7 @@ export function useHoverKeyboardEvent(props: BaseTableProps, tableRef: Ref<HTMLD
clearHoverRow,
addRowHoverKeyboardListener,
removeRowHoverKeyboardListener,
tableRefTabIndex,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/table/hooks/useRowHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export function useRowHighlight(props: BaseTableProps, tableRef: Ref<HTMLDivElem

const keyboardDownListener = (e: KeyboardEvent) => {
const code = e.code || e.key?.trim();

if (ARROW_DOWN_REG.test(code)) {
e.preventDefault();
const index = Math.min(data.value.length - 1, currentOperationRowIndex.value + 1);
Expand Down
Loading