Files
jellyfin-audio-player/src/components/TouchableHandler.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

import React, { PropsWithChildren, useCallback } from 'react';
import { Pressable, ViewStyle } from 'react-native';
2020-06-21 10:30:41 +02:00
interface TouchableHandlerProps<T = number> {
id: T;
onPress: (id: T) => void;
onLongPress?: (id: T) => void;
2022-06-15 11:45:38 +02:00
testID?: string;
}
function TouchableStyles({ pressed }: { pressed: boolean }): ViewStyle {
if (pressed) {
return { opacity: 0.5 };
} else {
return { opacity: 1 };
}
2020-06-21 10:30:41 +02:00
}
/**
* This is a generic handler that accepts id as a prop, and return it when it is
* pressed. This comes in handy with lists in which albums / tracks need to be selected.
*/
function TouchableHandler<T>({
id,
onPress,
onLongPress,
2022-06-15 11:45:38 +02:00
children,
testID,
}: PropsWithChildren<TouchableHandlerProps<T>>): JSX.Element {
2020-06-21 10:30:41 +02:00
const handlePress = useCallback(() => {
return onPress(id);
}, [id, onPress]);
2020-06-21 10:30:41 +02:00
const handleLongPress = useCallback(() => {
return onLongPress ? onLongPress(id) : undefined;
}, [id, onLongPress]);
2020-06-21 10:30:41 +02:00
return (
<Pressable
onPress={handlePress}
onLongPress={handleLongPress}
style={TouchableStyles}
2022-06-15 11:45:38 +02:00
testID={testID}
>
2020-06-21 10:30:41 +02:00
{children}
</Pressable>
2020-06-21 10:30:41 +02:00
);
}
2020-06-21 10:30:41 +02:00
export default TouchableHandler;