Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"peerDependencies": {
"moment": "^2.24.0",
"pikaday": "^1.8.0",
"vue": "^2.6.10"
"vue": "^3.0.5"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default [
sourcemap: true,
globals: {
moment: 'moment',
pikaday: 'Pikaday'
pikaday: 'Pikaday',
vue: 'vue'
},
}
},
Expand Down
73 changes: 40 additions & 33 deletions src/component.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
//@flow

import { h } from 'vue';
import moment from 'moment';
import Pikaday from 'pikaday';
import isDate from 'lodash/isDate';
import isString from 'lodash/isString';

import 'pikaday/css/pikaday.css';

function isEvent(value) {
return value instanceof Event || (value && value.constructor && value.constructor.name === 'Event');
function isEvent(modelValue) {
return modelValue instanceof Event || (modelValue && modelValue.constructor && modelValue.constructor.name === 'Event');
}

export default {
name: 'vue-pikaday',
inheritAttrs: false,
emits: ['update:modelValue', 'update:inputValue', 'update:visible'],
props: {
value: {
validator(value: typeof undefined | null | Date | Event | string): boolean {
modelValue: {
validator(modelValue: typeof undefined | null | Date | Event | string): boolean {
const allowedTypes: Array<typeof undefined | null> = [undefined, null];

if (isEvent(value)) {
if (isEvent(modelValue)) {
return true;
}

if (isDate(value)) {
if (isDate(modelValue)) {
return true;
}

if(isString(value) && moment(value).isValid()) {
if(isString(modelValue) && moment(modelValue).isValid()) {
return true;
}

for (const type of allowedTypes) {
const allowed: boolean = value === type || typeof value === type;
const allowed: boolean = modelValue === type || typeof modelValue === type;

if (allowed) {
return true;
Expand All @@ -43,6 +45,10 @@ export default {
},
required: true
},
visible: {
type: Boolean,
default: false
},
options: {
required: false,
default() {
Expand All @@ -57,7 +63,6 @@ export default {
},
data() {
return {
visible: false,
elAttrs: {
type: 'text'
},
Expand All @@ -72,29 +77,38 @@ export default {
},
mergedOptions() {
return Object.assign({}, this.defaultOptions, this.options);
},
inputValue(): string | null {
if (!this.isModelValueValid) {
return null;
}
const inputValue: moment = moment(this.modelValue);
return inputValue.isValid() ? inputValue.format(this.mergedOptions.format) : null;
},
isModelValueValid() {
return isDate(this.modelValue);
}
},
render(h: Function): Object {
render(): Object {
return h('input', {
attrs: this.elementAttributes,
on: this.$listeners,
value: this.inputValue(this.value)
...this.elementAttributes,
modelValue: this.inputValue
}, this.$slots.default);
},
mounted() {
this.create();

this.$watch('value', (value: typeof undefined | null | Date) => {
if (!isDate(value)) {
value = null;
this.$watch('modelValue', (modelValue: typeof undefined | null | Date) => {
if (!this.isModelValueValid) {
modelValue = null;
}
if (!this.visible) {
this.pikaday.setDate(value, true);
this.pikaday.setDate(modelValue, true);
}
this.change(value);
this.change(modelValue);
});
},
beforeDestroy() {
beforeUnmount() {
this.destroy();
},
watch: {
Expand All @@ -117,7 +131,7 @@ export default {

let defaultValue: typeof undefined | null | Date = this.value;

if (!this.value && this.autoDefault) {
if (!this.modelValue && this.autoDefault) {
defaultValue = moment().toDate();
this.change(defaultValue);
}
Expand All @@ -137,29 +151,22 @@ export default {
this.destroy();
this.create();
},
change(value: typeof undefined | null | Date) {
this.$emit('input', value);
this.$emit('input-value', this.inputValue(value));
},
inputValue(value: typeof undefined | null | Date): string | null {
if (!isDate(value)) {
return null;
}
const inputValue: moment = moment(value);
return inputValue.isValid() ? inputValue.format(this.mergedOptions.format) : null;
change(modelValue: typeof undefined | null | Date) {
this.$emit('update:modelValue', modelValue);
this.$emit('update:inputValue', this.inputValue);
},
onSelect() {
this.change(this.pikaday.getDate());
},
onOpen() {
this.visible = true;
this.$emit('update:visible', true);
},
onClose() {
if (!isDate(this.value)) {
if (!this.isModelValueValid) {
this.pikaday.setDate(null, true);
this.change(null);
}
this.visible = false;
this.$emit('update:visible', false);
},
show() {
this.pikaday.show();
Expand Down
22 changes: 0 additions & 22 deletions src/directives.js

This file was deleted.

10 changes: 2 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import VuePikaday from './component';
import { VuePikadayVisible } from './directives';

const VuePikadayPlugin = {
install(Vue) {
Vue.component(VuePikaday.name, VuePikaday);
Vue.directive('p-visible', VuePikadayVisible);
install(app) {
app.component(VuePikaday.name, VuePikaday);
}
};

if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(VuePikadayPlugin);
}

export default VuePikadayPlugin;
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3915,19 +3915,19 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000792, caniuse-lite@^1.0.30000989:
version "1.0.30000997"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000997.tgz#ba44a606804f8680894b7042612c2c7f65685b7e"
integrity sha512-BQLFPIdj2ntgBNWp9Q64LGUIEmvhKkzzHhUHR3CD5A9Lb7ZKF20/+sgadhFap69lk5XmK1fTUleDclaRFvgVUA==
version "1.0.30001180"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001180.tgz"
integrity sha512-n8JVqXuZMVSPKiPiypjFtDTXc4jWIdjxull0f92WLo7e1MSi3uJ3NvveakSh/aCl1QKFAvIz3vIj0v+0K+FrXw==

caniuse-lite@^1.0.30001035:
version "1.0.30001038"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001038.tgz#44da3cbca2ab6cb6aa83d1be5d324e17f141caff"
integrity sha512-zii9quPo96XfOiRD4TrfYGs+QsGZpb2cGiMAzPjtf/hpFgB6zCPZgJb7I1+EATeMw/o+lG8FyRAnI+CWStHcaQ==
version "1.0.30001180"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001180.tgz"
integrity sha512-n8JVqXuZMVSPKiPiypjFtDTXc4jWIdjxull0f92WLo7e1MSi3uJ3NvveakSh/aCl1QKFAvIz3vIj0v+0K+FrXw==

caniuse-lite@^1.0.30001039, caniuse-lite@^1.0.30001043:
version "1.0.30001048"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001048.tgz#4bb4f1bc2eb304e5e1154da80b93dee3f1cf447e"
integrity sha512-g1iSHKVxornw0K8LG9LLdf+Fxnv7T1Z+mMsf0/YYLclQX4Cd522Ap0Lrw6NFqHgezit78dtyWxzlV2Xfc7vgRg==
version "1.0.30001180"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001180.tgz"
integrity sha512-n8JVqXuZMVSPKiPiypjFtDTXc4jWIdjxull0f92WLo7e1MSi3uJ3NvveakSh/aCl1QKFAvIz3vIj0v+0K+FrXw==

capture-stack-trace@^1.0.0:
version "1.0.1"
Expand Down