Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
A few years ago I built a WordPress website for a company that rents out equipment. They wanted to allow their customers to rent equipment from their website, rather than having to call or stop in at the shop.
In order to add the booking functionality that integrates directly with their ERP I built a custom datepicker plugin using Svelte.
This post walks through building a basic datepicker built with Svelte.
Define the basic calendar grid
We start by defining a general grid for a calendar month and initializing the grid with let rows = initRows(). This initRows function just describes how many days are in a week, weeks in a month, etc. We'll fill this out with data for the selected month and year in a minute.
javascript
Fill out the calendar grid for the selected month and year
Next we fill out the calendar grid with the correct dates using the components onMount lifecycle function.
javascript
Updating the calendar grid
Now whenever we change the month and/or year we also need to update the calendar grid.
javascript
Displaying the calendar
Now that we have an array of days for the selected month and year we can display them in the calendar.
svelte
View the project
For a full look at how this basic datepicker was created you can check out the GitHub repo. Keep in mind this is only meant to be a starting point that you can build off so it is pretty basic, but it can be customized pretty extensively to meet your needs.