mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
RN: Refactor ScrollView Style Splitting
Summary: Rewrites `splitLayoutProps`, which is only used by `ScrollView`. - Improve type safety by avoiding `DangerouslyImpreciseStyle`. - Avoid allocating objects when it is not necessary. - Avoid allocating a object enumeratig layout props by using a switch statement. Changelog: [Internal] Reviewed By: JoshuaGross, kacieb Differential Revision: D25097226 fbshipit-source-id: 2050c03b681024212c06a48b7eb05f28c14415f9
This commit is contained in:
1 parent
7e7527e7db
commit
54a067e61e
4 files changed
+66
-59
No files matched your search
@@ -1138,7 +1138,7 @@ class ScrollView extends React.Component<Props, State> {
|
||||
...this.props,
|
||||
alwaysBounceHorizontal,
|
||||
alwaysBounceVertical,
|
||||
style: [baseStyle, this.props.style],
|
||||
style: StyleSheet.compose(baseStyle, this.props.style),
|
||||
// Override the onContentSizeChange from props, since this event can
|
||||
// bubble up from TextInputs
|
||||
onContentSizeChange: null,
|
||||
@@ -1224,10 +1224,10 @@ class ScrollView extends React.Component<Props, State> {
|
||||
const {outer, inner} = splitLayoutProps(flattenStyle(props.style));
|
||||
return React.cloneElement(
|
||||
refreshControl,
|
||||
{style: [baseStyle, outer]},
|
||||
{style: StyleSheet.compose(baseStyle, outer)},
|
||||
<ScrollViewClass
|
||||
{...props}
|
||||
style={[baseStyle, inner]}
|
||||
style={StyleSheet.compose(baseStyle, inner)}
|
||||
ref={this._setNativeRef}>
|
||||
{contentContainer}
|
||||
</ScrollViewClass>,
|
||||
|
||||
@@ -40,15 +40,12 @@ exports[`<ScrollView /> should render as expected: should deep render when not m
|
||||
snapToEnd={true}
|
||||
snapToStart={true}
|
||||
style={
|
||||
Array [
|
||||
Object {
|
||||
"flexDirection": "column",
|
||||
"flexGrow": 1,
|
||||
"flexShrink": 1,
|
||||
"overflow": "scroll",
|
||||
},
|
||||
undefined,
|
||||
]
|
||||
Object {
|
||||
"flexDirection": "column",
|
||||
"flexGrow": 1,
|
||||
"flexShrink": 1,
|
||||
"overflow": "scroll",
|
||||
}
|
||||
}
|
||||
>
|
||||
<RCTScrollContentView
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const splitLayoutProps = require('../splitLayoutProps');
|
||||
const splitLayoutProps = require('../splitLayoutProps').default;
|
||||
|
||||
test('splits style objects', () => {
|
||||
const style = {width: 10, margin: 20, padding: 30, transform: {scaleY: -1}};
|
||||
@@ -45,3 +45,9 @@ test('does not copy values to both returned objects', () => {
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
test('returns null values if argument is null', () => {
|
||||
const {outer, inner} = splitLayoutProps(null);
|
||||
expect(outer).toBe(null);
|
||||
expect(inner).toBe(null);
|
||||
});
|
||||
@@ -4,61 +4,65 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {DangerouslyImpreciseStyle} from './StyleSheet';
|
||||
import type {____ViewStyle_Internal} from './StyleSheetTypes';
|
||||
|
||||
const OUTER_PROPS = Object.assign(Object.create(null), {
|
||||
margin: true,
|
||||
marginHorizontal: true,
|
||||
marginVertical: true,
|
||||
marginBottom: true,
|
||||
marginTop: true,
|
||||
marginLeft: true,
|
||||
marginRight: true,
|
||||
flex: true,
|
||||
flexGrow: true,
|
||||
flexShrink: true,
|
||||
flexBasis: true,
|
||||
alignSelf: true,
|
||||
height: true,
|
||||
minHeight: true,
|
||||
maxHeight: true,
|
||||
width: true,
|
||||
minWidth: true,
|
||||
maxWidth: true,
|
||||
position: true,
|
||||
left: true,
|
||||
right: true,
|
||||
bottom: true,
|
||||
top: true,
|
||||
transform: true,
|
||||
});
|
||||
|
||||
function splitLayoutProps(
|
||||
props: ?DangerouslyImpreciseStyle,
|
||||
export default function splitLayoutProps(
|
||||
props: ?____ViewStyle_Internal,
|
||||
): {
|
||||
outer: DangerouslyImpreciseStyle,
|
||||
inner: DangerouslyImpreciseStyle,
|
||||
...
|
||||
outer: ?____ViewStyle_Internal,
|
||||
inner: ?____ViewStyle_Internal,
|
||||
} {
|
||||
const inner = {};
|
||||
const outer = {};
|
||||
if (props) {
|
||||
Object.keys(props).forEach((k: string) => {
|
||||
const value = props[k];
|
||||
if (OUTER_PROPS[k]) {
|
||||
outer[k] = value;
|
||||
} else {
|
||||
inner[k] = value;
|
||||
let outer: ?____ViewStyle_Internal = null;
|
||||
let inner: ?____ViewStyle_Internal = null;
|
||||
|
||||
if (props != null) {
|
||||
// $FlowIgnore[incompatible-exact] Will contain a subset of keys from `props`.
|
||||
// $FlowIgnore[incompatible-type] Values are preserved within a key.
|
||||
outer = {};
|
||||
// $FlowIgnore[incompatible-exact] Will contain a subset of keys from `props`.
|
||||
// $FlowIgnore[incompatible-type] Values are preserved within a key.
|
||||
inner = {};
|
||||
|
||||
for (const prop of Object.keys(props)) {
|
||||
switch (prop) {
|
||||
case 'margin':
|
||||
case 'marginHorizontal':
|
||||
case 'marginVertical':
|
||||
case 'marginBottom':
|
||||
case 'marginTop':
|
||||
case 'marginLeft':
|
||||
case 'marginRight':
|
||||
case 'flex':
|
||||
case 'flexGrow':
|
||||
case 'flexShrink':
|
||||
case 'flexBasis':
|
||||
case 'alignSelf':
|
||||
case 'height':
|
||||
case 'minHeight':
|
||||
case 'maxHeight':
|
||||
case 'width':
|
||||
case 'minWidth':
|
||||
case 'maxWidth':
|
||||
case 'position':
|
||||
case 'left':
|
||||
case 'right':
|
||||
case 'bottom':
|
||||
case 'top':
|
||||
case 'transform':
|
||||
outer[prop] = props[prop];
|
||||
break;
|
||||
default:
|
||||
inner[prop] = props[prop];
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {outer, inner};
|
||||
}
|
||||
|
||||
module.exports = splitLayoutProps;
|
||||
Reference in new issue
Block a user