|
1 | | -import React from 'react'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
2 | 2 | import { View, Dimensions, StyleSheet, TextStyle, StyleProp, ViewStyle } from 'react-native'; |
3 | 3 | import Modal, { ModalProps } from '../Modal'; |
4 | 4 | import ActionSheetItem from './item'; |
@@ -33,84 +33,80 @@ interface ActionSheetState { |
33 | 33 | control: 'props' | 'state'; |
34 | 34 | } |
35 | 35 |
|
36 | | -export default class ActionSheet extends React.Component<ActionSheetProps, ActionSheetState> { |
37 | | - constructor(props: ActionSheetProps) { |
38 | | - super(props); |
39 | | - this.state = { |
40 | | - stateVisible: !!props.visible, |
41 | | - control: 'props', |
42 | | - }; |
43 | | - } |
44 | | - static getDerivedStateFromProps(props: ActionSheetProps, state: ActionSheetState) { |
| 36 | +export default function ActionSheet(props: ActionSheetProps) { |
| 37 | + const { |
| 38 | + children, |
| 39 | + visible: props_visible, |
| 40 | + activeOpacity, |
| 41 | + underlayColor, |
| 42 | + cancelText = '取消', |
| 43 | + dividerStyle, |
| 44 | + onCancel, |
| 45 | + containerStyle, |
| 46 | + textStyle, |
| 47 | + ...other |
| 48 | + } = props; |
| 49 | + |
| 50 | + const visible = !!props_visible; |
| 51 | + |
| 52 | + const [state, setState] = useState({ |
| 53 | + stateVisible: !!visible, |
| 54 | + control: 'props', |
| 55 | + }); |
| 56 | + |
| 57 | + useEffect(() => { |
45 | 58 | if (props.visible === state.stateVisible && state.control === 'state') { |
46 | | - return { |
| 59 | + setState({ |
47 | 60 | control: 'props', |
48 | 61 | stateVisible: props.visible, |
49 | | - }; |
| 62 | + }); |
50 | 63 | } |
51 | 64 | if (props.visible !== state.stateVisible) { |
52 | 65 | if (state.control === 'state') { |
53 | | - return { |
54 | | - control: 'props', |
55 | | - }; |
| 66 | + setState({ ...state, control: 'props' }); |
56 | 67 | } |
57 | | - return { |
| 68 | + setState({ |
58 | 69 | control: 'props', |
59 | | - stateVisible: props.visible, |
60 | | - }; |
| 70 | + stateVisible: !!props.visible, |
| 71 | + }); |
61 | 72 | } |
62 | | - return null; |
63 | | - } |
64 | | - onClose = () => { |
65 | | - this.setState({ stateVisible: false, control: 'state' }); |
| 73 | + }, [state.stateVisible]); |
| 74 | + |
| 75 | + const onClose = () => { |
| 76 | + setState({ stateVisible: false, control: 'state' }); |
66 | 77 | }; |
67 | 78 |
|
68 | | - render() { |
69 | | - const { |
70 | | - children, |
71 | | - visible, |
72 | | - activeOpacity, |
73 | | - underlayColor, |
74 | | - cancelText = '取消', |
75 | | - dividerStyle, |
76 | | - onCancel, |
77 | | - containerStyle, |
78 | | - textStyle, |
79 | | - ...other |
80 | | - } = this.props; |
81 | | - const { stateVisible } = this.state; |
82 | | - return ( |
83 | | - <Modal |
84 | | - placement="bottom" |
85 | | - animationType="fade" // slide none fade |
86 | | - transparent={true} |
87 | | - {...other} |
88 | | - visible={stateVisible} |
89 | | - onClosed={this.onClose} |
90 | | - > |
91 | | - <> |
92 | | - {React.Children.toArray(children).map((item, index) => ( |
93 | | - <View key={index}> |
94 | | - {index !== 0 && <View style={StyleSheet.flatten([styles.itemDivider, dividerStyle?.itemDivider])} />} |
95 | | - {React.cloneElement(item as React.DetailedReactHTMLElement<any, HTMLElement>, { |
96 | | - activeOpacity: activeOpacity, |
97 | | - underlayColor: underlayColor, |
98 | | - })} |
99 | | - </View> |
100 | | - ))} |
101 | | - <View style={StyleSheet.flatten([styles.actionDivider, dividerStyle?.actionDivider])} /> |
102 | | - <ActionSheetItem |
103 | | - activeOpacity={activeOpacity} |
104 | | - underlayColor={underlayColor} |
105 | | - onPress={this.onClose} |
106 | | - children={cancelText} |
107 | | - containerStyle={containerStyle} |
108 | | - textStyle={textStyle} |
109 | | - /> |
110 | | - </> |
111 | | - </Modal> |
112 | | - ); |
113 | | - } |
| 79 | + return ( |
| 80 | + <Modal |
| 81 | + placement="bottom" |
| 82 | + animationType="fade" // slide none fade |
| 83 | + transparent={true} |
| 84 | + {...other} |
| 85 | + visible={state.stateVisible} |
| 86 | + onClosed={onClose} |
| 87 | + > |
| 88 | + <> |
| 89 | + {React.Children.toArray(children).map((item, index) => ( |
| 90 | + <View key={index}> |
| 91 | + {index !== 0 && <View style={StyleSheet.flatten([styles.itemDivider, dividerStyle?.itemDivider])} />} |
| 92 | + {React.cloneElement(item as React.DetailedReactHTMLElement<any, HTMLElement>, { |
| 93 | + activeOpacity: activeOpacity, |
| 94 | + underlayColor: underlayColor, |
| 95 | + })} |
| 96 | + </View> |
| 97 | + ))} |
| 98 | + <View style={StyleSheet.flatten([styles.actionDivider, dividerStyle?.actionDivider])} /> |
| 99 | + <ActionSheetItem |
| 100 | + activeOpacity={activeOpacity} |
| 101 | + underlayColor={underlayColor} |
| 102 | + onPress={onClose} |
| 103 | + children={cancelText} |
| 104 | + containerStyle={containerStyle} |
| 105 | + textStyle={textStyle} |
| 106 | + /> |
| 107 | + </> |
| 108 | + </Modal> |
| 109 | + ); |
114 | 110 | } |
115 | 111 |
|
116 | 112 | const styles = StyleSheet.create({ |
|
0 commit comments