What is end-to-end AI search?
Simplicity is critical for search engines, but building one that enables that simplicity is complex. Over the last 20+ years ...
Director of Product Management
Simplicity is critical for search engines, but building one that enables that simplicity is complex. Over the last 20+ years ...
Director of Product Management
Many new AI-powered search solutions have been released this year, and each promises to provide great results, but as ...
Marketing Campaign Production Manager
When you think of “customer experience,” what comes to mind? People, right? Specifically, consumers. Retail customers. That’s easy; the ...
Sr. SEO Web Digital Marketing Manager
A potential customer is about to land on the home page of your ecommerce platform, curious to see what cool ...
Search and Discovery writer
By now, everyone’s had the opportunity to experiment with AI tools like ChatGPT or Midjourney and ponder their inner ...
Director, Product Marketing
Search has been around for a while, to the point that it is now considered a standard requirement in many ...
Senior Machine Learning Engineer
With the advent of artificial intelligence (AI) technologies enabling services such as Alexa, Google search, and self-driving cars, the ...
VP Corporate Marketing
It’s no secret that B2B (business-to-business) transactions have largely migrated online. According to Gartner, by 2025, 80 ...
Sr. SEO Web Digital Marketing Manager
Twice a year, B2B Online brings together industry leaders to discuss the trends affecting the B2B ecommerce industry. At the ...
Director of Product Marketing & Strategy
This is Part 2 of a series that dives into the transformational journey made by digital merchandising to drive positive ...
Benoit Reulier &
Reshma Iyer
Get ready for the ride: online shopping is about to be completely upended by AI. Over the past few years ...
Director, User Experience & UI Platform
Remember life before online shopping? When you had to actually leave the house for a brick-and-mortar store to ...
Search and Discovery writer
If you imagine pushing a virtual shopping cart down the aisles of an online store, or browsing items in an ...
Sr. SEO Web Digital Marketing Manager
Remember the world before the convenience of online commerce? Before the pandemic, before the proliferation of ecommerce sites, when the ...
Search and Discovery writer
Artificial intelligence (AI) is no longer just the stuff of scary futuristic movies; it’s recently burst into the headlines ...
Search and Discovery writer
Imagine you are the CTO of a company that has just undergone a massive decade long digital transformation. You’ve ...
CTO @Algolia
Did you know that the tiny search bar at the top of many ecommerce sites can offer an outsized return ...
Director, Digital Marketing
Artificial intelligence (AI) has quickly moved from hot topic to everyday life. Now, ecommerce businesses are beginning to clearly see ...
VP of Product
Feb 28th 2022 engineering
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.
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:
npx create-instantsearch-app concert-search \ --app-id latency \ --api-key 059c79ddd276568e990286944276464a \ --index-name concert_events_instantsearchjs \ --template "React InstantSearch"
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.
To get the date range picker up and running, we need to install a couple of dependencies.
npm install @algolia/react-instantsearch-widget-date-range-picker @duetds/date-picker
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
import { DateRangePicker } from '@algolia/react-instantsearch-widget-date-range-picker'; import { defineCustomElements } from "@duetds/date-picker/dist/loader"; // Defines the custom elements from the date picker for use on the window object defineCustomElements(window);
Now that these packages are imported, we’re ready to get this on the page.
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.
<InstantSearch searchClient={searchClient} indexName="concert_events_instantsearchjs"> <div className="search-panel"> <div className="search-panel__filters"> <DateRangePicker attribute="date" /> </div> <div className="search-panel__results"> //... Results code </div> </div> </InstantSearch>
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).
:root { --duet-color-primary: #3c4ee0; --duet-color-text: #333; --duet-color-text-active: #fff; --duet-color-placeholder: #666; --duet-color-button: #f5f5f5; --duet-color-surface: #fff; --duet-color-overlay: rgba(0, 0, 0, 0.8); --duet-color-border: #d6d6e7; --duet-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; --duet-font-normal: 400; --duet-font-bold: 600; --duet-radius: 3px; --duet-z-index: 600; }
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.
.date-range-picker { display: grid; gap: 1rem; }
And with that, we have a functioning, user-friendly date picker. No more dealing with Unix timestamps and conversions.
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.
Powered by Algolia Recommend