Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Clear time when type is date
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomka committed Jan 15, 2018
1 parent 6391d30 commit 317d0b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>Datetime</h2>

<div class="example">
<div class="example-inputs">
<datetime type="datetime" v-model="datetime"></datetime>
<datetime type="date" v-model="datetime"></datetime>

<div class="values">
<p>
Expand Down
9 changes: 8 additions & 1 deletion src/Datetime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script>
import { DateTime } from 'luxon'
import DatetimePopup from './DatetimePopup'
import { clearTime } from './util'
export default {
components: {
Expand Down Expand Up @@ -112,7 +113,13 @@ export default {
methods: {
emitInput () {
this.$emit('input', this.date ? this.date.setZone(this.valueZone).toISO() : null)
let date = this.date ? this.date.setZone(this.valueZone) : null
if (date && this.type === 'date') {
date = clearTime(this.date)
}
this.$emit('input', date ? date.toISO() : null)
},
open () {
this.isOpen = true
Expand Down
4 changes: 4 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export function pad (number) {
return number < 10 ? '0' + number : number
}

export function clearTime (datetime) {
return datetime.set({ hour: 0, minute: 0, seconds: 0, milliseconds: 0 })
}

export function createFlowManagerFromType (type) {
let flow = []

Expand Down
23 changes: 19 additions & 4 deletions test/specs/Datetime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ describe('Datetime.vue', function () {

expect(vm.datetime).to.be.equal('2017-12-07T12:34:54.078-04:00')
})

it('should be a date with cleared time when type is date', function () {
const vm = createVM(this,
`<Datetime type="date" v-model="datetime"></Datetime>`,
{
components: { Datetime },
data () {
return {
datetime: '2017-12-07T19:34:54.078Z'
}
}
})

expect(vm.datetime).to.be.equal('2017-12-07T00:00:00.000Z')
})
})

describe('input value', function () {
Expand Down Expand Up @@ -361,7 +376,7 @@ describe('Datetime.vue', function () {
components: { Datetime },
data () {
return {
datetime: '2020-05-07T05:22:00.123Z'
datetime: '2020-05-07T00:00:00.000Z'
}
}
})
Expand All @@ -373,7 +388,7 @@ describe('Datetime.vue', function () {
vm.$('.vdatetime-popup__actions__button--confirm').click()
vm.$nextTick(() => {
expect(vm.$('.vdatetime-input').value).to.be.equal('May 20, 2020')
expect(vm.datetime).to.be.equal('2020-05-20T05:22:00.123Z')
expect(vm.datetime).to.be.equal('2020-05-20T00:00:00.000Z')
expect(vm.$('.vdatetime-overlay')).to.not.exist
expect(vm.$('.vdatetime-popup')).to.not.exist
done()
Expand All @@ -388,7 +403,7 @@ describe('Datetime.vue', function () {
components: { Datetime },
data () {
return {
datetime: '2020-05-07T05:22:00.123Z'
datetime: '2020-05-07T00:00:00.000Z'
}
}
})
Expand All @@ -400,7 +415,7 @@ describe('Datetime.vue', function () {
vm.$('.vdatetime-popup__actions__button--cancel').click()
vm.$nextTick(() => {
expect(vm.$('.vdatetime-input').value).to.be.equal('May 7, 2020')
expect(vm.datetime).to.be.equal('2020-05-07T05:22:00.123Z')
expect(vm.datetime).to.be.equal('2020-05-07T00:00:00.000Z')
expect(vm.$('.vdatetime-overlay')).to.not.exist
expect(vm.$('.vdatetime-popup')).to.not.exist
done()
Expand Down

0 comments on commit 317d0b4

Please sign in to comment.