vue-datepicker-next

repository·main·Indexed 41 days ago

https://github.com/mengxiong10/vue-datepicker-next

A versatile date and time picker component for Vue 3 supporting date, range, year, month, and time selection. Version 1.0.3 features extensive customization via SCSS themes, internationalization (locales), and custom formatting tokens. It provides flexible data binding through the valueType prop (date, timestamp, format, or token) and supports custom UI overrides using slots for headers, footers, and icons.

Tokens
14.1K
Snippets
49
Records
60
Agent score
50%

What's inside vue-datepicker-next

  1. Customize themes using SCSS

    main

    If your project uses SCSS, you can override default variables by creating a custom SCSS file. You can change the $namespace (which requires setting the prefix-class prop on the component) and colors like $default-color and $primary-color before importing the library's styles.

    $namespace: 'xmx'; // Change default prefix to 'xmx'. Then set <date-picker prefix-class="xmx" />
    
    $default-color: #555;
    $primary-color: #1284e7;
    
    @import '~vue-datepicker-next/scss/index.scss';
  2. Use vue-datepicker-next in a Vue 3 component

    main

    To use the date picker, import the DatePicker component and its corresponding CSS file. Use v-model:value to bind the selected date to a data property.

    Common configurations include:

    • type="datetime": Enables datetime selection.
    • valueType="format": Changes how the value is returned.
    • range: Enables date range selection.
    <script>
      import DatePicker from 'vue-datepicker-next';
      import 'vue-datepicker-next/index.css';
    
      export default {
        components: { DatePicker },
        data() {
          return {
            time0: null,
            time1: null,
            time2: null,
            time3: null,
          };
        },
      };
    </script>
    
    <template>
      <div>
        <!-- Basic date picker -->
        <date-picker v-model:value="time0"></date-picker>
        
        <!-- Datetime picker -->
        <date-picker v-model:value="time1" type="datetime"></date-picker>
        
        <!-- Picker with specific value type -->
        <date-picker v-model:value="time2" valueType="format"></date-picker>
        
        <!-- Range picker -->
        <date-picker v-model:value="time3" range></date-picker>
      </div>
    </template>
  3. Configure Internationalization (Locales)

    main

    The default language is English. To use a different locale, import the specific locale file from vue-datepicker-next/locale/. Importing a locale file makes it the active locale for the component.

    You can also partially override the default locale by passing a configuration object to the lang prop. The lang prop accepts either a string (the locale name) or an object containing specific overrides (e.g., formatLocale or monthBeforeYear).

    import DatePicker from 'vue-datepicker-next';
    import 'vue-datepicker-next/index.css';
    
    // Import a specific locale to make it active
    import 'vue-datepicker-next/locale/zh-cn';
    <script>
      export default {
        data() {
          return {
            // Override specific locale settings via object
            langObject: {
              formatLocale: {
                firstDayOfWeek: 1,
              },
              monthBeforeYear: false,
            },
            // Use a locale string
            langString: 'zh-cn',
          };
        },
      };
    </script>
    
    <template>
      <date-picker :lang="langObject"></date-picker>
      <date-picker :lang="langString"></date-picker>
    </template>
  4. Configure Internationalization (i18n)

    main

    By default, the language is English. You can switch to Chinese (or other languages) by importing the specific locale file. You can also override specific language settings using the lang prop with a configuration object.

    import DatePicker from 'vue-datepicker-next';
    import 'vue-datepicker-next/index.css';
    
    // Import Chinese locale
    import 'vue-datepicker-next/locale/zh-cn';
    <script>
      export default {
        data() {
          return {
            // Overriding specific settings
            langObject: {
              formatLocale: {
                firstDayOfWeek: 1,
              },
              monthBeforeYear: false,
            },
            // Using a locale string
            langString: 'zh-cn',
          };
        },
      };
    </script>
    
    <template>
      <date-picker :lang="langObject"></date-picker>
      <date-picker :lang="langString"></date-picker>
    </template>
  5. Customize the theme using SCSS

    main

    If your project uses SCSS, you can override default style variables by creating a custom SCSS file and importing the library's SCSS entry point.

    You can change the $namespace variable to customize the CSS class prefix. If you change $namespace to 'xmx', you must then use the prefix-class="xmx" prop on the <date-picker /> component.

    $namespace: 'xmx'; // change the 'mx' to 'xmx'. then <date-picker prefix-class="xmx" />
    
    $default-color: #555;
    $primary-color: #1284e7;
    
    @import '~vue-datepicker-next/scss/index.scss';
  6. Configure the v-model value type

    main

    Use the valueType prop to determine the data type returned by the component's v-model.

    Supported values:

    • "date" (default): Returns a JavaScript Date object.
    • "format": Returns a string matching the input value format.
    • "timestamp": Returns a numeric timestamp.
    • token: A specific format string pattern (e.g., a custom date format string).
    <date-picker v-model="value" value-type="format" />
  7. Customize time picker columns and steps

    main

    The time picker columns (Hour, Minute, Second) are displayed based on the format string (e.g., HH:mm:ss) by default. You can override this behavior using:

    Visibility Control:

    • showHour
    • showMinute
    • showSecond

    Stepped Intervals:

    • hourStep
    • minuteStep
    • secondStep

    Custom Options Lists:

    • hourOptions
    • minuteOptions
    • secondOptions
  8. Configure the type of value bound to v-model

    main

    Use the valueType prop to determine the data type returned by v-model.

    Available options:

    • "date" (default): Returns a JavaScript Date object.
    • "format": Returns a string formatted according to the input field's format.
    • "timestamp": Returns a numeric timestamp.
    • token: Returns a string formatted according to a specific parsable token.
    // Example usage of valueType
    <date-picker v-model="dateValue" value-type="timestamp" />
  9. Install and use vue-datepicker-next as a Vue plugin

    main

    You can install vue-datepicker-next as a Vue plugin to register the DatePicker component globally. This allows you to use <DatePicker /> anywhere in your application without local imports.

    import DatePicker from 'vue-datepicker-next';
    import 'vue-datepicker-next/dist/index.css';
    
    // In your main.ts or app entry point
    const app = createApp(App);
    app.use(DatePicker);
    app.mount('#app');
  10. Use `shortcuts` for Range Selection

    main

    When using a range picker, you can provide a shortcuts array to allow users to quickly select common date ranges. Each shortcut object must contain:

    • text: The label displayed for the shortcut.
    • onClick: A callback function that must return a Date object.
    [
      { text: 'today', onClick: () => new Date() },
      {
        text: 'Yesterday',
        onClick: () => {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          return date;
        },
      },
    ];