import type * as React from 'react'; import type { AnyFunction } from '@vkontakte/vkjs'; import type { PickOnlyFunctionProps } from '../types'; const isFunctionExistInProps = >( props: Props, key: PropertyKey, ): key is Extract => typeof props[key] === 'function'; /** * Полезен, когда нужно сохранить пользовательские события. * * Приоритет даём пользовательскому событию. Например, можно будет отловить был ли вызван * `event.preventDefault()` через `event.defaultPrevented`. * * @private */ export const getMergedSameEventsByProps = < T extends keyof React.JSX.IntrinsicElements | React.JSXElementConstructor, MainProps extends React.ComponentProps, OnlyFnPropsByMain extends PickOnlyFunctionProps, >( mainProps: MainProps, secondProps: React.ComponentProps, ): { [K in keyof OnlyFnPropsByMain]?: | ((this: any, ...args: Parameters) => void) | undefined; } => { const result: { [K in keyof OnlyFnPropsByMain]?: | ((this: any, ...args: Parameters) => void) | undefined; } = {}; for (const eventName in mainProps) { if ( mainProps.hasOwnProperty(eventName) && secondProps.hasOwnProperty(eventName) && isFunctionExistInProps(mainProps, eventName) && isFunctionExistInProps(secondProps, eventName) ) { result[eventName] = function mergeSameEventsByProps( ...args: Parameters ) { secondProps[eventName].apply(this, args); mainProps[eventName].apply(this, args); }; } } return result; };