Comments (9)
Hi @tautvilas @mogarick I have implemented an year list which appears on the click of year . You can select a particular year and navigate back to current month of that year . There are two props minYear and maxYear for Calendar component using which max and min year can be specified . If not then 1900 for min and current year for max will be set .
FInd the code here .
https://github.com/bansalayush/react-native-calendars/tree/feat/year-flow
I hope this helps
from react-native-calendars.
@ducpt2 I've implemented the above snippet:
First you need to apply the changes in this pull request to react-native-calendars.
Then use this code:
import { Calendar } from 'react-native-calendars';
import ReversedFlatList from 'react-native-reversed-flat-list';
const initYear = 1949;
const currentYear = Number((new Date()).getFullYear());
const years = Array(currentYear - initYear + 3).fill().map((v,i)=>i + 1);
// react-native-calendar + select year from list on press year in calendar header
export class CalendarWithList extends Component<any,any> {
constructor(props) {
super(props);
this.state = {
month: undefined,
yearListVisible: false,
current: this.props.current,
}
this.renderYear = this.renderYear.bind(this);
this.onPressYear = this.onPressYear.bind(this);
this.onPressHeader = this.onPressHeader.bind(this);
}
componentWillReceiveProps(nextProps) {
if(this.props.current !== nextProps.current) {
this.setState({current: nextProps.current})
}
}
renderYear({item}) {
const year = initYear + item;
return <TouchableOpacity
onPress={() => this.onSelectYear(year)}
style={{height: 50, alignItems: 'center'}}>
<Text>{year}</Text>
</TouchableOpacity>
}
onPressYear(month) {
const current = month.toString('yyyy-MM-dd');
this.setState({
yearListVisible: true,
month,
current
})
}
onPressHeader() {
this.setState({
yearListVisible: false,
})
}
onSelectYear(year) {
this.setState(({month}) => {
month.setFullYear(year);
const current = month.toString('yyyy-MM-dd');
return {
yearListVisible: false,
current
}
})
}
render() {
const { yearListVisible, month } = this.state;
const themeForMonth = {
textMonthFontFamily: 'System',
textMonthFontWeight: '300',
textMonthFontSize: 16,
...this.props.theme
};
return yearListVisible ?
<View style={[{flex: 1}, this.props.style]}>
<View style={{padding: 10, height: 45, justifyContent: 'center'}}>
<TouchableOpacity onPress={this.onPressHeader} style={{alignItems: 'center'}}>
<Text style={{
fontSize: themeForMonth.textMonthFontSize,
fontFamily: themeForMonth.textMonthFontFamily,
fontWeight: themeForMonth.textMonthFontWeight,
color: themeForMonth.monthTextColor || themeForMonth.textDefaultColor || '#2d4150'}}>
{month.toString('MMMM yyyy')}
</Text>
</TouchableOpacity>
</View>
<ReversedFlatList
style={{marginTop: 5, maxHeight: 270}}
data={years}
keyExtractor={item => item}
renderItem={this.renderYear}
/>
</View> :
<Calendar {...this.props}
current={this.state.current}
onPressYear={this.onPressYear}
/>
}
Hope it helps
from react-native-calendars.
Thank you for your answer @tautvilas.
I think it would be really great to integrate such kind of functionality in the calendar components.
I was using an awesome component (IMHO) called react-infinite-calendar by @clauderic.
Unfortunately it's not fully implemented yet for react native. There's only a partial implementation as a concept proof.
I started toward completing the React Native version but time is not on my side and that's why I needed to look elsewhere for a React Native solution and I found Wix React Native Calendars.
Maybe you can look at React Infinite Calendar and think about doing something similar or even joining forces with @clauderic for the CalendarList component. I'd like to know about what you think.
Meanwhile I'm gonna try to make some time to continue with my effort with Infinite Calendar or with your CalendarList component to add this functionality.
Thank you again for your attention. 👍
from react-native-calendars.
Hi, currently such components are not being planned for this package. I would suggest you to implement your own year/month pickers using platform native combobox components. After user chooses year/month you can update current
prop of <Calendar />
to allow user to interact with that month.
from react-native-calendars.
try this and let me know if there are any errors
import React, { useState, useEffect } from 'react';
import { View, Text, TouchableNativeFeedback, TouchableOpacity } from 'react-native';
import { Calendar, LocaleConfig } from 'react-native-calendars';
import dayjs from 'dayjs';
const CustomCalendar = () => {
const [arr, setArr] = useState([]);
const [customDate, setCustomDate] = useState(dayjs());
const [isModalVisible, setIsModalVisible] = useState(false);
useEffect(() => {
for (let i = dayjs().year(); i > dayjs().year() - 100; i--) {
setArr((prev) => [...prev, i]);
}
}, []);
return (
<>
<Calendar
renderHeader={() => (
<TouchableNativeFeedback onPress={() => setIsYearModalVisible(true)}>
<View>
<Text>
{customDate.month() + 1}월 {customDate.year()}
</Text>
</View>
</TouchableNativeFeedback>
)}
onPressArrowLeft={() => setCustomDate((prev) => dayjs(prev.format('YYYY-MM-DD')).subtract(1, 'month'))}
onPressArrowRight={() => setCustomDate((prev) => dayjs(prev.format('YYYY-MM-DD')).add(1, 'month'))}
initialDate={customDate.format('YYYY-MM-DD')}
allowSelectionOutOfRange
current={customDate.format('YYYY-MM-DD').toString()}
markingType='multi-dot'
markedDates={{ [answers[item.id]]: { selected: true } }}
onDayPress={({ dateString }) => {
setAnswers({
...answers,
[item.id]: dateString,
});
}}
/>
{isModalVisible && arr.length > 0 && (
<View
style={{
zIndex: 10,
position: 'absolute',
width: '100%',
height: '100%',
backgroundColor: 'green',
}}
>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: 40,
}}
>
<View style={{ flex: 7, alignItems: 'center' }}>
<Text style={{ fontSize: 20 }}>select Year</Text>
</View>
<TouchableNativeFeedback onPress={() => setIsModalVisible(false)}>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}
></View>
</TouchableNativeFeedback>
</View>
<ScrollView>
<View
style={{
alignItems: 'center',
zIndex: 25,
padding: 15,
width: '100%',
height: '100%',
}}
>
{arr.map((year) => (
<TouchableOpacity
key={year}
onPress={() => {
setCustomDate((prev) => dayjs().subtract(dayjs().year() - year, 'years'));
setIsModalVisible(false);
}}
>
<View style={{ padding: 20, width: '100%' }}>
<Text style={{ fontSize: 16, fontWeight: 'bold' }}>{year}</Text>
</View>
</TouchableOpacity>
))}
</View>
</ScrollView>
</View>
)}
</>
);
};
from react-native-calendars.
@bansalayush The link you dropped to your code is not updated. Just couldn't find it!
Could you drop me an updated link? I'd love to use it, I'ts exactly what I need!
Thanks!
from react-native-calendars.
Will update the code over the weekend and post it back here . :)
from react-native-calendars.
@bansalayush can you share your code or suggest the way you do that?
This is awesome. Thank you alot.
from react-native-calendars.
Will update the code over the weekend and post it back here . :)
The GitHub page above that you posted is no longer there. Is it possible for you to paste the code here. I am looking at building this functionality and this would help tremendously. Thanks.
from react-native-calendars.
Related Issues (20)
- ExpandableCalendar
- Performance : limit rerender of calendar
- March 31 missing in Calendar
- Expandable calendar height issue: Last week of month is not shown with customized design HOT 4
- Invariant Violation: requireNativeComponent: "RNSVGSvgView" was not found in the UIManager got this issue every time when trying to install react-native-calender
- Create Event similar to IOS calendars with Timeline compoenent, on LongPress where backgorundColor changes and reflects selectedTime of 1h?
- Facing date selection change issue on Expandable calendar week change
- im facing issue while long pressing on timeline im getting a day befores date,as an event
- Expandable calendar always shows the first day auto selected HOT 8
- How to get Data inside AgendaList
- Use custom child component rather Agenda/Timeline while using CalendarProvider HOT 1
- Calender
- bug in calendarListScreen.tsx in Example
- Unable to select days when ExpandableCalendar is closed
- TypeError: Cannot read property 'getTime' of undefined, js engine: hermes when using custom component
- dayComponent too many renders HOT 1
- UI lags when rendering a lot of calendars in CalendarList
- Custom header of CalendarProvider is error
- ExpandableCalendar error under React native 0.74 HOT 5
- TimeLineList Props to achieve a horizontal orientation || Documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-calendars.