Skip to content

Commit 2959d49

Browse files
coadofacebook-github-bot
authored andcommitted
Replace $FlowFixMeProps in ElementBox and refactor (facebook#48605)
Summary: Pull Request resolved: facebook#48605 Changelog: [General][Changed] - Improved types in ElementBox and refactored a code Reviewed By: NickGerleman Differential Revision: D68018112 fbshipit-source-id: 369b5fb06d1f9d0bd450f487ab792b23b1d094af
1 parent 48a7840 commit 2959d49

File tree

2 files changed

+60
-52
lines changed

2 files changed

+60
-52
lines changed

packages/react-native/Libraries/Inspector/ElementBox.js

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,72 @@
1010

1111
'use strict';
1212

13+
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
14+
import type {InspectedElementFrame} from './Inspector';
15+
16+
import React from 'react';
17+
1318
const View = require('../Components/View/View');
1419
const flattenStyle = require('../StyleSheet/flattenStyle');
1520
const StyleSheet = require('../StyleSheet/StyleSheet');
1621
const Dimensions = require('../Utilities/Dimensions').default;
1722
const BorderBox = require('./BorderBox');
1823
const resolveBoxStyle = require('./resolveBoxStyle');
19-
const React = require('react');
20-
21-
class ElementBox extends React.Component<$FlowFixMeProps> {
22-
render(): React.Node {
23-
const style = flattenStyle(this.props.style) || {};
24-
let margin: ?$ReadOnly<Style> = resolveBoxStyle('margin', style);
25-
let padding: ?$ReadOnly<Style> = resolveBoxStyle('padding', style);
26-
27-
const frameStyle = {...this.props.frame};
28-
const contentStyle: {width: number, height: number} = {
29-
width: this.props.frame.width,
30-
height: this.props.frame.height,
31-
};
32-
33-
if (margin != null) {
34-
margin = resolveRelativeSizes(margin);
35-
36-
frameStyle.top -= margin.top;
37-
frameStyle.left -= margin.left;
38-
frameStyle.height += margin.top + margin.bottom;
39-
frameStyle.width += margin.left + margin.right;
40-
41-
if (margin.top < 0) {
42-
contentStyle.height += margin.top;
43-
}
44-
if (margin.bottom < 0) {
45-
contentStyle.height += margin.bottom;
46-
}
47-
if (margin.left < 0) {
48-
contentStyle.width += margin.left;
49-
}
50-
if (margin.right < 0) {
51-
contentStyle.width += margin.right;
52-
}
53-
}
5424

55-
if (padding != null) {
56-
padding = resolveRelativeSizes(padding);
25+
type Props = $ReadOnly<{
26+
frame: InspectedElementFrame,
27+
style?: ?ViewStyleProp,
28+
}>;
29+
30+
function ElementBox({frame, style}: Props): React.Node {
31+
const flattenedStyle = flattenStyle(style) || {};
32+
let margin: ?$ReadOnly<Style> = resolveBoxStyle('margin', flattenedStyle);
33+
let padding: ?$ReadOnly<Style> = resolveBoxStyle('padding', flattenedStyle);
5734

58-
contentStyle.width -= padding.left + padding.right;
59-
contentStyle.height -= padding.top + padding.bottom;
35+
const frameStyle = {...frame};
36+
const contentStyle: {width: number, height: number} = {
37+
width: frame.width,
38+
height: frame.height,
39+
};
40+
41+
if (margin != null) {
42+
margin = resolveRelativeSizes(margin);
43+
44+
frameStyle.top -= margin.top;
45+
frameStyle.left -= margin.left;
46+
frameStyle.height += margin.top + margin.bottom;
47+
frameStyle.width += margin.left + margin.right;
48+
49+
if (margin.top < 0) {
50+
contentStyle.height += margin.top;
51+
}
52+
if (margin.bottom < 0) {
53+
contentStyle.height += margin.bottom;
6054
}
55+
if (margin.left < 0) {
56+
contentStyle.width += margin.left;
57+
}
58+
if (margin.right < 0) {
59+
contentStyle.width += margin.right;
60+
}
61+
}
6162

62-
return (
63-
<View style={[styles.frame, frameStyle]} pointerEvents="none">
64-
<BorderBox box={margin} style={styles.margin}>
65-
<BorderBox box={padding} style={styles.padding}>
66-
<View style={[styles.content, contentStyle]} />
67-
</BorderBox>
68-
</BorderBox>
69-
</View>
70-
);
63+
if (padding != null) {
64+
padding = resolveRelativeSizes(padding);
65+
66+
contentStyle.width -= padding.left + padding.right;
67+
contentStyle.height -= padding.top + padding.bottom;
7168
}
69+
70+
return (
71+
<View style={[styles.frame, frameStyle]} pointerEvents="none">
72+
<BorderBox box={margin} style={styles.margin}>
73+
<BorderBox box={padding} style={styles.padding}>
74+
<View style={[styles.content, contentStyle]} />
75+
</BorderBox>
76+
</BorderBox>
77+
</View>
78+
);
7279
}
7380

7481
const styles = StyleSheet.create({

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5176,10 +5176,11 @@ declare module.exports: BoxInspector;
51765176
`;
51775177

51785178
exports[`public API should not change unintentionally Libraries/Inspector/ElementBox.js 1`] = `
5179-
"declare const React: $FlowFixMe;
5180-
declare class ElementBox extends React.Component<$FlowFixMeProps> {
5181-
render(): React.Node;
5182-
}
5179+
"type Props = $ReadOnly<{
5180+
frame: InspectedElementFrame,
5181+
style?: ?ViewStyleProp,
5182+
}>;
5183+
declare function ElementBox(Props): React.Node;
51835184
declare module.exports: ElementBox;
51845185
"
51855186
`;

0 commit comments

Comments
 (0)