import React, {useCallback, useMemo} from 'react'; import {FlatList, SafeAreaView,…
- User
import React, {useCallback, useMemo} from 'react'; import {FlatList, SafeAreaView, StyleSheet, Text, View} from 'react-native';
const Test = () => { const generateYearList = useCallback(() => { const currentYear = new Date().getFullYear(); const yearList = []; for (let i = -50; i < 50; i++) { yearList.push({year: currentYear + i}); } return yearList; }, []);
const monthList = useMemo( () => [ {name: 'January', days: 31}, {name: 'February', days: 28}, {name: 'March', days: 31}, {name: 'April', days: 30}, {name: 'May', days: 31}, {name: 'June', days: 30}, {name: 'July', days: 31}, {name: 'August', days: 31}, {name: 'September', days: 30}, {name: 'October', days: 31}, {name: 'November', days: 30}, {name: 'December', days: 31}, ], [], );
const daysInMonth = useCallback((year, month) => { if (month === 2) { if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) return 29; else return 28; } return [1, 3, 5, 7, 8, 10, 12].includes(month) ? 31 : 30; }, []);
const MonthList = React.memo(({year, month}) => { const currentDate = useMemo(() => new Date(), []); const days = useMemo(() => { const arr = []; const firstDay = new Date(year.year, month.month - 1, 1).getDay(); const daysInMonthCount = daysInMonth(year.year, month.month); for (let i = 1; i <= firstDay; i++) { arr.push(<View key={
empty-${i}} style={[styles.day, styles.emptyDay]} />); } for (let i = 1; i <= daysInMonthCount; i++) { const isCurrentDate = year.year === currentDate.getFullYear() && month.month - 1 === currentDate.getMonth() && i === currentDate.getDate(); arr.push( <Text style={[styles.dayText, isCurrentDate && styles.red]}>{i} , ); } return arr; }, [currentDate, daysInMonth, month, year]);const isCurrentMonth = useMemo(() => year.year === currentDate.getFullYear() && month.month - 1 === currentDate.getMonth(), [ currentDate, month, year, ]); return ( <View style={styles.month}> <Text style={[styles.monthName, isCurrentMonth && styles.red]}>{month.name}</Text> <View style={styles.days}>{days}</View> </View> );});
const YearList = React.memo(({year}) => { const currentDate = useMemo(() => new Date(), []); const monthData = useMemo(() => monthList.map((month, index) => ({...month, month: index + 1})), []); const isCurrentYear = useMemo(() => year.year === currentDate.getFullYear(), [currentDate, year]);
return ( <View> <Text style={[styles.title, isCurrentYear && styles.red]}>{year.year}</Text> <View style={styles.line} /> <View style={styles.monthsContainer}> {monthData.map((month, index) => ( <MonthList key={index} month={month} year={year} /> ))} </View> </View> );});
const yearList = useMemo(() => generateYearList(), [generateYearList]);
const currentYearIndex = yearList.findIndex(item => item.year === new Date().getFullYear());
return ( <FlatList // initialScrollIndex={currentYearIndex} data={yearList} renderItem={({item}) => } keyExtractor={(item, index) => } /> ); };
- Assistant
Để tuỳ chỉnh màn hình giao diện item ngày thành hàng ngang có số lượng là 7, chúng ta cần sử dụng thuộc tính
flexBasisđể chỉ định kích thước chiều rộng cho mỗi ngày trong tuần, cũng như sử dụng thuộc tínhflexWrapđể chỉ định việc các ngày có thể xuống dòng khi cần thiết.Bên dưới là mã lệnh cần được cập nhật trong stylesheet:
days: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between', }, day: { flexBasis: '14%', height: 20, alignItems: 'center', justifyContent: 'center', },Trong đó,
flexBasis: '14%'sẽ định nghĩa rằng mỗi ngày sẽ được hiển thị trên một cột và chiếm 14% của chiều rộng của parent container.flexDirection: 'row'sẽ chỉ định cho các ngày được hiển thị trên một hàng ngang, trong khiflexWrap: 'wrap'sẽ đảm bảo các ngày có thể được xuống dòng khi cần thiết.justifyContent: 'space-between'sẽ chỉ định để các ngày đựng cạnh nhau và căn đều khoảng cách giữa chúng.Sau khi cập nhật các thuộc tính này, item ngày sẽ được hiển thị trên hàng ngang có số lượng 7 ngày, với chiều rộng của mỗi ngày được cân đối đều.