11import { number } from 'prop-types' ;
2- import React , { useState , useRef , useEffect , useMemo , Fragment } from 'react' ;
2+ import React , { useState , useRef } from 'react' ;
33import {
44 StyleSheet ,
55 TouchableOpacity ,
@@ -21,25 +21,22 @@ const DEVICE_WIDTH = Dimensions.get('window').width;
2121const DEVICE_HEIGHT = Dimensions . get ( 'window' ) . height ;
2222
2323export interface DragDrawerProps extends ViewProps {
24+ /** 抽屉高度 */
2425 drawerHeight ?: number ;
26+ /** 抽屉颜色 */
2527 drawerBackgroundColor ?: string ;
28+ /** 自定义图标 */
29+ icon ?: IconsName | React . ReactElement | React . ReactNode ;
2630 children ?: React . ReactNode ;
2731}
28- export interface DragDrawerProps extends ViewProps { }
32+
2933function DragDrawer ( props : DragDrawerProps ) {
30- const { drawerBackgroundColor = '#fff' , drawerHeight = 300 , children } = props ;
34+ const { drawerBackgroundColor = '#fff' , drawerHeight = 300 , children, icon } = props ;
3135
32- const [ zIndexValue , setZIndexValue ] = useState ( 0 ) ;
3336 const [ animatedViewHeight , setAnimatedViewHeight ] = useState ( new Animated . Value ( drawerHeight ) ) ;
3437 const [ viewHeight , setViewHeight ] = useState ( drawerHeight ) ;
3538 const [ showAnimate , setShowAnimate ] = useState ( false ) ;
3639
37- const openDrawer = ( ) => {
38- setZIndexValue ( 3002 ) ;
39- } ;
40- const closeDrawer = ( ) => {
41- setZIndexValue ( 0 ) ;
42- } ;
4340 const dynamicDrawerStyles : any = {
4441 backgroundColor : drawerBackgroundColor ,
4542 top : null ,
@@ -48,6 +45,9 @@ function DragDrawer(props: DragDrawerProps) {
4845 width : '100%' ,
4946 } ;
5047
48+ /**
49+ * 拖曳效果动画
50+ * */
5151 const panResponder = useRef (
5252 PanResponder . create ( {
5353 onMoveShouldSetPanResponder : ( ) => true ,
@@ -65,7 +65,6 @@ function DragDrawer(props: DragDrawerProps) {
6565 } , // 可选的异步监听函数
6666 ) ( evt , gestureState ) ;
6767 } ,
68-
6968 onPanResponderRelease : ( e , gestureState ) => {
7069 let values = DEVICE_HEIGHT + 30 - gestureState . moveY ;
7170 if ( gestureState . dy >= 0 ) {
@@ -104,30 +103,35 @@ function DragDrawer(props: DragDrawerProps) {
104103 } ) ,
105104 ) . current ;
106105
106+ /**
107+ * 自定义图标
108+ * */
109+ const IconCustom = ( icon ?: IconsName | React . ReactElement | React . ReactNode ) => {
110+ if ( icon ) {
111+ return < > { typeof icon === 'string' ? < Icon name = { icon as IconsName } size = { 25 } color = "#8F8F8F" /> : icon } </ > ;
112+ } else {
113+ return < Icon name = "minus" size = { 25 } color = "#8F8F8F" /> ;
114+ }
115+ } ;
116+
107117 return (
108- < Fragment >
109- < Animated . View
110- style = { [
111- styles . drawer ,
112- dynamicDrawerStyles ,
113- {
114- height : animatedViewHeight ,
115- } ,
116- ] }
117- { ...panResponder . panHandlers }
118- >
119- < Animated . View style = { [ styles . viewPosition ] } >
120- < View style = { { } } >
121- < TouchableOpacity activeOpacity = { 1 } >
122- < View style = { [ styles . homeContainer ] } >
123- < Icon name = "minus" size = { 25 } fill = "#8F8F8F" />
124- </ View >
125- </ TouchableOpacity >
126- </ View >
127- </ Animated . View >
128- { children }
118+ < Animated . View
119+ style = { [
120+ styles . drawer ,
121+ dynamicDrawerStyles ,
122+ {
123+ height : animatedViewHeight ,
124+ } ,
125+ ] }
126+ { ...panResponder . panHandlers }
127+ >
128+ < Animated . View style = { [ styles . viewPosition ] } >
129+ < TouchableOpacity activeOpacity = { 1 } >
130+ < View style = { [ styles . homeContainer ] } > { IconCustom ( icon ) } </ View >
131+ </ TouchableOpacity >
129132 </ Animated . View >
130- </ Fragment >
133+ { children }
134+ </ Animated . View >
131135 ) ;
132136}
133137
@@ -136,7 +140,6 @@ const styles = StyleSheet.create({
136140 flexDirection : 'row' ,
137141 justifyContent : 'center' ,
138142 alignItems : 'center' ,
139- backgroundColor : '#fff' ,
140143 } ,
141144 homeContainer : {
142145 width : 50 ,
@@ -153,17 +156,6 @@ const styles = StyleSheet.create({
153156 flex : 1 ,
154157 zIndex : 3004 ,
155158 } ,
156- positionFull : {
157- position : 'absolute' ,
158- top : 0 ,
159- bottom : 0 ,
160- left : 0 ,
161- right : 0 ,
162- } ,
163- overlay : {
164- backgroundColor : '#000' ,
165- zIndex : 3002 ,
166- } ,
167159} ) ;
168160
169161export default DragDrawer ;
0 commit comments