Skip to content

Commit 1885dc2

Browse files
committed
add .sync modifier, discard defaultDate and defaultRange, update readme
1 parent c549a5c commit 1885dc2

File tree

7 files changed

+72
-23
lines changed

7 files changed

+72
-23
lines changed

demo/index.html

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ <h2>calendar</h2>
8888
:days-disabled-end="daysDisabledEnd"
8989
:disabled-func="disabledFunc"
9090
:first-day-of-week="1"
91-
:default-date="date"
91+
:sync-date.sync="date"
9292
:lang="lang" @change="onChange"></calendar>
9393
<button @click.stop.prevent="setDate(-1)">Yesterday</button>
9494
<button @click.stop.prevent="setDate(0)">Today</button>
@@ -179,7 +179,7 @@ <h2>date-range</h2>
179179
class="calendar"
180180
:days-disabled-start="daysDisabledStart"
181181
:days-disabled-end="daysDisabledEnd"
182-
:default-range="range"
182+
:sync-range="range"
183183
:lang="lang" @change="onChange"></daterange>
184184
<button @click.stop.prevent="setRange(-7)">Last 7 days</button>
185185
<button @click.stop.prevent="setRange(-30)">Last 1 month</button>
@@ -250,7 +250,7 @@ <h2>custom style</h2>
250250
</section>
251251
<script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>
252252
<script src="https://unpkg.com/vue/dist/vue.js"></script>
253-
<script src="../dist/vue-date-range.js"></script>
253+
<script src="../dist/vue-date-range.min.js"></script>
254254
<script>
255255
new Vue({
256256
el: '#calendar',
@@ -271,9 +271,15 @@ <h2>custom style</h2>
271271
return false
272272
},
273273
lang: 'en',
274-
date: moment()
274+
date: moment(),
275+
defaultDate: moment().add(1, 'months')
275276
};
276277
},
278+
created: function () {
279+
setTimeout(() => {
280+
this.date = moment().add(1, 'months')
281+
}, 1000)
282+
},
277283
methods: {
278284
onChange(date) {
279285
this.date = date;
@@ -320,6 +326,14 @@ <h2>custom style</h2>
320326
}
321327
};
322328
},
329+
created: function () {
330+
setTimeout(() => {
331+
this.range = {
332+
startDate: moment().add(1, 'months'),
333+
endDate: moment().add(7, 'days').add(1, 'months')
334+
}
335+
}, 1000)
336+
},
323337
methods: {
324338
onChange(range) {
325339
this.range = range;

dist/vue-date-range.min.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-date-range",
3-
"version": "2.1.1",
3+
"version": "2.2.1",
44
"description": "A vue component for choosing dates and date ranges. Uses Moment.js for date operations.",
55
"main": "dist/vue-date-range.min.js",
66
"scripts": {

readme.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default {
7777
```
7878

7979
## browser
80-
Download vue-date-range.js from dist/ and import in your web page. Example:
80+
Download vue-date-range.min.js from dist/ and import in your web page. Example:
8181

8282
```
8383
...
@@ -87,21 +87,21 @@ Download vue-date-range.js from dist/ and import in your web page. Example:
8787
:show-lunar="true"
8888
:first-day-of-week="1"
8989
:disable-days-before-today="disableDaysBeforeToday"
90-
:default-date="date"
90+
:sync-date="date"
9191
:lang="lang" @change="onChange"></calendar>
9292
</div>
9393
...
9494
<div id="range" class="calendar-wrapper">
9595
<span>{{range.startDate.format('YYYY-MM-DD')}}</span>~<span>{{range.endDate.format('YYYY-MM-DD')}}</span>
96-
<daterange class="calendar" :default-range="range" :lang="lang" @change="onChange"></daterange>
96+
<daterange class="calendar" :sync-range="range" :lang="lang" @change="onChange"></daterange>
9797
<button @click.stop.prevent="setRange(-7)">Last 7 days</button>
9898
<button @click.stop.prevent="setRange(-30)">Last 1 month</button>
9999
</div>
100100
...
101101
102102
<script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>
103103
<script src="https://unpkg.com/vue/dist/vue.js"></script>
104-
<script src="../dist/vue-date-range.js"></script>
104+
<script src="../dist/vue-date-range.min.js"></script>
105105
<script>
106106
new Vue({
107107
el: '#calendarLunar',
@@ -155,6 +155,13 @@ Download vue-date-range.js from dist/ and import in your web page. Example:
155155
</script>
156156
```
157157

158+
# .sync
159+
For Vue2.3.0+, we can use [`.sync` modifier](https://vuejs.org/v2/guide/components.html#sync-Modifier):
160+
```javascript
161+
<calendar sync-date.sync="date"></calendar>
162+
<date-range sync-range.sync="range"></date-range>
163+
```
164+
158165
# Props
159166
## Calendar
160167
* show-lunar: Show lunar or not. Default is false.
@@ -204,15 +211,23 @@ Download vue-date-range.js from dist/ and import in your web page. Example:
204211
|sl-si|Slovenian|
205212
|uk|Ukrainian|
206213

207-
* default-date: Init the selected date. Only for Calendar.
214+
* sync-date: The default selected date. Can be used as the “two-way binding” for date (Vue 2.3.0+). e.g.:
215+
```html
216+
<calendar sync-date.sync="date"></calendar>
217+
```
218+
* ~~default-date: Init the selected date. Only for Calendar.~~(use syncDate instead)
208219
* range: The selected date range. e.g.:
209220

210221
```javascript
211222
range: {startDate: moment(), endDate: moment().add(7, 'days')}
212223
```
213224

214225
## DateRange
215-
This component is build on ``Calendar``, so it has all the props of ``Calendar`` except ``default-date``
226+
This component is build on ``Calendar``, so it has all the props of ``Calendar`` except ``sync-date``
216227
Also it has its specific props:
217228

218-
* defaultRange: Use to init the date range
229+
* sync-range: The default date range. Can be used as the “two-way binding” for range (Vue 2.3.0+). e.g.:
230+
```html
231+
<date-range sync-range.sync="range"></date-range>
232+
```
233+
* ~~defaultRange: Used to init the date range~~

src/Calendar.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@
7373
type: String,
7474
default: 'zh'
7575
},
76-
defaultDate: {
76+
// use syncDate instead
77+
// defaultDate: {
78+
// type: Object
79+
// },
80+
syncDate: {
7781
type: Object
7882
}
7983
},
@@ -82,7 +86,7 @@
8286
weekDays: [],
8387
days: [],
8488
dayOfMonth: moment(), // Any day of current displaying month
85-
date: this.defaultDate || moment()
89+
date: this.syncDate || moment()
8690
}
8791
},
8892
watch: {
@@ -92,7 +96,7 @@
9296
this.resetDayOfMonth()
9397
},
9498
// show month that contains defaultDate
95-
defaultDate (val) {
99+
syncDate (val) {
96100
this.date = val
97101
this.resetDayOfMonth()
98102
}
@@ -212,6 +216,7 @@
212216
},
213217
handleDayClick (day) {
214218
this.date = day.dayMoment
219+
this.$emit('update:syncDate', day.dayMoment)
215220
this.$emit('change', day.dayMoment)
216221
},
217222
changeMonth (delta) {

src/DateRange.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@
5252
type: String,
5353
default: 'zh'
5454
},
55-
defaultRange: {
55+
// defaultRange: {
56+
// type: Object
57+
// }
58+
syncRange: {
5659
type: Object
5760
}
5861
},
5962
data () {
6063
return {
61-
rangeData: this.defaultRange || {
64+
rangeData: this.syncRange || {
6265
startDate: null,
6366
endDate: null
6467
},
@@ -67,7 +70,7 @@
6770
}
6871
},
6972
watch: {
70-
defaultRange (val) {
73+
syncRange (val) {
7174
this.rangeData = val
7275
this.step = 0
7376
}
@@ -94,6 +97,7 @@
9497
const eTs = this.rangeData.endDate.unix()
9598
const startDate = sTs <= eTs ? this.rangeData.startDate : this.rangeData.endDate
9699
const endDate = sTs > eTs ? this.rangeData.startDate : this.rangeData.endDate
100+
this.$emit('update:syncRange', {startDate, endDate})
97101
this.$emit('change', {startDate, endDate})
98102
}
99103
}

test/unit/Calendar.spec.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const month = moment().month()
1515
describe('Test Calendar:', () => {
1616
commonUnit(Calendar)
1717

18-
it('days between range should have in-range class and when range is changed it should also be true', () => {
18+
it('days between range should have in-range class and when range is changed it should also be true', (done) => {
1919
const start = moment(`${year}-${formatMonth(month+1)}-14`)
2020
const end = moment(`${year}-${formatMonth(month+1)}-16`)
2121

@@ -42,10 +42,11 @@ describe('Test Calendar:', () => {
4242
vm.$nextTick(() => {
4343
$spans = vm.$el.querySelectorAll(".days span .in-range")
4444
expect($spans.length).to.equal(2)
45+
done()
4546
})
4647
})
4748

48-
it('the default date should has selected class and when it change Calender\'s month will change too', () => {
49+
it('the syncDate should has selected class and when it change Calender\'s month will change too', (done) => {
4950
// next month
5051
let nextMonth = month + 1
5152
let nextYear = year
@@ -55,25 +56,27 @@ describe('Test Calendar:', () => {
5556
}
5657
nextMonth = formatMonth(nextMonth + 1)
5758

58-
const defaultDate = moment(`${nextYear}-${nextMonth}-15`)
59+
const syncDate = moment(`${nextYear}-${nextMonth}-15`)
5960

6061
let vm = getRenderedVm(Calendar)
6162

62-
vm.defaultDate = defaultDate
63+
vm.syncDate = syncDate
6364

6465
vm.$nextTick(() => {
6566
const _month = vm.$el.querySelector(".month-year span span").innerText
6667
expect(_month).to.equal(nextMonth)
6768

6869
const $spans = vm.$el.querySelectorAll(".days span")
69-
const pos = getDayPositionInCalendar(defaultDate, 0)
70+
const pos = getDayPositionInCalendar(syncDate, 0)
7071
for (var i = 0, len = $spans.length; i < len; i++) {
7172
if (i === pos-1) {
7273
var $solar = $spans[i].querySelector(".solar")
7374
var hasCls = $solar.className.indexOf('selected') > -1
7475
expect(hasCls).to.equal(true)
7576
}
7677
}
78+
79+
done()
7780
})
7881
})
7982
})

0 commit comments

Comments
 (0)