refactor: remove onLink prop for now

This commit is contained in:
Satyajit Sahoo
2020-04-27 01:44:53 +02:00
parent 38020de80b
commit b747e527a4
3 changed files with 37 additions and 21 deletions

View File

@@ -119,6 +119,7 @@ export default function BottomTabBarItem({
style,
onPress,
href,
accessibilityRole,
...rest
}: BottomTabBarButtonProps) => {
if (Platform.OS === 'web' && href) {
@@ -127,18 +128,28 @@ export default function BottomTabBarItem({
return (
<Link
{...rest}
// @ts-ignore
accessibilityRole="link"
onLink={onPress}
to={href}
style={[styles.button, style]}
onPress={(e: any) => {
if (
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
(e.button == null || e.button === 0) // ignore everything but left clicks
) {
e.preventDefault();
onPress?.(e);
}
}}
>
{children}
</Link>
);
} else {
return (
<TouchableWithoutFeedback {...rest} onPress={onPress}>
<TouchableWithoutFeedback
{...rest}
accessibilityRole={accessibilityRole}
onPress={onPress}
>
<View style={style}>{children}</View>
</TouchableWithoutFeedback>
);

View File

@@ -71,6 +71,7 @@ const Touchable = ({
style,
onPress,
href,
accessibilityRole,
delayPressIn,
...rest
}: TouchableWithoutFeedbackProps & {
@@ -84,18 +85,29 @@ const Touchable = ({
return (
<Link
{...rest}
// @ts-ignore
accessibilityRole="link"
onLink={onPress}
to={href}
style={[styles.button, style]}
onPress={(e: any) => {
if (
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
(e.button == null || e.button === 0) // ignore everything but left clicks
) {
e.preventDefault();
onPress?.(e);
}
}}
>
{children}
</Link>
);
} else {
return (
<TouchableItem {...rest} delayPressIn={delayPressIn} onPress={onPress}>
<TouchableItem
{...rest}
accessibilityRole={accessibilityRole}
delayPressIn={delayPressIn}
onPress={onPress}
>
<View style={style}>{children}</View>
</TouchableItem>
);

View File

@@ -5,12 +5,9 @@ import useLinkTo from './useLinkTo';
type Props = {
to: string;
target?: string;
onLink?: (
e: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent
) => void;
} & (TextProps & { children: React.ReactNode });
export default function Link({ to, children, onLink, ...rest }: Props) {
export default function Link({ to, children, ...rest }: Props) {
const linkTo = useLinkTo();
const onPress = (
@@ -24,13 +21,13 @@ export default function Link({ to, children, onLink, ...rest }: Props) {
let shouldHandle = false;
if (Platform.OS !== 'web' || !event) {
shouldHandle = event ? !event.defaultPrevented : true;
shouldHandle = event ? !e.defaultPrevented : true;
} else if (
!event.defaultPrevented && // onPress prevented default
!e.defaultPrevented && // onPress prevented default
// @ts-ignore
!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) && // ignore clicks with modifier keys
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
// @ts-ignore
(event.button == null || event.button === 0) && // ignore everything but left clicks
(e.button == null || e.button === 0) && // ignore everything but left clicks
(rest.target == null || rest.target === '_self') // let browser handle "target=_blank" etc.
) {
event.preventDefault();
@@ -38,11 +35,7 @@ export default function Link({ to, children, onLink, ...rest }: Props) {
}
if (shouldHandle) {
if (onLink) {
onLink(e);
} else {
linkTo(to);
}
linkTo(to);
}
};