nb-date-picker

This is a date picker component for Vue.js 3+.

Loading component...

Installation

Yarn
yarn add @vlalg-nimbus/nb-inputs
NPM
npm install @vlalg-nimbus/nb-inputs

Usage

Vue 3
import { createApp } from 'vue'
import App from './App.vue'

import NbInputComponents from '@vlalg-nimbus/nb-inputs'
import "@vlalg-nimbus/nb-inputs/dist/style.css";

// Import the Calendar component from the npm package to use in the nb-date-picker component
import { Calendar } from '@vlalg-nimbus/nb-calendar';
import "@vlalg-nimbus/nb-calendar/dist/style.css"

const app = createApp(App)
app.use(NbInputComponents)
app.component('Calendar', Calendar); // Register the Calendar component globally
app.mount('#app')
Nuxt 3
import NbInputComponents from '@vlalg-nimbus/nb-inputs'
import "@vlalg-nimbus/nb-inputs/dist/style.css";

// Import the Calendar component from the npm package to use in the nb-date-picker component
import { Calendar } from '@vlalg-nimbus/nb-calendar';
import "@vlalg-nimbus/nb-calendar/dist/style.css"

export default defineNuxtPlugin(context => {
  context.vueApp.use(NbInputComponents)
  context.vueApp.component('Calendar', Calendar); // Register the Calendar component globally
})

To use, simply call the component, in this case it will be NbDatePicker or nb-date-picker.

Mode 1
<template>
  <NbDatePicker />
</template>
Mode 2
<template>
  <nb-date-picker />
</template>
Mode 3
<template>
  <nb-date-picker></nb-date-picker>
</template>

Preview & Playground

Select the component you want to edit/test

Loading Sandbox...

Props

Items with an (*) mean they are required

To set initial value, use the inputText prop.

nameValue typeDefaultDescription
inputTextString''Defines the initial date value. Accepts String in ISO format (YYYY-MM-DD, YYYY-MM-DDTHH:mm, YYYY-MM-DDTHH:mm:ss, YYYY-MM, YYYY-Www) or JSON string for ranges.

Other props

