Skip to content
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

feat(file-upload): Adding onSelection event - FRONT-4589 #3596

Merged
merged 4 commits into from
Sep 2, 2024
Merged
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
49 changes: 49 additions & 0 deletions src/implementations/vanilla/components/file-upload/file-upload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { queryOne, formatBytes } from '@ecl/dom-utils';
import EventManager from '@ecl/event-manager';

/**
* @param {HTMLElement} element DOM element for component instantiation and scope
Expand Down Expand Up @@ -26,6 +27,18 @@ export class FileUpload {
return fileUpload;
}

/**
* @event FileUpload#onSelection
*/

/**
* An array of supported events for this component.
*
* @type {Array<string>}
* @memberof FileUpload
*/
supportedEvents = ['onSelection'];

constructor(
element,
{
Expand All @@ -45,6 +58,7 @@ export class FileUpload {
}

this.element = element;
this.eventManager = new EventManager();

// Options
this.groupSelector = groupSelector;
Expand Down Expand Up @@ -88,6 +102,36 @@ export class FileUpload {
ECL.components.set(this.element, this);
}

/**
* Register a callback function for a specific event.
*
* @param {string} eventName - The name of the event to listen for.
* @param {Function} callback - The callback function to be invoked when the event occurs.
* @returns {void}
* @memberof FileUpload
* @instance
*
* @example
* // Registering a callback for the 'onSelection' event
* fileUpload.on('onSelection', (event) => {
* console.log('Open event occurred!', event);
* });
*/
on(eventName, callback) {
this.eventManager.on(eventName, callback);
}

/**
* Trigger a component event.
*
* @param {string} eventName - The name of the event to trigger.
* @param {any} eventData - Data associated with the event.
* @memberof FileUpload
*/
trigger(eventName, eventData) {
this.eventManager.trigger(eventName, eventData);
}

/**
* Destroy component.
*/
Expand All @@ -103,6 +147,8 @@ export class FileUpload {

/**
* @param {Event} e
*
* @fires FileUpload#onSelection
*/
handleChange(e) {
if (!('files' in e.target)) {
Expand Down Expand Up @@ -135,6 +181,9 @@ export class FileUpload {
this.labelReplace,
);
}
// Trigger custom onSelection event
const eventDetails = { files: e.target.files, event: e };
this.trigger('onSelection', eventDetails);
}
}

Expand Down
Loading