Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 64 additions & 68 deletions packages/core/src/ActionSheet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { View, Dimensions, StyleSheet, TextStyle, StyleProp, ViewStyle } from 'react-native';
import Modal, { ModalProps } from '../Modal';
import ActionSheetItem from './item';
Expand Down Expand Up @@ -33,84 +33,80 @@ interface ActionSheetState {
control: 'props' | 'state';
}

export default class ActionSheet extends React.Component<ActionSheetProps, ActionSheetState> {
constructor(props: ActionSheetProps) {
super(props);
this.state = {
stateVisible: !!props.visible,
control: 'props',
};
}
static getDerivedStateFromProps(props: ActionSheetProps, state: ActionSheetState) {
export default function ActionSheet(props: ActionSheetProps) {
const {
children,
visible: props_visible,
activeOpacity,
underlayColor,
cancelText = '取消',
dividerStyle,
onCancel,
containerStyle,
textStyle,
...other
} = props;

const visible = !!props_visible;

const [state, setState] = useState({
stateVisible: !!visible,
control: 'props',
});

useEffect(() => {
if (props.visible === state.stateVisible && state.control === 'state') {
return {
setState({
control: 'props',
stateVisible: props.visible,
};
});
}
if (props.visible !== state.stateVisible) {
if (state.control === 'state') {
return {
control: 'props',
};
setState({ ...state, control: 'props' });
}
return {
setState({
control: 'props',
stateVisible: props.visible,
};
stateVisible: !!props.visible,
});
}
return null;
}
onClose = () => {
this.setState({ stateVisible: false, control: 'state' });
}, [state.stateVisible]);

const onClose = () => {
setState({ stateVisible: false, control: 'state' });
};

render() {
const {
children,
visible,
activeOpacity,
underlayColor,
cancelText = '取消',
dividerStyle,
onCancel,
containerStyle,
textStyle,
...other
} = this.props;
const { stateVisible } = this.state;
return (
<Modal
placement="bottom"
animationType="fade" // slide none fade
transparent={true}
{...other}
visible={stateVisible}
onClosed={this.onClose}
>
<>
{React.Children.toArray(children).map((item, index) => (
<View key={index}>
{index !== 0 && <View style={StyleSheet.flatten([styles.itemDivider, dividerStyle?.itemDivider])} />}
{React.cloneElement(item as React.DetailedReactHTMLElement<any, HTMLElement>, {
activeOpacity: activeOpacity,
underlayColor: underlayColor,
})}
</View>
))}
<View style={StyleSheet.flatten([styles.actionDivider, dividerStyle?.actionDivider])} />
<ActionSheetItem
activeOpacity={activeOpacity}
underlayColor={underlayColor}
onPress={this.onClose}
children={cancelText}
containerStyle={containerStyle}
textStyle={textStyle}
/>
</>
</Modal>
);
}
return (
<Modal
placement="bottom"
animationType="fade" // slide none fade
transparent={true}
{...other}
visible={state.stateVisible}
onClosed={onClose}
>
<>
{React.Children.toArray(children).map((item, index) => (
<View key={index}>
{index !== 0 && <View style={StyleSheet.flatten([styles.itemDivider, dividerStyle?.itemDivider])} />}
{React.cloneElement(item as React.DetailedReactHTMLElement<any, HTMLElement>, {
activeOpacity: activeOpacity,
underlayColor: underlayColor,
})}
</View>
))}
<View style={StyleSheet.flatten([styles.actionDivider, dividerStyle?.actionDivider])} />
<ActionSheetItem
activeOpacity={activeOpacity}
underlayColor={underlayColor}
onPress={onClose}
children={cancelText}
containerStyle={containerStyle}
textStyle={textStyle}
/>
</>
</Modal>
);
}

const styles = StyleSheet.create({
Expand Down
35 changes: 17 additions & 18 deletions packages/core/src/ActionSheet/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@ export interface ActionSheetItemProps {

export interface ActionSheetItemState {}

export default class ActionSheetItem extends React.Component<ActionSheetItemProps, ActionSheetItemState> {
render() {
const {
onPress = () => {},
activeOpacity = 1,
underlayColor = '#f1f1f1',
containerStyle,
textStyle,
children,
} = this.props;
return (
<TouchableHighlight activeOpacity={activeOpacity} underlayColor={underlayColor} onPress={onPress}>
<View style={StyleSheet.flatten([styles.actionSheetItem, containerStyle])}>
<Text style={StyleSheet.flatten([styles.actionSheetItemText, textStyle])}>{children}</Text>
</View>
</TouchableHighlight>
);
}
export default function ActionSheetItem(props: ActionSheetItemProps) {
const {
onPress = () => {},
activeOpacity = 1,
underlayColor = '#f1f1f1',
containerStyle,
textStyle,
children,
} = props;

return (
<TouchableHighlight activeOpacity={activeOpacity} underlayColor={underlayColor} onPress={onPress}>
<View style={StyleSheet.flatten([styles.actionSheetItem, containerStyle])}>
<Text style={StyleSheet.flatten([styles.actionSheetItemText, textStyle])}>{children}</Text>
</View>
</TouchableHighlight>
);
}

const styles = StyleSheet.create({
Expand Down