Skip to content

Commit 5288845

Browse files
authored
Merge pull request #505 from nullptr-z/dev
refactor(ActionSheet): 使用函数组件重构 Class组件写法
2 parents 829af81 + 87fcf63 commit 5288845

File tree

2 files changed

+81
-86
lines changed

2 files changed

+81
-86
lines changed

packages/core/src/ActionSheet/index.tsx

Lines changed: 64 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { View, Dimensions, StyleSheet, TextStyle, StyleProp, ViewStyle } from 'react-native';
33
import Modal, { ModalProps } from '../Modal';
44
import ActionSheetItem from './item';
@@ -33,84 +33,80 @@ interface ActionSheetState {
3333
control: 'props' | 'state';
3434
}
3535

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(() => {
4558
if (props.visible === state.stateVisible && state.control === 'state') {
46-
return {
59+
setState({
4760
control: 'props',
4861
stateVisible: props.visible,
49-
};
62+
});
5063
}
5164
if (props.visible !== state.stateVisible) {
5265
if (state.control === 'state') {
53-
return {
54-
control: 'props',
55-
};
66+
setState({ ...state, control: 'props' });
5667
}
57-
return {
68+
setState({
5869
control: 'props',
59-
stateVisible: props.visible,
60-
};
70+
stateVisible: !!props.visible,
71+
});
6172
}
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' });
6677
};
6778

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+
);
114110
}
115111

116112
const styles = StyleSheet.create({

packages/core/src/ActionSheet/item.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,23 @@ export interface ActionSheetItemProps {
2727

2828
export interface ActionSheetItemState {}
2929

30-
export default class ActionSheetItem extends React.Component<ActionSheetItemProps, ActionSheetItemState> {
31-
render() {
32-
const {
33-
onPress = () => {},
34-
activeOpacity = 1,
35-
underlayColor = '#f1f1f1',
36-
containerStyle,
37-
textStyle,
38-
children,
39-
} = this.props;
40-
return (
41-
<TouchableHighlight activeOpacity={activeOpacity} underlayColor={underlayColor} onPress={onPress}>
42-
<View style={StyleSheet.flatten([styles.actionSheetItem, containerStyle])}>
43-
<Text style={StyleSheet.flatten([styles.actionSheetItemText, textStyle])}>{children}</Text>
44-
</View>
45-
</TouchableHighlight>
46-
);
47-
}
30+
export default function ActionSheetItem(props: ActionSheetItemProps) {
31+
const {
32+
onPress = () => {},
33+
activeOpacity = 1,
34+
underlayColor = '#f1f1f1',
35+
containerStyle,
36+
textStyle,
37+
children,
38+
} = props;
39+
40+
return (
41+
<TouchableHighlight activeOpacity={activeOpacity} underlayColor={underlayColor} onPress={onPress}>
42+
<View style={StyleSheet.flatten([styles.actionSheetItem, containerStyle])}>
43+
<Text style={StyleSheet.flatten([styles.actionSheetItemText, textStyle])}>{children}</Text>
44+
</View>
45+
</TouchableHighlight>
46+
);
4847
}
4948

5049
const styles = StyleSheet.create({

0 commit comments

Comments
 (0)