Giter VIP home page Giter VIP logo

vue-date-range's Introduction

vue-date-range

Build Status Coverage Status

A vue component for choosing dates and date ranges. Uses Moment.js for date operations. Support Chinese lunar.

vue-date-range-demo.gif

Live Demo

use

npm

npm install vue-date-range

Calendar

<calendar class="calendar"
          :show-lunar="true"
          :first-day-of-week="1"
          :disable-days-before-today="disableDaysBeforeToday"
          :sync-date.sync="date"
          :lang="lang" @change="onChange"></calendar>
...
import {Calendar} from 'vue-date-range';
export default {
  components: {
    Calendar
  },
  data() {
    return {
      disableDaysBeforeToday: true,
      lang: 'zh',
      date: moment()
    };
  },
  methods: {
    onChange(date) {
      this.date = date;
    }
  }
}

DateRange

<daterange class="calendar" :sync-range.sync="range" :lang="lang" @change="onChange"></daterange>
...
import {DateRange} from 'vue-date-range';
export default {
  components: {
    DateRange
  },
  data() {
    return {
      lang: 'en',
      range: {
        startDate: moment(),
        endDate: moment().add(7, 'days')
      }
    };
  },
  methods: {
    onChange(range) {
      this.range = range;
    },
    setRange (p) {
      if (typeof p === 'number') {
        console.log(p)
        this.range = {
          startDate: moment().add(p, 'days'),
          endDate: moment()
        }
      }
    },
  }
}

browser

Download vue-date-range.min.js from dist/ and import in your web page. Example:

...
<div id="calendarLunar" class="calendar-wrapper">
    <span>{{date.format('YYYY-MM-DD')}}</span>
    <calendar class="calendar"
              :show-lunar="true"
              :first-day-of-week="1"
              :disable-days-before-today="disableDaysBeforeToday"
              :sync-date="date"
              :lang="lang" @change="onChange"></calendar>
</div>
...
<div id="range" class="calendar-wrapper">
    <span>{{range.startDate.format('YYYY-MM-DD')}}</span>~<span>{{range.endDate.format('YYYY-MM-DD')}}</span>
    <daterange class="calendar" :sync-range="range" :lang="lang" @change="onChange"></daterange>
    <button @click.stop.prevent="setRange(-7)">Last 7 days</button>
    <button @click.stop.prevent="setRange(-30)">Last 1 month</button>
</div>
...

<script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="../dist/vue-date-range.min.js"></script>
<script>
    new Vue({
        el: '#calendarLunar',
        components: {
          'calendar':daterange.Calendar
        },
        data() {
          return {
            disableDaysBeforeToday: true,
            lang: 'zh',
            date: moment()
          };
        },
        methods: {
          onChange(date) {
            this.date = date;
          }
        }
    });


    new Vue({
        el: '#range',
        components: {
            'daterange':daterange.DateRange
        },
        data() {
          return {
            lang: 'en',
            range: {
              startDate: moment(),
              endDate: moment().add(7, 'days')
            }
          };
        },
        methods: {
          onChange(range) {
            this.range = range;
          },
          setRange (p) {
            if (typeof p === 'number') {
              console.log(p)
              this.range = {
                startDate: moment().add(p, 'days'),
                endDate: moment()
              }
            }
          },
        }
    });
</script>

.sync

For Vue2.3.0+, we can use .sync modifier:

<calendar :sync-date.sync="date"></calendar>
<date-range :sync-range.sync="range"></date-range>

v-model

We can also use v-model modifier (these can be configured in 2.2.0+):

<calendar v-model="date"></calendar>
<date-range v-model="range"></date-range>

Props

Calendar

  • open-transition: Open transition or not. Default is true.

  • show-lunar: Show lunar or not. Default is false.

  • disable-days-before-today: Disable days before today or not.

  • days-disabled-start: Disable days after this day.

  • days-disabled-end: Disable days before this day.

  • disabled-func: Used to decide if the day is disabled or not.

      ...
       // set odd days to disabled
       disabledFunc: function (dayMoment) {
         var date = dayMoment.date()
         if (date % 2 === 1) {
           return true
         }
         return false
       }
      ...
  • day-class-func: Used to add class to day.

      ...
        // add 'today, important' class to today 
        dayClassFunc: function (dayMoment) {
          if (dayMoment.format('YYYY-MM-DD') === moment().format('YYYY-MM-DD')) {
            return ['today', 'important']
          }
        }
      ...
  • first-day-of-week: Set the first day of Week. Default is 0 (Sunday).

  • month-year-format: The displaying format for month and year. Default is 'MM - YYYY'.

  • lang: Language

