2021-12-31 15:04:37 +01:00
|
|
|
import React, { PropsWithChildren, useCallback } from 'react';
|
2020-08-28 16:28:49 +02:00
|
|
|
import { Pressable, ViewStyle } from 'react-native';
|
2020-06-21 10:30:41 +02:00
|
|
|
|
2021-12-31 15:04:37 +01: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;
|
2020-08-28 16:28:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2021-12-31 15:04:37 +01:00
|
|
|
function TouchableHandler<T>({
|
|
|
|
|
id,
|
|
|
|
|
onPress,
|
|
|
|
|
onLongPress,
|
2022-06-15 11:45:38 +02:00
|
|
|
children,
|
|
|
|
|
testID,
|
2021-12-31 15:04:37 +01:00
|
|
|
}: PropsWithChildren<TouchableHandlerProps<T>>): JSX.Element {
|
2020-06-21 10:30:41 +02:00
|
|
|
const handlePress = useCallback(() => {
|
|
|
|
|
return onPress(id);
|
2020-07-07 13:33:08 +02:00
|
|
|
}, [id, onPress]);
|
2020-06-21 10:30:41 +02:00
|
|
|
|
2020-08-28 16:28:49 +02:00
|
|
|
const handleLongPress = useCallback(() => {
|
|
|
|
|
return onLongPress ? onLongPress(id) : undefined;
|
|
|
|
|
}, [id, onLongPress]);
|
|
|
|
|
|
2020-06-21 10:30:41 +02:00
|
|
|
return (
|
2020-08-28 16:28:49 +02:00
|
|
|
<Pressable
|
|
|
|
|
onPress={handlePress}
|
|
|
|
|
onLongPress={handleLongPress}
|
|
|
|
|
style={TouchableStyles}
|
2022-06-15 11:45:38 +02:00
|
|
|
testID={testID}
|
2020-08-28 16:28:49 +02:00
|
|
|
>
|
2020-06-21 10:30:41 +02:00
|
|
|
{children}
|
2020-08-28 16:28:49 +02:00
|
|
|
</Pressable>
|
2020-06-21 10:30:41 +02:00
|
|
|
);
|
2021-12-31 15:04:37 +01:00
|
|
|
}
|
2020-06-21 10:30:41 +02:00
|
|
|
|
|
|
|
|
export default TouchableHandler;
|