3.4.4 • Published 1 year ago

svelty-picker-raz v3.4.4

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

A fork of Svelty Picker

  • Added the close "close" event.
     

📆 Svelty Picker NPM version

Simple date & time picker implemented in svelte.

Features:

  • datepicker
  • timepicker (with meridian support)
  • various formatting options
  • keyboard navigation
  • includes <input> element
  • easily themable
  • restriction by start and end date
  • validator action for <input> using svelte-use-forms (optional)
  • custom element

screenshot

⚙️ Install

npm install svelty-picker

👀 Example

<script>
  import SveltyPicker from 'svelty-picker'
  
  let myDate = '2021-11-11';
</script>

<SveltyPicker inputClasses="form-control" format="yyyy-mm-dd hh:ii" bind:value={myDate}></SveltyPicker>

Try yourself in REPL.

📓 Options

PropertyTypeDefaultDescription
namestringdatehtml attribute for underlying <input> element
disabledboolfalsehtml attribute for underlying <input> element
placeholderstringnullhtml attribute for underlying <input> element
requiredboolfalsehtml attribute for underlying <input> element
valuestringnullstring representation of selected value
initialDateDatenullinitial date object, if you prefer that to value
startDatestring|Datenulllimit minimal selectable date
endDatestring|Datenulllimit maximal selectable date
startViewnumber2Which mode should picker at, 0 - decade, 1 - year, 2 - month (default), 3 - time picker
pickerOnlyboolfalsePicker is always visible and input field is then hidden, but still present
themestringsdt-calendar-colorscss class defining css variables
modestringautorestrict picker's mode. Possible values: auto|date|datetime|time. By default it try to guess the mode from format
formatstringyyyy-mm-ddFormat of entered date/time. See format settings for available options
minuteIncrementnumber1number in range 1-60 to set the increment of minutes choosable
weekStartnumber1number in range 0-6 to select first day of the week. Sunday is 0
inputIdstring | id attribute for input element
inputClassesstring | input css class string
todayBtnClassesstringsdt-action-btn sdt-today-btntoday button css classes
clearBtnClassesstringsdt-action-btn sdt-clear-btnclear button css classes
todayBtnbooltrueShow today button
clearBtnbooltrueShow clear button
clearTogglebooltrueAllows to clear selected date when clicking on the same date when in mode='date' or mode='auto' resolving to 'date'
autoclosebooltrueHides picker after selection is done. If mode includes time picker, it closes automatically only after minute selection
i18nobjectenlocalization object, english is by default
positionFnfunctioninternalfunction used to position picker. Used as action. Acceps following object: { inputEl, visible}, where visible is visible parameter & inputEl is underlying <input> element
validatorActionarraynullBind validator action for inner <input> element. Designed to be used with svelte-use-form.

Note: Properties starting by theme to i18n are configurable globally by overriding it in globally available config

import { config } from 'svelty-picker';

// set new locale 
config.i18n = {
  // my localization object
}
// default for all pickers in the app
config.todayBtn = false;

Format settings

Date format can be defined under formatType property. It has two options: standard and php, where standard is the default.

standard format settings:

FormatDescriptionExample
dday of the month without leading zeros1 to 31
ddday of the month, 2 digits with leading zeros01 to 31
Dshort textual representation of a weekday (i18n.daysShort)Mon through Sun
DDlong textual representation of a weekday (i18n.days)Sunday through Saturday
SEnglish ordinal suffix for the day of the month, (i18n.suffix)st, nd, rd or th. Works well with d
mnumeric representation of month without leading zeros1 to 12
mmnumeric representation of the month, 2 digits with leading zeros01 to 12
Mshort textual representation of a month, three letters (i18n.monthsShort)Jan through Dec
MMfull textual representation of a month, such as January or March (i18n.months)January through December
yytwo digit representation of a year99 or 03
yyyyfull numeric representation of a year, 4 digits1999, 2003
hhour without leading zeros - 24-hour format0 - 23
hhhour, 2 digits with leading zeros - 24-hour format00 - 23
Hhour without leading zeros - 12-hour format1 - 12
HHhour, 2 digits with leading zeros - 12-hour format01 - 12
iminutes, 2 digits with leading zeros00 - 59
iialias for i00 - 59
sseconds, 2 digits with leading zeros00
ssalias for s00
pmeridian in lower case ('am' or 'pm') - according to locale file (i18n.meridiem)am or pm
Pmeridian in upper case ('AM' or 'PM') - according to locale file (i18n.meridiem)AM or PM
ttimestamp in milliseconds (although milliseconds are always 0). For timestamp in seconds use php format

php format settings:

FormatDescriptionExample
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
lA full textual representation of the day of the weekSunday through Saturday
NISO 8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
YA full numeric representation of a year, at least 4 digits0787, 1999, 2003
yA two digit representation of a year99 or 03
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds with leading zeros00
Utimestamp in seconds. For timestamp with miliseconds use standard format

CSS variables

/** defaults */
.sdt-calendar-colors {
  --sdt-primary: #286090;
  --sdt-color: #000;
  --sdt-bg-main: #fff;
  --sdt-bg-today: var(--sdt-primary);
  --sdt-bg-clear: #dc3545;
  --sdt-today-bg: #1e486d;
  --sdt-clear-color: #dc3545;
  --sdt-btn-bg-hover: #eee;
  --sdt-btn-header-bg-hover: #dfdfdf;
  --sdt-clock-bg: #eeeded;
  --sdt-clock-bg-minute: rgb(238, 237, 237, 0.25);
  --sdt-clock-bg-shadow: 0 0 128px 2px #ddd inset;
  --sdt-shadow: #ccc;
  --sdt-disabled-date: #b22222;
}

🗯️ Events

Component emits input, change and blur events.

  • input is dispatched on <input> element therefore you can get current value like from every native event:
  • change event is using Svelte's eventDispatcher, therefore triggered event contains detail property
  • close when the datepicker is supposed to close (used for pickerOnly = true)
<script>
function onInput(event) {
  console.log(event.target.value) // logs currently selected date or empty string
}

function onChange(event) {
  console.log(event.detail) // logs currently selected date or null
}
</script>

<SveltyPicker on:input={onInput} on:change={onChange}></SveltyPicker>

🌐 Localization

By default date picker uses english locale. And at the moment there is only few locales available (PRs for additional locales are more than welcome).

So if you want to change it to german (or other locale in the future), use this:

<script>
  import SveltyPicker from 'svelty-picker.svelte';
  import { de } from 'svelty-picker/i18n';
</script>

<SveltyPicker i18n={de}></SveltyPicker>

Global locale setting

You can also change locale globally through setting i18n property in global config like below. So all pickers created AFTER this setting has been changed, will be in the new locale.

<script>
  import SveltyPicker, { config } from 'svelty-picker';
  import { de } from 'svelty-picker/i18n';

  // all pickers in the app will have german locale set
  config.i18n = de;
</script>

<SveltyPicker></SveltyPicker>

Adding additional locales

Localization file has following structure.

// default i18n structure
export const en = {
  days:        ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
  daysShort:   ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  daysMin:     ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
  months:      ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  meridiem:    ['am', 'pm'],
  suffix:      ['st', 'nd', 'rd', 'th'],
  todayBtn:    'Today',
  clearBtn:    'Clear',
  timeView:    'Show time view',
  backToDate:  'Back to calendar view'
}

🏆 Thanks to:

Licence:

MIT

3.4.4

1 year ago

3.4.3

1 year ago