Playbook logo
14.18.0

Typeahead

Typeahead is auto suggestion or completion based on what the user is starting to type, gets refined as the user types more.

Default

Colors
All Colors

With Context

Crayola Crayons




With Pills

Colors

Windows


Typeahead kit is data-driven. The minimum default fields are label and value.

This is an example of an option: { label: 'Windows', value: '#FFA500' }

Rails: Default Options

You can also pass default_options which will populate the initial pill selections:

default_options: [{ label: 'Windows', value: '#FFA500' }]

Rails: Subscribing to JS Events

JavaScript events are triggered based on actions you take within the kit such as selection, removal and clearing.
This kit utilizes a default id prop named react-select-input. It is highly advised to send your own unique id prop when using this kit to ensure these events do not unintentionally affect other instances of the kit in the same view. The examples below will use the unique id prop named typeahead-pills-example1:

pb-typeahead-kit-typeahead-pills-example1-result-option-select event to perform custom work when an option is clicked.
pb-typeahead-kit-typeahead-pills-example1-result-option-remove event to perform custom work when a pill is clicked.
pb-typeahead-kit-typeahead-pills-example1-result-option-clear event to perform custom work when all pills are removed upon clicking the X.

Rails: Publishing JS Events

The same rule regarding the id prop applies to publishing JS events. The examples below will use the unique id prop named typeahead-pills-example1:

pb-typeahead-kit-typeahead-pills-example1:clear event to clear all options.

Without Pills (Single Select)

Colors
All Colors

Passing is_multi: false will allow the user to only select one option from the drop down. Note: this will disable pills prop.

With Pills (Async Data)

Github Users
type the name of a Github user

Rails: Providing the load_options Promise

*Additional required props: * async: true, pills: true

The prop load_options, when used in conjunction with async: true and pills: true, points to a JavaScript function located within the global window namespace. This function should return a Promise which resolves with the list of formatted options as described in prior examples above. This function is identical to the function provided to the React version of this kit. See the code example for more details.

React

loadOptions

*Additional required props: * async: true

As outlined in the react-select Async docs, loadOptions expects to return a Promise that resolves resolves with the list of formatted options as described in prior examples above. See the code example for more details.

getOptionLabel + getOptionValue

If your server returns data that requires differing field names other than label and value

See react-select docs for more information: https://react-select.com/advanced#replacing-builtins

With Pills (Async Data w/ Users)

Github Users
type the name of a Github user

If the data field imageUrl is present, FormPill will receive that field as a prop and display the image.

Inline

Synths

Oberheim

Cities
Add cities

Multi Kit Options

Badges
Verve
Small Pills

Kandinsky


Error State

Products
Please make a valid selection

Typeahead w/ Error shows that an option must be selected or the selected option is invalid (i.e., when used in a form it signals a user to fix an error).

Margin Bottom

None
All Colors
XXS
All Colors
XS
All Colors
Default - SM
All Colors
MD
All Colors
LG
All Colors
XL
All Colors

With Pills (Custom Color)

Colors

Windows


Change the form pill color by passing the optional pill_color prop. Product, Data, and Status colors are available options. Check them out here in the Form Pill colors example.

Truncated Text

Truncation Within Typeahead

For Form Pills with longer text, the truncate global prop can be used to truncate the label within each Form Pill. Hover over the truncated Form Pill and a Tooltip containing the text or tag section of the Form Pill will appear. See here for more information on the truncate global prop.

Dynamic Options

Pick a Shade

You can also set up a typeahead to render options dynamically based on input from a select. To achieve this:

  • The typeahead must have a unique id
  • Use the search_context_selector prop on the typeahead. The value here must match the id of the select so the Typeahead knows where to read the current "context" from.
  • Use options_by_context to pass in a hash whose keys match the possible values of your “context” select. Each key maps to an array of { label, value } objects. The typeahead automatically displays only the subset of options matching the current context.
  • Additionally, the optional clear_on_context_change prop controls whether the typeahead clears or not when a change happens in the linked select. This prop is set to true by default so that whenever a selection is made in the select, the Typeahead automatically clears its current input/selection.
Dynamic Options (Pure Rails)

Pick a Shade

The dynamic rendering of options for the typeahead can also be achieved with a pure Rails implementation (not react rendered). For this implementation, use all the props as above with the following additions:

  • search_term_minimum_length: this sets the minimum input in the typeahead needed to display the dropdown. This is set to 3 by default. Set it to 0 for the dropdown to always display when the typeahead is interacted with.
Disabled

Colors
All Colors


Things to Avoid

Avoid using on questionaires, surverys, text input and textarea when users answers/inputs will be individualized.


Available Props

Props Type Values Default

margin_bottom

enum
none
xxs
xs
sm
md
lg
xl
sm

id

string

async

boolean
false

default_options

hasharray

error

string

get_option_label

string

get_option_value

string

inline

boolean
false

label

string

load_options

string

multi_kit

string

name

string

options

hasharray

input_options

hashprop

is_multi

boolean
true

pills

boolean
false

placeholder

string

plus_icon

boolean
false

search_term_minimum_length

string
3

search_debounce_timeout

string
250

value

string

pill_color

enum
primary
neutral
success
warning
error
info
data_1
data_2
data_3
data_4
data_5
data_6
data_7
data_8
windows
siding
roofing
doors
gutters
solar
insulation
accessories
primary

required

boolean
false

validation

hashprop

wrapped

boolean
false

clear_on_context_change

boolean
true

options_by_context

hashprop

search_context_selector

string

disabled

boolean
false