Share on facebookShare on linkedinShare on twitterShare by email

When crafting a search interface, it’s often enough to use what comes prepackaged with Algolia’s InstantSearch libraries. Each library comes in a different flavor of JavaScript for your ease of use. Each one has all the main pieces you need to craft a strong UI.

Sometimes you want to go beyond what’s available.

At that point, you’re presented with two options: craft your own with our connector APIs or find a pre-built custom widget.

It should come as no surprise to anyone that knows me that when presented with wanting a React date picker, I wanted to write as little code as possible to make it work, but I also wanted the best UI possible for Santa. When it came to bonus challenge number 5, I needed something extra. My first stop? The Algolia Code Exchange!

After a quick search around date components, I found exactly what I needed: the aptly-named @algolia/react-instantsearch-widget-date-range-picker! There’s also a VanillaJS version if you’re not using React.

Setting up the project

If you’d like to follow along, you’ll need to install a new React InstantSearch project. The easiest way to do that is to run the following command:

This monster command will set up a React InstantSearch project and connect it to Algolia’s hosted concert Index. Change directory into the created folder and run yarn start for a solid starting point.

At this point, you’ve got a full search experience for a set of concerts.

If you want to filter by date, you could set up a <RefinementList> component and set its attribute prop to date, but those dates are Unix timestamps (for easiest comparisons). This isn’t ideal for the user experience. Let’s make that better.

Installing and configuring the widget

To get the date range picker up and running, we need to install a couple of dependencies.

This will install the official Algolia React Date picker widget and its dependency the Duet Date picker.

From here, we need to import the packages into our src/App.js file and initialize the Duet Date Picker for use.

// Add right after the imports included in the create-instantsearch-app code

Now that these packages are imported, we’re ready to get this on the page.

Adding the date range picker

To add the picker to the page, we need to select a spot within the <InstantSearch> component. The base of the app is a search-panel. By default, all we have inside this is the results, but we can add a filter panel, as well.

The DateRangePicker accepts an attribute prop. This prop accepts a date-based attribute from the hits in our Index.

Quick note: The DateRangePicker accepts a Unix timestamp with milliseconds since the Epoch, not seconds. Depending on your data structure, you may need to create a secondary timestamp in your data.

When you view the rendered page, you should now have a date picker. There are a few small UI snags to clean up.

The Duet Date picker uses a slew of CSS Custom Properties for much of its styling. At the start of our src/App.css file, we need to paste those in and configure for our app (as appropriate).

The two picker areas are a little close together still, so let’s fix that with a little CSS, as well. This can go in src/App.css but should be closer to the bottom. There are many ways to get the space between the two items, the quick and easy solution is to use CSS Grid and the gap property.

And with that, we have a functioning, user-friendly date picker. No more dealing with Unix timestamps and conversions.

Take this further

This works great in the small sample dataset, but can also be brought into any Algolia InstantSearch application. If you’re looking to take this example further, create a better UI around each Hit by editing the hit component in src/App.js. You could also show the current filters applied with the CurrentRefinements component.

Share on facebookShare on linkedinShare on twitterShare by email
About the authors

Loading amazing content…

Subscribe to the blog updates
Thank you for subscribing, stay tuned!