|
@@ -1,32 +1,23 @@
|
|
-import { css, cx } from "@linaria/core";
|
|
|
|
|
|
+import { css } from "@linaria/core";
|
|
import { Tooltip } from "ant-design-vue";
|
|
import { Tooltip } from "ant-design-vue";
|
|
-import _ from "lodash";
|
|
|
|
-import { computed, defineComponent } from "vue";
|
|
|
|
-import { any, array, bool, func } from "vue-types";
|
|
|
|
|
|
+import { defineComponent, reactive, onMounted, onUnmounted } from "vue";
|
|
|
|
+import { any, array, func, number, object } from "vue-types";
|
|
|
|
|
|
export interface ColumnItem {
|
|
export interface ColumnItem {
|
|
label?: string;
|
|
label?: string;
|
|
component?: ((...args: any[]) => any) | Record<string, any>;
|
|
component?: ((...args: any[]) => any) | Record<string, any>;
|
|
dataIndex?: string;
|
|
dataIndex?: string;
|
|
- setterIndex?: string;
|
|
|
|
props?: { [name: string]: any };
|
|
props?: { [name: string]: any };
|
|
itemProps?: { [name: string]: any };
|
|
itemProps?: { [name: string]: any };
|
|
- getValue?: (v: any, data?: any) => any;
|
|
|
|
- isDisable?: (value: any, data: any) => boolean;
|
|
|
|
- isLoading?: (value: any, data: any) => boolean;
|
|
|
|
- isVisible?: (value: any, data: any) => boolean;
|
|
|
|
changeExtra?: (data: any) => any;
|
|
changeExtra?: (data: any) => any;
|
|
- children?: ColumnItem[];
|
|
|
|
}
|
|
}
|
|
|
|
|
|
export default defineComponent({
|
|
export default defineComponent({
|
|
props: {
|
|
props: {
|
|
columns: array<ColumnItem>().isRequired,
|
|
columns: array<ColumnItem>().isRequired,
|
|
editor: any(),
|
|
editor: any(),
|
|
- data: any().isRequired,
|
|
|
|
- disabled: bool(),
|
|
|
|
|
|
+ data: any(),
|
|
onChange: func(),
|
|
onChange: func(),
|
|
- onChangeEnd: func(),
|
|
|
|
},
|
|
},
|
|
setup(props) {
|
|
setup(props) {
|
|
return () => <FormUI {...props} />;
|
|
return () => <FormUI {...props} />;
|
|
@@ -35,11 +26,8 @@ export default defineComponent({
|
|
|
|
|
|
const FormUI = (props: {
|
|
const FormUI = (props: {
|
|
columns: ColumnItem[];
|
|
columns: ColumnItem[];
|
|
- data: any;
|
|
|
|
editor: any;
|
|
editor: any;
|
|
- disabled?: boolean;
|
|
|
|
onChange?: (...arg: any) => void;
|
|
onChange?: (...arg: any) => void;
|
|
- onChangeEnd?: (...arg: any) => void;
|
|
|
|
}) => {
|
|
}) => {
|
|
const { columns, ...restProps } = props;
|
|
const { columns, ...restProps } = props;
|
|
return (
|
|
return (
|
|
@@ -48,7 +36,7 @@ const FormUI = (props: {
|
|
if (!column.dataIndex) {
|
|
if (!column.dataIndex) {
|
|
return renderUI({ props: restProps, column, index });
|
|
return renderUI({ props: restProps, column, index });
|
|
}
|
|
}
|
|
- return renderFormItem({ column, ...restProps, index });
|
|
|
|
|
|
+ return <RenderFormItem column={column} index={index} {...restProps} />;
|
|
})}
|
|
})}
|
|
</>
|
|
</>
|
|
);
|
|
);
|
|
@@ -73,89 +61,95 @@ const renderUI = ({ props, column, index }: any) => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-export const renderFormItem = (props: {
|
|
|
|
- column: ColumnItem;
|
|
|
|
- index?: number;
|
|
|
|
- data: any;
|
|
|
|
- editor: any;
|
|
|
|
- disabled?: boolean;
|
|
|
|
- onChange?: (...arg: any) => void;
|
|
|
|
- onChangeEnd?: (...arg: any) => void;
|
|
|
|
-}) => {
|
|
|
|
- const { column, data, index, onChange, onChangeEnd } = props;
|
|
|
|
-
|
|
|
|
- // const compValue = computed(() => {
|
|
|
|
- // const { data, column } = props;
|
|
|
|
- // if (!column.dataIndex) return;
|
|
|
|
- // const itemValue = _.get(data, column.dataIndex);
|
|
|
|
- // return column.getValue ? column.getValue(itemValue) : itemValue;
|
|
|
|
- // });
|
|
|
|
-
|
|
|
|
- const editor = props.editor;
|
|
|
|
- const compValue = editor ? editor.commands.get(column.dataIndex) : "";
|
|
|
|
- // console.log(column.dataIndex, compValue.value);
|
|
|
|
-
|
|
|
|
- // const compValue = computed(() => {
|
|
|
|
- // const { data, column } = props;
|
|
|
|
- // if (!column.dataIndex) return;
|
|
|
|
- // const itemValue = _.get(data, column.dataIndex);
|
|
|
|
- // return column.getValue ? column.getValue(itemValue, data) : itemValue;
|
|
|
|
- // });
|
|
|
|
-
|
|
|
|
- const changeVal = (value: any, ...args: any[]) => {
|
|
|
|
- const { column } = props;
|
|
|
|
- let params = {
|
|
|
|
- dataIndex: column.dataIndex,
|
|
|
|
- setterIndex: column.setterIndex,
|
|
|
|
- value,
|
|
|
|
- ...args,
|
|
|
|
|
|
+export const RenderFormItem = defineComponent({
|
|
|
|
+ props: {
|
|
|
|
+ column: object<ColumnItem>(),
|
|
|
|
+ index: number(),
|
|
|
|
+ editor: any(),
|
|
|
|
+ onChange: func(),
|
|
|
|
+ onChangeEnd: func(),
|
|
|
|
+ },
|
|
|
|
+ setup(props) {
|
|
|
|
+ const state = reactive({
|
|
|
|
+ value: undefined,
|
|
|
|
+ });
|
|
|
|
+ function handleValueChange() {
|
|
|
|
+ const { column, editor } = props;
|
|
|
|
+ const command = editor ? editor.commands.get(column?.dataIndex) : "";
|
|
|
|
+ if (command) {
|
|
|
|
+ console.log("change==>", column?.dataIndex, state.value);
|
|
|
|
+ state.value = command.value;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ const initCommands = () => {
|
|
|
|
+ const { column, editor } = props;
|
|
|
|
+ const command = editor ? editor.commands.get(column?.dataIndex) : "";
|
|
|
|
+ if (command) {
|
|
|
|
+ state.value = command.value;
|
|
|
|
+
|
|
|
|
+ command.on("change:value", handleValueChange);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ onMounted(() => {
|
|
|
|
+ initCommands();
|
|
|
|
+ });
|
|
|
|
+ onUnmounted(() => {
|
|
|
|
+ const { column, editor } = props;
|
|
|
|
+ const command = editor ? editor.commands.get(column?.dataIndex) : "";
|
|
|
|
+ if (command) {
|
|
|
|
+ command.off("change:value", handleValueChange);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ const changeVal = (value: any, ...args: any[]) => {
|
|
|
|
+ const { column } = props;
|
|
|
|
+ let params = {
|
|
|
|
+ dataIndex: column?.dataIndex,
|
|
|
|
+ value: { value },
|
|
|
|
+ ...args,
|
|
|
|
+ };
|
|
|
|
+ if (column?.changeExtra) params = column.changeExtra?.(params);
|
|
|
|
+ props.onChange?.(params);
|
|
|
|
+ return params;
|
|
};
|
|
};
|
|
- if (column.changeExtra) params = column.changeExtra?.(params);
|
|
|
|
- onChange?.(params);
|
|
|
|
- return params;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- const changeValEnd = (value: any, ...args: any[]) => {
|
|
|
|
- const params = changeVal(value, ...args);
|
|
|
|
- onChangeEnd?.(params);
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- // const isVisible = column.isVisible?.(compValue.value, data);
|
|
|
|
- // if (column.isVisible && !isVisible) return null;
|
|
|
|
-
|
|
|
|
- // const disabled = props.disabled || column.isDisable?.(compValue.value, data);
|
|
|
|
|
|
|
|
- const component = column.component;
|
|
|
|
|
|
+ const changeValEnd = (value: any, ...args: any[]) => {
|
|
|
|
+ const params = changeVal(value, ...args);
|
|
|
|
+ props.onChangeEnd?.(params);
|
|
|
|
+ };
|
|
|
|
|
|
- return (
|
|
|
|
- <div
|
|
|
|
- key={index}
|
|
|
|
- class={formItemStyles}
|
|
|
|
- {...column.itemProps}
|
|
|
|
- onClick={(e) => e.stopPropagation()}
|
|
|
|
- >
|
|
|
|
- {column.label ? (
|
|
|
|
- <Tooltip title={column.label} placement="top">
|
|
|
|
- <component
|
|
|
|
- value={compValue.value}
|
|
|
|
- {...column.props}
|
|
|
|
- loading={column.isLoading?.(compValue.value, data)}
|
|
|
|
- onChange={changeVal}
|
|
|
|
- onChangeEnd={changeValEnd}
|
|
|
|
- />
|
|
|
|
- </Tooltip>
|
|
|
|
- ) : (
|
|
|
|
- <component
|
|
|
|
- value={compValue.value}
|
|
|
|
- {...column.props}
|
|
|
|
- loading={column.isLoading?.(compValue.value, data)}
|
|
|
|
- onChange={changeVal}
|
|
|
|
- onChangeEnd={changeValEnd}
|
|
|
|
- />
|
|
|
|
- )}
|
|
|
|
- </div>
|
|
|
|
- );
|
|
|
|
-};
|
|
|
|
|
|
+ const component = props.column?.component || number;
|
|
|
|
+ return () => {
|
|
|
|
+ const { column, index } = props;
|
|
|
|
+ return (
|
|
|
|
+ <div
|
|
|
|
+ key={index}
|
|
|
|
+ class={formItemStyles}
|
|
|
|
+ {...column?.itemProps}
|
|
|
|
+ onClick={(e) => e.stopPropagation()}
|
|
|
|
+ >
|
|
|
|
+ {column?.label ? (
|
|
|
|
+ <Tooltip title={column.label} placement="top">
|
|
|
|
+ <component
|
|
|
|
+ value={state.value}
|
|
|
|
+ {...column.props}
|
|
|
|
+ onChange={changeVal}
|
|
|
|
+ onChangeEnd={changeValEnd}
|
|
|
|
+ />
|
|
|
|
+ </Tooltip>
|
|
|
|
+ ) : (
|
|
|
|
+ <component
|
|
|
|
+ value={state.value}
|
|
|
|
+ {...column?.props}
|
|
|
|
+ onChange={changeVal}
|
|
|
|
+ onChangeEnd={changeValEnd}
|
|
|
|
+ />
|
|
|
|
+ )}
|
|
|
|
+ </div>
|
|
|
|
+ );
|
|
|
|
+ };
|
|
|
|
+ },
|
|
|
|
+});
|
|
|
|
|
|
const formItemStyles = css`
|
|
const formItemStyles = css`
|
|
height: 100%;
|
|
height: 100%;
|