@modern-kit/react
Version:
44 lines (41 loc) • 1 kB
JavaScript
import { isFunction } from '@modern-kit/utils';
import React from 'react';
const EventExtender = ({
children,
capture,
shouldAwait = false,
beforeEvent,
afterEvent
}) => {
const child = React.Children.only(children);
const asyncEvent = async (eventType) => {
if (beforeEvent) {
await beforeEvent(eventType);
}
const originEvent = child.props[capture];
if (isFunction(originEvent)) {
await originEvent(eventType);
}
if (afterEvent) {
await afterEvent(eventType);
}
};
const syncEvent = (eventType) => {
if (beforeEvent) {
beforeEvent(eventType);
}
const originEvent = child.props[capture];
if (isFunction(originEvent)) {
originEvent(eventType);
}
if (afterEvent) {
afterEvent(eventType);
}
};
const enhancedProps = {
[capture]: shouldAwait ? asyncEvent : syncEvent
};
return React.cloneElement(child, enhancedProps);
};
export { EventExtender };
//# sourceMappingURL=index.mjs.map