Dropdown.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import { FlatList, Keyboard, Pressable, StyleSheet, Modal, View, Text } from 'react-native';
  3. import Button from './Button';
  4. import Dialog from './Dialog';
  5. import TextView from './TextView';
  6. import app from '../../app.json';
  7. //const DialogMaxWidth = $vw(85) > 500 ? 500 : $vw(85);
  8. //const DialogIOSWidth = $vw(75) > 450 ? 450 : $vw(75);
  9. export default Dropdown = ({
  10. list = [],
  11. title = '',
  12. value,
  13. onChange,
  14. nameKey,
  15. valueKey,
  16. prefixText='',//前缀
  17. suffixText='',//后缀
  18. itemHeight=50,
  19. prefixList='',//列表前缀
  20. suffixList='',//列表后缀
  21. rippleStyle=ripple,
  22. style = styles.valueView,
  23. textStyle = styles.valueText,
  24. placeholderStyle=styles.placeText,
  25. placeholder='',
  26. showText = true,
  27. showIcon = true,
  28. iconColor = '#888',
  29. iconStyle = styles.iconStyle,
  30. autoSelect = true,
  31. customerItemView
  32. }) => {
  33. const refFlat = useRef();
  34. const [visible, showDialog] = useState(false);
  35. const [selected, changeValue] = useState('');
  36. const [currentIndex, setCurrent] = useState(0);
  37. useEffect(() => {
  38. if (value !== selected) {
  39. changeItem();
  40. }
  41. }, [value, []]);
  42. useEffect(() => {
  43. if (autoSelect && list.length > 0) {
  44. if (value == undefined) {
  45. const item = list[0];
  46. if (nameKey) {
  47. changeValue(prefixText+item[nameKey]+suffixText);
  48. } else {
  49. changeValue(item);
  50. }
  51. setChange(valueKey ? item[valueKey] : item, 0);
  52. } else {
  53. changeItem(true);
  54. }
  55. }
  56. }, [list]);
  57. const changeItem = (init) => {
  58. if (nameKey && valueKey) {
  59. for (var i = 0; i < list.length; i++) {
  60. let item = list[i];
  61. if (item[valueKey] == value) {
  62. changeValue(prefixText+item[nameKey]+suffixText);
  63. if (list.length > 20) {
  64. setCurrent(i > 5 ? i - 4 : 0);
  65. }
  66. return;
  67. }
  68. }
  69. if (init) {
  70. const item = list[0];
  71. setChange(item[valueKey], 0);
  72. }
  73. } else {
  74. if (init) {
  75. let _i = list.indexOf(value);
  76. if (_i >= 0) {
  77. setChange(list[_i], _i);
  78. } else {
  79. setChange(list[0], 0);
  80. }
  81. } else {
  82. changeValue(prefixText+value+suffixText);
  83. }
  84. }
  85. }
  86. const showList = () => {
  87. Keyboard.dismiss();
  88. showDialog(true);
  89. /*if (currentIndex > 0) {
  90. console.log(refFlat.current);
  91. setTimeout(() => {
  92. if (refFlat.current) {
  93. refFlat.current.scrollToIndex({
  94. animated: false,
  95. index: currentIndex,
  96. viewPosition: 0.5
  97. })
  98. }
  99. }, 100)
  100. }*/
  101. }
  102. const renderItem = ({ item, index, separators }) => {
  103. const _value = (valueKey ? item[valueKey] : item);
  104. if (customerItemView) {
  105. return customerItemView(item, index, () => {
  106. showDialog(false);
  107. setChange(_value, index);
  108. })
  109. } else {
  110. return (
  111. <Button
  112. text={prefixList + (nameKey ? item[nameKey] : item) + suffixList}
  113. style={styles.itemView}
  114. textStyle={styles.itemText}
  115. onClick={() => {
  116. showDialog(false);
  117. setChange(_value, index);
  118. }}
  119. iconRight={(_value == value) && <MaterialIcons name="radio-button-checked" color={colorAccent} size={22}/>}
  120. />
  121. )
  122. }
  123. }
  124. const setChange = (v, i) => {
  125. setTimeout(() => {
  126. if (onChange) {
  127. onChange(v, i)
  128. }
  129. }, 300);
  130. }
  131. return (
  132. <>
  133. <Pressable
  134. style={({pressed }) => [
  135. pressed && isIOS && {
  136. backgroundColor: rippleColor
  137. },
  138. style
  139. ]}
  140. android_ripple={rippleStyle}
  141. onPress={() => showList()}>
  142. { showText &&
  143. ( selected
  144. ? <TextView style={[textStyle, styles.textView]} numberOfLines={1}>{selected}</TextView>
  145. : <TextView style={[placeholderStyle, styles.textView]} numberOfLines={1}>{placeholder}</TextView>
  146. )
  147. }
  148. { showIcon && (isIOS || app.isLumiWhitelabel
  149. ? <MaterialIcons
  150. name={'keyboard-arrow-down'}
  151. size={24}
  152. color={iconColor}
  153. style={iconStyle}
  154. />
  155. : <MaterialIcons
  156. name={'arrow-drop-down'}
  157. size={24}
  158. color={iconColor}
  159. style={iconStyle}
  160. />)
  161. }
  162. </Pressable>
  163. <Modal
  164. visible={visible}
  165. transparent={true}
  166. animationType="fade"
  167. statusBarTranslucent={true}>
  168. <View style={styles.dialog}>
  169. <Text
  170. style={StyleSheet.absoluteFillObject}
  171. onPress={() => showDialog(false)}>
  172. </Text>
  173. <View style={styles.dialogContent}>
  174. { title !== '' && <TextView style={styles.title}>{title}</TextView> }
  175. <FlatList
  176. data={list}
  177. ref={refFlat}
  178. renderItem={renderItem}
  179. initialScrollIndex={currentIndex}
  180. keyExtractor={(item, index) => index}
  181. style={{maxHeight: $vh(55)}}
  182. getItemLayout={(data, index) => (
  183. {length: itemHeight, offset: itemHeight * index, index}
  184. )}
  185. />
  186. </View>
  187. </View>
  188. </Modal>
  189. </>
  190. );
  191. }
  192. const styles = StyleSheet.create({
  193. dialog: {
  194. flex: 1,
  195. alignItems: 'center',
  196. justifyContent: 'center',
  197. backgroundColor: 'rgba(0,0,0,.6)'
  198. },
  199. dialogContent: {
  200. width: Dialog.dialogWidth,
  201. marginLeft: 'auto',
  202. marginRight: 'auto',
  203. paddingTop: isIOS ? 12 : 8,
  204. paddingBottom: isIOS ? 12 : 8,
  205. backgroundColor: colorLight,
  206. borderRadius: isIOS ? 10 : 3
  207. },
  208. title: {
  209. color: '#000',
  210. paddingTop: 8,
  211. paddingLeft: 16,
  212. paddingBottom: 16,
  213. fontSize: 17,
  214. fontWeight: 'bold'
  215. },
  216. valueView: {
  217. paddingLeft: 16,
  218. paddingRight: 8,
  219. alignItems: 'center',
  220. flexDirection: 'row'
  221. },
  222. valueText: {
  223. color: '#000',
  224. fontSize: 16
  225. },
  226. itemView: {
  227. borderRadius: 0,
  228. backgroundColor: colorLight
  229. },
  230. itemText: {
  231. flex: 1,
  232. color: textPrimary,
  233. fontSize: 14,
  234. textAlign: 'left',
  235. fontWeight: 'normal'
  236. },
  237. placeText: {
  238. flex: 1,
  239. color: textPlacehoder
  240. },
  241. iconStyle: {
  242. marginLeft: 8
  243. },
  244. textView: {
  245. flex: 1,
  246. flexDirection: 'column'
  247. }
  248. });