Files
workspace/code/fms/fms-vue/src/components/fms-table/utils/createEditableCellRenderer.js
T
2026-07-22 17:31:48 +08:00

81 lines
2.7 KiB
JavaScript

import { h } from "vue";
import { Checkbox, Input, InputNumber, Select } from "antdv-next";
function resolveOption(option, row) {
return typeof option === "function" ? option(row) : option;
}
export function createEditableCellRenderer({ getRowKey, getActiveRowKey }) {
return function EditableCell({ row, col, cellValue }) {
const editor = typeof col.editor === "string" ? { type: col.editor } : col.editor || {};
const activeRowKey = getActiveRowKey();
const isEditing = activeRowKey != null && String(getRowKey(row)) === String(activeRowKey);
if (!isEditing) {
if (editor.type === "checkbox") {
return h(Checkbox, {
checked: cellValue === (editor.checkedValue ?? 1),
style: { pointerEvents: "none" },
});
}
if (editor.type === "select") {
const options = resolveOption(editor.options, row) || [];
const option = options.find((item) => item.value === cellValue);
return h("span", { class: "fms-cell-text" }, option?.label ?? cellValue ?? "");
}
return h("span", { class: "fms-cell-text" }, cellValue ?? "");
}
const update = (value) => {
row[col.dataIndex] = value;
editor.onChange?.(value, row, col);
};
const common = {
size: "small",
style: { width: "100%" },
onClick: (event) => event.stopPropagation(),
};
if (editor.type === "number") {
return h(InputNumber, {
...common,
value: cellValue,
min: resolveOption(editor.min, row),
max: resolveOption(editor.max, row),
precision: resolveOption(editor.precision, row),
disabled: resolveOption(editor.disabled, row),
"onUpdate:value": update,
});
}
if (editor.type === "select") {
return h(Select, {
...common,
value: cellValue,
options: resolveOption(editor.options, row) || [],
disabled: resolveOption(editor.disabled, row),
"onUpdate:value": update,
});
}
if (editor.type === "checkbox") {
const checkedValue = editor.checkedValue ?? 1;
const uncheckedValue = editor.uncheckedValue ?? 0;
return h(Checkbox, {
checked: cellValue === checkedValue,
disabled: resolveOption(editor.disabled, row),
onClick: (event) => event.stopPropagation(),
"onUpdate:checked": (checked) => update(checked ? checkedValue : uncheckedValue),
});
}
return h(Input, {
...common,
value: cellValue,
disabled: resolveOption(editor.disabled, row),
maxlength: resolveOption(editor.maxlength, row),
placeholder: resolveOption(editor.placeholder, row),
status: resolveOption(editor.status, row),
allowClear: editor.allowClear ?? true,
"onUpdate:value": update,
});
};
}