Coder Social home page Coder Social logo

Comments (15)

arinhouck avatar arinhouck commented on May 5, 2024 74

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.

tautvilas avatar tautvilas commented on May 5, 2024 19

Hi, you can specify which dates are disabled in markedDates prop

from react-native-calendars.

seba9999 avatar seba9999 commented on May 5, 2024 13

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.

arinhouck avatar arinhouck commented on May 5, 2024 12

@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.

NarendraSingh88 avatar NarendraSingh88 commented on May 5, 2024 10

@arinhouck @404sand808s what is moment()?

from react-native-calendars.

ArtemBozhenko avatar ArtemBozhenko commented on May 5, 2024 2

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.

NoahSimajji avatar NoahSimajji commented on May 5, 2024 2

image
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.

Jean-Kenned avatar Jean-Kenned commented on May 5, 2024 1

@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.

jeffmwells avatar jeffmwells commented on May 5, 2024

@tautvilas Seconding @arinhouck , how would one disable Weekends without a bunch of extra processing?

from react-native-calendars.

404sand808s avatar 404sand808s commented on May 5, 2024

Curious about this as well.

from react-native-calendars.

rodrix avatar rodrix commented on May 5, 2024

Something new?! I need it. :*

from react-native-calendars.

jebora avatar jebora commented on May 5, 2024

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.

404sand808s avatar 404sand808s commented on May 5, 2024

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.

jebora avatar jebora commented on May 5, 2024

@NarendraSingh88 it's a JS library for time/date manipulation. https://momentjs.com/

from react-native-calendars.

PetroviciMihail avatar PetroviciMihail commented on May 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.