framework7 icon indicating copy to clipboard operation
framework7 copied to clipboard

Typescript event.detail

Open jdspugh opened this issue 7 years ago • 10 comments

  • Framework7 version: 3.4.2
  • Platform and Target: All

Describe the bug

Typescript typing for event.detail is missing.

To Reproduce

Add some code that uses event.detail

Dom7(document).on("page:init", '.page[data-name="exercise-edit"]', function( event: Event ) { let page = event.detail;

Expected behavior

No error.

Actual Behavior

Type error.

jdspugh avatar Oct 23 '18 08:10 jdspugh

@JasonKleban do you have idea how can we add it?

nolimits4web avatar Oct 23 '18 11:10 nolimits4web

Seems the PageData type is also missing and needs to be defined. So I'm using this until a deeper solution is found:

Dom7(document).on("page:init", '.page[data-name="exercise-edit"]', function(
  event: Event
) {
  let page = (<Event & {detail:PageData}>event).detail;
  ...

import { View } from "framework7/components/view/view";
import { Dom7 } from "dom7";
interface PageData {
  app?: object;
  view?: View.View;
  router?: object;
  name?: string;
  el: HTMLElement;
  $el: Dom7;
  navbarEl: HTMLElement;
  $navbarEl: Dom7;
  from?: string;
  to?: string;
  position: string;
  direction: string;
  route: {
    url: string;
    path: string;
    query: object;
    params: object;
    name: string;
    hash: object;
    route: object;
    context: object;
  }
  pageFrom: object;
  context?: object;
}

jdspugh avatar Nov 01 '18 05:11 jdspugh

Ok I found another solution for this. Using the "declare global" will allow us to extract the "detail" parameter from all Events. But is this what we want? Cause not all events will have the "detail" parameter available.

declare global {
  interface Event {
    detail: PageData;
  }
}
import { View } from "framework7/components/view/view";
import { Dom7 } from "dom7";
interface PageData {
  app?: object;
  view?: View.View;
  router?: object;
  name?: string;
  el: HTMLElement;
  $el: Dom7;
  navbarEl: HTMLElement;
  $navbarEl: Dom7;
  from?: string;
  to?: string;
  position: string;
  direction: string;
  route: {
    url: string;
    path: string;
    query: object;
    params: object;
    name: string;
    hash: object;
    route: object;
    context: object;
  };
  pageFrom: object;
  context?: object;
}

jdspugh avatar Nov 01 '18 07:11 jdspugh

Hey @jdspugh. Page Data definition is available in Router module: https://github.com/framework7io/framework7/blob/master/src/core/modules/router/router.d.ts#L140

import { Router } from 'framework7/modules/router/router';

// Router.Page -> is what you are looking for

nolimits4web avatar Nov 01 '18 08:11 nolimits4web

@nolimits4web Great, thanks. So this code does the trick to be able to access event.detail without any type errors:

import { Router } from 'node_modules/framework7/modules/router/router';
declare global {
  interface Event {
    detail: Router.Page;
  }
}

jdspugh avatar Nov 01 '18 08:11 jdspugh

Yeah, but the issue is that event.detail not always contains Page Data, like in case Sortable events. So still looking for a solution how to handle this correctly

nolimits4web avatar Nov 01 '18 09:11 nolimits4web

Sorry I haven't had time to dig into this.... would it work if overloads for event methods knows that handlers for page events get Page Data and Sortable events get sortable-related data?

  on(eventName : 'page:init', handler : (event : { detail: ... }) => void) : void
...
  on(eventName : 'textarea:resize', handler : ...) : void
  on(event : string, handler : () => void) : void

Know what I mean?

JasonKleban avatar Nov 03 '18 21:11 JasonKleban

That's the way I imagined it would be done.

jdspugh avatar Nov 06 '18 02:11 jdspugh

I don’t use those features of F7 so I’m not in a good position to judge the ergonomics of an implementation, but I could comment or answer questions about how to solve certain technical problems about it.

JasonKleban avatar Nov 06 '18 02:11 JasonKleban

@JasonKleban yeah, but how can we specify it? I mean, for example, we have DomEvents in router.d.ts and others but we don't use them anywhere. I guess we need somehow make them extend Event type?

nolimits4web avatar Nov 06 '18 08:11 nolimits4web