0.2.3 • Published 3 years ago

better-datepicker v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Better-datepicker

A PC Datepicker with only 9KB

npm.io code-test npm.io npm.io npm.io install size

Doc

https://jumodada.github.io/better-datepicker/

CDN

https://cdn.jsdelivr.net/npm/better-datepicker@0.2.2/

Installing

Using npm:

$ npm i better-datepicker

Using yarn:

$ yarn add better-datepicker

Usage

es5

import  {createDatePicker} from 'better-datepicker'
import 'better-datepicker/dist/index.css'

CommonJS

const {createDatePicker} = require('better-datepicker').default
require('better-datepicker/dist/index.css')

Example

<input type="text" id="input">
  const input = document.querySelector('#input')
  createDatePicker(input,{
    placement: 'bottom',
    type: 'date',
    zIndex: 2000,
    format: 'yyyy/MM/dd'
  })

you can also

  <div id="wrapper">
     <span>Including children, find the first inputElement</span>
     <input type="text">
  </div>
  <script >
    const input = document.querySelector('#wrapper')
    createDatePicker(input,{
      placement: 'bottom',
      type: 'date',
      zIndex: 2000,
      format: 'yyyy/MM/dd'
    })
  </script>

todo: support virtual dom

Options Validator

If the format is illegal, it will use default value

Options

OptionsDescriptionTypeAccepted ValuesDefault
placeholderTo set placeholderstring--
typeType of datepickerstringdate/date-range/month/month-range/year/year-range/weekdate
formatTo set the date formatstring-yyyy/MM/dd
classesTo set the picker wrapper classes(It will have a logo prefix: better-datepicker)string[]--
zIndexz-index of Pickernumber-2000
styleTo set the picker wrapper style(zIndex Priority is lower than above)Object--
placementPlacement of datepickerstringtop/bottom/right/leftbottom
disabledDateSpecifies the date that cannot be selectedfunction--
offsetDistance between Picker and inputElementnumber-12
insertToName of the node insertedstringbody/parentbody
bindingBind the value of the inputElementboolean-true
themeColorTheme color(selected、hover、range-start、range-end), like #2ECC71string--
rangeBgColorThe backgroundColor that element is in range ,It's usually lighter than the themeColorstring--
tdColorTd text colorstring--
thColorTh text colorstring--

Default Options

global config

  import {defaultOptions} from "better-datepicker"
  defaultOptions({
    placement: 'bottom',
    type: 'date',
    zIndex: 2000,
    format: 'yyyy/MM/dd'
  })

Theme

you can use defaultOptions

  import {defaultOptions} from "better-datepicker"
  defaultOptions({
    themeColor: '#1890ff',
    rangeBgColor: '#e6f7ff',
    tdColor: '#5f5f5f',
    thColor: '#5f5f5f'
  })

or

only changes theme of the current instance

     const input = document.querySelector('#wrapper')
     const instance = createDatePicker(input,{
         themeColor: '#1890ff',
         rangeBgColor: '#e6f7ff',
         tdColor: '#5f5f5f',
         thColor: '#5f5f5f'
     })

Locale

global config

  import {locale} from "better-datepicker"
  import zhCn from 'better-datepicker/dist/locale_es/zh-cn'    // es
  //require('better-datepicker/dist/locale_umd/zh-cn') // cjs
  locale(zhCn)
Locale/LanguageAbbreviation/Link
Afghanistanaf
Brazilbr
Englishen
Australiaen-au
Canadaen-au
Englanden-gb
Irelanden-ie
Spaines
Finlandfi
Angolafo
Faroe Islandsfr
Croatiahr
Haitiht
Italyit
Japanja
Korea(South)ko
Kuwaitku
Lebanonlb
Laoslo
Ukraineuk
Uzbekistanuz
Chinesezh
Simplified Chinesezh-cn
Hong Kong Chinazh-hk
Taiwan Chinazh-tw

or configure your own region

  import {locale} from "better-datepicker"
  const CubaLocale =  {
    name: 'Cuba',
    weekStart: 6,  //Saturday is set as the first day of the week
    months: ["کانونی دووەم", "شوبات", "ئازار", "نیسان", "ئایار", "حوزەیران", "تەمموز", "ئاب", "ئەیلوول", "تشرینی یەكەم", "تشرینی دووەم", "كانونی یەکەم"],
    weekdays: ["ی", "د", "س", "چ", "پ", "ه", "ش"], //short for week
    weekFormat: 'yyyy-ww'
  }
  locale(CubaLocale)

Instance methods

nameDescriptionparams
destroyeddestroyed the datepicker,clear InputElement value-
onChangecalled callback when date has changed(callback)
updateUpdate configuration, remove old datepicker(options)
getCurrentDateget current date-
openopen the datepicker-
closeclose the datepicker-
clearclear the date-

DESTROY

  import {destroy} from "better-datepicker"

  const picker1 = createDatePicker('#input1')
  
  // picker1.destroyed()

  const picker1 = createDatePickerc('#input2')
  destroy([picker1,picker2]) // destroyed picker1 and picker 2
  destroy() //destroyed all 

USE IN VUE

Reactive updates have been supported since version 0.2.2

When a property of props is changed, the datepicker is also updated

<template>
  <el-input ref='input'></el-input>
</template>

<script lang='ts'>
import '../../../assets/svg/svg'
import { defineComponent, toRefs } from 'vue'

import { createDatePicker } from '../../../../../src'

export default defineComponent({
  name: 'datepicker',
  data() {
    return {
      datepicker: null,
    }
  },
  mounted() {
    const input = this.$refs.input
    this.datepicker = createDatePicker(input.$el, this.$props)
  }
})
</script>

TODO LIST

versionsDescription
0.5.0support unlinkPanels、zIndex、readonly、default-value、className、style、size ....
0.7.0support extends plugins
0.9.0support time picker

Browser support

todo

License

MIT

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.6

3 years ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.3

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago