Skip to content

Commit

Permalink
chore:组件事件列表支持动态构建
Browse files Browse the repository at this point in the history
  • Loading branch information
hsm-lv committed Jan 2, 2024
1 parent d9ace50 commit 3e4a892
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 193 deletions.
6 changes: 4 additions & 2 deletions packages/amis-editor-core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ export interface PluginInterface
*
* 事件定义集合
*/
events?: RendererPluginEvent[];
events?: RendererPluginEvent[] | ((schema: any) => RendererPluginEvent[]);

/**
*
Expand Down Expand Up @@ -997,7 +997,9 @@ export interface SubRendererPluginAction
> {}

export interface PluginEvents {
[propName: string]: RendererPluginEvent[];
[propName: string]:
| RendererPluginEvent[]
| ((schema: any) => RendererPluginEvent[]);
}

export interface PluginActions {
Expand Down
3 changes: 2 additions & 1 deletion packages/amis-editor/src/plugin/Form/InputTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export class TreeControlPlugin extends BasePlugin {
panelTitle = '树选择';

// 事件定义
events: RendererPluginEvent[] = TREE_BASE_EVENTS;
events: (schema: any) => RendererPluginEvent[] = (schema: any) =>
TREE_BASE_EVENTS(schema);

// 动作定义
actions: RendererPluginAction[] = [
Expand Down
134 changes: 68 additions & 66 deletions packages/amis-editor/src/plugin/Form/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,84 +84,86 @@ export class SelectControlPlugin extends BasePlugin {
};

// 事件定义
events: RendererPluginEvent[] = [
{
eventName: 'change',
eventLabel: '值变化',
description: '选中值变化时触发',
dataSchema: (manager: EditorManager) => {
const {value, selectedItems, items} =
resolveOptionEventDataSchame(manager);
events: (schema: any) => RendererPluginEvent[] = (schema: any) => {
return [
{
eventName: 'change',
eventLabel: '值变化',
description: '选中值变化时触发',
dataSchema: (manager: EditorManager) => {
const {value, selectedItems, items} =
resolveOptionEventDataSchame(manager);

return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
selectedItems,
items
return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
selectedItems,
items
}
}
}
}
}
];
}
},
{
eventName: 'focus',
eventLabel: '获取焦点',
description: '输入框获取焦点时触发',
dataSchema: (manager: EditorManager) => {
const {value, items} = resolveOptionEventDataSchame(manager);
];
}
},
{
eventName: 'focus',
eventLabel: '获取焦点',
description: '输入框获取焦点时触发',
dataSchema: (manager: EditorManager) => {
const {value, items} = resolveOptionEventDataSchame(manager);

return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
items
return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
items
}
}
}
}
}
];
}
},
{
eventName: 'blur',
eventLabel: '失去焦点',
description: '输入框失去焦点时触发',
dataSchema: (manager: EditorManager) => {
const {value, items} = resolveOptionEventDataSchame(manager);
];
}
},
{
eventName: 'blur',
eventLabel: '失去焦点',
description: '输入框失去焦点时触发',
dataSchema: (manager: EditorManager) => {
const {value, items} = resolveOptionEventDataSchame(manager);

return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
items
return [
{
type: 'object',
properties: {
data: {
type: 'object',
title: '数据',
properties: {
value,
items
}
}
}
}
}
];
}
},
...OPTION_EDIT_EVENTS,
...OPTION_EDIT_EVENTS_OLD
];
];
}
},
...OPTION_EDIT_EVENTS,
...OPTION_EDIT_EVENTS_OLD(schema)
];
};

// 动作定义
actions: RendererPluginAction[] = [
Expand Down
4 changes: 2 additions & 2 deletions packages/amis-editor/src/plugin/Form/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export class TreeSelectControlPlugin extends BasePlugin {
panelTitle = '树选择';

// 事件定义
events: RendererPluginEvent[] = [
...TREE_BASE_EVENTS,
events: (schema: any) => RendererPluginEvent[] = (schema: any) => [
...TREE_BASE_EVENTS(schema),
{
eventName: 'focus',
eventLabel: '获取焦点',
Expand Down
27 changes: 25 additions & 2 deletions packages/amis-editor/src/renderer/event-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ export class EventControl extends React.Component<
[prop: string]: boolean;
} = {};

const pluginEvents =
const tmpEvents =
events[
rawType || (!data.type || data.type === 'text' ? 'plain' : data.type)
] || [];
const pluginEvents =
typeof tmpEvents === 'function' ? tmpEvents(data) : [...tmpEvents];

pluginEvents.forEach((event: RendererPluginEvent) => {
eventPanelActive[event.eventName] = true;
Expand Down Expand Up @@ -191,11 +193,32 @@ export class EventControl extends React.Component<
prevProps: EventControlProps,
prevState: EventControlState
) {
const {value} = this.props;
const {value, data, events, rawType} = this.props;

if (value !== prevProps.value) {
this.setState({onEvent: value});
}

if (data !== prevProps.data) {
const eventPanelActive: {
[prop: string]: boolean;
} = {};
const tmpEvents =
events[
rawType || (!data.type || data.type === 'text' ? 'plain' : data.type)
] || [];
const pluginEvents =
typeof tmpEvents === 'function' ? tmpEvents(data) : [...tmpEvents];

pluginEvents.forEach((event: RendererPluginEvent) => {
eventPanelActive[event.eventName] = true;
});

this.setState({
events: pluginEvents,
eventPanelActive
});
}
}

generateEmptyDefault(events: RendererPluginEvent[]) {
Expand Down
Loading

0 comments on commit 3e4a892

Please sign in to comment.