JSPM

@commercetools-uikit/search-select-field

10.42.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3471
  • Score
    100M100P100Q130320F
  • License MIT

A search select field built on top of search select input, allowing users to asynchronously search for options

Package Exports

  • @commercetools-uikit/search-select-field

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@commercetools-uikit/search-select-field) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

SearchSelectField

Description

A search select field built on top of search select input, allowing users to asynchronously search for options

Installation

yarn add @commercetools-uikit/search-select-field
npm --save install @commercetools-uikit/search-select-field

Additionally install the peer dependencies (if not present)

yarn add react
npm --save install react

Usage

import React from 'react';
import SearchSelectField from '@commercetools-uikit/async-select-field';

const Example = () => (
  <SearchSelectField
    title="customer"
    id="customer"
    name="customer"
    isRequired={true}
    horizontalConstraint="m"
    optionType="single-lined"
    isAutofocussed={false}
    backspaceRemovesValue={true}
    isClearable={true}
    isDisabled={false}
    isReadOnly={false}
    isMulti={false}
    onChange={
      (/* event */) => {
        /** onChange logic */
      }
    }
    noOptionsMessage="No exact match found"
    loadingMessage="loading exact matches"
    placeholder="Select customer"
    loadOptions={
      (/* inputValue */) => {
        // async fetch logic
      }
    }
    renderError={(key) => {
      // This example shows how to handle custom errors on top of the
      // predefined errors of FieldErrors which this component and other
      // Field components use under the hood.
      switch (key) {
        case 'missing':
          return 'This field is required.';
        case 'duplicate':
          return 'This customer is already attached to the store. Customers must be unique.';
        case 'customError':
          return 'A custom error.';
        default:
          return null;
      }
    }}
    cacheOptions={false}
  />
);

export default Example;

Properties

Props Type Required Default Description
horizontalConstraint enum
Possible values:
's', 'm', 'l', 'xl', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'
Horizontal size limit of the input fields.
aria-label string Aria label (for assistive tech)
aria-labelledby string HTML ID of an element that should be used as the label (for assistive tech)
id string The id of the search input. This forwarded as react-select's "inputId"
containerId string The id to set on the SelectContainer component. This is forwarded as react-select's "id"
name string Name of the HTML Input (optional - without this, no input will be rendered)
placeholder string Placeholder text for the select value
components objectOf(func) Map of components to overwrite the default ones, see what components you can override
tabIndex string Sets the tabIndex attribute on the input
value custom The value of the select; reflected by the selected option
backspaceRemovesValue bool Remove the currently focused option when the user presses backspace
hasError bool Indicates the input field has an error
hasWarning bool Indicates the input field has a warning
isReadOnly bool Is the select read-only
isDisabled bool Is the select disabled
isClearable bool Is the select value clearable
isOptionDisabled func Override the built-in logic to detect whether an option is disabled
isMulti bool Support multiple selected options
isAutofocussed bool Focus the control when it is mounted. Renamed autoFocus of react-select
noOptionsMessage func Can be used to render a custom value when there are no options (either because of no search results, or all options have been used, or there were none in the first place). Gets called with { inputValue: String }. inputValue will be an empty string when no search text is present.
maxMenuHeight number Maximum height of the menu before scrolling
menuPortalTarget SafeHTMLElement Dom element to portal the select menu to
menuPortalZIndex number z-index value for the menu portal
menuShouldBlockScroll bool whether the menu should block scroll while open
showOptionGroupDivider bool Determines if option groups will be separated by a divider
onBlur func Handle blur events on the control
onChange func Called with a fake event when value changes.
The event's target.name will be the name supplied in props. The event's target.value will hold the value. The value will be the selected option, or an array of options in case isMulti is true.
Signature: (event, action) => void
onFocus func Handle focus events on the control
onInputChange func Handle change events on the input
Signature: (inputValue, action) => void
tabSelectsValue bool Select the currently focused option when the user presses tab
loadOptions func Function that returns a promise, which is the set of options to be used once the promise resolves.
loadingMessage <string, func> The text shown while the options are being loaded
cacheOptions any If cacheOptions is truthy, then the loaded data will be cached. The cache will remain until cacheOptions changes value.
filterOption func Custom method to filter whether an option should be displayed in the menu
optionType enum
Possible values:
'single-property', 'double-property', 'multiple-properties'
The style of the an option in the dropdown menu. It could be single lined option or an option with more and custom info
errors object A map of errors. Error messages for known errors are rendered automatically.
Unknown errors will be forwarded to renderError
errors.missing bool
renderError func Called with custom errors. This function can return a message which will be wrapped in an ErrorMessage. It can also return null to show no error.
Signature: (key, error) => React.node
isRequired bool Indicates if the value is required. Shows an the "required asterisk" if so.
touched custom Indicates whether the field was touched. Errors will only be shown when the field was touched.
title <string, node> Title of the label
hint custom Hint for the label. Provides a supplementary but important information regarding the behaviour of the input (e.g warn about uniqueness of a field, when it can only be set once), whereas description can describe it in more depth. Can also receive a hintIcon.
description <string, node> Provides a description for the title.
onInfoButtonClick func Function called when info button is pressed.
Info button will only be visible when this prop is passed.
Signature: (event) => void
hintIcon node Icon to be displayed beside the hint text.
Will only get rendered when hint is passed as well.
badge node Badge to be displayed beside the label.
Might be used to display additional information about the content of the field (E.g verified email)

data-* props

The component further forwards all data- attributes to the underlying SearchSelectInput component.

The underlying @commercetools-uikit/search-select-input is built on top of @commercetools-uikit/async-select-input which on its own turn is built on top of react-select v3. @commercetools-uikit/async-select-input supports mostly the same properties as react-select with some minor changes in the behaviour of some of the props. The @commercetools-uikit/search-select-input which is built on top @commercetools-uikit/async-select-input has predefined values for some the props. The props that have predefined values in @commercetools-uikit/search-select-input are as follows:

In case you need one of the currently excluded props, feel free to open a PR adding them to either @commercetools-uikit/search-select-input or @commercetools-uikit/async-select-input.

errors

This object is a key-value map. The renderError prop will be called for each entry with the key and the value. The return value will be rendered inside an ErrorMessage component underneath the input.

The SearchSelectField supports some errors out of the box. Return undefined from renderError for these and the default errors will be shown instead. This prevents consumers from having to reimplement the same error messages for known errors while still keeping the flexibility of showing custom error messages for them.

When the key is known, and when the value is truthy, and when renderError returned undefined for that error entry, then the SearchSelectField will render an appropriate error automatically.

Known error keys are:

  • missing: tells the user that this field is required