nameValue typeDefaultDescription
nbId (*)StringSets the id attribute to differentiate from other components
inputName (*)StringSets the name attribute for the input element
displayString'b'Defines the display type. Accepts ib and b.
tabIndexNumber0Defines the tab index. Set this property to make the input focusable by the keyboard.
hasTabIndexEnterBooleantrueDefines if pressing Enter should trigger the entered event.
ariaLabelString'Alternate Text Button'Defines the aria-label attribute for screen readers.
ariaAttrsObject{}Allows passing custom aria attributes as an object. Keys will automatically receive the aria- prefix. Example: { 'describedby': 'help-id', 'invalid': 'false' }
themeString'light'Defines the theme. Accepts light and dark.
disabledBooleanfalseDefines if the input is disabled
requiredBooleanfalseDefines if the input is required
inputReadonlyBooleanfalseDefines if the input is readonly
inputTypeString'date'Defines the input type. Accepts date, month, time, datetime-local, week.
inputTextString''Defines the initial input value. Accepts String in ISO format (YYYY-MM-DD, YYYY-MM-DDTHH:mm, YYYY-MM-DDTHH:mm:ss, YYYY-MM, YYYY-Www) or JSON string for ranges.
localeString'pt-BR'Defines the locale. Accepts pt-BR and en-US.
allowRangeBooleanfalseDefines if date range selection is allowed. When enabled, the input accepts ranges in format "DD/MM/YYYY - DD/MM/YYYY".
useCustomCalendarBooleantrueDefines if the component should use the custom calendar component. If false, uses the native browser date picker.
calendarZIndexNumber10000Defines the z-index of the calendar popup when using custom calendar. Useful when the calendar needs to appear above modals or other elements.
calendarWidthNumber350Defines the width of the calendar popup in pixels. Minimum value is 280.
minString''Defines the minimum selectable date. Accepts String (YYYY-MM-DD). For time types, accepts String (HH:mm or HH:mm:ss).
maxString''Defines the maximum selectable date. Accepts String (YYYY-MM-DD). For time types, accepts String (HH:mm or HH:mm:ss).
stepString/Number''Defines the step for date selection. For inputType="date", step is in days. For inputType="time" or inputType="datetime-local", step is in seconds.
hasTrimBooleanfalseDefines if the input value should be trimmed (spaces removed from start and end).
fontFamilyString"'Lato', sans-serif"Defines the font-family for the input
fontSizeStringnull (default: 1.4em)Defines the font-size for the input. If null, uses 1.4em as default size. Pass a string with the font-size and the unit (e.g., "1.2em").
fontWeightNumber400Defines the font-weight for the input
textColorString'#ffffff'Defines the text color for the input
caretColorString''Defines the caret (cursor) color. If empty, uses theme default.
selectionBgColorString''Defines the background color for selected text. If empty, uses theme default.
selectionTextColorString''Defines the text color for selected text. If empty, uses theme default.
hasBorderRadiusBooleanfalseDefines if the input should have border radius
borderRadiusNumber0.5Defines the border radius in rem units. Only applies if hasBorderRadius is true and inputStyle is not 'line'.
inputWidthNumber200Defines the input width in pixels. Only applies when display is not 'b'.
inputStyleString'background'Defines the input style. Accepts background, line, and border.
activeTextStyleString'normal'Defines the text style when input is active. Accepts normal, italic, and oblique.
inputUppercaseBooleanfalseDefines if the input text should be displayed in uppercase
inputAutocompleteString'on'Defines the autocomplete attribute. Accepts on and off.
textAlignString'left'Defines the text alignment. Accepts left, center, and right.
showLabelBooleanfalseDefines if the label should be displayed
labelString'Label text'Defines the label text
labelBackgroundString'transparent'Defines the background color for the label when active (floating)
labelPaddingString'1px 5px'Defines the padding for the label when active (floating)
labelBorderRadiusNumber0Defines the border radius for the label when active (floating)
labelLeftNumber5Defines the left position of the label when inactive (inside input)
inputLabelMarginActiveNumber15Defines the top margin of the input when label is active (floating)
labelActiveTopNumber-13Defines the top position of the label when active (floating)
labelActiveLeftNumber5Defines the left position of the label when active (floating)
fontFamilyLabelString"'Lato', sans-serif"Defines the font-family for the label
fontSizeLabelString'1em'Defines the font-size for the label when inactive
fontSizeLabelActiveString'0.8em'Defines the font-size for the label when active (floating)
fontWeightLabelNumber400Defines the font-weight for the label
lightTextColorLabelString'#333333'Defines the text color for the label in light theme
lightTextColorLabelActiveString'#333333'Defines the text color for the label when active in light theme
darkTextColorLabelString'#ffffff'Defines the text color for the label in dark theme
darkTextColorLabelActiveString'#ffffff'Defines the text color for the label when active in dark theme
lightBgColorString'#f8f8f2'Defines the background color for the input in light theme
lightBgColorFocusString'#eaeaea'Defines the background color for the input when focused in light theme
lightDisabledBgColorString'#dfdfd9'Defines the background color for the input when disabled in light theme
lightTextColorString'#000000'Defines the text color for the input in light theme
darkBgColorString'#353734'Defines the background color for the input in dark theme
darkBgColorFocusString'#272936'Defines the background color for the input when focused in dark theme
darkDisabledBgColorString'rgba(40, 42, 54, 1)'Defines the background color for the input when disabled in dark theme
darkTextColorString'#000000'Defines the text color for the input in dark theme
fontFamilyMsgString"'Lato', sans-serif"Defines the font-family for the message
fontSizeMsgString'1em'Defines the font-size for the message
fontWeightMsgNumber400Defines the font-weight for the message
textMessageColorString'#f15574'Defines the text color for the message
showMsgBooleanfalseDefines if the message should be displayed
hasMsgBooleanfalseDefines if there is a message to display
messageString'Default message text'Defines the message text
hasCustomMsgBooleanfalseDefines if the message uses custom styling
sizeMediaQueryString'sm'Defines the size media query. Accepts xs, sm, md, and lg. Currently always uses 'sm'.
tabindexString/Number0Defines the tabindex attribute for the input

