Skip to content

Commit

Permalink
feat(file-upload): Adding onSelection event - FRONT-4589 (#3596)
Browse files Browse the repository at this point in the history
* feat(file-upload): Adding onSelection event - FRONT-4589

* feat(file-upload): Fixing API docs - FRONT-4589

---------

Co-authored-by: Romain Emery <[email protected]>
  • Loading branch information
planctus and emeryro committed Sep 6, 2024
1 parent 18cdd1b commit e9d76be
Showing 1 changed file with 49 additions and 0 deletions.
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

0 comments on commit e9d76be

Please sign in to comment.