Skip to content

Commit c79e74b

Browse files
authored
fix(react): fix types for new stencil
1 parent cf223e4 commit c79e74b

File tree

9 files changed

+47
-23
lines changed

9 files changed

+47
-23
lines changed

packages/react/CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ Render first route even if url is same, fixes [#19392](https://github.com/ionic-
1010

1111
## Breaking Changes
1212

13+
### Events have been updated to use proper types from React
14+
15+
The types for events (such as `onClick`) were not typed correctly prior to RC3. Before, they were the normal browser events, but now they are the proper React Synthetic events. Therefore, you might have type errors that need to be remedied:
16+
17+
```typescript
18+
function handleClick(e: MouseEvent) {
19+
...
20+
}
21+
```
22+
23+
Will need to become:
24+
25+
```typescript
26+
function handleClick(e: React.MouseEvent) {
27+
...
28+
}
29+
```
30+
31+
Some Ionic components have the option to pass the event to them (like `IonPopover`). For these, you can access the `nativeEvent` property on the React synthetic event.
32+
1333
### Components with href attributes and the new routerLink prop
1434
As of RC3, components that use the href prop (`IonButton`, `IonItem`, etc..), no longer run these links through the router. As a result, page transitions are no longer applied to these links. To maintain page transitions, use the new `routerLink` prop instead. The href prop should be used when you want to enforce a full browser transition on the page, or when you need to link to an external resource.
1535

packages/react/src/components/IonPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React from 'react';
22

33
import { NavContext } from '../contexts/NavContext';
44

5-
import { ReactProps } from './ReactProps';
5+
import { IonicReactProps } from './IonicReactProps';
66

7-
export const IonPage = /*@__PURE__*/(() => class IonPageInternal extends React.Component<React.HTMLAttributes<HTMLElement> & ReactProps> {
7+
export const IonPage = /*@__PURE__*/(() => class IonPageInternal extends React.Component<React.HTMLAttributes<HTMLElement> & IonicReactProps> {
88
context!: React.ContextType<typeof NavContext>;
99
ref = React.createRef<HTMLDivElement>();
1010

packages/react/src/components/IonRouterOutlet.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react';
44

55
import { NavContext } from '../contexts/NavContext';
66

7-
import { ReactProps } from './ReactProps';
7+
import { IonicReactProps } from './IonicReactProps';
88
import { IonRouterOutletInner } from './inner-proxies';
99
import { createForwardRef } from './utils';
1010

@@ -43,4 +43,4 @@ const IonRouterOutletContainer = /*@__PURE__*/(() => class extends React.Compone
4343
}
4444
})();
4545

46-
export const IonRouterOutlet = createForwardRef<Props & ReactProps, HTMLIonRouterOutletElement>(IonRouterOutletContainer, 'IonRouterOutlet');
46+
export const IonRouterOutlet = createForwardRef<Props & IonicReactProps, HTMLIonRouterOutletElement>(IonRouterOutletContainer, 'IonRouterOutlet');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export interface IonicReactProps {
3+
class?: string;
4+
style?: {[key: string]: any };
5+
}

packages/react/src/components/ReactProps.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/react/src/components/createComponent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import ReactDom from 'react-dom';
33

44
import { NavContext } from '../contexts/NavContext';
55

6-
import { ReactProps } from './ReactProps';
76
import { RouterDirection } from './hrefprops';
87
import { attachEventProps, createForwardRef, dashToPascalCase, isCoveredByReact } from './utils';
98
import { deprecationWarning } from './utils/dev';
@@ -98,5 +97,5 @@ export const createReactComponent = <PropType, ElementType>(
9897
return NavContext;
9998
}
10099
};
101-
return createForwardRef<PropType & ReactProps, ElementType>(ReactComponent, displayName);
100+
return createForwardRef<PropType, ElementType>(ReactComponent, displayName);
102101
};

packages/react/src/components/navigation/IonBackButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { JSX as LocalJSX } from '@ionic/core';
22
import React from 'react';
33

44
import { NavContext } from '../../contexts/NavContext';
5-
import { ReactProps } from '../ReactProps';
5+
import { IonicReactProps } from '../IonicReactProps';
66
import { IonBackButtonInner } from '../inner-proxies';
77

8-
type Props = LocalJSX.IonBackButton & ReactProps & {
8+
type Props = LocalJSX.IonBackButton & IonicReactProps & {
99
ref?: React.RefObject<HTMLIonBackButtonElement>;
1010
};
1111

packages/react/src/components/navigation/IonTabBar.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { IonTabBarInner } from '../inner-proxies';
66
import { IonTabButton } from '../proxies';
77

88
type Props = LocalJSX.IonTabBar & {
9-
ref?: React.RefObject<HTMLIonTabBarElement>;
10-
navigate: (path: string, direction: 'back' | 'none') => void;
11-
currentPath: string;
12-
children?: React.ReactNode;
9+
navigate?: (path: string, direction: 'back' | 'none') => void;
10+
currentPath?: string;
11+
slot?: 'bottom' | 'top';
1312
};
1413

1514
interface Tab {
@@ -48,7 +47,7 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
4847
const activeTab = Object.keys(state.tabs)
4948
.find(key => {
5049
const href = state.tabs[key].originalHref;
51-
return props.currentPath.startsWith(href);
50+
return props.currentPath!.startsWith(href);
5251
});
5352

5453
if (activeTab === undefined || (activeTab === state.activeTab && state.tabs[activeTab].currentHref === props.currentPath)) {
@@ -68,10 +67,13 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
6867
}
6968

7069
private onTabButtonClick = (e: CustomEvent<{ href: string, selected: boolean, tab: string }>) => {
71-
if (this.state.activeTab === e.detail.tab) {
72-
this.props.navigate(this.state.tabs[e.detail.tab].originalHref, 'back');
73-
} else {
74-
this.props.navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
70+
const { navigate } = this.props;
71+
if (navigate) {
72+
if (this.state.activeTab === e.detail.tab) {
73+
navigate(this.state.tabs[e.detail.tab].originalHref, 'back');
74+
} else {
75+
navigate(this.state.tabs[e.detail.tab].currentHref, 'none');
76+
}
7577
}
7678
}
7779

@@ -96,7 +98,7 @@ const IonTabBarUnwrapped = /*@__PURE__*/(() => class extends React.Component<Pro
9698
}
9799
})();
98100

99-
export const IonTabBar: React.FC<LocalJSX.IonTabBar & { currentPath?: string, navigate?: (path: string) => void; }> = props => {
101+
export const IonTabBar: React.FC<Props> = props => {
100102
const context = useContext(NavContext);
101103
return (
102104
<IonTabBarUnwrapped

packages/react/src/components/utils/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Platforms, getPlatforms as getPlatformsCore, isPlatform as isPlatformCore } from '@ionic/core';
22
import React from 'react';
33

4-
export type IonicReactExternalProps<PropType, ElementType> = PropType & React.HTMLAttributes<ElementType>;
4+
import { IonicReactProps } from '../IonicReactProps';
5+
6+
export type IonicReactExternalProps<PropType, ElementType> = PropType & Omit<React.HTMLAttributes<ElementType>, 'style'> & IonicReactProps;
57

68
export const createForwardRef = <PropType, ElementType>(ReactComponent: any, displayName: string) => {
79
const forwardRef = (props: IonicReactExternalProps<PropType, ElementType>, ref: React.Ref<ElementType>) => {

0 commit comments

Comments
 (0)