All files / client/src/events wrapper.js

100% Statements 3/3
100% Branches 9/9
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                18x             1x       1x                    
/**
 * Event Wrapper for Coherent.js
 *
 * Wraps native DOM events with component context, providing handlers
 * access to component state, setState, and props.
 */
 
/**
 * @typedef {object} CoherentEvent
 * @property {Event} originalEvent - The native DOM event
 * @property {Element} target - The element with the data-coherent-* attribute
 * @property {function(): void} preventDefault - Delegates to originalEvent.preventDefault()
 * @property {function(): void} stopPropagation - Delegates to originalEvent.stopPropagation()
 * @property {function|null} component - The component function (if available)
 * @property {object|null} state - Current component state (if available)
 * @property {function|null} setState - State setter function (if available)
 * @property {object|null} props - Component props (if available)
 */
 
/**
 * Wrap a native DOM event with component context
 *
 * @param {Event} originalEvent - The native DOM event
 * @param {Element} target - The element that matched the data attribute selector
 * @param {object|null} componentRef - Optional component reference object
 * @param {function} [componentRef.component] - The component function
 * @param {object} [componentRef.state] - Current component state
 * @param {function} [componentRef.setState] - State setter function
 * @param {object} [componentRef.props] - Component props
 * @returns {CoherentEvent} Wrapped event with component context
 */
export function wrapEvent(originalEvent, target, componentRef = null) {
  return {
    // Native event access
    originalEvent,
    target,
 
    // Delegate common methods
    preventDefault() {
      originalEvent.preventDefault();
    },
 
    stopPropagation() {
      originalEvent.stopPropagation();
    },
 
    // Component context (null if no componentRef provided)
    component: componentRef?.component ?? null,
    state: componentRef?.state ?? null,
    setState: componentRef?.setState ?? null,
    props: componentRef?.props ?? null,
  };
}