SelectSpecification.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import * as WebBrowser from 'expo-web-browser';
  2. import * as React from 'react';
  3. import { StyleSheet, View } from 'react-native';
  4. import { ScrollView } from 'react-native-gesture-handler';
  5. import { Modal, Portal } from 'react-native-paper';
  6. import { Div, Image, Text, Button } from 'react-native-magnus';
  7. import { Flex } from '@ant-design/react-native';
  8. import { useCreation, useMap } from '@umijs/hooks';
  9. import useModel from 'flooks';
  10. import Detail from './model';
  11. import Plus from '../../components/Plus';
  12. export default function SelectSpecification() {
  13. const { showSelect, selectInfo, addCart, changeSelect } = useModel(Detail, [
  14. 'showSelect',
  15. 'selectInfo',
  16. ]);
  17. const {
  18. id,
  19. img,
  20. name,
  21. discountAmount,
  22. amount,
  23. specifications,
  24. } = selectInfo || { specifications: [] };
  25. // 全部的分类 一级包含二级形式 list
  26. const selectspecifications = useCreation(() => {
  27. return specifications.filter((item) => {
  28. return !item.parent && item.children.length > 0;
  29. });
  30. }, [specifications]);
  31. // 全部的一级分类的 Map
  32. const classify1Map = useCreation(() => {
  33. const map = new Map();
  34. selectspecifications.forEach((item) => {
  35. map.set(item.id, item);
  36. });
  37. return map;
  38. }, [selectspecifications]);
  39. // 选择模块
  40. const [selectMap, selectMapEvent] = useMap([]);
  41. React.useEffect(() => {
  42. selectMapEvent.reset();
  43. }, [id]);
  44. // 全部选中的二级分类 list
  45. const selectClassify2 = useCreation(() => {
  46. let list = [];
  47. [...selectMap.values()].forEach((item) => {
  48. list = list.concat([...item.values()]);
  49. });
  50. return list;
  51. }, [selectMap]);
  52. // 全部选中的二级分类id list
  53. const selectClassifyIds = useCreation(() => {
  54. return selectClassify2.map((item) => {
  55. return item.id;
  56. });
  57. }, [selectClassify2]);
  58. // 未被选的一级分类
  59. const notSelectClassify1 = useCreation(() => {
  60. return [...classify1Map.values()].filter((item) => {
  61. return !selectMap.get(item.id) || selectMap.get(item.id).size === 0;
  62. });
  63. }, [selectMap, classify1Map]);
  64. // 总价
  65. const totalAmount = useCreation(() => {
  66. let money = discountAmount || amount;
  67. selectClassify2.forEach((item) => {
  68. money += item.amount || 0;
  69. });
  70. return money;
  71. }, [discountAmount, amount, selectClassify2]);
  72. return (
  73. <Portal>
  74. <Modal
  75. animationType="slide"
  76. visible={showSelect}
  77. onDismiss={() => changeSelect(false)}
  78. contentContainerStyle={styles.contentContainerStyle}
  79. >
  80. <Flex align="stretch">
  81. <Image
  82. w={80}
  83. h={80}
  84. style={styles.icon}
  85. resizeMode="cover"
  86. source={{ uri: img }}
  87. />
  88. <Flex.Item style={styles.info}>
  89. <Text size="s1" bold>
  90. {name}
  91. </Text>
  92. {selectClassify2.length !== 0 && (
  93. <Text fontSize="sm" color="gray300">
  94. 已选择{' '}
  95. {selectClassify2
  96. .map((item) => {
  97. return item.name;
  98. })
  99. .join('/')}
  100. </Text>
  101. )}
  102. <Flex.Item />
  103. <Text fontSize="sm" color="red500">
  104. ¥{totalAmount}
  105. {discountAmount !== null && (
  106. <Text
  107. textdecorationline="line-through"
  108. fontSize="sm"
  109. color="gray300"
  110. ml={10}
  111. >
  112. ¥{amount}
  113. </Text>
  114. )}
  115. </Text>
  116. </Flex.Item>
  117. {/* <Plus
  118. plusEvent={() => {
  119. addCart(id);
  120. }}
  121. /> */}
  122. </Flex>
  123. <ScrollView contentContainerStyle={{ flexGrow: 1 }}>
  124. {selectspecifications.map((item) => {
  125. return (
  126. <Div key={item.id} py={20}>
  127. <Text>{item.name}</Text>
  128. <Div row flexWrap="wrap">
  129. {item.children.map((child) => {
  130. const choosed = selectClassifyIds.indexOf(child.id) !== -1;
  131. return (
  132. <Div key={child.id} w="33.33%" px={5} mt={5}>
  133. <Button
  134. rounded="xs"
  135. bg={choosed ? 'brand200' : 'gray200'}
  136. color={choosed ? 'brand500' : 'gray600'}
  137. fontSize="xs"
  138. block
  139. onPress={() => {
  140. if (choosed) {
  141. const selects = selectMapEvent.get(child.parent);
  142. selects.delete(child.id);
  143. selectMapEvent.set(child.parent, selects);
  144. } else if (
  145. classify1Map.get(child.parent).multiple &&
  146. selectMapEvent.get(child.parent)
  147. ) {
  148. const selects = selectMapEvent.get(child.parent);
  149. selects.set(child.id, child);
  150. selectMapEvent.set(child.parent, selects);
  151. } else {
  152. selectMapEvent.set(
  153. child.parent,
  154. new Map([[child.id, child]])
  155. );
  156. }
  157. }}
  158. >
  159. {child.name}
  160. </Button>
  161. </Div>
  162. );
  163. })}
  164. </Div>
  165. </Div>
  166. );
  167. })}
  168. </ScrollView>
  169. <Button
  170. disabled={notSelectClassify1.length}
  171. block
  172. bg="brand500"
  173. fontSize="xl"
  174. onPress={() => addCart(id, selectClassifyIds.join(','), 1)}
  175. >
  176. 选好了
  177. </Button>
  178. </Modal>
  179. </Portal>
  180. );
  181. }
  182. const styles = StyleSheet.create({
  183. contentContainerStyle: {
  184. backgroundColor: '#fff',
  185. height: '70%',
  186. position: 'absolute',
  187. bottom: 0,
  188. left: 0,
  189. right: 0,
  190. padding: 20,
  191. justifyContent: 'flex-start',
  192. zIndex: 3,
  193. },
  194. icon: {
  195. width: 80,
  196. height: 80,
  197. borderRadius: 3,
  198. },
  199. info: {
  200. marginLeft: 8,
  201. },
  202. list: {
  203. paddingTop: 15,
  204. },
  205. });