1- import React from 'react' ;
2- import { View , StyleSheet , Text , Animated , LayoutChangeEvent , Easing } from 'react-native' ;
1+ import React , { useState } from 'react' ;
2+ import { View , StyleSheet , Text , Animated , LayoutChangeEvent } from 'react-native' ;
33import Item from './item' ;
44import Button , { ButtonProps } from '../Button' ;
55import Icon , { IconsName } from '../Icon' ;
@@ -15,91 +15,74 @@ interface IState {
1515 listHeight : number ;
1616}
1717
18- export default class MenuDropdown extends React . Component < MenuDropdownProps > {
19- static defaultProps : MenuDropdownProps = {
20- title : '菜单' ,
21- } ;
22-
23- static Item : typeof Item ;
24-
25- state : IState = {
18+ export default function MenuDropdown ( props : MenuDropdownProps ) {
19+ const [ state , setState ] = useState < IState > ( {
2620 btnIcon : 'down' ,
2721 visibleMenu : false ,
2822 listHeightValue : new Animated . Value ( 0 ) ,
2923 listHeight : 0 ,
30- } ;
24+ } ) ;
25+
26+ const { title = '菜单' , children, size, ...btnProps } = props ;
27+ const { btnIcon, listHeightValue, listHeight, visibleMenu } = state ;
3128
32- handleonPress = ( ) => {
33- const { visibleMenu } = this . state ;
34- this . setState ( {
29+ const handleonPress = ( ) => {
30+ setState ( {
31+ ... state ,
3532 visibleMenu : ! visibleMenu ,
3633 btnIcon : visibleMenu ? 'down' : 'up' ,
3734 } ) ;
3835 if ( visibleMenu ) {
39- this . animateClose ( ) ;
36+ animateClose ( ) ;
4037 } else {
41- this . animateStart ( ) ;
38+ animateStart ( ) ;
4239 }
4340 } ;
44-
45- animateStart = ( ) => {
46- Animated . timing ( this . state . listHeightValue , {
41+ const animateStart = ( ) => {
42+ Animated . timing ( listHeightValue , {
4743 toValue : 1 ,
4844 duration : 500 ,
4945 useNativeDriver : false , // 动画值在不同的驱动方式之间是不能兼容的。因此如果你在某个动画中启用了原生驱动,那么所有和此动画依赖相同动画值的其他动画也必须启用原生驱动。
5046 } ) . start ( ) ;
5147 } ;
52-
53- animateClose = ( ) => {
54- // this.setState({
55- // listHeightValue: new Animated.Value(0),
56- // });
57- Animated . timing ( this . state . listHeightValue , {
48+ const animateClose = ( ) => {
49+ Animated . timing ( listHeightValue , {
5850 toValue : 0 ,
5951 duration : 400 ,
6052 useNativeDriver : false , // 动画值在不同的驱动方式之间是不能兼容的。因此如果你在某个动画中启用了原生驱动,那么所有和此动画依赖相同动画值的其他动画也必须启用原生驱动。
6153 } ) . start ( ) ;
6254 } ;
63-
64- menuContainer = ( event : LayoutChangeEvent ) => {
55+ const menuContainer = ( event : LayoutChangeEvent ) => {
6556 const { height } = event . nativeEvent . layout ;
66- this . setState ( {
57+ setState ( {
58+ ...state ,
6759 listHeight : height ,
6860 } ) ;
6961 } ;
70-
71- render ( ) {
72- const { title, children, size, ...btnProps } = this . props ;
73-
74- const { btnIcon, listHeightValue, listHeight } = this . state ;
75- return (
76- < View style = { [ styles . menuBox , { height : this . state . listHeight + 50 } ] } >
77- < Button { ...btnProps } onPress = { this . handleonPress } size = { size } >
78- < Text > { title } </ Text >
79- < Icon name = { btnIcon } size = { 17 } />
80- </ Button >
81- { /* {
82- visibleMenu && */ }
83- < Animated . View
84- style = { [
85- styles . list ,
86- // eslint-disable-next-line
87- {
88- opacity : listHeightValue ,
89- height : listHeightValue . interpolate ( {
90- inputRange : [ 0 , 1 ] ,
91- outputRange : [ 0 , listHeight || 5 ] ,
92- } ) ,
93- top : size === 'large' ? 35 : size === 'small' ? 21 : 30 ,
94- } ,
95- ] }
96- >
97- < View onLayout = { this . menuContainer } > { children } </ View >
98- </ Animated . View >
99- { /* } */ }
100- </ View >
101- ) ;
102- }
62+ return (
63+ < View style = { [ styles . menuBox , { height : listHeight + 50 } ] } >
64+ < Button { ...btnProps } onPress = { handleonPress } size = { size } >
65+ < Text > { title } </ Text >
66+ < Icon name = { btnIcon } size = { 17 } />
67+ </ Button >
68+ < Animated . View
69+ style = { [
70+ styles . list ,
71+ // eslint-disable-next-line
72+ {
73+ opacity : listHeightValue ,
74+ height : listHeightValue . interpolate ( {
75+ inputRange : [ 0 , 1 ] ,
76+ outputRange : [ 0 , listHeight || 5 ] ,
77+ } ) ,
78+ top : size === 'large' ? 35 : size === 'small' ? 21 : 30 ,
79+ } ,
80+ ] }
81+ >
82+ < View onLayout = { menuContainer } > { children } </ View >
83+ </ Animated . View >
84+ </ View >
85+ ) ;
10386}
10487
10588MenuDropdown . Item = Item ;
0 commit comments