20260723225430

This commit is contained in:
oneao committed 2026-07-23 22:54:30 +08:00
1 parent 4d118a0037
commit 4f40ad9f11
2 files changed
+24 -9

No files matched your search

@@ -22,7 +22,7 @@ const props = defineProps({
activeRowKey: { type: [String, Number], default: null }, activeRowKey: { type: [String, Number], default: null },
}); });
const emit = defineEmits(["update:columns"]); const emit = defineEmits(["update:columns", "row-click"]);
function getRowKey(row) { function getRowKey(row) {
return typeof props.rowKey === "function" ? props.rowKey(row) : row?.[props.rowKey]; return typeof props.rowKey === "function" ? props.rowKey(row) : row?.[props.rowKey];
@@ -30,9 +30,19 @@ function getRowKey(row) {
const selectedRowKeySet = computed(() => new Set(props.rowSelection?.selectedRowKeys || [])); const selectedRowKeySet = computed(() => new Set(props.rowSelection?.selectedRowKeys || []));
// 追踪用户点击的单元格列,用于编辑态自动聚焦到对应列
const activeColKey = ref(null);
function onStkRowClick(ev, row, data) {
const td = ev.target.closest?.("td");
activeColKey.value = td?.dataset?.colKey || null;
emit("row-click", ev, row, data);
}
const EditableCell = createEditableCellRenderer({ const EditableCell = createEditableCellRenderer({
getRowKey, getRowKey,
getActiveRowKey: () => props.activeRowKey, getActiveRowKey: () => props.activeRowKey,
getActiveColKey: () => activeColKey.value,
}); });
const { SelectionCell, SelectionHeaderCell } = createSelectionCellRenderers({ const { SelectionCell, SelectionHeaderCell } = createSelectionCellRenderers({
@@ -154,6 +164,7 @@ defineExpose({ scrollToRow });
:row-key="rowKey" :row-key="rowKey"
:bordered="bordered" :bordered="bordered"
:no-data-full="noDataFull" :no-data-full="noDataFull"
@row-click="onStkRowClick"
> >
<template v-for="(slot, name) in forwardSlots" :key="name" #[name]="slotProps"> <template v-for="(slot, name) in forwardSlots" :key="name" #[name]="slotProps">
<slot :name="name" v-bind="slotProps || {}" /> <slot :name="name" v-bind="slotProps || {}" />
@@ -5,11 +5,13 @@ function resolveOption(option, row) {
return typeof option === "function" ? option(row) : option; return typeof option === "function" ? option(row) : option;
} }
export function createEditableCellRenderer({ getRowKey, getActiveRowKey }) { export function createEditableCellRenderer({ getRowKey, getActiveRowKey, getActiveColKey }) {
return function EditableCell({ row, col, cellValue }) { return function EditableCell({ row, col, cellValue }) {
const editor = typeof col.editor === "string" ? { type: col.editor } : col.editor || {}; const editor = typeof col.editor === "string" ? { type: col.editor } : col.editor || {};
const activeRowKey = getActiveRowKey(); const activeRowKey = getActiveRowKey();
const isEditing = activeRowKey != null && String(getRowKey(row)) === String(activeRowKey); const isEditing = activeRowKey != null && String(getRowKey(row)) === String(activeRowKey);
const activeColKey = getActiveColKey?.();
const shouldFocus = isEditing && (activeColKey == null || col.dataIndex === activeColKey);
const update = (value) => { const update = (value) => {
row[col.dataIndex] = value; row[col.dataIndex] = value;
@@ -41,13 +43,15 @@ export function createEditableCellRenderer({ getRowKey, getActiveRowKey }) {
size: "small", size: "small",
style: { width: "100%" }, style: { width: "100%" },
onClick: (event) => event.stopPropagation(), onClick: (event) => event.stopPropagation(),
// 编辑态首次渲染自动聚焦 // 仅被点击的列首次渲染时自动聚焦
ref: (instance) => { ref: shouldFocus
if (instance) { ? (instance) => {
const target = instance.focus ? instance : instance.$el; if (instance) {
target?.focus?.(); const target = instance.focus ? instance : instance.$el;
} target?.focus?.();
}, }
}
: undefined,
}; };
if (editor.type === "number") { if (editor.type === "number") {