Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wslyhbb/vuejs-datepicker

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wslyhbb/vuejs-datepicker

A simple Vue.js datepicker component. Supports disabling of dates, inline mode, translations. Fork of initial vuejs-datepicker which is abandoned.

  • 4.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23
decreased by-62.3%
Maintainers
1
Weekly downloads
 
Created
Source

@wslyhbb/vuejs-datepicker @wslyhbb/vuejs-datepicker @wslyhbb/vuejs-datepicker @wslyhbb/vuejs-datepicker @wslyhbb/vuejs-datepicker

Datepicker 2

A datepicker Vue component.

fork

this is a fork of https://github.com/charliekassel/vuejs-datepicker, which includes following breaking changes:

  • Upgrade all libraries
  • uses vue-eslint-parser for eslint.
  • Fixed SSR support for NuxtJS (breaking change v2) see also regarding CSS / styles
  • Merged #611: show calendar on focus prop
  • Merged #626: fixes missing close and open events
  • Based on #536: Allow custom types, I changed the method to getTypedDate which should return a fully parsed Date object.
  • Added a beforeDateInput slot.
  • uses date-fns for translations (breaking change v2+) see date-fns / translations
  • dropped internal formatting rules and also used date-fns (breaking change 2.x+) see date-fns / formatting

Demo

To view a demo online:

Vue 2

https://codesandbox.io/s/vue-datepicker-demo-hjktwc

Vue 3

https://stackblitz.com/edit/vitejs-vite-hgm3ct

To view demo examples locally clone the repo and run npm install && npm run serve

Install

Vue versionnpm Package
Vue 2@wslyhbb/vuejs-datepicker@3
Vue 3@wslyhbb/vuejs-datepicker@4
# Vue 2
npm install @wslyhbb/vuejs-datepicker@3

yarn add @wslyhbb/vuejs-datepicker@3
# Vue 3
npm install @wslyhbb/vuejs-datepicker

yarn add @wslyhbb/vuejs-datepicker
import Datepicker from '@wslyhbb/vuejs-datepicker';

export default {
  // ...
  components: {
    Datepicker
  }
  // ...
}

Upgrade

To upgrade to version 2+ check:

date-fns and translations

I have changed the translations from internal to use the date-fns, therefore you need to change imports for the "locale" imports e.g.: import {en, es} from 'vuejs-datepicker/dist/locale' change to: import { enUS } from 'date-fns/locale'

Please see Translations section how to set up your language.

date-fns and formatting

change custom date formatting to date-fns date formatting: https://date-fns.org/docs/format Examples:

OldNew (date-fns)Displays
dsu MMM yyyydo MMM yyyy12th Feb 2016
D dsu MMM yyyyeee do MMM yyyySat 12th Feb 2016

regarding CSS

