feat(editor): 数据源事件配置

This commit is contained in:
roymondchen 2023-09-21 10:58:39 +08:00
parent 36988cd3e0
commit 8b7a1e4e3c
5 changed files with 40 additions and 3 deletions

View File

@ -52,6 +52,7 @@ import { computed, inject } from 'vue';
import { Delete } from '@element-plus/icons-vue'; import { Delete } from '@element-plus/icons-vue';
import { has } from 'lodash-es'; import { has } from 'lodash-es';
import type { EventOption } from '@tmagic/core';
import { TMagicButton } from '@tmagic/design'; import { TMagicButton } from '@tmagic/design';
import type { FieldProps, FormState } from '@tmagic/form'; import type { FieldProps, FormState } from '@tmagic/form';
import { ActionType } from '@tmagic/schema'; import { ActionType } from '@tmagic/schema';
@ -75,11 +76,22 @@ const eventNameConfig = computed(() => {
text: '事件', text: '事件',
type: 'select', type: 'select',
labelWidth: '40px', labelWidth: '40px',
options: (mForm: FormState, { formValue }: any) => options: (mForm: FormState, { formValue }: any) => {
services?.eventsService.getEvent(formValue.type).map((option: any) => ({ let events: EventOption[] = [];
if (!services) return events;
if (props.config.src === 'component') {
events = services.eventsService.getEvent(formValue.type);
} else if (props.config.src === 'datasource') {
events = services.dataSourceService.getFormEvent(formValue.type);
}
return events.map((option) => ({
text: option.label, text: option.label,
value: option.value, value: option.value,
})), }));
},
}; };
return { ...defaultEventNameConfig, ...props.config.eventNameConfig }; return { ...defaultEventNameConfig, ...props.config.eventNameConfig };
}); });

View File

@ -1,6 +1,7 @@
import { reactive } from 'vue'; import { reactive } from 'vue';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import type { EventOption } from '@tmagic/core';
import type { FormConfig } from '@tmagic/form'; import type { FormConfig } from '@tmagic/form';
import type { DataSourceSchema } from '@tmagic/schema'; import type { DataSourceSchema } from '@tmagic/schema';
import { guid } from '@tmagic/utils'; import { guid } from '@tmagic/utils';
@ -16,6 +17,7 @@ interface State {
editable: boolean; editable: boolean;
configs: Record<string, FormConfig>; configs: Record<string, FormConfig>;
values: Record<string, Partial<DataSourceSchema>>; values: Record<string, Partial<DataSourceSchema>>;
events: Record<string, EventOption[]>;
} }
type StateKey = keyof State; type StateKey = keyof State;
@ -26,6 +28,7 @@ class DataSource extends BaseService {
editable: true, editable: true,
configs: {}, configs: {},
values: {}, values: {},
events: {},
}); });
public set<K extends StateKey, T extends State[K]>(name: K, value: T) { public set<K extends StateKey, T extends State[K]>(name: K, value: T) {
@ -52,6 +55,14 @@ class DataSource extends BaseService {
this.get('values')[type] = value; this.get('values')[type] = value;
} }
public getFormEvent(type = 'base') {
return this.get('events')[type] || [];
}
public setFormEvent(type: string, value: EventOption[] = []) {
this.get('events')[type] = value;
}
public add(config: DataSourceSchema) { public add(config: DataSourceSchema) {
const newConfig = { const newConfig = {
...config, ...config,

View File

@ -418,6 +418,7 @@ export interface HistoryState {
export interface EventSelectConfig { export interface EventSelectConfig {
name: string; name: string;
type: 'event-select'; type: 'event-select';
src: 'datasource' | 'component';
/** 事件名称表单配置 */ /** 事件名称表单配置 */
eventNameConfig?: FormItem; eventNameConfig?: FormItem;
/** 动作类型配置 */ /** 动作类型配置 */

View File

@ -29,6 +29,18 @@ const fillConfig = (config: FormConfig): FormConfig => [
}, },
], ],
}, },
{
type: 'panel',
title: '事件配置',
display: false,
items: [
{
name: 'events',
src: 'datasource',
type: 'event-select',
},
],
},
]; ];
export const getFormConfig = (type: string, configs: Record<string, FormConfig>): FormConfig => { export const getFormConfig = (type: string, configs: Record<string, FormConfig>): FormConfig => {

View File

@ -205,6 +205,7 @@ export const eventTabConfig: TabPaneConfig = {
items: [ items: [
{ {
name: 'events', name: 'events',
src: 'component',
type: 'event-select', type: 'event-select',
}, },
], ],