Skip to content

Support emit event from Custom Component #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/custom-component/create-custom-component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BuilderInfo, Components, ExtendedComponentSchema, Utils as FormioUtils } from 'formiojs';
import { FormioCustomComponentInfo, FormioCustomElement } from '../formio.common';
import { FormioCustomComponentInfo, FormioCustomElement, FormioEvent } from '../formio.common';
import { clone, isNil, isArray } from 'lodash';

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

// Bind customOptions
for (const key in this.component.customOptions) {
if (this.component.customOptions.hasOwnProperty(key)) {
this._customAngularElement[key] = this.component.customOptions[key];
}
}
// Bind validate options
for (const key in this.component.validate) {
if (this.component.validate.hasOwnProperty(key)) {
this._customAngularElement[key] = this.component.validate[key];
}
}
// Bind options explicitly set
const fieldOptions = customComponentOptions.fieldOptions;
if (isArray(fieldOptions) && fieldOptions.length > 0) {
for (const key in fieldOptions) {
Expand All @@ -125,6 +128,14 @@ export function createCustomFormioComponent(customComponentOptions: FormioCustom
}
}

// Attach event listener for emit event
this._customAngularElement.addEventListener('formioEvent', (event: CustomEvent<FormioEvent>) => {
this.emit(event.detail.eventName, {
...event.detail.data,
component: this.component
});
});

// Ensure we bind the value (if it isn't a multiple-value component with no wrapper)
if (!this._customAngularElement.value && !this.component.disableMultiValueWrapper) {
this.restoreValue();
Expand Down
8 changes: 8 additions & 0 deletions src/formio.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ export interface FormioCustomComponentInfo extends BuilderInfo {

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

export interface FormioEvent {
eventName: string;
data?: {
[key: string]: any;
};
}

export interface FormioCustomComponent<T> {
value: T; // Should be an @Input
valueChange: EventEmitter<T>; // Should be an @Output
disabled: boolean;
formioEvent?: EventEmitter<FormioEvent>; // Should be an @Output
}