layout.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { ScrollView, View, Text } from "@tarojs/components";
  2. import './layout.scss'
  3. import { NaviBarTitleShowType, TemplateType } from "@/utils/types";
  4. import { useEffect, useState } from "react";
  5. import Taro, { usePageScroll } from "@tarojs/taro";
  6. let useNavigation;
  7. if (process.env.TARO_ENV == 'rn') {
  8. useNavigation = require("@react-navigation/native").useNavigation
  9. }
  10. export default function Layout(props: {
  11. children: React.ReactNode,
  12. type: TemplateType,
  13. refresh?: Function,
  14. more?: Function,
  15. triggered?: boolean,
  16. title?: string,
  17. titleColor?: string,
  18. header?: React.ReactNode,
  19. secondPage?: boolean,
  20. isFastSleepTheme?: boolean,
  21. titleShowStyle: NaviBarTitleShowType
  22. }) {
  23. const { children, type, triggered } = props;
  24. const [showTitle, setShowTitle] = useState(props.titleShowStyle == NaviBarTitleShowType.alwayShow)
  25. let navigation;
  26. if (useNavigation) {
  27. navigation = useNavigation()
  28. }
  29. useEffect(() => {
  30. Taro.setNavigationBarTitle({
  31. title: showTitle ? props.title ? props.title : '' : ''
  32. })
  33. if (navigation) {
  34. navigation.setOptions({
  35. headerTitle: showTitle ? props.title ? props.title : '' : '',
  36. });
  37. }
  38. }, [showTitle])
  39. function onScroll(e) {
  40. console.log('a')
  41. if (props.titleShowStyle == NaviBarTitleShowType.scrollToShow) {
  42. if (e.detail.scrollTop > 70) {
  43. setShowTitle(true)
  44. }
  45. else {
  46. setShowTitle(false)
  47. }
  48. }
  49. }
  50. usePageScroll((e) => {
  51. if (props.titleShowStyle == NaviBarTitleShowType.scrollToShow) {
  52. if (e.scrollTop > 70) {
  53. setShowTitle(true)
  54. }
  55. else {
  56. setShowTitle(false)
  57. }
  58. }
  59. })
  60. function pageDetail() {
  61. return <View>
  62. {
  63. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  64. {
  65. props.isFastSleepTheme ? <Text className='layout_title fast_sleep_text'
  66. style={{
  67. fontSize: props.secondPage ? 28 : 36
  68. }}
  69. >{props.title}</Text> :
  70. <Text className='layout_title'
  71. style={{
  72. color: props.titleColor ? props.titleColor : '#fff',
  73. fontSize: props.secondPage ? 28 : 36
  74. }}
  75. >{props.title}</Text>
  76. }
  77. </View>
  78. }
  79. {
  80. children
  81. }
  82. <View style={{ height: 50 }} />
  83. </View>
  84. }
  85. switch (type) {
  86. case TemplateType.list:
  87. return <ScrollView className="layout_container"
  88. style={{ WebkitOverflowScrolling: 'auto' }}
  89. onScroll={onScroll}
  90. scrollY refresherEnabled={true}
  91. refresherThreshold={100} refresherBackground="#000"
  92. refresherDefaultStyle="white"
  93. onRefresherRefresh={() => { props.refresh!() }}
  94. upperThreshold={50}
  95. onScrollToLower={() => { props.more ? props.more() : null }}
  96. refresherTriggered={triggered}>
  97. {
  98. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  99. <Text className='layout_title'
  100. style={{
  101. color: props.titleColor ? props.titleColor : '#fff',
  102. fontSize: props.secondPage ? 28 : 36
  103. }}
  104. >{props.title}</Text>
  105. </View>
  106. }
  107. {children}
  108. <View style={{ height: 50 }} />
  109. </ScrollView>
  110. case TemplateType.grid:
  111. return <ScrollView className="layout_container"
  112. enableFlex={true}
  113. onScroll={onScroll}
  114. style={{ WebkitOverflowScrolling: 'auto' }}
  115. scrollY refresherEnabled={true}
  116. refresherThreshold={100} refresherBackground="#000"
  117. refresherDefaultStyle="white"
  118. onRefresherRefresh={() => { props.refresh!() }}
  119. upperThreshold={50}
  120. onScrollToLower={() => { props.more ? props.more() : null }}
  121. refresherTriggered={triggered}>
  122. {
  123. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  124. <Text className='layout_title'
  125. style={{
  126. color: props.titleColor ? props.titleColor : '#fff',
  127. fontSize: props.secondPage ? 28 : 36
  128. }}
  129. >{props.title}</Text>
  130. </View>
  131. }
  132. <View className="grid_bg">
  133. {children}
  134. <View style={{ height: 50 }} />
  135. </View>
  136. </ScrollView>
  137. case TemplateType.flex:
  138. return <View className='flex'>
  139. {/* <View className="flex-expand" /> */}
  140. {
  141. process.env.TARO_ENV == 'weapp' ? pageDetail() :
  142. <ScrollView onScroll={onScroll}>
  143. {
  144. pageDetail()
  145. }
  146. </ScrollView>
  147. }
  148. </View>
  149. case TemplateType.customHeader:
  150. return <View className='flex'>
  151. {/* <View className="flex-expand" /> */}
  152. {
  153. props.header
  154. }
  155. {children}
  156. <View style={{ height: 50 }} />
  157. </View>
  158. }
  159. return <View />
  160. }