Events

changed

Fired when the input value changes, returns the current value in different formats depending on the inputType:

For inputType="date":

  • Returns: String (format: YYYY-MM-DD)
  • Example: "2024-01-15"

For inputType="month":

  • Returns: String (format: YYYY-MM)
  • Example: "2024-01"

For inputType="time":

  • Returns: String (format: HH:mm or HH:mm:ss)
  • Example: "14:30" or "14:30:45"

For inputType="datetime-local":

  • Returns: String (format: YYYY-MM-DDTHH:mm or YYYY-MM-DDTHH:mm:ss)
  • Example: "2024-01-15T14:30" or "2024-01-15T14:30:45"

For inputType="week":

  • Returns: String (format: YYYY-Www - ISO 8601 week format)
  • Example: "2024-W03"

For range mode (allowRange="true"):

  • Returns: Object with startDate and endDate properties (Date objects)
  • Example: { startDate: Date, endDate: Date } or { startDate: null, endDate: null } when cleared

When input is cleared:

  • Returns: '' (empty string) for single date or { startDate: null, endDate: null } for range mode

Detailed Return Formats

The changed event returns values in different formats based on the inputType. Here are the detailed return formats:

Single Date Selection (inputType="date")

Returns: String in format YYYY-MM-DD

Example:

"2024-01-15"

When selection is cleared:

""

Date Range Selection (allowRange="true")

Returns: Object with startDate and endDate properties

{
  startDate: Date,               // Start date of the range (or null)
  endDate: Date                  // End date of the range (or null)
}

Example:

{
  startDate: new Date('2024-01-15'),
  endDate: new Date('2024-01-20')
}

When range is cleared:

{
  startDate: null,
  endDate: null
}

When only start date is selected (partial range):

{
  startDate: new Date('2024-01-15'),
  endDate: null
}

Week Selection (inputType="week")

Returns: String in ISO 8601 week format YYYY-Www

Example:

"2024-W03"  // Week 3 of year 2024

When selection is cleared:

""

Month Selection (inputType="month")

Returns: String in format YYYY-MM

Example:

"2024-01"  // January 2024

When selection is cleared:

""

Time Selection (inputType="time")

Returns: String in format HH:mm or HH:mm:ss

Example:

"14:30"        // 2:30 PM
"14:30:45"     // 2:30:45 PM (if seconds are included)

When selection is cleared:

""

DateTime Selection (inputType="datetime-local")

Returns: String in format YYYY-MM-DDTHH:mm or YYYY-MM-DDTHH:mm:ss

Example:

"2024-01-15T14:30"        // January 15, 2024 at 2:30 PM
"2024-01-15T14:30:45"     // January 15, 2024 at 2:30:45 PM (if seconds are included)

When selection is cleared:

""

current-value

Fired whenever the input value changes (same as changed but with a different name for compatibility). Returns the same format as changed.


focused

Fired when the input receives focus.

Returns: No return value

Example:

@focused="onInputFocused"

const onInputFocused = () => {
  console.log('Input focused')
}

blurred

Fired when the input loses focus.

Returns: No return value

Example:

@blurred="onInputBlurred"

const onInputBlurred = () => {
  console.log('Input blurred')
}

clicked

Fired when the input or its container is clicked.

Returns: No return value

Example:

@clicked="onInputClicked"

const onInputClicked = () => {
  console.log('Input clicked')
}

