Skip to content

Instantly share code, notes, and snippets.

@JapuDCret
Last active May 31, 2019 14:25
Show Gist options
  • Save JapuDCret/d327a7c1c8f8c72f548f7476d8dfb6a2 to your computer and use it in GitHub Desktop.
Save JapuDCret/d327a7c1c8f8c72f548f7476d8dfb6a2 to your computer and use it in GitHub Desktop.
import * as React from 'react';
type OuterProps<ParamType> =
{
params: ParamType;
doLongPress(params: ParamType): Promise<void>;
doSingleClick(params: ParamType): Promise<void>;
selected: boolean;
longPressTime: number;
onlyLongPress: boolean;
}
type InnerProps =
{
handleButtonPress(e: React.SyntheticEvent): void;
handleButtonRelease(e: React.SyntheticEvent): void;
}
export type InternalLongPressProps<ParamType, OriginalProps> =
OriginalProps &
OuterProps<ParamType>;
export type LongPressProps<ParamType, OriginalProps> =
InternalLongPressProps<ParamType, OriginalProps> &
Partial<InnerProps>;
export const withLongPress = <ParamType, OriginalProps extends {}>(Component: React.ComponentType<InternalLongPressProps<ParamType, OriginalProps>>):
React.ComponentType<LongPressProps<ParamType, OriginalProps>> =>
class WithLongPress extends React.Component<LongPressProps<ParamType, OriginalProps>> {
private buttonPressTimer?: number;
public render() {
return (
<Component
{...this.props}
handleButtonPress={this.handleButtonPress}
handleButtonRelease={this.handleButtonRelease}
/>
);
}
private handleButtonPress = this.$handleButtonPress.bind(this);
private $handleButtonPress(e: React.SyntheticEvent): void {
//...
}
private handleButtonRelease = this.$handleButtonRelease.bind(this);
private $handleButtonRelease(e: React.SyntheticEvent): void {
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment