Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/javadbat/jb-date-input

jalali date input with date picker support both touch screen for mobile user and keyboard typing for desktop user
https://github.com/javadbat/jb-date-input

calendar custom-elements datepicker jalali jalali-date-picker javascript

Last synced: 7 days ago
JSON representation

jalali date input with date picker support both touch screen for mobile user and keyboard typing for desktop user

Awesome Lists containing this project

README

        

# jb-date-input

[![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/jb-date-input)
[![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://raw.githubusercontent.com/javadbat/jb-date-input/master/LICENSE)
[![NPM Downloads](https://img.shields.io/npm/dw/jb-date-input)](https://www.npmjs.com/package/jb-date-input)

## Pure js Jalali Date Picker Web-Component

web component date input (picker) to get date (jalali & gregorian) from user.

- support jalali date as well as gregorian date

- support keyboard arrow key and fast date input with keyboard

- customizable style with css variables

- can set min and max date value

- web component so it can be used in every framework and even purejs project

- responsive and mobile friendly (support swipe in touch devices and handle virtual keyboard)

- support typescript

- good typing experience for desktop user

- it use your page font by default.

- have 3 value type so you can get inputed value in gregorian, jalali or timestamp base on your project need

- customizable value format so you can get your value in standard iso format or custom format like `1400/12/08` or `1400_12_08`

- support `esm` import build for modern `ECMA Script` nodejs app.

- compatible with native HTML `form` element to send data to server.

Demo & Sample:

in github pages:
in codepen:

## instructions

### getting started

#### using npm

1- install package:

```command
npm i jb-date-input
```

2- import package in one of your `.js` file:

```js
import 'jb-date-input';
```

3- use it in your `.html` file like any other tag:

```HTML

```
#### using cdn

beware that jb-date-input umd build do not exclude external dependancy and bundled as a standalone module, so only use this way if you dont access npm in your app.

1- you can just add script tag to your html file and then use web component how ever you need.

```HTML

```
2- use it in your `.html` file like any other tag:

```HTML

```
## format

default format of date input is 'YYYY-MM-DDTHH:mm:ss.SSS[Z]' that compatible and exact format of `new Date().toISOString()`
you can change it however you need and `[Z]` mean the exact Z charecter that used in ISO standard format `YYYY-MM-DDTHH:mm:ss.SSSZ[Z]` => `2012-06-21T00:00:00.000+3:30Z`
you can change format by format attribute:

```html

```

## Value Type

we have 3 value type:

```html



```
by setting value type you can tell component what type of value you provideing to it and expecting from it. remember that value type is not effect input type, for example user input jalali date but you will get gregorian date when call `e.target.value`. you can also provide and get js `Date` type for more performance if you like see "get value" section for get and for set just set value like: `element.value = new Date()`.

## min and max date limit

you can set minimum date and maximum date range for your date input in 2 way:

1- by html attribute in string format of your value

```html


```
2- by call `setMinDate` and `setMaxDate` function and providing string or Date format:

```javascript
const today = new Date();
document.querySelector('jb-date-input').setMinDate(today)
const max = new Date('2022-08-15T08:51:23.176Z');
document.querySelector('jb-date-input').setMaxDate(max);
// or string
document.querySelector('jb-date-input').setMinDate('2022-08-15T08:51:23.176Z');

```
## placeholder

you can set placeholder to show it to user when input is empty. to doing so just set `placeholder` attribute in HTML DOM or `placeholder` in JavaScript:

```html


```
```js
document.querySelector('jb-date-input').placeholder = `Enter Date Here`;
```
## custom validation

beside of min and max you can also set your own custom validation like any other jb web components family to achive this you must create a array of validations and assign them to component

```js
const validationList = [
{
validator:/^13.*$/g,
message:'تاریخ باید تنها در قرن 13 شمسی باشد'
},
{
validator:({text, inputObject, valueObject, valueText})=>{
//you can use raw imputed text or formatted text in expected value in arguments
//you have access to both jalali and gregorian date object here in valueObject
//inputObject is a object contain imputed day & month & year unprocessed base on format so it have value before date imputed completely
// remember valueObject and valueText are both empty and null when date is incomplete
//if you want to validate incomplete date you can use inputText
return valueObject.jalali.day == 15;
},
message:'باید تاریخ حتما 15 ماه انتخاب شود'
}
];
document.querySelector('jb-date-input').validation.list = validationList
```

remember your min and max date must be in the same format and valueType of your value.
to trigger validation and check is the element has a valid value:

```js
// if show error was false, in case of error component dont show error itself and function will return if data valid or not
const showError = true
const validationObj = dom.validation.checkValidity(showError)
```

## events

```js
//when default property are defined best time for impl your config like min and max date
document.querySelector('jb-date-input').addEventListener('init',this.onCalendarElementInitiated);

//when calendar init all property and function and dom created and bind successfully
document.querySelector('jb-date-input').addEventListener('load',this.onCalendarElementLoaded);
document.querySelector('jb-date-input').addEventListener('change',(e)=>{
//value in string
console.log(e.target.value)
//value in js Date object
console.log(e.target.valueInDate)
});
```

## date input type

jb-calendar support both jalali and gregorian(miladi) calendar input type. like value-type that let you determine how you want to provide/expect data to/from jb-date-input you can specify how user must fill the date input.
to achive this you have to set `input-type` attribute or set `inputType` object to component dom directly.
to set it as attribute you can set value like this:

```HTML

```

and for doing it with direct DOM assignment you can use following js code:

```js
//to show gregorian calendar
document.querySelector('jb-date-input').inputType = "GREGORIAN"
document.querySelector('jb-date-input').inputType = "JALALI"
```

## set default date for calendar when opened

when date input value is empty we show today year and month in opened calendar by default but you can change it to another date. for example you want user fill they birthdate you can set it to 20 years ago so user can pick his/her birthday easier and faster. to doing so all you have to do is to use `setCalendarDefaultDateView`function like this:

```javascript
const year = 1360
const month = 5
//just set year and month for current input-type date type
document.querySelector('jb-date-input').setCalendarDefaultDateView(year,month);
//set default year and month for gregorian input-type
document.querySelector('jb-date-input').setCalendarDefaultDateView(year,month,'GREGORIAN');
//set default year and month for jalali input-type
document.querySelector('jb-date-input').setCalendarDefaultDateView(year,month,'JALALI');
```
## get value
you can get the selected date by using following method:
```javascript
// return string value base on your provided format and value type
document.querySelector('jb-date-input').value

// return javascript Date value (or null)
document.querySelector('jb-date-input').valueInDate

```
note that providing & getting value with `Date` is faster and more perfomant than using value string

## show persian number
if you want to show persian number instead of English number char you just have to set `show-persian-number` attribute like this:
```javascript

//or

```
## customize calendar button trigger

you can change calendar icon base on your own need to doing so you just have to put your custom html inside web component with `slot="calendar-trigger-icon"` like below:

```html





















```
## Change Month List
you may want to change the default month list for both of Jalali and Gregorian calendars base on your country month labels. here how you can do it:
```js
document.querySelector('jb-date-input').setMonthList('JALALI',['حَمَل','ثَور','جَوزا','سَرَطان','اَسَد','سُنبُله','میزان','عَقرَب','قَوس','جَدْی','دَلو','حوت']);
document.querySelector('jb-date-input').setMonthList('GREGORIAN',['1','2','3','4','5','6','7','8','9','10','11','12']);
```
## overflow handler

sometimes you place date input inside modal or end of the pages so when user open the input picker it overflow the page and some part of picker will be invisible.
to fix this we add a feature called `overflowHandler` by set this to `SLIDE` the picker will move, on mouse enter it's territory so user can easily pick date
```js
document.querySelector('jb-date-input').elements.popover.overflowHandler = "SLIDE"
```
## set custom style

in some cases in your project you need to change default style of web-component for example you need zero margin or different border-radius and etc.
if you want to set a custom style to this web-component all you need is to set css variable in parent scope of web-component.
#### usage example:

```css
body{
/* if you need more margin */
--jb-date-input-margin: 16px 32px;
/* if you dont want rounded corner */
--jb-input-border-radius:0px;
/* if you want different text color*/
--jb-input-value-color:red;
}
```

you can customize jb-date-input look by setting css variable in your app.
jb-date-input use [jb-input](https://github.com/javadbat/jb-input) and [jb-calendar](https://github.com/javadbat/jb-calendar) and [jb-popover](https://github.com/javadbat/jb-popover) underneath so to change the styles of your component read custom style section of these components and set their css variable.
more than above here is the css variable that we use in jb-date-input itself:

#### variable list

| css variable name | description |
| ------------- | ------------- |
| --jb-date-input-margin | web-component margin default is `0 0` |
| --jb-date-input-calendar-trigger-display | set it no none to hide calendar icon |
| --jb-date-input-calendar-trigger-width | set calendar icon width |
| --jb-date-input-calendar-trigger-height | set calendar icon height |

## add custom element in input box

in jb-input you can put icon or any other custom html DOM in input box. to doing so you just have to place custom DOM in `jb-date-input` tag and add `slot="start-section"` or `slot="end-section"` to place it before or after input field.
example:

```HTML

after

before

```

## Other Related Docs:

- see [jb-date-input-react](https://github.com/javadbat/jb-date-input-react) if you want to use this component in react

- see [All JB Design system Component List](https://github.com/javadbat/design-system/blob/master/docs/component-list.md) for more components

- use [Contribution Guide](https://github.com/javadbat/design-system/blob/master/docs/contribution-guide.md) if you want to contribute in this component.