addr. language
da Danish
en English
es Spanish
fi Finnish
fr French
hr Hrvatski
it Italian
lt Lithuanian
nl Nederlandse
de German
pt-br Portuguese
vi Vietnamese
zh Chinese
ja Japanese
he Hebrew
cs Czech
ru Russian
bg Bulgarian
sv Swedish
th Thai
ro Roman
sl-si Slovenian
uk Ukrainian
  • sync-date: The default selected date. Can be used as the “two-way binding” for date (Vue 2.3.0+). e.g.:

    <calendar :sync-date.sync="date"></calendar>
  • range: The selected date range. e.g.:

    range: {startDate: moment(), endDate: moment().add(7, 'days')}
  • day-of-month-prop: Any day of initial selected month. If not set, it will equal to value or syncDate orrange.startDate or moment().

DateRange

This component is build on Calendar, so it has all the props of Calendar except sync-date Also it has its specific props:

  • emitChangeOnStep0: If set to true, it will emit result after selecting one date. Or it emits result after selecting two dates. Default is false.
  • sync-range: The default date range. Can be used as the “two-way binding” for range (Vue 2.3.0+). e.g.:
    <date-range :sync-range.sync="range"></date-range>

custom style

This is a day html structure example:

<span title="重阳" class="v-date-cell v-date-selected">
  <div class="v-date-cell-text">
    <p class="v-date-solar">3</p>
    <p class="v-date-lunar">十八</p>
  </div>
</span>

The span tag will has different classes (v-date-selected, v-date-passive, v-date-in-range, v-date-start-day, v-date-end-day) according to the dates selected.

You can set your custom style using these classes. e.g.:

.v-date-day-cell.v-date-start-day {
  border-bottom-left-radius: 50%;
  border-top-left-radius: 50%;
  background-color: transparent;
}
.v-date-day-cell.v-date-end-day {
  border-bottom-right-radius: 50%;
  border-top-right-radius: 50%;
  background-color: transparent;
}
.v-date-day-cell.v-date-in-range {
  background-color: orange;
}

Thanks

License

MIT

vue-date-range's People

Contributors

darkside73 avatar demircancelebi avatar paradeto avatar timmw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-date-range's Issues

License?

Hey, I'm looking at using vue-date-range in my project. Could you please add a license? Thanks!

Disable multiple breaks between dates

Currently (from what I know) days-disabled-start: and days-disabled-end: only allow disabling one date break (example: disable all dates between 04/07/2017 and 22/07/2017).

It would be better if instead of a single break, we could disable multiple ones.

Example:
Disable Dates:
Start: 04/07/2017; End: 22/07/2017
Start: 08/08/2017; End: 10/08/2017

Thanks in advance.

Select dateRange doesnt update Calendar view

Version: "vue-date-range": "^3.0.0"

Here is my code:

      <date-range
        ref="datepicker"
        v-show="showPicker"
        class="calendar dropdown"
        :sync-range.sync="range"
        :lang="currentLocale"
        :emitChangeOnStep0="true"
        v-on-click-away="hideDatepicker"
        :days-disabled-start="maxDate"
        :days-disabled-end="minDate"
      />

I need to update range by button like that 'selectWeek' method:

      this.startDate = Moment().subtract(7, 'days')
      this.endDate = Moment()

