Skip to content

Commit 181e84e

Browse files
authored
Merge pull request #443 from merobal/codingsans
Support emit event from Custom Component
2 parents 13d4793 + e32f5c7 commit 181e84e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/custom-component/create-custom-component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BuilderInfo, Components, ExtendedComponentSchema, Utils as FormioUtils } from 'formiojs';
2-
import { FormioCustomComponentInfo, FormioCustomElement } from '../formio.common';
2+
import { FormioCustomComponentInfo, FormioCustomElement, FormioEvent } from '../formio.common';
33
import { clone, isNil, isArray } from 'lodash';
44

55
const BaseInputComponent = Components.components.input;
@@ -106,16 +106,19 @@ export function createCustomFormioComponent(customComponentOptions: FormioCustom
106106
superAttach = super.attach(element);
107107
}
108108

109+
// Bind customOptions
109110
for (const key in this.component.customOptions) {
110111
if (this.component.customOptions.hasOwnProperty(key)) {
111112
this._customAngularElement[key] = this.component.customOptions[key];
112113
}
113114
}
115+
// Bind validate options
114116
for (const key in this.component.validate) {
115117
if (this.component.validate.hasOwnProperty(key)) {
116118
this._customAngularElement[key] = this.component.validate[key];
117119
}
118120
}
121+
// Bind options explicitly set
119122
const fieldOptions = customComponentOptions.fieldOptions;
120123
if (isArray(fieldOptions) && fieldOptions.length > 0) {
121124
for (const key in fieldOptions) {
@@ -125,6 +128,14 @@ export function createCustomFormioComponent(customComponentOptions: FormioCustom
125128
}
126129
}
127130

131+
// Attach event listener for emit event
132+
this._customAngularElement.addEventListener('formioEvent', (event: CustomEvent<FormioEvent>) => {
133+
this.emit(event.detail.eventName, {
134+
...event.detail.data,
135+
component: this.component
136+
});
137+
});
138+
128139
// Ensure we bind the value (if it isn't a multiple-value component with no wrapper)
129140
if (!this._customAngularElement.value && !this.component.disableMultiValueWrapper) {
130141
this.restoreValue();

src/formio.common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ export interface FormioCustomComponentInfo extends BuilderInfo {
8686

8787
export type FormioCustomElement = NgElement & WithProperties<{ value: any } & ExtendedComponentSchema>;
8888

89+
export interface FormioEvent {
90+
eventName: string;
91+
data?: {
92+
[key: string]: any;
93+
};
94+
}
95+
8996
export interface FormioCustomComponent<T> {
9097
value: T; // Should be an @Input
9198
valueChange: EventEmitter<T>; // Should be an @Output
9299
disabled: boolean;
100+
formioEvent?: EventEmitter<FormioEvent>; // Should be an @Output
93101
}

0 commit comments

Comments
 (0)