@carbon/react
Version:
React components for the Carbon Design System
29 lines (26 loc) • 826 B
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Composes multiple event handler functions into a single event handler. The
* composed handler calls each provided function sequentially with the event and
* any additional arguments. If any handler calls `event.preventDefault()`,
* further handlers are skipped.
*
* @param handlers - An array of event handler functions.
* @returns A composite event handler.
*/
const composeEventHandlers = handlers => (event, ...args) => {
for (const handler of handlers) {
if (event.defaultPrevented) {
break;
}
if (typeof handler === 'function') {
handler(event, ...args);
}
}
};
export { composeEventHandlers };