dropdown_select.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useState } from 'react';
  2. import { StyleProp, StyleSheet, Text, View, ViewStyle } from 'react-native';
  3. import { Dropdown } from 'react-native-element-dropdown';
  4. interface DropdownOption {
  5. label: string;
  6. value: string;
  7. }
  8. interface DropdownSelectProps {
  9. dropdownOptions: DropdownOption[]; //receive an array of key-value pair objects for dropdown options. Example: { label: '男', value: 'male' },{ label:'女', value: 'female'}
  10. placeholder: string;
  11. maxHeight?: number;
  12. extendedStyle?: StyleProp<ViewStyle>;
  13. onSelect: (value: string) => void;
  14. }
  15. const DropdownSelect: React.FC<DropdownSelectProps> = ({
  16. dropdownOptions,
  17. placeholder,
  18. maxHeight,
  19. extendedStyle,
  20. onSelect
  21. }) => {
  22. const [value, setValue] = useState<string | null>(null);
  23. const [isFocus, setIsFocus] = useState(false);
  24. return (
  25. <View style={styles.container}>
  26. <Dropdown
  27. data={dropdownOptions}
  28. style={[styles.dropdown, extendedStyle]}
  29. placeholderStyle={styles.placeholderStyle}
  30. selectedTextStyle={styles.selectedTextStyle}
  31. itemTextStyle={{ color: '#888888' }}
  32. containerStyle={{ borderRadius: 12 }}
  33. search={false}
  34. maxHeight={maxHeight ? maxHeight : 150}
  35. labelField="label"
  36. valueField="value"
  37. placeholder={!isFocus ? placeholder : '...'}
  38. searchPlaceholder="Search..."
  39. value={value}
  40. autoScroll={false}
  41. onFocus={() => setIsFocus(true)}
  42. onBlur={() => setIsFocus(false)}
  43. onChange={(item) => {
  44. setValue(item.value);
  45. setIsFocus(false);
  46. onSelect(item.value);
  47. }}
  48. />
  49. </View>
  50. );
  51. };
  52. const styles = StyleSheet.create({
  53. container: {
  54. flex: 1,
  55. maxWidth: '100%',
  56. position: 'relative'
  57. },
  58. dropdown: {
  59. flex: 1,
  60. borderColor: '#bbbbbb',
  61. borderWidth: 1,
  62. borderRadius: 12,
  63. zIndex: 10000,
  64. paddingRight: 15
  65. },
  66. selectedTextStyle: {
  67. color: '#888',
  68. fontSize: 16,
  69. paddingLeft: 6
  70. },
  71. placeholderStyle: {
  72. color: '#888',
  73. paddingLeft: 6
  74. }
  75. });
  76. export default DropdownSelect;