Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fast, Searchable Dropdown Inputs with React

DZone's Guide to

Fast, Searchable Dropdown Inputs with React

Input fields that are limited to one of 6,000 possible values… That’s a usability nightmare, right?

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

Input fields that are limited to one of 6,000 possible values… That’s a usability nightmare, right? 

Dump it in a dropdown, and you overwhelm your users. How many even know vanilla dropdowns support search? And besides, you can't style those. Your designer will throw a sh*t fit. Default browser crap in his beautiful design? No, no, that won't do.

Looks vanilla, works vanilla. First open is slow, but search is fast if you know it exists. If you don't, you're screwed.

You could give users an input field and validate against the list of possible entries… no way that's going to be a nightmare, eh? Is itstanfordStanfordstanford university,Stanford university, or Stanford University? Some people will enter stnraofd.

No, no, the answer is both. You need an input field and a dropdown. Input filters dropdown, dropdown guides users.

Step 1: react-select

Jed Watson's react-select library gives you input fields with dropdowns. Users can use the input field or use the dropdown.

Implementation looks like this:

import Select from 'react-select';
import 'react-select/dist/react-select.css';

const options = [
    // ...
    { value: 'Stanford University', label: 'Stanford' },
    // ...
];

const field = ({ options }) => (
    <Select
        name="university"
        value="one"
        options={options}
        onChange={val => console.log(val)}
    />
);

The <Select> component does everything: input field, styled non-vanilla dropdown, mouse interaction, keyboard shortcuts, filtering. The only gotcha is that options have to be an array of { value, label }objects. Even if both value and label are the same, I tried.

A few seconds to render the dropdown. A few seconds to filter. The browser's UI thread blocked, and you can't even see what you're typing.

5,258 entries is a lot of entries 

Step 2: react-virtualized-select

Brian Vaughn's react-virtualized-select solves the first problem – opening the dropdown. It's a higher order component that does a thing and then your thing works better.

I think it implements paging and hides it behind scroll events. Only a few elements render at a time, and everyone's life is better.

Here's how you use it:

import Select from 'react-virtualized-select';
import 'react-select/dist/react-select.css';
import 'react-virtualized/styles.css'
import 'react-virtualized-select/styles.css'

const options = [
    // ...
    { value: 'Stanford University', label: 'Stanford' },
    // ...
];

const field = ({ options }) => (
    <Select
        name="university"
        value="one"
        options={options}
        onChange={val => console.log(val)}
    />
);

We changed the import Select from to usereact-virtualized-select and… that's all. 

It opens quickly, and I was typing that whole time that nothing was happening. Browser's UI thread still blocking.

Step 3: react-select-fast-filter-options

Brian Vaughn's react-select-fast-filter-options is practically too long to mention in a tweet, and it solves the second problem: fast search.

It builds an index of your options and uses advanced computer sciencey algorithms discovered some time in the 60's, probably. We rarely have enough data to worry about actual computer science on the front end, but sometimes we do.

Here's how you use it:

import Select from 'react-virtualized-select';
import createFilterOptions from 'react-select-fast-filter-options';
import 'react-select/dist/react-select.css';
import 'react-virtualized/styles.css'
import 'react-virtualized-select/styles.css'

const options = [
    // ...
    { value: 'Stanford University', label: 'Stanford' },
    // ...
];

const filterOptions = createFilterOptions({ options });

const field = ({ options }) => (
    <Select
        name="university"
        value="one"
        options={options}
        filterOptions={filterOptions}
        onChange={val => console.log(val)}
    />
);

We added a filterOptions prop to Select, which specifies a custom filter implementation, and we used createFilterOptions to instantiate that implementation. No need to worry about how it actually works because It Just Works™.

Looks good, works good. Faster even than the vanilla browser implementation 

The only gotcha is that you have to pass the same options to bothSelect and createFilterOptions. Dynamically generating{ value, label } objects from an array won't work.

The good news is that the memoization MobX does for @computedvalues is good enough, so you can do something like this:

class FormData {
    @observable universities = ['Stanford', 'UVA', ...];

    @computed get options() {
        return this.universities.map(name => ({ value: name, label: name }));
    }

    @computed get filterOptions() {
        const options = this.options;
        return createFilterOptions({ options });
    }
}

I don't know if it would work with Redux. As long as you're careful about the reference thing, you should be fine.

But Is It a Good Idea?

Yes. Use this approach for all select fields. Even the small ones! There's no harm, and it's faster. Winning.

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
react ,dropdown ,web dev

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}