entered

Fired when the user presses Enter while the input is focused. Only fires if hasTabIndexEnter is true and the input is not disabled or readonly.

Returns: The current input value (same format as changed)

Example:

@entered="onEnterPressed"

const onEnterPressed = (value) => {
  console.log('Enter pressed with value:', value)
}

Slots

message

Custom slot for the message area. Allows custom HTML/content to be displayed as the message.

Example:

<nb-date-picker nb-id="datepicker-1" input-name="date">
  <template #message>
    <span>Custom error message</span>
  </template>
</nb-date-picker>

Examples

Basic Date Selection

<template>
  <NbDatePicker
    nb-id="datepicker-basic"
    input-name="date"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

Date Range Selection

<template>
  <NbDatePicker
    nb-id="datepicker-range"
    input-name="date-range"
    :allow-range="true"
    :use-custom-calendar="true"
    @changed="onRangeChanged"
  />
</template>

<script setup>
const onRangeChanged = (value) => {
  console.log('Selected range:', value)
  // value will be: { startDate: Date, endDate: Date }
}
</script>

Week Selection

<template>
  <NbDatePicker
    nb-id="datepicker-week"
    input-name="week"
    input-type="week"
    @changed="onWeekChanged"
  />
</template>

<script setup>
const onWeekChanged = (value) => {
  console.log('Selected week:', value) // Format: YYYY-Www
}
</script>

Month Selection

<template>
  <NbDatePicker
    nb-id="datepicker-month"
    input-name="month"
    input-type="month"
    @changed="onMonthChanged"
  />
</template>

<script setup>
const onMonthChanged = (value) => {
  console.log('Selected month:', value) // Format: YYYY-MM
}
</script>

Time Selection

<template>
  <NbDatePicker
    nb-id="datepicker-time"
    input-name="time"
    input-type="time"
    @changed="onTimeChanged"
  />
</template>

<script setup>
const onTimeChanged = (value) => {
  console.log('Selected time:', value) // Format: HH:mm or HH:mm:ss
}
</script>

DateTime Selection

<template>
  <NbDatePicker
    nb-id="datepicker-datetime"
    input-name="datetime"
    input-type="datetime-local"
    @changed="onDateTimeChanged"
  />
</template>

<script setup>
const onDateTimeChanged = (value) => {
  console.log('Selected datetime:', value) // Format: YYYY-MM-DDTHH:mm
}
</script>

DatePicker with Label

<template>
  <NbDatePicker
    nb-id="datepicker-label"
    input-name="date"
    :show-label="true"
    label="Select a date"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

Dark Theme DatePicker

<template>
  <NbDatePicker
    nb-id="datepicker-dark"
    input-name="date"
    theme="dark"
    :input-width="300"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

DatePicker with Min/Max Dates

<template>
  <NbDatePicker
    nb-id="datepicker-limited"
    input-name="date"
    min="2024-01-01"
    max="2024-12-31"
    :step="7"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

DatePicker with Custom Calendar Disabled

<template>
  <NbDatePicker
    nb-id="datepicker-native"
    input-name="date"
    :use-custom-calendar="false"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

DatePicker with Custom Styling

<template>
  <NbDatePicker
    nb-id="datepicker-custom"
    input-name="date"
    :has-border-radius="true"
    :border-radius="0.8"
    input-style="border"
    :input-width="250"
    text-align="center"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

DatePicker with Message

<template>
  <NbDatePicker
    nb-id="datepicker-message"
    input-name="date"
    :show-msg="true"
    :has-msg="true"
    message="Please select a valid date"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>

DatePicker with Custom Locale

<template>
  <NbDatePicker
    nb-id="datepicker-locale"
    input-name="date"
    locale="en-US"
    @changed="onDateChanged"
  />
</template>

<script setup>
const onDateChanged = (value) => {
  console.log('Selected date:', value)
}
</script>