couponTabView.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //the size of the TabView will follow its parent-container's size.
  2. import * as React from 'react';
  3. import {
  4. View,
  5. Text,
  6. useWindowDimensions,
  7. StyleSheet,
  8. ImageSourcePropType,
  9. ScrollView
  10. } from 'react-native';
  11. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  12. import { IndividualCouponComponent } from '../accountPages/walletPageComponent';
  13. export interface TabItem {
  14. imgURL: ImageSourcePropType;
  15. date: string;
  16. time: string;
  17. chargeStationName: string;
  18. chargeStationAddress: string;
  19. distance: string;
  20. }
  21. interface TabViewComponentProps {
  22. titles: string[];
  23. }
  24. const expiredCoupons = [
  25. {
  26. title: 'abc優惠券',
  27. price: '999',
  28. detail: '這是一段有關迎新優惠券的說明',
  29. date: '至14/3/2025'
  30. },
  31. {
  32. title: 'abc優惠券',
  33. price: '888',
  34. detail: '這是另一段有關迎新優惠券的說明',
  35. date: '至15/4/2025'
  36. },
  37. {
  38. title: 'abc優惠券',
  39. price: '777',
  40. detail: '這是第三段有關迎新優惠券的說明',
  41. date: '至16/9/2026'
  42. },
  43. {
  44. title: 'abc優惠券',
  45. price: '777',
  46. detail: '這是第三段有關迎新優惠券的說明',
  47. date: '至16/9/2026'
  48. },
  49. {
  50. title: 'abc優惠券',
  51. price: '777',
  52. detail: '這是第三段有關迎新優惠券的說明',
  53. date: '至16/9/2026'
  54. },
  55. {
  56. title: 'abc優惠券',
  57. price: '777',
  58. detail: '這是第三段有關迎新優惠券的說明',
  59. date: '至16/9/2026'
  60. }
  61. // Add more coupon objects as needed
  62. ];
  63. const coupons = [
  64. {
  65. title: '迎新優惠券',
  66. price: '111',
  67. detail: 'aaaaasdasdasdasd',
  68. date: '至14/3/2025'
  69. },
  70. {
  71. title: '折扣優惠券',
  72. price: '222',
  73. detail: 'dfgdfgdfgdfg',
  74. date: '至15/4/2025'
  75. },
  76. {
  77. title: '三張優惠券',
  78. price: '333',
  79. detail: 'zxczxczxczxczxc',
  80. date: '至16/9/2026'
  81. }
  82. ];
  83. const CouponTabViewComponent: React.FC<TabViewComponentProps> = ({
  84. titles
  85. }) => {
  86. const layout = useWindowDimensions();
  87. //tab 1
  88. const FirstRoute = () => (
  89. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  90. <View className="flex-1 flex-col">
  91. {coupons.map((coupon, index) => (
  92. <IndividualCouponComponent
  93. key={index}
  94. title={coupon.title}
  95. price={coupon.price}
  96. detail={coupon.detail}
  97. date={coupon.date}
  98. />
  99. ))}
  100. </View>
  101. </ScrollView>
  102. );
  103. //tab 2
  104. const SecondRoute = () => (
  105. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  106. <View className="flex-1 flex-col">
  107. {expiredCoupons.map((coupon, index) => (
  108. <IndividualCouponComponent
  109. key={index}
  110. title={coupon.title}
  111. price={coupon.price}
  112. detail={coupon.detail}
  113. date={coupon.date}
  114. />
  115. ))}
  116. </View>
  117. </ScrollView>
  118. );
  119. const renderScene = SceneMap({
  120. firstRoute: FirstRoute,
  121. secondRoute: SecondRoute
  122. });
  123. const [routes] = React.useState([
  124. { key: 'firstRoute', title: titles[0] },
  125. { key: 'secondRoute', title: titles[1] }
  126. ]);
  127. const [index, setIndex] = React.useState(0);
  128. const renderTabBar = (props: any) => (
  129. <TabBar
  130. {...props}
  131. renderLabel={({ route, focused }) => (
  132. <Text
  133. style={{
  134. color: focused ? '#025c72' : '#888888',
  135. fontWeight: focused ? '900' : 'thin',
  136. fontSize: 20
  137. }}
  138. >
  139. {route.title}
  140. </Text>
  141. )}
  142. indicatorStyle={{
  143. backgroundColor: '#025c72'
  144. }}
  145. style={{
  146. backgroundColor: 'white',
  147. borderColor: '#DBE4E8',
  148. elevation: 0,
  149. marginHorizontal: 15,
  150. borderBottomWidth: 0.5
  151. }}
  152. />
  153. );
  154. return (
  155. <TabView
  156. navigationState={{ index, routes }}
  157. renderScene={renderScene}
  158. onIndexChange={setIndex}
  159. initialLayout={{ width: layout.width }}
  160. renderTabBar={renderTabBar}
  161. />
  162. );
  163. };
  164. export default CouponTabViewComponent;
  165. const styles = StyleSheet.create({
  166. container: { flexDirection: 'row' },
  167. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  168. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 }
  169. });