But when i use this method, property 'date' of component 'Calendar' didn`t change:

1. State before click 'selectWeek'
image
2. State after click 'selectWeek'
image

I have looked into src and in Calendar.vue found, that date doesnt properly update when range changed:

   // line 167
    watch: {
      range (val) {
        // this.date = val.startDate
        this.resetDayOfMonth()
      }
   }

and in resetDayOfMonth() method we see this

      resetDayOfMonth () {
        const {
          date,
          dayOfMonth
        } = this
        // If no date is selected then it's not necessary to update dayOfMonth
        if (!date) return
        if (formatter(date, 'YYYY-MM') !== formatter(dayOfMonth, 'YYYY-MM')) {
          let _diff = Number(date.diff(dayOfMonth, 'months'))
          _diff = _diff <= 0 ? _diff - 1 : _diff
          dayOfMonth.add(_diff, 'months')
          this.dayOfMonth = dayOfMonth.clone()
        }
      }

In line if (!date) return we return nothing, because in watch.range method we didn`t change date!
If i uncommented this line // this.date = val.startDate it works fine

Move classes to parent for better styling.

The active classes should be moved to the parent .ayou-day-cell so that users can style things like .in-range:first-of-type etc. It would allow for more flexible styling options.

My goal would be to style the first and last days in unique ways then the days in between in different ways. to create a rounded row.

example

Another option that might be less obtrusive would be to add .start-date and .end-date classes.

Calendar should handle no initial date

I'm using the calendar together with two input fields for a start and end date, which are synced whenever one of the fields or the calendar is updated.

At the beginning, no date is inserted. This is no problem for the input fields, but the calendar will break when no Moment date is inserted.

Could you change the behaviour to allow for an initial value of null?

Scoped styles

Any chance to have styles scoped to avoid collision with outer CSS? Some class names are quite common like .cell and .selected and they currently override some of my app global styles

Duplicate keys detected: 'index'.

vue.esm.js?efeb:578 [Vue warn]: Duplicate keys detected: 'index'. This may cause an update error.

found in

---> <Calendar>
       <DateRange>
         <TravelRequest> at src/components/TravelRequest.vue
           <App> at src/App.vue
             <Root>

Couldn't find preset "es2015" ?

Error: Couldn't find preset "es2015" relative to directory "/Users/MacBook/Documents/2017/fiiish-m/node_modules/vue-date-range"

when i remove the "presets": ["es2015", "stage-2"], it wiil be ok.
so why add the babelrc to the direction?

Day Hook

I'm wondering how difficult it would be to design a hook for DayCell that would return an array of classes.

The idea being users could pass a function that returns an array of classes if that day matches a set of criteria.

The reason I'd want it is to show blacked out dates by passing an array of dates then I could tap into a hook with a simple if look like...

hook: function(day) {
  if (_.contains(date_array, day.dayMoment.date())) {
    return ['is-custom-blackout'];
  }
  return false;
}

but it would also allow any other users to create any kind of custom class binding that they want like is it's a weekend day, or a birthday etc.

How can I set my custom style ?

Hi, i'm sorry but I can't understand how to apply my styles ? I'm using a component <DateRange class="calendar" :first-day-of-week="0" :lang="lang" @change="onChange">

document is not defined

I'm trying to use this on Nuxt.js
But Im having document is not defined.

import Vue from 'vue';
import {DateRange} from 'vue-date-range';

Vue.use(DateRange);

nuxt.config.js

 { src : '~/plugins/vue-date-range', ssr : false},

Thanks!

"v-date-end-day" class is before "v-date-start-day" in DateRange

First of all, thank you for this nice component. It works really well out of the box!

I've found a weird behaviour and I'm not sure if it's wanted or not:

When I select one date and then the other one (which is before the startDate), the class for the startDate is on the later date and the class for the endDate is on the earlier date.

Example:

  • I click on 31. July 2018
  • I click on 1. July 2018

.v-date-start-day is on 31. July
.v-date-end-day is on 1. July

Is there a way to always have a class on the earlier date and one on the later date?

v-date-selected on wrong cell

If I select a range of dates, then programmatically change my daterange object, the startDate cell of the range I selected keeps its v-date-selected class, but shouldn't.

To reproduce:

  1. click date cell August 19, 2018 to select the startDate
  2. click date cell August 24, 2018 to select the endDate
  3. programmatically set this.daterange.startDate = this.daterange.endDate = moment('2018-08-03');

You will see the cell for August 19, 2018 still has v-date-selected.

vue-date-range-bug

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.