Skip to content

Commit ceea75c

Browse files
committed
fix(Flex): 重构为function组件
1 parent ef036b7 commit ceea75c

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { View, ViewProps } from 'react-native';
33

44
export interface FlexItemProps extends ViewProps {}
55

6-
export default class FlexItem extends Component<FlexItemProps> {
7-
render() {
8-
return <View {...this.props} />;
9-
}
6+
export default function FlexItem(props: FlexItemProps) {
7+
return <View {...props} />;
108
}

packages/core/src/Flex/index.tsx

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { View, ViewProps, FlexStyle } from 'react-native';
33
import FlexItem from './FlexItem';
44

@@ -26,50 +26,51 @@ export interface FlexProps extends ViewProps {
2626
wrap?: 'wrap' | 'nowrap' | 'wrap-reverse';
2727
}
2828

29-
export default class Flex extends Component<FlexProps> {
30-
static Item = FlexItem;
31-
static defaultProps: FlexProps = {
32-
direction: 'row',
33-
justify: 'start',
34-
wrap: 'nowrap',
35-
align: 'start',
36-
};
37-
render() {
38-
const { direction, justify, align, wrap, children, style } = this.props;
39-
const sty: FlexProps['style'] = {};
40-
if (direction) {
41-
sty.flexDirection = direction;
42-
}
43-
if (wrap) {
44-
sty.flexWrap = wrap;
45-
}
46-
if (justify) {
47-
sty.justifyContent = justify
48-
.replace(/^start$/g, 'flex-start')
49-
.replace(/^end$/g, 'flex-end')
50-
.replace(/^between$/g, 'space-between')
51-
.replace(/^around$/g, 'space-around')
52-
.replace(/^evenly$/g, 'space-evenly') as FlexStyle['justifyContent'];
53-
}
54-
if (align) {
55-
sty.alignItems = align.replace(/^start$/g, 'flex-start').replace(/^end$/g, 'flex-end') as FlexStyle['alignItems'];
56-
}
57-
return (
58-
<View testID="RNE__Flex__wrap" style={[sty, style]}>
59-
{children &&
60-
React.Children.map(children, (child: React.ReactNode) => {
61-
if (!React.isValidElement(child)) {
62-
return null;
63-
}
64-
if (child.type && (child.type as any).displayName === 'FlexItem') {
65-
return React.cloneElement(<FlexItem />, {
66-
...child.props,
67-
style: [{ flex: 1 }, child.props.style],
68-
});
69-
}
70-
return child;
71-
})}
72-
</View>
73-
);
29+
function Flex(props: FlexProps) {
30+
const { direction, justify, align, wrap, children, style } = props;
31+
const sty: FlexProps['style'] = {};
32+
if (direction) {
33+
sty.flexDirection = direction;
7434
}
35+
if (wrap) {
36+
sty.flexWrap = wrap;
37+
}
38+
if (justify) {
39+
sty.justifyContent = justify
40+
.replace(/^start$/g, 'flex-start')
41+
.replace(/^end$/g, 'flex-end')
42+
.replace(/^between$/g, 'space-between')
43+
.replace(/^around$/g, 'space-around')
44+
.replace(/^evenly$/g, 'space-evenly') as FlexStyle['justifyContent'];
45+
}
46+
if (align) {
47+
sty.alignItems = align.replace(/^start$/g, 'flex-start').replace(/^end$/g, 'flex-end') as FlexStyle['alignItems'];
48+
}
49+
return (
50+
<View testID="RNE__Flex__wrap" style={[sty, style]}>
51+
{children &&
52+
React.Children.map(children, (child: React.ReactNode) => {
53+
if (!React.isValidElement(child)) {
54+
return null;
55+
}
56+
if (child.type && (child.type as any).displayName === 'FlexItem') {
57+
return React.cloneElement(<FlexItem />, {
58+
...child.props,
59+
style: [{ flex: 1 }, child.props.style],
60+
});
61+
}
62+
return child;
63+
})}
64+
</View>
65+
);
7566
}
67+
Flex.defaultProps = {
68+
direction: 'row',
69+
justify: 'start',
70+
wrap: 'nowrap',
71+
align: 'start',
72+
} as FlexProps;
73+
74+
Flex.Item = FlexItem;
75+
76+
export default Flex;

0 commit comments

Comments
 (0)