Skip to content

Commit b5fefd2

Browse files
M-i-k-e-lethanshar
andauthored
Typescript/tab controller move to function component (#1098)
* Move TabBar to functional component * Fix Fader in RTL * Fader - add START and END (deprecate LEFT and RIGHT) * Update src/components/tabController/TabBar.tsx Simplify the ignore Co-authored-by: Ethan Sharabi <[email protected]> * Rename getSnapBreakpoints to snapBreakpoints * Remove _.memoize Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 8f20379 commit b5fefd2

File tree

6 files changed

+316
-320
lines changed

6 files changed

+316
-320
lines changed

demo/src/screens/componentScreens/FaderScreen.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {StyleSheet, ScrollView} from 'react-native';
4-
import {
5-
Colors,
6-
Text,
7-
View,
8-
Fader,
9-
withScrollReached,
10-
WithScrollReachedProps
11-
} from 'react-native-ui-lib';
4+
import {Colors, Text, View, Fader, withScrollReached, WithScrollReachedProps} from 'react-native-ui-lib';
125
import {renderHeader} from '../ExampleScreenPresenter';
136

147
const numberOfItems = 3;
@@ -17,9 +10,7 @@ const itemWidth = 100;
1710
const itemHeight = 100;
1811
const tintColor = undefined;
1912

20-
const horizontal =
21-
faderPosition === Fader.position.LEFT ||
22-
faderPosition === Fader.position.RIGHT;
13+
const horizontal = faderPosition === Fader.position.START || faderPosition === Fader.position.END;
2314

2415
class FaderScreen extends Component<WithScrollReachedProps> {
2516
renderItem = (index: number) => {
@@ -33,8 +24,7 @@ class FaderScreen extends Component<WithScrollReachedProps> {
3324
render() {
3425
const {scrollReachedProps} = this.props;
3526
const visible =
36-
faderPosition === Fader.position.BOTTOM ||
37-
faderPosition === Fader.position.RIGHT
27+
faderPosition === Fader.position.BOTTOM || faderPosition === Fader.position.END
3828
? !scrollReachedProps.isScrollAtEnd
3929
: !scrollReachedProps.isScrollAtStart;
4030

@@ -53,11 +43,7 @@ class FaderScreen extends Component<WithScrollReachedProps> {
5343
>
5444
{_.times(numberOfItems, this.renderItem)}
5545
</ScrollView>
56-
<Fader
57-
visible={visible}
58-
position={faderPosition}
59-
tintColor={tintColor}
60-
/>
46+
<Fader visible={visible} position={faderPosition} tintColor={tintColor}/>
6147
</View>
6248
</View>
6349
</View>

generatedTypes/components/fader/index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/// <reference types="react" />
22
export declare enum FaderPosition {
3+
/**
4+
* @deprecated please use START instead
5+
*/
36
LEFT = "LEFT",
7+
START = "START",
8+
/**
9+
* @deprecated please use END instead
10+
*/
411
RIGHT = "RIGHT",
12+
END = "END",
513
TOP = "TOP",
614
BOTTOM = "BOTTOM"
715
}
@@ -11,7 +19,7 @@ export declare type FaderProps = {
1119
*/
1220
visible?: boolean;
1321
/**
14-
* The position of the fader (the image is different), defaults to Fader.position.RIGHT
22+
* The position of the fader (the image is different), defaults to Fader.position.END
1523
*/
1624
position?: FaderPosition;
1725
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { ViewProps, ScrollViewProps } from 'react-native';
3+
export declare type FadedScrollViewProps = ViewProps & ScrollViewProps & {
4+
children?: React.ReactNode | React.ReactNode[];
5+
};
6+
declare const _default: React.ComponentClass<FadedScrollViewProps, any> | React.FunctionComponent<FadedScrollViewProps>;
7+
export default _default;

src/components/fader/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ import View from '../view';
55
import Image from '../image';
66

77
export enum FaderPosition {
8+
/**
9+
* @deprecated please use START instead
10+
*/
811
LEFT = 'LEFT',
12+
START = 'START',
13+
/**
14+
* @deprecated please use END instead
15+
*/
916
RIGHT = 'RIGHT',
17+
END = 'END',
1018
TOP = 'TOP',
1119
BOTTOM = 'BOTTOM'
1220
}
@@ -17,7 +25,7 @@ export type FaderProps = {
1725
*/
1826
visible?: boolean;
1927
/**
20-
* The position of the fader (the image is different), defaults to Fader.position.RIGHT
28+
* The position of the fader (the image is different), defaults to Fader.position.END
2129
*/
2230
position?: FaderPosition;
2331
/**
@@ -39,15 +47,17 @@ function Fader(props: FaderProps) {
3947

4048
const fadeSize = getFadeSize();
4149
const getStyles = useCallback(() => {
42-
const position = props.position || FaderPosition.RIGHT;
50+
const position = props.position || FaderPosition.END;
4351
let containerStyle, imageStyle, imageSource;
4452
switch (position) {
4553
case FaderPosition.LEFT:
54+
case FaderPosition.START:
4655
containerStyle = {...staticStyles.containerLeft, width: fadeSize};
4756
imageStyle = {height: '100%', width: fadeSize};
4857
imageSource = require('./gradientLeft.png');
4958
break;
5059
case FaderPosition.RIGHT:
60+
case FaderPosition.END:
5161
containerStyle = {...staticStyles.containerRight, width: fadeSize};
5262
imageStyle = {height: '100%', width: fadeSize};
5363
imageSource = require('./gradientRight.png');
@@ -80,6 +90,7 @@ function Fader(props: FaderProps) {
8090
<View pointerEvents={'none'} style={styles.containerStyle}>
8191
{(props.visible || _.isUndefined(props.visible)) && (
8292
<Image
93+
supportRTL
8394
source={styles.imageSource}
8495
tintColor={props.tintColor}
8596
style={styles.imageStyle}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React, {useCallback} from 'react';
2+
import {ViewProps, ScrollView, ScrollViewProps, NativeSyntheticEvent, NativeScrollEvent} from 'react-native';
3+
import Fader from '../fader';
4+
import withScrollEnabler, {WithScrollEnablerProps} from '../../commons/withScrollEnabler';
5+
import withScrollReached, {WithScrollReachedProps} from '../../commons/withScrollReached';
6+
import forwardRef, {ForwardRefInjectedProps} from '../../commons/forwardRef';
7+
8+
export type FadedScrollViewProps = ViewProps &
9+
ScrollViewProps & {
10+
children?: React.ReactNode | React.ReactNode[];
11+
};
12+
13+
type ScrollReachedProps = FadedScrollViewProps & WithScrollReachedProps;
14+
type ScrollEnabledProps = ScrollReachedProps & WithScrollEnablerProps;
15+
type Props = ScrollEnabledProps & ForwardRefInjectedProps;
16+
17+
const FADER_SIZE = 76;
18+
19+
const FadedScrollView = (props: Props) => {
20+
const {scrollEnablerProps, scrollReachedProps, children, onScroll: propsOnScroll, ...other} = props;
21+
const showStart = scrollEnablerProps.scrollEnabled && !scrollReachedProps.isScrollAtStart;
22+
const showEnd = scrollEnablerProps.scrollEnabled && !scrollReachedProps.isScrollAtEnd;
23+
24+
const onScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
25+
scrollReachedProps.onScroll(event);
26+
propsOnScroll?.(event);
27+
},
28+
[scrollReachedProps.onScroll, propsOnScroll]);
29+
30+
if (children) {
31+
return (
32+
<>
33+
<ScrollView
34+
horizontal
35+
showsHorizontalScrollIndicator={false}
36+
scrollEventThrottle={16}
37+
decelerationRate={'fast'}
38+
{...other}
39+
scrollEnabled={scrollEnablerProps.scrollEnabled}
40+
onContentSizeChange={scrollEnablerProps.onContentSizeChange}
41+
onLayout={scrollEnablerProps.onLayout}
42+
onScroll={onScroll}
43+
ref={props.forwardedRef}
44+
>
45+
{children}
46+
</ScrollView>
47+
<Fader visible={showStart} position={Fader.position.START} size={FADER_SIZE}/>
48+
<Fader visible={showEnd} position={Fader.position.END} size={FADER_SIZE}/>
49+
</>
50+
);
51+
}
52+
53+
return null;
54+
};
55+
56+
// TODO: fix withScrollEnabler props (add <>)
57+
export default withScrollReached<FadedScrollViewProps>(withScrollEnabler(forwardRef<Props>(FadedScrollView)), {
58+
horizontal: true,
59+
threshold: 50
60+
});

0 commit comments

Comments
 (0)