|
@@ -1,15 +1,15 @@
|
|
|
-import { defineComponent, PropType, ref, unref, nextTick } from 'vue';
|
|
|
-import { injectTable } from '../hooks/useProvinceTable';
|
|
|
+import { defineComponent, PropType, ref, unref, nextTick, watchEffect } from 'vue';
|
|
|
import ClickOutSide from '/@/components/ClickOutSide/index.vue';
|
|
|
|
|
|
import { RenderEditableCellParams } from '../types/table';
|
|
|
import { ComponentType } from '../types/componentType';
|
|
|
|
|
|
import { componentMap } from '../componentMap';
|
|
|
-import '../style/editable-cell.less';
|
|
|
import { isString, isBoolean } from '/@/utils/is';
|
|
|
import { FormOutlined, CloseOutlined, CheckOutlined } from '@ant-design/icons-vue';
|
|
|
|
|
|
+import '../style/editable-cell.less';
|
|
|
+
|
|
|
const prefixCls = 'editable-cell';
|
|
|
const EditableCell = defineComponent({
|
|
|
name: 'EditableCell',
|
|
@@ -37,14 +37,35 @@ const EditableCell = defineComponent({
|
|
|
type: String as PropType<ComponentType>,
|
|
|
default: 'Input',
|
|
|
},
|
|
|
+ editable: {
|
|
|
+ type: Boolean as PropType<boolean>,
|
|
|
+ default: false,
|
|
|
+ },
|
|
|
+ editRow: {
|
|
|
+ type: Boolean as PropType<boolean>,
|
|
|
+ default: false,
|
|
|
+ },
|
|
|
+ record: {
|
|
|
+ type: Object as PropType<EditRecordRow>,
|
|
|
+ },
|
|
|
+ placeholder: {
|
|
|
+ type: String as PropType<string>,
|
|
|
+ },
|
|
|
},
|
|
|
emits: ['submit', 'cancel'],
|
|
|
setup(props, { attrs, emit }) {
|
|
|
- const table = injectTable();
|
|
|
const elRef = ref<any>(null);
|
|
|
|
|
|
const isEditRef = ref(false);
|
|
|
const currentValueRef = ref<string | boolean>(props.value);
|
|
|
+ const defaultValueRef = ref<string | boolean>(props.value);
|
|
|
+
|
|
|
+ watchEffect(() => {
|
|
|
+ defaultValueRef.value = props.value;
|
|
|
+ if (isBoolean(props.editable)) {
|
|
|
+ isEditRef.value = props.editable;
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
function handleChange(e: any) {
|
|
|
if (e && e.target && Reflect.has(e.target, 'value')) {
|
|
@@ -65,37 +86,55 @@ const EditableCell = defineComponent({
|
|
|
|
|
|
function handleCancel() {
|
|
|
isEditRef.value = false;
|
|
|
+ currentValueRef.value = defaultValueRef.value;
|
|
|
emit('cancel');
|
|
|
}
|
|
|
|
|
|
+ if (props.record) {
|
|
|
+ /* eslint-disable */
|
|
|
+ props.record.onCancel = handleCancel;
|
|
|
+ /* eslint-disable */
|
|
|
+ props.record.onSubmit = handleSubmit;
|
|
|
+ }
|
|
|
+
|
|
|
function handleSubmit() {
|
|
|
const { dataKey, dataIndex } = props;
|
|
|
- if (!dataKey || !dataIndex) {
|
|
|
- return;
|
|
|
- }
|
|
|
- isEditRef.value = false;
|
|
|
+ if (!dataKey || !dataIndex) return;
|
|
|
|
|
|
- const { getDataSource } = table;
|
|
|
- const dataSource = getDataSource();
|
|
|
- const target = dataSource.find((item) => item.key === dataKey);
|
|
|
- if (target) {
|
|
|
- target[dataIndex] = unref(currentValueRef);
|
|
|
- emit('submit', { dataKey, dataIndex, value: unref(currentValueRef) });
|
|
|
+ if (props.record) {
|
|
|
+ /* eslint-disable */
|
|
|
+ props.record[dataIndex] = unref(currentValueRef) as string;
|
|
|
}
|
|
|
+ isEditRef.value = false;
|
|
|
}
|
|
|
|
|
|
function onClickOutside() {
|
|
|
+ if (props.editRow) return;
|
|
|
const { component } = props;
|
|
|
|
|
|
if (component && component.includes('Input')) {
|
|
|
handleCancel();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ function renderValue() {
|
|
|
+ const { value } = props;
|
|
|
+ if (props.editRow) {
|
|
|
+ return !unref(isEditRef) ? value : null;
|
|
|
+ }
|
|
|
+ return (
|
|
|
+ !unref(isEditRef) && (
|
|
|
+ <div class={`${prefixCls}__normal`} onClick={handleEdit}>
|
|
|
+ {value}
|
|
|
+ <FormOutlined class={`${prefixCls}__normal-icon`} />
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
return () => {
|
|
|
- const { value, component, componentProps = {} } = props;
|
|
|
+ const { component, componentProps = {} } = props;
|
|
|
|
|
|
const Comp = componentMap.get(component!) as any;
|
|
|
- // const propsData: any = {};
|
|
|
return (
|
|
|
<div class={prefixCls}>
|
|
|
{unref(isEditRef) && (
|
|
@@ -103,6 +142,7 @@ const EditableCell = defineComponent({
|
|
|
{() => (
|
|
|
<div class={`${prefixCls}__wrapper`}>
|
|
|
<Comp
|
|
|
+ placeholder={props.placeholder}
|
|
|
{...{
|
|
|
...attrs,
|
|
|
...componentProps,
|
|
@@ -114,21 +154,20 @@ const EditableCell = defineComponent({
|
|
|
onChange={handleChange}
|
|
|
onPressEnter={handleSubmit}
|
|
|
/>
|
|
|
- <div class={`${prefixCls}__action`}>
|
|
|
- <CheckOutlined class={[`${prefixCls}__icon`, 'mx-2']} onClick={handleSubmit} />
|
|
|
- <CloseOutlined class={[`${prefixCls}__icon `]} onClick={handleCancel} />
|
|
|
- </div>
|
|
|
+ {!props.editRow && (
|
|
|
+ <div class={`${prefixCls}__action`}>
|
|
|
+ <CheckOutlined
|
|
|
+ class={[`${prefixCls}__icon`, 'mx-2']}
|
|
|
+ onClick={handleSubmit}
|
|
|
+ />
|
|
|
+ <CloseOutlined class={[`${prefixCls}__icon `]} onClick={handleCancel} />
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
</div>
|
|
|
)}
|
|
|
</ClickOutSide>
|
|
|
)}
|
|
|
-
|
|
|
- {!unref(isEditRef) && (
|
|
|
- <div class={`${prefixCls}__normal`} onClick={handleEdit}>
|
|
|
- {value}
|
|
|
- <FormOutlined class={`${prefixCls}__normal-icon`} />
|
|
|
- </div>
|
|
|
- )}
|
|
|
+ {renderValue()}
|
|
|
</div>
|
|
|
);
|
|
|
};
|
|
@@ -138,15 +177,16 @@ const EditableCell = defineComponent({
|
|
|
export function renderEditableCell({
|
|
|
dataIndex,
|
|
|
component,
|
|
|
- componentOn = {},
|
|
|
componentProps = {},
|
|
|
+ placeholder,
|
|
|
}: RenderEditableCellParams) {
|
|
|
- return ({ text, record }: { text: string; record: any }) => {
|
|
|
+ return ({ text, record }: { text: string; record: EditRecordRow }) => {
|
|
|
return (
|
|
|
<EditableCell
|
|
|
- {...componentOn}
|
|
|
{...componentProps}
|
|
|
+ placeholder={placeholder}
|
|
|
value={text}
|
|
|
+ record={record}
|
|
|
dataKey={record.key}
|
|
|
dataIndex={dataIndex}
|
|
|
component={component}
|
|
@@ -154,3 +194,32 @@ export function renderEditableCell({
|
|
|
);
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+export function renderEditableRow({
|
|
|
+ dataIndex,
|
|
|
+ component,
|
|
|
+ componentProps = {},
|
|
|
+ placeholder,
|
|
|
+}: RenderEditableCellParams) {
|
|
|
+ return ({ text, record }: { text: string; record: EditRecordRow }) => {
|
|
|
+ return (
|
|
|
+ <EditableCell
|
|
|
+ {...componentProps}
|
|
|
+ value={text}
|
|
|
+ placeholder={placeholder}
|
|
|
+ editRow={true}
|
|
|
+ editable={record.editable}
|
|
|
+ dataKey={record.key}
|
|
|
+ record={record}
|
|
|
+ dataIndex={dataIndex}
|
|
|
+ component={component}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+export type EditRecordRow<T = { [key: string]: any }> = {
|
|
|
+ editable: boolean;
|
|
|
+ onCancel: Fn;
|
|
|
+ onSubmit: Fn;
|
|
|
+} & T;
|