birthday_select.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, { useState } from 'react';
  2. import { View, StyleSheet } from 'react-native';
  3. import DropdownSelect from './dropdown_select';
  4. type BirthdaySelectProps = {
  5. onSelect: (year: string, month: string, day: string) => void;
  6. };
  7. const BirthdaySelect: React.FC<BirthdaySelectProps> = ({ onSelect }) => {
  8. const [selectedYear, setSelectedYear] = useState('');
  9. const [selectedMonth, setSelectedMonth] = useState('');
  10. const [selectedDay, setSelectedDay] = useState('');
  11. const birthYearOptions = Array.from({ length: 100 }, (_, i) => {
  12. const year = new Date().getFullYear() - i;
  13. return { label: year.toString(), value: year.toString() };
  14. });
  15. const birthMonthOptions = Array.from({ length: 12 }, (_, i) => {
  16. const month = i + 1;
  17. return { label: month.toString(), value: month.toString() };
  18. });
  19. const birthDayOptions = Array.from({ length: 31 }, (_, i) => {
  20. const day = i + 1;
  21. return { label: day.toString(), value: day.toString() };
  22. });
  23. const handleSelect = (type: string, value: string) => {
  24. if (type === 'year') {
  25. setSelectedYear(value);
  26. } else if (type === 'month') {
  27. setSelectedMonth(value);
  28. } else if (type === 'day') {
  29. setSelectedDay(value);
  30. }
  31. if (selectedYear && selectedMonth && selectedDay) {
  32. onSelect(selectedYear, selectedMonth, selectedDay);
  33. }
  34. };
  35. return (
  36. <View style={styles.container}>
  37. <DropdownSelect
  38. onSelect={(value) => handleSelect('year', value)}
  39. dropdownOptions={birthYearOptions}
  40. placeholder={'年份'}
  41. extendedStyle={{ paddingLeft: 10 }}
  42. />
  43. <DropdownSelect
  44. onSelect={(value) => handleSelect('month', value)}
  45. dropdownOptions={birthMonthOptions}
  46. placeholder={'月份'}
  47. extendedStyle={{ paddingLeft: 10 }}
  48. />
  49. <DropdownSelect
  50. onSelect={(value) => handleSelect('day', value)}
  51. dropdownOptions={birthDayOptions}
  52. placeholder={'日期'}
  53. extendedStyle={{ paddingLeft: 10 }}
  54. />
  55. </View>
  56. );
  57. };
  58. const styles = StyleSheet.create({
  59. container: {
  60. display: 'flex',
  61. flexDirection: 'row',
  62. gap: 10
  63. }
  64. });
  65. export default BirthdaySelect;