|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {LayoutChangeEvent} from 'react-native'; |
| 3 | +import {Text, View} from 'react-native-ui-lib'; |
| 4 | +import { |
| 5 | + renderHeader, |
| 6 | + renderBooleanOption, |
| 7 | + renderSliderOption |
| 8 | +} from '../../ExampleScreenPresenter'; |
| 9 | +import AutoLockScrollView from './AutoLockScrollView'; |
| 10 | +import AutoLockFlatList from './AutoLockFlatList'; |
| 11 | + |
| 12 | +class WithScrollEnablerScreen extends Component { |
| 13 | + state = { |
| 14 | + isListView: false, |
| 15 | + isHorizontal: false, |
| 16 | + numberOfItems: 3, |
| 17 | + contentWidth: undefined, |
| 18 | + contentHeight: undefined, |
| 19 | + layoutWidth: undefined, |
| 20 | + layoutHeight: undefined |
| 21 | + }; |
| 22 | + |
| 23 | + onContentSizeChange = (contentWidth: number, contentHeight: number) => { |
| 24 | + const { |
| 25 | + contentWidth: currentContentWidth, |
| 26 | + contentHeight: currentContentHeight |
| 27 | + } = this.state; |
| 28 | + if ( |
| 29 | + currentContentWidth !== contentWidth || |
| 30 | + currentContentHeight !== contentHeight |
| 31 | + ) { |
| 32 | + this.setState({contentWidth, contentHeight}); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + onLayout = ({ |
| 37 | + nativeEvent: { |
| 38 | + layout: {width, height} |
| 39 | + } |
| 40 | + }: LayoutChangeEvent) => { |
| 41 | + const {layoutWidth, layoutHeight} = this.state; |
| 42 | + if (width !== layoutWidth || height !== layoutHeight) { |
| 43 | + this.setState({layoutWidth: width, layoutHeight: height}); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + renderList = () => { |
| 48 | + const {isListView, isHorizontal, numberOfItems} = this.state; |
| 49 | + const Container = isListView ? AutoLockScrollView : AutoLockFlatList; |
| 50 | + |
| 51 | + return ( |
| 52 | + // @ts-ignore |
| 53 | + <Container |
| 54 | + key={`${isHorizontal}`} |
| 55 | + horizontal={isHorizontal} |
| 56 | + numberOfItems={numberOfItems} |
| 57 | + onContentSizeChange={this.onContentSizeChange} |
| 58 | + onLayout={this.onLayout} |
| 59 | + /> |
| 60 | + ); |
| 61 | + }; |
| 62 | + |
| 63 | + renderData = () => { |
| 64 | + const {contentWidth, contentHeight, layoutWidth, layoutHeight} = this.state; |
| 65 | + const contentText = `Content {width, height}: ${contentWidth}, ${contentHeight}`; |
| 66 | + const layoutText = `Layout {width, height}: ${layoutWidth}, ${layoutHeight}`; |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <Text text70>{contentText}</Text> |
| 70 | + <Text text70>{layoutText}</Text> |
| 71 | + </> |
| 72 | + ); |
| 73 | + }; |
| 74 | + |
| 75 | + renderOptions = () => { |
| 76 | + const {isListView, isHorizontal} = this.state; |
| 77 | + const orientationText = isHorizontal ? 'Horizontal' : 'Vertical'; |
| 78 | + const listTypeText = isListView ? 'ListView' : 'FlatList'; |
| 79 | + return ( |
| 80 | + <> |
| 81 | + <View row> |
| 82 | + <View flex marginR-10> |
| 83 | + {renderBooleanOption.call(this, orientationText, 'isHorizontal')} |
| 84 | + </View> |
| 85 | + <View flex marginL-10> |
| 86 | + {renderBooleanOption.call(this, listTypeText, 'isListView')} |
| 87 | + </View> |
| 88 | + </View> |
| 89 | + {renderSliderOption.call( |
| 90 | + this, |
| 91 | + 'Number of items shown', |
| 92 | + 'numberOfItems', |
| 93 | + { |
| 94 | + min: 1, |
| 95 | + max: 5, |
| 96 | + step: 1, |
| 97 | + initial: 3 |
| 98 | + } |
| 99 | + )} |
| 100 | + </> |
| 101 | + ); |
| 102 | + }; |
| 103 | + |
| 104 | + render() { |
| 105 | + return ( |
| 106 | + <View margin-10> |
| 107 | + {renderHeader('withScrollEnabler', {'marginB-10': true})} |
| 108 | + {this.renderOptions()} |
| 109 | + {this.renderData()} |
| 110 | + {this.renderList()} |
| 111 | + </View> |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default WithScrollEnablerScreen; |
0 commit comments