|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import { View, ViewProps, FlexStyle } from 'react-native'; |
3 | 3 | import FlexItem from './FlexItem'; |
4 | 4 |
|
@@ -26,50 +26,51 @@ export interface FlexProps extends ViewProps { |
26 | 26 | wrap?: 'wrap' | 'nowrap' | 'wrap-reverse'; |
27 | 27 | } |
28 | 28 |
|
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; |
74 | 34 | } |
| 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 | + ); |
75 | 66 | } |
| 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