As this bundle is also useable for SSR rendering, you have to take care of css yourself. (see also https://github.com/vuejs/rollup-plugin-vue/issues/266) I strongly recommend to create a custom component, that wraps the vuejs-datepicker. Then it is easy to add custom css and have a consistent style for the datepicker.

Method 1: try to include the css directly.

Ensure you have postcss-import up and running. (https://github.com/postcss/postcss-import) E.g.via:

<style lang="scss">
/* purgecss start ignore */
@import "@wslyhbb/vuejs-datepicker/dist/vuejs-datepicker.css"
/* purgecss end ignore */
</style>

If you do not use purgeCSS, you can safely remove the comment lines

Method 2: via global nuxt config:

add inside nuxt.config:

css: [
 {
  src: '@wslyhbb/vuejs-datepicker/dist/vuejs-datepicker.css',
  lang: 'css'
 },
Method 3: Copy the relevant css selectors to your custom component.
<style lang="scss">
	.vdp-datepicker {
		overflow: visible;
		display: inline-block;
		....
	}
	...
</style>

Usage

<datepicker></datepicker>

value prop if passed should be a Date object

<script>
var state = {
  date: new Date(2016, 9,  16)
}
</script>
<datepicker :value="state.date"></datepicker>

support name attribute for normal html form submission

<datepicker :value="state.date" name="uniquename"></datepicker>

Using v-model

<datepicker v-model="state.date" name="uniquename"></datepicker>

Emits events

<datepicker @selected="doSomethingInParentComponentFunction" @opened="datepickerOpenedFunction" @closed="datepickerClosedFunction">

Inline always open version

<datepicker :inline="true"></datepicker>

Available props

PropTypeDefaultDescription
autofocusStringSets html autofocus attribute on input
valueDate|StringDate value of the datepicker
nameStringInput name property
idStringInput id
formatString|Functiondd MMM yyyyDate formatting string or function
full-month-nameBooleanfalseTo show the full month name
languageObjectenUSTranslation for days and months
maxlengthStringSets html maxlength attribute on input
disabledBooleanfalseIf true, disable Datepicker on screen
disabled-datesObjectSee below for configuration
placeholderStringInput placeholder text
inlineBooleanTo show the datepicker always open
calendar-classString|ObjectCSS class applied to the calendar el
input-classString|ObjectCSS class applied to the input el
wrapper-classString|ObjectCSS class applied to the outer div
monday-firstBooleanfalse(Deprecated for first-day-of-week) To start the week
first-day-of-weekStringsunSets the first day of the week.
two-letter-abbrfalseShow day abbreviations in two letters
clear-buttonBooleanfalseShow an icon for clearing the date
clear-button-iconString(Deprecated for slot) Use icon for button (ex: fa fa-times)
calendar-buttonBooleanfalseShow an icon that that can be clicked
calendar-button-iconString(Deprecated for slot) Use icon for button (ex: fa fa-calendar)
calendar-button-icon-contentString(Deprecated for slot) Use for material-icons (ex: event)
day-cell-contentFunctionUse to render custom content in day cell
bootstrap-stylingBooleanfalseOutput bootstrap v4 styling classes.
initial-viewStringminimumViewIf set, open on that view
requiredBooleanfalseSets html required attribute on input
show-edge-datesBooleanfalseIf false, dates from previous/next months won't show
tabindexString|NumberTabindex for the input
typeableBooleanfalseIf true, allow the user to type the date
use-utcBooleanfalseuse UTC for time calculations
open-dateDate|StringIf set, open on that date
patternStringSets html pattern attribute on input
minimum-viewString'day'If set, lower-level views won't show
maximum-viewString'year'If set, higher-level views won't show
parse-typed-dateFunction: DateUse to parse custom date for typed input
highlightedObjectDates to be highlighted

Events

These events are emitted on actions in the datepicker

EventOutputDescription
openedThe picker is opened
closedThe picker is closed
selectedDate|nullA date has been selected
selectedDisabledObjectA disabled date has been selected
inputDate|nullInput value has been modified
clearedSelected date has been cleared
changedMonthObjectMonth page has been changed
changedYearObjectYear page has been changed
changedDecadeObjectDecade page has been changed

Date formatting

String formatter

Uses date-fns for date formatting. See https://date-fns.org/docs/format Examples:

ExampleDisplays
d MMM yyyy12 Feb 2016
d MMMM yyyy12 February 2016
yyyy-MM-dd2016-02-12
do MMM yyyy12th Feb 2016
eee do MMM yyyySat 12th Feb 2016
TokenDescExampleVersion <2.0
dday1d
dd0 prefixed day01dd
cccabbr dayMonD
dodate of Month1st 2nd ... 30th 31st(su)
Mmonth number (1 based)1 (for Jan)M
MM0 prefixed month01MM
MMMabbreviated month nameJanMMM
MMMMmonth nameJanuaryMMMM
yytwo digit year16yy
yyyyfour digit year2016yyyy
Function formatter

Delegates date formatting to provided function. Function will be called with date and it has to return formated date as a string. This allow us to use date-fns, globalize or any other library to format date.

<script>
  methods: {
    customFormatter(date) {
      return format(date, 'MMMM Do yyyy, h:mm:ss a');
    }
  }
</script>
<datepicker :format="customFormatter"></datepicker>

Disabled Dates

Dates can be disabled in a number of ways.

<script>
var state = {
  disabledDates: {
    to: new Date(2016, 0, 5), // Disable all dates up to specific date
    from: new Date(2016, 0, 26), // Disable all dates after specific date
    days: [6, 0], // Disable Saturday's and Sunday's
    daysOfMonth: [29, 30, 31], // Disable 29th, 30th and 31st of each month
    dates: [ // Disable an array of dates
      new Date(2016, 9, 16),
      new Date(2016, 9, 17),
      new Date(2016, 9, 18)
    ],
    ranges: [{ // Disable dates in given ranges (exclusive).
      from: new Date(2016, 11, 25),
      to: new Date(2016, 11, 30)
    }, {
      from: new Date(2017, 1, 12),
      to: new Date(2017, 2, 25)
    }],
    // a custom function that returns true if the date is disabled
    // this can be used for wiring your own logic to disable a date if none
    // of the above conditions serve your purpose
    // this function should accept a date and return true if it is disabled
    customPredictor: function(date) {
      // disables the date if it is a multiple of 5
      if(date.getDate() % 5 == 0){
        return true
      }
    }
  }
}
</script>
<datepicker :disabledDates="state.disabledDates"></datepicker>

Highlighted Dates

Dates can be highlighted (e.g. for marking an appointment) in a number of ways. Important: By default disabled dates are ignored, to highlight disabled dates set the includeDisabled property to true. Note: Both to and from properties are required to define a range of dates to highlight.

<script>
var state = {
  highlighted: {
    to: new Date(2016, 0, 5), // Highlight all dates up to specific date
    from: new Date(2016, 0, 26), // Highlight all dates after specific date
    days: [6, 0], // Highlight Saturday's and Sunday's
    daysOfMonth: [15, 20, 31], // Highlight 15th, 20th and 31st of each month
    dates: [ // Highlight an array of dates
      new Date(2016, 9, 16),
      new Date(2016, 9, 17),
      new Date(2016, 9, 18)
    ],
    // a custom function that returns true of the date is highlighted
    // this can be used for wiring you own logic to highlight a date if none
    // of the above conditions serve your purpose
    // this function should accept a date and return true if is highlighted
    customPredictor: function(date) {
      // highlights the date if it is a multiple of 4
      if(date.getDate() % 4 == 0){
        return true
      }
    },
    includeDisabled: true // Highlight disabled dates
  }
}
</script>
<datepicker :highlighted="state.highlighted"></datepicker>

Slots

Slots will help you customize content. .

beforeCalendarHeader

Sometimes you need to show custom content before the calendar header. For such cases you can use the named slot beforeCalendarHeader.

An example would be to use bootstrap's input-group-prepend and input-group-append to show some custom text:

<datepicker :bootstrap-styling="true">
  <template v-slot:beforeCalendarHeader">
    <div class="calender-header">
      Choose a Date
    </div>
  </template>
</datepicker>
calendarBtn

To implement calendar button.

beforeDateInput

To implement some custom styling on DateInput, you might need to add elemnt before the DateInput. Similar to afterDateInput, just it is before in the html DOM.

clearBtn

To implement clear button.

afterDateInput

To implement some custom styling (for instance to add an animated placeholder) on DateInput, you might need to add elements as DateInput siblings. Slot named afterDateInput allows you to do that:

<datepicker>
  <template v-slot:afterDateInput">
    <span class="animated-placeholder">
      Choose a Date
    </span>
  </template>
</datepicker>

Translations

How to apply language

See also https://date-fns.org/docs/I18n Especially if you use webpack!

  1. You need to load the language file for the locale, e.g. Node:
import { de } from 'date-fns/locale';
  1. specify the language in the datepicker component:
<datepicker :language="de"></datepicker>

Available languages are all that date-fs supports. See demo file or https://github.com/date-fns/date-fns/tree/main/src/locale for a list of available languages and the correct language code for it.

Keywords

FAQs

Package last updated on 02 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc