Authored by chenl

修改了YH_ViewPager为android专用。review by yuliang。

  1 +/**
  2 + * Description:
  3 + *
  4 + * Author: Bruce.Lu
  5 + * Version: 1.0
  6 + * Created on 2017/2/23.
  7 + */
  8 +import React from 'react';
  9 +import ReactNative from 'react-native';
  10 +
  11 +let {
  12 + requireNativeComponent,
  13 + View,
  14 +} = ReactNative;
  15 +var dismissKeyboard = require('dismissKeyboard');
  16 +
  17 +let YH_ViewPagerView = requireNativeComponent('YH_ViewPager', null);
  18 +
  19 +var UIManager = require('UIManager');
  20 +
  21 +type Event = Object;
  22 +var VIEWPAGER_REF = 'viewPager';
  23 +
  24 +export type ViewPagerScrollState = $Enum<{
  25 + idle: string,
  26 + dragging: string,
  27 + settling: string,
  28 +}>;
  29 +
  30 +export default class YH_ViewPager extends React.Component {
  31 + props: {
  32 + initialPage?: number,
  33 + onPageScroll?: Function,
  34 + onPageScrollStateChanged?: Function,
  35 + onPageSelected?: Function,
  36 + pageMargin?: number,
  37 + keyboardDismissMode?: 'none' | 'on-drag',
  38 + scrollEnabled?: boolean,
  39 + };
  40 +
  41 + static propTypes = {
  42 + ...View.propTypes,
  43 + initialPage: React.PropTypes.number,
  44 + onPageScroll: React.PropTypes.func,
  45 + onPageScrollStateChanged: React.PropTypes.func,
  46 + onPageSelected: React.PropTypes.func,
  47 + pageMargin: React.PropTypes.number,
  48 + keyboardDismissMode: React.PropTypes.oneOf([
  49 + 'none', // default
  50 + 'on-drag',
  51 + ]),
  52 + scrollEnabled: React.PropTypes.bool,
  53 + };
  54 +
  55 + constructor(props) {
  56 + super(props);
  57 + }
  58 +
  59 + componentDidMount() {
  60 + if (this.props.initialPage != null) {
  61 + this.setPageWithoutAnimation(this.props.initialPage);
  62 + }
  63 + }
  64 +
  65 + _childrenWithOverridenStyle = (): Array => {
  66 + // Override styles so that each page will fill the parent. Native component
  67 + // will handle positioning of elements, so it's not important to offset
  68 + // them correctly.
  69 + return React.Children.map(this.props.children, function(child) {
  70 + if (!child) {
  71 + return null;
  72 + }
  73 + var newProps = {
  74 + ...child.props,
  75 + style: [child.props.style, {
  76 + position: 'absolute',
  77 + left: 0,
  78 + top: 0,
  79 + right: 0,
  80 + bottom: 0,
  81 + width: undefined,
  82 + height: undefined,
  83 + }],
  84 + collapsable: false,
  85 + };
  86 + if (child.type &&
  87 + child.type.displayName &&
  88 + (child.type.displayName !== 'RCTView') &&
  89 + (child.type.displayName !== 'View')) {
  90 + console.warn('Each ViewPager child must be a <View>. Was ' + child.type.displayName);
  91 + }
  92 + return React.createElement(child.type, newProps);
  93 + });
  94 + };
  95 +
  96 + _onPageScroll = (e: Event) => {
  97 + if (this.props.onPageScroll) {
  98 + this.props.onPageScroll(e);
  99 + }
  100 + if (this.props.keyboardDismissMode === 'on-drag') {
  101 + dismissKeyboard();
  102 + }
  103 + };
  104 +
  105 + _onPageScrollStateChanged = (e: Event) => {
  106 + if (this.props.onPageScrollStateChanged) {
  107 + this.props.onPageScrollStateChanged(e.nativeEvent.pageScrollState);
  108 + }
  109 + };
  110 +
  111 + _onPageSelected = (e: Event) => {
  112 + if (this.props.onPageSelected) {
  113 + this.props.onPageSelected(e);
  114 + }
  115 + };
  116 +
  117 + setPage = (selectedPage: number) => {
  118 + UIManager.dispatchViewManagerCommand(
  119 + ReactNative.findNodeHandle(this),
  120 + UIManager.AndroidViewPager.Commands.setPage,
  121 + [selectedPage],
  122 + );
  123 + };
  124 +
  125 + setPageWithoutAnimation = (selectedPage: number) => {
  126 + UIManager.dispatchViewManagerCommand(
  127 + ReactNative.findNodeHandle(this),
  128 + UIManager.AndroidViewPager.Commands.setPageWithoutAnimation,
  129 + [selectedPage],
  130 + );
  131 + };
  132 +
  133 +
  134 + render() {
  135 + return (<YH_ViewPagerView
  136 + {...this.props}
  137 + ref={VIEWPAGER_REF}
  138 + style={this.props.style}
  139 + onPageScroll={this._onPageScroll}
  140 + onPageScrollStateChanged={this._onPageScrollStateChanged}
  141 + onPageSelected={this._onPageSelected}
  142 + children={this._childrenWithOverridenStyle()}
  143 + />);
  144 + }
  145 +}
