@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
33 lines (32 loc) • 1.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeCallback = void 0;
/**
* Merge callbacks by returning a function that calls both functions. This is useful for components
* that need to provide callback functionality, but want to allow additional functionality to be
* provided
* @example
* const MyComponent = ({ myProp, ...elemProps }) => {
* return (
* <button onClick={mergeCallback(cb, elemProps.onClick)}>Click Me</button>
* )
* }
* @param elemProps Props passed into a component
* @param componentProps Props the component provides
*/
function mergeCallback(callback1, callback2) {
if (callback2) {
return ((...args) => {
const result = callback1.apply(null, args);
// Similar to if a prop returns `null`, returning `null` from a callback prevents further
// callbacks from being fired.
if (result !== null) {
callback2.apply(null, args);
}
});
}
else {
return callback1;
}
}
exports.mergeCallback = mergeCallback;
;