fix(react): adding href prop when routerLink is specified (#19481)

This commit is contained in:
Ely Lucas
2019-09-27 14:28:10 -06:00
committed by GitHub
parent a02319b06b
commit cb26f73f80

View File

@ -17,7 +17,7 @@ interface IonicReactInternalProps<ElementType> extends React.HTMLAttributes<Elem
export const createReactComponent = <PropType, ElementType>( export const createReactComponent = <PropType, ElementType>(
tagName: string, tagName: string,
hrefComponent = false routerLinkComponent = false
) => { ) => {
const displayName = dashToPascalCase(tagName); const displayName = dashToPascalCase(tagName);
const ReactComponent = class extends React.Component<IonicReactInternalProps<PropType>> { const ReactComponent = class extends React.Component<IonicReactInternalProps<PropType>> {
@ -41,7 +41,7 @@ export const createReactComponent = <PropType, ElementType>(
attachEventProps(node, this.props, prevProps); attachEventProps(node, this.props, prevProps);
} }
private handleClick = (e: MouseEvent) => { private handleClick = (e: React.MouseEvent<PropType>) => {
const { routerLink, routerDirection } = this.props; const { routerLink, routerDirection } = this.props;
if (routerLink !== undefined) { if (routerLink !== undefined) {
e.preventDefault(); e.preventDefault();
@ -62,16 +62,19 @@ export const createReactComponent = <PropType, ElementType>(
return acc; return acc;
}, {}); }, {});
const newProps: any = { const newProps: IonicReactInternalProps<PropType> = {
...propsToPass, ...propsToPass,
ref: forwardedRef, ref: forwardedRef,
style style
}; };
if (hrefComponent) { if (routerLinkComponent) {
if (this.props.routerLink && !this.props.href) {
newProps.href = this.props.routerLink;
}
if (newProps.onClick) { if (newProps.onClick) {
const oldClick = newProps.onClick; const oldClick = newProps.onClick;
newProps.onClick = (e: MouseEvent) => { newProps.onClick = (e: React.MouseEvent<PropType>) => {
oldClick(e); oldClick(e);
if (!e.defaultPrevented) { if (!e.defaultPrevented) {
this.handleClick(e); this.handleClick(e);