Skip to content

Commit 89f8dd9

Browse files
committed
added logic to fix runAfterInteraction issue
1 parent 5b0c535 commit 89f8dd9

13 files changed

+443
-328
lines changed

LabelContainer.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import React, { PureComponent } from 'react';
2-
import { View } from 'react-native';
1+
import React, {PureComponent, ReactNode} from 'react';
2+
import {View, ViewProps} from 'react-native';
33

4-
class LabelContainer extends PureComponent {
4+
type Props = ViewProps & {renderContent: (value: number) => ReactNode};
55

6+
type State = {
7+
value: number;
8+
};
9+
class LabelContainer extends PureComponent<Props, State> {
610
state = {
711
value: Number.NaN,
812
};
9-
10-
setValue = value => {
11-
this.setState({ value });
12-
}
13+
14+
setValue = (value: number) => {
15+
this.setState({value});
16+
};
1317

1418
render() {
15-
const { renderContent, ...restProps } = this.props;
16-
const { value } = this.state;
17-
return (
18-
<View {...restProps}>
19-
{renderContent(value)}
20-
</View>
21-
);
19+
const {renderContent, ...restProps} = this.props;
20+
const {value} = this.state;
21+
return <View {...restProps}>{renderContent(value)}</View>;
2222
}
2323
}
2424

File renamed without changes.

helpers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './helpers';
2+
export * from './pan-responder-factory';

helpers/pan-responder-factory.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { useRef } from 'react';
2+
import {
3+
GestureResponderEvent,
4+
PanResponder,
5+
PanResponderCallbacks,
6+
PanResponderGestureState,
7+
} from 'react-native';
8+
9+
const trueFunc = () => true;
10+
const falseFunc = () => false;
11+
12+
type Props = {
13+
onPanResponderMove: NonNullable<PanResponderCallbacks['onPanResponderMove']>;
14+
onPanResponderGrant: NonNullable<
15+
PanResponderCallbacks['onPanResponderGrant']
16+
>;
17+
onPanResponderRelease: NonNullable<
18+
PanResponderCallbacks['onPanResponderRelease']
19+
>;
20+
};
21+
22+
export class PanResponderFactory {
23+
// external
24+
private onPanResponderMove!: Props['onPanResponderMove'];
25+
private onPanResponderGrant!: Props['onPanResponderGrant'];
26+
private onPanResponderRelease!: Props['onPanResponderRelease'];
27+
28+
constructor(props: Props) {
29+
this.updateValues(props);
30+
}
31+
32+
public updateValues(props: Props) {
33+
this.onPanResponderMove = props.onPanResponderMove;
34+
this.onPanResponderGrant = props.onPanResponderGrant;
35+
this.onPanResponderRelease = props.onPanResponderRelease;
36+
}
37+
38+
public usePanResponder = () => {
39+
const panResponder = useRef(
40+
PanResponder.create({
41+
onMoveShouldSetPanResponderCapture: falseFunc,
42+
onPanResponderTerminationRequest: falseFunc,
43+
onStartShouldSetPanResponderCapture: trueFunc,
44+
onPanResponderTerminate: trueFunc,
45+
onShouldBlockNativeResponder: trueFunc,
46+
onMoveShouldSetPanResponder: (
47+
evt: GestureResponderEvent,
48+
gestureState: PanResponderGestureState,
49+
) => Math.abs(gestureState.dx) > 2 * Math.abs(gestureState.dy),
50+
onPanResponderGrant: (...args) => this.onPanResponderGrant(...args),
51+
onPanResponderMove: (...args) => this.onPanResponderMove(...args),
52+
onPanResponderRelease: (...args) => this.onPanResponderRelease(...args),
53+
}),
54+
);
55+
56+
return panResponder.current;
57+
};
58+
}

hooks.tsx

Lines changed: 0 additions & 207 deletions
This file was deleted.

hooks/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from "./use-label-container-props";
2+
export * from "./use-low-high";
3+
export * from "./use-selected-rail";
4+
export * from "./use-thumb-follower";
5+
export * from "./use-width-layout";

hooks/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface InProps {
2+
low: number;
3+
high: number;
4+
min: number;
5+
max: number;
6+
step: number;
7+
}

hooks/use-label-container-props.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useCallback, useState } from 'react';
2+
import { LayoutChangeEvent } from 'react-native';
3+
4+
import styles from '../styles';
5+
6+
/**
7+
* @param floating
8+
* @returns {{onLayout: ((function({nativeEvent: *}): void)|undefined), style: [*, {top}]}}
9+
*/
10+
export const useLabelContainerProps = (floating: boolean) => {
11+
const [labelContainerHeight, setLabelContainerHeight] = useState(0);
12+
const onLayout = useCallback(({ nativeEvent }: LayoutChangeEvent) => {
13+
const {
14+
layout: { height },
15+
} = nativeEvent;
16+
setLabelContainerHeight(height);
17+
}, []);
18+
19+
const top = floating ? -labelContainerHeight : 0;
20+
const style = [
21+
floating ? styles.labelFloatingContainer : styles.labelFixedContainer,
22+
{ top },
23+
];
24+
return { style, onLayout: onLayout };
25+
};

hooks/use-low-high.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useRef } from 'react';
2+
3+
import { clamp } from '../helpers';
4+
5+
/**
6+
* low and high state variables are fallbacks for props (props are not required).
7+
* This hook ensures that current low and high are not out of [min, max] range.
8+
* It returns an object which contains:
9+
* - ref containing correct low, high, min, max and step to work with.
10+
* - setLow and setHigh setters
11+
* @param lowProp
12+
* @param highProp
13+
* @param min
14+
* @param max
15+
* @param step
16+
* @returns {{inPropsRef: React.MutableRefObject<{high: (*|number), low: (*|number)}>, setLow: (function(number): undefined), setHigh: (function(number): undefined)}}
17+
*/
18+
export const useLowHigh = (
19+
lowProp: number | undefined,
20+
highProp: number | undefined,
21+
min: number,
22+
max: number,
23+
step: number,
24+
) => {
25+
const validLowProp = lowProp === undefined ? min : clamp(lowProp, min, max);
26+
const validHighProp =
27+
highProp === undefined ? max : clamp(highProp, min, max);
28+
const inPropsRef = useRef({
29+
low: validLowProp,
30+
high: validHighProp,
31+
step,
32+
// These 2 fields will be overwritten below.
33+
min: validLowProp,
34+
max: validHighProp,
35+
});
36+
const { low: lowState, high: highState } = inPropsRef.current;
37+
const inPropsRefPrev = { lowPrev: lowState, highPrev: highState };
38+
39+
// Props have higher priority.
40+
// If no props are passed, use internal state variables.
41+
const low = clamp(lowProp === undefined ? lowState : lowProp, min, max);
42+
const high = clamp(highProp === undefined ? highState : highProp, min, max);
43+
44+
// Always update values of refs so pan responder will have updated values
45+
Object.assign(inPropsRef.current, { low, high, min, max });
46+
47+
const setLow = (value: number) => (inPropsRef.current.low = value);
48+
const setHigh = (value: number) => (inPropsRef.current.high = value);
49+
return { inPropsRef, inPropsRefPrev, setLow, setHigh };
50+
};

0 commit comments

Comments
 (0)