Comments (15)
I should have followed up here earlier, I disabled based on month change. This worked out perfectly.
const DISABLED_DAYS = ['Saturday', 'Sunday']
state = {
markedDates: this.getDaysInMonth(moment().month(), moment().year(), DISABLED_DAYS)
}
onMonthChange={(date) => {
this.setState({
markedDates: this.getDaysInMonth(date.month - 1, date.year, DISABLED_DAYS)
})
}}
getDaysInMonth(month, year, days) {
let pivot = moment().month(month).year(year).startOf('month')
const end = moment().month(month).year(year).endOf('month')
let dates = {}
const disabled = { disabled: true }
while(pivot.isBefore(end)) {
days.forEach((day) => {
dates[pivot.day(day).format("YYYY-MM-DD")] = disabled
})
pivot.add(7, 'days')
}
return dates
}
from react-native-calendars.
Hi, you can specify which dates are disabled in markedDates
prop
from react-native-calendars.
For anyone reaching here ... And trying to use @arinhouck answer...
Juste know that const disabled = { disabled: true }
isn't enough to disable the onDayPress trigger
replace it with : const disabled = { disabled: true, disableTouchEvent: true };
Also there's a mistake in the getDaysInMonth
fucntion :
During the forEach trying to create the key for the dates
object the variable pivot
is modified so it skip some weeks ...
Here is my version (I've change the ['Saturday', 'Sunday'] with days indexes to get ride of the locales) :
const DatePicker = ({
date,
setDate,
initDate = new Date(),
minimumDate = new Date(),
disabledDaysIndexes = [6, 7],
}) => {
const [markedDates, setMarkedDates] = useState({});
// And don't forget to disabled the "first" dates on init :
useEffect(() => {
getDisabledDays(
initDate.getMonth(),
initDate.getFullYear(),
disabledDaysIndexes
);
}, []);
const getDisabledDays = (month, year, daysIndexes) => {
let pivot = moment().month(month).year(year).startOf('month');
const end = moment().month(month).year(year).endOf('month');
let dates = {};
const disabled = { disabled: true, disableTouchEvent: true };
while (pivot.isBefore(end)) {
daysIndexes.forEach((day) => {
const copy = moment(pivot);
dates[copy.day(day).format('YYYY-MM-DD')] = disabled;
});
pivot.add(7, 'days');
}
setMarkedDates(dates);
return dates;
};
<Calendar
theme={{
textSectionTitleDisabledColor: '#d9e1e8',
}}
markedDates={markedDates}
current={initDate}
minDate={minimumDate}
onDayPress={(day) => {
setDate(day.dateString);
}}
firstDay={1}
enableSwipeMonths={true}
disabledDaysIndexes={disabledDaysIndexes}
onMonthChange={(date) => {
getDisabledDays(date.month - 1, date.year, disabledDaysIndexes);
}}
/>;
};
from react-native-calendars.
@tautvilas I may be missing something on this comment. I see you can explicitly disable a date (eg. 10/06/2017), but a given set of days in a week (eg. disable all weekend days like Saturday or Sunday) is infinite. How is that done with this plugin?
from react-native-calendars.
@arinhouck @404sand808s what is moment()
?
from react-native-calendars.
simple solution
const checkDay = (value: string) => value.split(" ")[1] === 'Saturday' || value.split(" ")[1] === 'Sunday'
<Calendar
dayComponent={({accessibilityLabel, date, state}: any) => {
return (
<View>
<Text style={[styles.days, (checkDay(accessibilityLabel) || state) && styles.disabled]}>
{date.day}
</Text>
</View>
);
}}
/>
from react-native-calendars.
Hello guys, i have this issue. When first day of month is Sunday then will became like this.. That's weird. Other day is working fine, just when this happened.
And yaa i am using this formula as well.
const getDisabledDays = (month, year, daysIndexes) => {
let pivot = moment().month(month).year(year).startOf("month")
const end = moment().month(month).year(year).endOf("month")
let dates = {}
const disabled = { disabled: true, disableTouchEvent: true }
while (pivot.isBefore(end)) {
daysIndexes.forEach((day) => {
const copy = moment(pivot)
dates[copy.day(day).format("YYYY-MM-DD")] = disabled
})
pivot.add(7, "days")
}
setDisableDates(dates)
return dates
}
from react-native-calendars.
@NoahSimajji I had the same problem, I fixed this with this approach:
const getDisabledDays = (month, year, daysIndexes) => {
let pivotDay = moment().month(month).year(year).startOf('month')
const lastDayOfMonth = moment().month(month).year(year).endOf('month')
let disabledDates = {}
const disabledProps = { disabled: true, disableTouchEvent: true }
let allDayOfMonthChecked = false
while (!allDayOfMonthChecked) {
if (!pivotDay.isBefore(lastDayOfMonth)) {
allDayOfMonthChecked = true
}
daysIndexes.forEach(day => {
const copy = moment(pivotDay)
disabledDates[copy.day(day).format('YYYY-MM-DD')] = disabledProps
})
pivotDay.add(7, 'days')
}
setDisableDates(disabledDates)
return disabledDates
}
Please @NoahSimajji , let me know if this works for you
from react-native-calendars.
@tautvilas Seconding @arinhouck , how would one disable Weekends without a bunch of extra processing?
from react-native-calendars.
Curious about this as well.
from react-native-calendars.
Something new?! I need it. :*
from react-native-calendars.
implementing a day component and setting state to disabled
if it's a weekend is a workaround. example from @eddiegroves here: #280
from react-native-calendars.
Worked this up:
`let markedDates = {
[moment().format('YYYY-MM-DD')]: {marked: true, dotColor: PINK_DARK}, // mark today with a red dot
[this.state.selected]: {selected: true}
}
for (i = 1; i < 13; i++ ) { // figure out if the next X days are weekend days and if so disable them
let day = moment().add(i, 'd')
if (day.isoWeekday() === 6 || day.isoWeekday() === 7) {
markedDates[day.format('YYYY-MM-DD')] = {disabled: true}
}
}`
After that pass markedDates to the calendar component as a markedDates prop. I only needed it for the next week or two so I ran it few times. If you need a fix to disable weekends far into the future Iām not sure if this would be efficient.
from react-native-calendars.
@NarendraSingh88 it's a JS library for time/date manipulation. https://momentjs.com/
from react-native-calendars.
onDayPress={(day) => {
var date = new Date(day.dateString);
if (date.getDay() != 6 && date.getDay() != 7)
console.log("selected day", day);
}}
to disable just the press event, no ui marking
from react-native-calendars.
Related Issues (20)
- Make AgendaList scroll to today by default HOT 3
- There is a way to handle with week change in <weekCalendar /> HOT 1
- Types are wrong with AgendaSchedule hardcoded
- Webpack loader errors in solito nextjs app HOT 1
- React Native Calendars height is fixed by default
- React Native Calendars height is fixed by default cannot be changed why?Is there any way to decrease the height of calendar? HOT 1
- Add StandWithPalentine banner
- How will manually pick a date to start from ? HOT 1
- setNativeProps is not a function when pressing the knob of Agenda (tested with chrome)
- Asking a question regarding day header styling HOT 1
- 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
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.