From 87fcf63ddfc4b1ef5e552b47a2550d48137efe65 Mon Sep 17 00:00:00 2001 From: zhouzheng Date: Fri, 17 Feb 2023 10:00:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ActionSheet):=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=BB=84=E4=BB=B6=E9=87=8D=E6=9E=84=20Class?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/ActionSheet/index.tsx | 132 ++++++++++++------------ packages/core/src/ActionSheet/item.tsx | 35 +++---- 2 files changed, 81 insertions(+), 86 deletions(-) diff --git a/packages/core/src/ActionSheet/index.tsx b/packages/core/src/ActionSheet/index.tsx index a4ecfdc42..b7d903204 100644 --- a/packages/core/src/ActionSheet/index.tsx +++ b/packages/core/src/ActionSheet/index.tsx @@ -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'; @@ -33,84 +33,80 @@ interface ActionSheetState { control: 'props' | 'state'; } -export default class ActionSheet extends React.Component { - 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 ( - - <> - {React.Children.toArray(children).map((item, index) => ( - - {index !== 0 && } - {React.cloneElement(item as React.DetailedReactHTMLElement, { - activeOpacity: activeOpacity, - underlayColor: underlayColor, - })} - - ))} - - - - - ); - } + return ( + + <> + {React.Children.toArray(children).map((item, index) => ( + + {index !== 0 && } + {React.cloneElement(item as React.DetailedReactHTMLElement, { + activeOpacity: activeOpacity, + underlayColor: underlayColor, + })} + + ))} + + + + + ); } const styles = StyleSheet.create({ diff --git a/packages/core/src/ActionSheet/item.tsx b/packages/core/src/ActionSheet/item.tsx index 41da92251..88ab2d7f2 100644 --- a/packages/core/src/ActionSheet/item.tsx +++ b/packages/core/src/ActionSheet/item.tsx @@ -27,24 +27,23 @@ export interface ActionSheetItemProps { export interface ActionSheetItemState {} -export default class ActionSheetItem extends React.Component { - render() { - const { - onPress = () => {}, - activeOpacity = 1, - underlayColor = '#f1f1f1', - containerStyle, - textStyle, - children, - } = this.props; - return ( - - - {children} - - - ); - } +export default function ActionSheetItem(props: ActionSheetItemProps) { + const { + onPress = () => {}, + activeOpacity = 1, + underlayColor = '#f1f1f1', + containerStyle, + textStyle, + children, + } = props; + + return ( + + + {children} + + + ); } const styles = StyleSheet.create({