Sha256: 90cc7e4078063dd1bb8eb9990aa72d079fe6a462e953041db413dd08974e994a

Contents?: true

Size: 1.8 KB

Versions: 63

Compression:

Stored size: 1.8 KB

Contents

/* @flow */

import React, { useState } from 'react'

import {
  Caption,
  Typeahead,
  User,
} from '../..'

/**
 *
 * @const filterResults
 * @ignore
 * @returns {[Object]} - a custom mapping of objects, minimally containing
 * `value` and `label` among other possible fields
 * @summary - for doc example purposes only
 */

const filterResults = (results) =>
  results.items.map((result) => {
    return {
      label: result.login,
      value: result.id,
    }
  })

/**
 *
 * @const promiseOptions
 * @ignore
 * @returns {Promise} - fetch API data results from Typeahead input text
 * @see - https://react-select.com/home#async
 * @summary - for doc example purposes only
 */

const promiseOptions = (inputValue) =>
  new Promise((resolve) => {
    if (inputValue) {
      fetch(`https://api.github.com/search/users?q=${inputValue}`)
        .then((response) => response.json())
        .then((results) => resolve(filterResults(results)))
    } else {
      resolve([])
    }
  })

const TypeaheadWithPillsAsync = () => {
  const [users, setUsers] = useState([])
  const handleOnChange = (value) => setUsers(value)

  return (
    <>
      <If condition={users && users.length > 0}>
        <Caption
            marginBottom="xs"
            text="State (Users)"
        />
        <For
            each="user"
            of={users}
        >
          <User
              align="left"
              key={user.value}
              marginBottom="md"
              name={user.label}
              orientation="horizontal"
          />
        </For>
      </If>
      <Typeahead
          async
          isMulti
          label="Github Users"
          loadOptions={promiseOptions}
          onChange={handleOnChange}
          placeholder="type the name of a Github user"
      />
    </>
  )
}

export default TypeaheadWithPillsAsync

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
playbook_ui-7.9.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.8.4 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.8.3 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.8.2 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.8.1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.8.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.7.0.pre.alpha1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.7.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.6.2.pre.alpha1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.6.2 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.6.1.pre.alpha1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.6.1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.6.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.5.1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.5.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.4.2 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.4.1 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.4.0.pre.alpha6 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.4.0.pre.alpha5 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx
playbook_ui-7.4.0 app/pb_kits/playbook/pb_typeahead/docs/_typeahead_with_pills_async.jsx