Skip to content

Commit dd2f6a8

Browse files
committed
chore(ActionSheet): getDerivedStateFromProps replace UNSAFE_componentWillReceiveProps
1 parent 3b13e53 commit dd2f6a8

File tree

3 files changed

+37
-94
lines changed

3 files changed

+37
-94
lines changed

example/examples/src/routes/ActionSheet/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ export default class Index extends Component<IndexProps, IndexState> {
3838
<Button onPress={this.onOpen}>打开 ActionSheet</Button>
3939
<ActionSheet
4040
visible={this.state.visible}
41-
onCancel={true}
41+
maskClosable={true}
4242
dividerStyle={{
4343
itemDivider: {height: 2},
4444
}}>
4545
<ActionSheetItem
46-
onPress={() => Toast.info('你点击了按钮一', 2, 'info')}>
46+
onPress={() => {
47+
Toast.info('你点击了按钮一', 2, 'info');
48+
}}>
4749
按钮一
4850
</ActionSheetItem>
4951
<ActionSheetItem

packages/core/src/ActionSheet/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function Demo() {
5050
## Props
5151

5252
```ts
53-
import { ModalProps, StyleProp, ViewStyle } from 'react-native';
53+
import { StyleProp, ViewStyle } from 'react-native';
54+
import { ModalProps } from '@uiw/react-native';
5455

5556
export interface DividerStyle {
5657
itemDivider?: StyleProp<ViewStyle>,

packages/core/src/ActionSheet/index.tsx

Lines changed: 31 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import React from 'react';
2-
import {
3-
View,
4-
Dimensions,
5-
StyleSheet,
6-
TextStyle,
7-
StyleProp,
8-
ViewStyle,
9-
TouchableOpacity,
10-
Modal,
11-
ModalProps,
12-
Animated,
13-
} from 'react-native';
2+
import { View, Dimensions, StyleSheet, TextStyle, StyleProp, ViewStyle } from 'react-native';
3+
import Modal, { ModalProps } from '../Modal';
144
export { default as ActionSheetItem } from './item';
155
import ActionSheetItem from './item';
166

@@ -40,55 +30,42 @@ export interface ActionSheetProps extends ModalProps {
4030
}
4131

4232
interface ActionSheetState {
43-
animatedHeight: number;
4433
stateVisible: boolean;
34+
control: 'props' | 'state';
4535
}
4636

4737
export default class ActionSheet extends React.Component<ActionSheetProps, ActionSheetState> {
48-
private fadeAnim: Animated.Value = new Animated.Value(0);
49-
private animatedRef: React.RefObject<View> = React.createRef();
5038
constructor(props: ActionSheetProps) {
5139
super(props);
5240
this.state = {
53-
animatedHeight: 0,
5441
stateVisible: !!props.visible,
42+
control: 'props',
5543
};
5644
}
57-
58-
onClose = () => {
59-
Animated.timing(this.fadeAnim, {
60-
toValue: 0,
61-
duration: 150,
62-
useNativeDriver: true,
63-
}).start(({ finished }) => {
64-
this.setState({ stateVisible: false });
65-
});
66-
};
67-
UNSAFE_componentWillReceiveProps(nextProps: ActionSheetProps) {
68-
if (nextProps.visible) {
69-
this.setState({ stateVisible: true });
70-
Animated.timing(this.fadeAnim, {
71-
toValue: 0,
72-
duration: 0,
73-
useNativeDriver: true,
74-
}).start(({ finished }) => {
75-
this.animatedRef.current &&
76-
this.animatedRef.current.measure(
77-
(_frameOffsetX, _frameOffsetY, _width, _height, pageOffsetX, pageOffsetY) => {
78-
this.setState({ animatedHeight: _height }, () => {
79-
Animated.timing(this.fadeAnim, {
80-
toValue: -_height,
81-
duration: 150,
82-
useNativeDriver: true,
83-
}).start();
84-
});
85-
},
86-
);
87-
});
88-
} else {
89-
this.onClose();
45+
static getDerivedStateFromProps(props: ActionSheetProps, state: ActionSheetState) {
46+
if (props.visible === state.stateVisible && state.control === 'state') {
47+
return {
48+
control: 'props',
49+
stateVisible: props.visible,
50+
};
51+
}
52+
if (props.visible !== state.stateVisible) {
53+
if (state.control === 'state') {
54+
return {
55+
control: 'props',
56+
};
57+
}
58+
return {
59+
control: 'props',
60+
stateVisible: props.visible,
61+
};
9062
}
63+
return null;
9164
}
65+
onClose = () => {
66+
this.setState({ stateVisible: false, control: 'state' });
67+
};
68+
9269
render() {
9370
const {
9471
children,
@@ -105,26 +82,14 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
10582
const { stateVisible } = this.state;
10683
return (
10784
<Modal
85+
placement="bottom"
10886
animationType="fade" // slide none fade
10987
transparent={true}
110-
visible={stateVisible}
111-
onRequestClose={this.onClose}
11288
{...other}
89+
visible={stateVisible}
90+
onClosed={this.onClose}
11391
>
114-
<TouchableOpacity
115-
activeOpacity={1}
116-
style={[styles.position, styles.spread]}
117-
onPress={() => onCancel && this.onClose()}
118-
>
119-
<Animated.View style={[styles.spread, styles.backdrop]}></Animated.View>
120-
</TouchableOpacity>
121-
<Animated.View
122-
style={[
123-
styles.actionSheet,
124-
{ bottom: -this.state.animatedHeight, transform: [{ translateY: this.fadeAnim }] },
125-
]}
126-
ref={this.animatedRef}
127-
>
92+
<>
12893
{React.Children.toArray(children).map((item, index) => (
12994
<View key={index}>
13095
{index !== 0 && <View style={StyleSheet.flatten([styles.itemDivider, dividerStyle?.itemDivider])} />}
@@ -143,38 +108,13 @@ export default class ActionSheet extends React.Component<ActionSheetProps, Actio
143108
containerStyle={containerStyle}
144109
textStyle={textStyle}
145110
/>
146-
</Animated.View>
111+
</>
147112
</Modal>
148113
);
149114
}
150115
}
151116

152117
const styles = StyleSheet.create({
153-
position: {
154-
position: 'absolute',
155-
backgroundColor: 'transparent',
156-
top: 0,
157-
bottom: 0,
158-
left: 0,
159-
right: 0,
160-
zIndex: 9998,
161-
},
162-
backdrop: {
163-
backgroundColor: '#000',
164-
opacity: 0.2,
165-
},
166-
spread: {
167-
width: MainWidth,
168-
height: MainHeight,
169-
},
170-
actionSheet: {
171-
width: MainWidth,
172-
position: 'absolute',
173-
left: 0,
174-
right: 0,
175-
backgroundColor: '#fff',
176-
zIndex: 9999,
177-
},
178118
actionDivider: {
179119
backgroundColor: 'rgba(197,217,232,.3)',
180120
width: MainWidth,

0 commit comments

Comments
 (0)