How to update markedDates from onDayPress event
See original GitHub issueDescription
So⦠how to toggle (mark/unmark) dates when onDayPress
was called?
Some like this:
Expected Behavior
I tried to do this with Object.keys(), push and splice but markedDates
havenāt a index to manipulate it, so itās very difficult to me to do this.
Observed Behavior
Date marking
!Disclaimer! Make sure that markedDates param is immutable. If you change markedDates object content but the reference to it does not change calendar update will not be triggered.
Environment
-
yarn info react-native-calendars
:{latest: '1.16.1'}
-
yarn info react-native
:{latest: '0.51.0'}
- Phone/emulator/simulator & version:
Genymotion 2.11.0
Reproducible Demo
The same code can to see below:
some imports:
import React, { Component } from 'react'
import moment from 'moment'
import { ScrollView, View, Text } from 'react-native'
import { Calendar } from 'react-native-calendars'
defining constants:
const _format = 'YYYY-MM-DD'
const _today = moment().format(_format)
const _maxDate = moment().add(15, 'days').format(_format)
// It is not possible to reserve some to current day.
let _markedDates = {
[_today]: {disabled: true}
}
onDayPress method called:
const onDaySelect = (day) => {
const selectedDay = moment(day.dateString).format(_format)
const arrDates = Object.keys(_markedDates)
// .map((value, id, ssss) => ({id, value}))
.map((d, i) => {
console.log('d, i: ', _markedDates.indexOf(i))
// const markedDay = moment(d.value).format(_format)
// if (_today !== markedDay) {
// console.log('push: ', markedDay)
// } else if () {
// console.log('splice: ', d, i)
// }
})
// console.log('array dates: ', arrDates)
}
and the calendar component below:
const WixCalendar = (props) => {
return (
<Calendar
// theme={{
// selectedDayBackgroundColor: 'steelblue',
// dotColor: '#00adf5',
// }}
// we use moment.js to give the minimum and maximum dates.
minDate={_today}
maxDate={_maxDate}
// hideArrows={true}
onDayPress={(day) => onDaySelect(day)}
markedDates={_markedDates}
/>
)
}
export default WixCalendar
EDIT:
Iāve been working to improved this with clean JS:
https://codepen.io/francisrod01/pen/RxKQeO
I clicked in day 31 but the day doesnāt marked in the calendar.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:11
You can track the marked dates as state in a stateful React component, hereās an example Iāve based of your expo example:
@francisrod01 If youāre new to React it will take a while for it to āclickā but keep at it and things like this will be easier! š