Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate events being fired in ionic/react #17321

Merged
merged 7 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion react/src/components/IonModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Components } from '@ionic/core';
import { createOverlayComponent } from './createOverlayComponent';
import { Omit } from './types';
import { Omit } from '../types';

export type ModalOptions = Omit<Components.IonModalAttributes, 'component' | 'componentProps'> & {
children: React.ReactNode;
Expand Down
2 changes: 1 addition & 1 deletion react/src/components/IonPopover.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Components } from '@ionic/core';
import { createOverlayComponent } from './createOverlayComponent';
import { Omit } from './types';
import { Omit } from '../types';

export type PopoverOptions = Omit<Components.IonPopoverAttributes, 'component' | 'componentProps'> & {
children: React.ReactNode;
Expand Down
2 changes: 1 addition & 1 deletion react/src/components/createControllerComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { attachEventProps } from './utils'
import { ensureElementInBody, dashToPascalCase } from './utils';
import { OverlayComponentElement, OverlayControllerComponentElement } from './types';
import { OverlayComponentElement, OverlayControllerComponentElement } from '../types';

export function createControllerComponent<T extends object, E extends OverlayComponentElement, C extends OverlayControllerComponentElement<E>>(tagName: string, controllerTagName: string) {
const displayName = dashToPascalCase(tagName);
Expand Down
2 changes: 1 addition & 1 deletion react/src/components/createOverlayComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { attachEventProps } from './utils'
import { ensureElementInBody, dashToPascalCase } from './utils';
import { OverlayComponentElement, OverlayControllerComponentElement } from './types';
import { OverlayComponentElement, OverlayControllerComponentElement } from '../types';

export function createOverlayComponent<T extends object, E extends OverlayComponentElement, C extends OverlayControllerComponentElement<E>>(tagName: string, controllerTagName: string) {
const displayName = dashToPascalCase(tagName);
Expand Down
6 changes: 5 additions & 1 deletion react/src/components/navigation/IonTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class IonTabBar extends Component<Props, State> {


onTabButtonClick = (e: CustomEvent<{ href: string, selected: boolean, tab: string }>) => {
this.props.history.push(e.detail.href);
const targetUrl = (this.state.activeTab === e.detail.tab) ?
this.state.tabs[e.detail.tab].originalHref :
this.state.tabs[e.detail.tab].currentHref;

this.props.history.push(targetUrl);
}

renderChild = (activeTab: string) => (child: React.ReactElement<Components.IonTabButtonAttributes & { onIonTabButtonClick: (e: CustomEvent) => void }>) => {
Expand Down
30 changes: 25 additions & 5 deletions react/src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
/**
* Checks if an event is supported in the current execution environment.
* @license Modernizr 3.0.0pre (Custom Build) | MIT
*/
function isCoveredByReact(eventNameSuffix: string) {
const eventName = 'on' + eventNameSuffix;
let isSupported = eventName in document;

if (!isSupported) {
const element = document.createElement('div');
element.setAttribute(eventName, 'return;');
isSupported = typeof (<any>element)[eventName] === 'function';
}

return isSupported;
}

function syncEvent(node: Element, eventName: string, newEventHandler: (e: Event) => any) {
const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1);
const eventStore = (node as any).__events || ((node as any).__events = {});
const oldEventHandler = eventStore[eventNameLc];
const oldEventHandler = eventStore[eventName];

// Remove old listener so they don't double up.
if (oldEventHandler) {
node.removeEventListener(eventNameLc, oldEventHandler);
node.removeEventListener(eventName, oldEventHandler);
}

// Bind new listener.
if (newEventHandler) {
node.addEventListener(eventNameLc, eventStore[eventNameLc] = function handler(e: Event) {
node.addEventListener(eventName, eventStore[eventName] = function handler(e: Event) {
newEventHandler.call(this, e);
});
}
Expand All @@ -35,7 +50,12 @@ export function attachEventProps<E extends HTMLElement>(node: E, props: any) {
}

if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) {
syncEvent(node, name.substring(2), props[name]);
const eventName = name.substring(2);
const eventNameLc = eventName[0].toLowerCase() + eventName.substring(1);

if (!isCoveredByReact(eventNameLc)) {
syncEvent(node, eventNameLc, props[name]);
}
} else {
(node as any)[name] = props[name];
}
Expand Down
25 changes: 2 additions & 23 deletions react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import { addIcons } from 'ionicons';
import { ICON_PATHS } from 'ionicons/icons';
import { IonicConfig } from '@ionic/core';
import { defineCustomElements } from '@ionic/core/loader';

export * from './components';

export interface IonicGlobal {
config?: any;
ael?: (elm: any, eventName: string, cb: (ev: Event) => void, opts: any) => void;
raf?: (ts: number) => void;
rel?: (elm: any, eventName: string, cb: (ev: Event) => void, opts: any) => void;
}

export interface IonicWindow extends Window {
Ionic: IonicGlobal;
}

export function registerIonic(config: IonicConfig = {}) {
const win: IonicWindow = window as any;
const Ionic = (win.Ionic = win.Ionic || {});
addIcons(ICON_PATHS);
export * from './types';

Ionic.config = config;
defineCustomElements(window);
}
export * from './register';
14 changes: 14 additions & 0 deletions react/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { addIcons } from 'ionicons';
import { ICON_PATHS } from 'ionicons/icons';
import { IonicConfig } from '@ionic/core';
import { defineCustomElements } from '@ionic/core/loader';
import { IonicWindow } from './types';

export function registerIonic(config: IonicConfig = {}) {
const win: IonicWindow = window as any;
const Ionic = (win.Ionic = win.Ionic || {});
addIcons(ICON_PATHS);

Ionic.config = config;
defineCustomElements(window);
}
12 changes: 12 additions & 0 deletions react/src/components/types.ts → react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ export interface OverlayComponentElement extends HTMLStencilElement {
'present': () => Promise<void>;
'dismiss': (data?: any, role?: string | undefined) => Promise<boolean>;
}

export interface OverlayControllerComponentElement<E extends OverlayComponentElement> extends HTMLStencilElement {
'create': (opts: any) => Promise<E>;
}

export interface IonicGlobal {
config?: any;
ael?: (elm: any, eventName: string, cb: (ev: Event) => void, opts: any) => void;
raf?: (ts: number) => void;
rel?: (elm: any, eventName: string, cb: (ev: Event) => void, opts: any) => void;
}

export interface IonicWindow extends Window {
Ionic: IonicGlobal;
}