@@ -8,138 +8,15 @@ @@ -8,138 +8,15 @@
8 import React from 'react'; 8 import React from 'react';
9 import ReactNative from 'react-native'; 9 import ReactNative from 'react-native';
10 10
11 -let {  
12 - requireNativeComponent,  
13 - View,  
14 -} = ReactNative;  
15 -var dismissKeyboard = require('dismissKeyboard');  
16 -  
17 -let YH_ViewPagerView = requireNativeComponent('YH_ViewPager', null);  
18 -  
19 -var UIManager = require('UIManager');  
20 -  
21 -type Event = Object;  
22 -var VIEWPAGER_REF = 'viewPager';  
23 -  
24 -export type ViewPagerScrollState = $Enum<{  
25 - idle: string,  
26 - dragging: string,  
27 - settling: string,  
28 -}>;  
29 -  
30 export default class YH_ViewPager extends React.Component { 11 export default class YH_ViewPager extends React.Component {
31 - props: {  
32 - initialPage?: number,  
33 - onPageScroll?: Function,  
34 - onPageScrollStateChanged?: Function,  
35 - onPageSelected?: Function,  
36 - pageMargin?: number,  
37 - keyboardDismissMode?: 'none' | 'on-drag',  
38 - scrollEnabled?: boolean,  
39 - };  
40 12
41 - static propTypes = {  
42 - ...View.propTypes,  
43 - initialPage: React.PropTypes.number,  
44 - onPageScroll: React.PropTypes.func,  
45 - onPageScrollStateChanged: React.PropTypes.func,  
46 - onPageSelected: React.PropTypes.func,  
47 - pageMargin: React.PropTypes.number,  
48 - keyboardDismissMode: React.PropTypes.oneOf([  
49 - 'none', // default  
50 - 'on-drag',  
51 - ]),  
52 - scrollEnabled: React.PropTypes.bool,  
53 - };  
54 13
55 constructor(props) { 14 constructor(props) {
56 super(props); 15 super(props);
57 } 16 }
58 17
59 - componentDidMount() {  
60 - if (this.props.initialPage != null) {  
61 - this.setPageWithoutAnimation(this.props.initialPage);  
62 - }  
63 - }  
64 -  
65 - _childrenWithOverridenStyle = (): Array => {  
66 - // Override styles so that each page will fill the parent. Native component  
67 - // will handle positioning of elements, so it's not important to offset  
68 - // them correctly.  
69 - return React.Children.map(this.props.children, function(child) {  
70 - if (!child) {  
71 - return null;  
72 - }  
73 - var newProps = {  
74 - ...child.props,  
75 - style: [child.props.style, {  
76 - position: 'absolute',  
77 - left: 0,  
78 - top: 0,  
79 - right: 0,  
80 - bottom: 0,  
81 - width: undefined,  
82 - height: undefined,  
83 - }],  
84 - collapsable: false,  
85 - };  
86 - if (child.type &&  
87 - child.type.displayName &&  
88 - (child.type.displayName !== 'RCTView') &&  
89 - (child.type.displayName !== 'View')) {  
90 - console.warn('Each ViewPager child must be a <View>. Was ' + child.type.displayName);  
91 - }  
92 - return React.createElement(child.type, newProps);  
93 - });  
94 - };  
95 -  
96 - _onPageScroll = (e: Event) => {  
97 - if (this.props.onPageScroll) {  
98 - this.props.onPageScroll(e);  
99 - }  
100 - if (this.props.keyboardDismissMode === 'on-drag') {  
101 - dismissKeyboard();  
102 - }  
103 - };  
104 -  
105 - _onPageScrollStateChanged = (e: Event) => {  
106 - if (this.props.onPageScrollStateChanged) {  
107 - this.props.onPageScrollStateChanged(e.nativeEvent.pageScrollState);  
108 - }  
109 - };  
110 -  
111 - _onPageSelected = (e: Event) => {  
112 - if (this.props.onPageSelected) {  
113 - this.props.onPageSelected(e);  
114 - }  
115 - };  
116 -  
117 - setPage = (selectedPage: number) => {  
118 - UIManager.dispatchViewManagerCommand(  
119 - ReactNative.findNodeHandle(this),  
120 - UIManager.AndroidViewPager.Commands.setPage,  
121 - [selectedPage],  
122 - );  
123 - };  
124 -  
125 - setPageWithoutAnimation = (selectedPage: number) => {  
126 - UIManager.dispatchViewManagerCommand(  
127 - ReactNative.findNodeHandle(this),  
128 - UIManager.AndroidViewPager.Commands.setPageWithoutAnimation,  
129 - [selectedPage],  
130 - );  
131 - };  
132 -  
133 18
134 render() { 19 render() {
135 - return (<YH_ViewPagerView  
136 - {...this.props}  
137 - ref={VIEWPAGER_REF}  
138 - style={this.props.style}  
139 - onPageScroll={this._onPageScroll}  
140 - onPageScrollStateChanged={this._onPageScrollStateChanged}  
141 - onPageSelected={this._onPageSelected}  
142 - children={this._childrenWithOverridenStyle()}  
143 - />); 20 + return null;
144 } 21 }
145 } 22 }