Browse Source

[RN] Refactor AspectRatioDetector

Factor out the dimensions detection login into a DimensionsDetector component.
master
Saúl Ibarra Corretgé 7 years ago
parent
commit
0ad1c88cd2

+ 27
- 28
react/features/base/aspect-ratio/components/AspectRatioDetector.native.js View File

@@ -1,33 +1,33 @@
1
-import PropTypes from 'prop-types';
1
+// @flow
2
+
2 3
 import React, { Component } from 'react';
3
-import { View } from 'react-native';
4 4
 import { connect } from 'react-redux';
5 5
 
6
+import { DimensionsDetector } from '../../dimensions-detector';
7
+
6 8
 import { setAspectRatio } from '../actions';
7
-import styles from './styles';
8 9
 
9 10
 /**
10
- * A root {@link View} which captures the 'onLayout' event and figures out
11
- * the aspect ratio of the app.
11
+ * AspectRatioDetector component's property types.
12 12
  */
13
-class AspectRatioDetector extends Component {
13
+type Props = {
14
+
14 15
     /**
15
-     * AspectRatioDetector component's property types.
16
-     *
17
-     * @static
16
+     * The "onDimensionsHandler" handler.
18 17
      */
19
-    static propTypes = {
20
-        /**
21
-         * The "onLayout" handler.
22
-         */
23
-        _onLayout: PropTypes.func,
18
+    _onDimensionsChanged: Function,
24 19
 
25
-        /**
26
-         * Any nested components.
27
-         */
28
-        children: PropTypes.node
29
-    };
20
+    /**
21
+     * Any nested components.
22
+     */
23
+    children: React$Node
24
+};
30 25
 
26
+/**
27
+ * A root {@link View} which captures the 'onLayout' event and figures out
28
+ * the aspect ratio of the app.
29
+ */
30
+class AspectRatioDetector extends Component<Props> {
31 31
     /**
32 32
      * Renders the root view and it's children.
33 33
      *
@@ -35,11 +35,10 @@ class AspectRatioDetector extends Component {
35 35
      */
36 36
     render() {
37 37
         return (
38
-            <View
39
-                onLayout = { this.props._onLayout }
40
-                style = { styles.aspectRatioDetector } >
38
+            <DimensionsDetector
39
+                onDimensionsChanged = { this.props._onDimensionsChanged } >
41 40
                 { this.props.children }
42
-            </View>
41
+            </DimensionsDetector>
43 42
         );
44 43
     }
45 44
 }
@@ -50,21 +49,21 @@ class AspectRatioDetector extends Component {
50 49
  * @param {Function} dispatch - Redux action dispatcher.
51 50
  * @private
52 51
  * @returns {{
53
- *     _onLayout: Function
52
+ *     _onDimensionsChanged: Function
54 53
  * }}
55 54
  */
56 55
 function _mapDispatchToProps(dispatch) {
57 56
     return {
58 57
         /**
59
-         * Handles the "on layout" View's event and dispatches aspect ratio
58
+         * Handles the "on dimensions changed" event and dispatches aspect ratio
60 59
          * changed action.
61 60
          *
62
-         * @param {Object} event - The "on layout" event object/structure passed
63
-         * by react-native.
61
+         * @param {number} width - The new width for the component.
62
+         * @param {number} height - The new height for the component.
64 63
          * @private
65 64
          * @returns {void}
66 65
          */
67
-        _onLayout({ nativeEvent: { layout: { height, width } } }) {
66
+        _onDimensionsChanged(width: number, height: number) {
68 67
             dispatch(setAspectRatio(width, height));
69 68
         }
70 69
     };

+ 72
- 0
react/features/base/dimensions-detector/components/DimensionsDetector.native.js View File

@@ -0,0 +1,72 @@
1
+// @flow
2
+
3
+import React, { Component } from 'react';
4
+import { View } from 'react-native';
5
+
6
+import styles from './styles';
7
+
8
+/**
9
+ * AspectRatioDetector component's property types.
10
+ */
11
+type Props = {
12
+
13
+    /**
14
+     * The "onLayout" handler.
15
+     */
16
+    onDimensionsChanged: Function,
17
+
18
+    /**
19
+     * Any nested components.
20
+     */
21
+    children: React$Node
22
+};
23
+
24
+/**
25
+ * A {@link View} which captures the 'onLayout' event and calls a prop with the
26
+ * component size.
27
+ */
28
+export default class DimensionsDetector extends Component<Props> {
29
+    /**
30
+     * Initializes a new DimensionsDetector instance.
31
+     *
32
+     * @param {Object} props - The read-only properties with which the new
33
+     * instance is to be initialized.
34
+     */
35
+    constructor(props: Object) {
36
+        super(props);
37
+
38
+        this._onLayout = this._onLayout.bind(this);
39
+    }
40
+
41
+    _onLayout: (Object) => void;
42
+
43
+    /**
44
+     * Handles the "on layout" View's event and calls the onDimensionsChanged
45
+     * prop.
46
+     *
47
+     * @param {Object} event - The "on layout" event object/structure passed
48
+     * by react-native.
49
+     * @private
50
+     * @returns {void}
51
+     */
52
+    _onLayout({ nativeEvent: { layout: { height, width } } }) {
53
+        const { onDimensionsChanged } = this.props;
54
+
55
+        onDimensionsChanged && onDimensionsChanged(width, height);
56
+    }
57
+
58
+    /**
59
+     * Renders the root view and it's children.
60
+     *
61
+     * @returns {Component}
62
+     */
63
+    render() {
64
+        return (
65
+            <View
66
+                onLayout = { this._onLayout }
67
+                style = { styles.dimensionsDetector } >
68
+                { this.props.children }
69
+            </View>
70
+        );
71
+    }
72
+}

+ 1
- 0
react/features/base/dimensions-detector/components/index.js View File

@@ -0,0 +1 @@
1
+export { default as DimensionsDetector } from './DimensionsDetector';

react/features/base/aspect-ratio/components/styles.js → react/features/base/dimensions-detector/components/styles.js View File

@@ -5,9 +5,9 @@ import { createStyleSheet } from '../../styles';
5 5
  */
6 6
 export default createStyleSheet({
7 7
     /**
8
-     * The style of {@link AspectRatioDetector} used on react-native.
8
+     * The style of {@link DimensionsDetector} used on react-native.
9 9
      */
10
-    aspectRatioDetector: {
10
+    dimensionsDetector: {
11 11
         alignSelf: 'stretch',
12 12
         flex: 1
13 13
     }

+ 1
- 0
react/features/base/dimensions-detector/index.js View File

@@ -0,0 +1 @@
1
+export * from './components';

Loading…
Cancel
Save