List

Maintainer:
korvin89
GitHub
filterable
sortable
filterPlaceholder
emptyPlaceholder
import {List} from '@gravity-ui/uikit';

ItemsHeight

Determines the item list height (or a function that returns the height value for a list). It can be helpful when setting the list height dynamically, e.g., (items: []) => number.

Items

Provides an array of items for a list:

<List items={["one", "two", "three", "four", "five", "six", "seven"]} itemsHeight={160} />

An item can be a scalar or an arbitrary value and must be truthy in any case. If it is an arbitrary value, make sure to specify the filtering and rendering functions. The default render only provides an item as text.

The special item.disabled field disables an item.

The render and height customization provides plenty of room for experimenting. For example, the code below allows you to emulate groups:

<List items={[{title: 'one', group: true,disabled: true}, {title: 'two'},
    {
      title: 'three',
      group: true,
      disabled: true,
    },
    {
      title: 'four',
    },
  ]} onItemClick={(value) => console.log(value)}
  renderItem={(item) => {
    if (item.group) {
      return (
        <div className={'group'}>
          <div className={'select-text'}>{item.title}</div>
        </div>
      );
    }
    return (
      <div className={'select'}>
        <div className={'select-text'}>{item.title}</div>
      </div>
    );
  }}
  itemHeight={(item) => (item.group ? 24 : 36)}
  itemsHeight={160}
  filterItem={(filter) => (item) => item.title.includes(filter)}
/>

Filterable

The filterable property disables the input to search for an item if its value is false. Its default value is true.

<List items={["one", "two", "three", "four", "five", "six", "seven"]} itemsHeight={160} filterable={false} />

Sortable

The sortable property enables swapping list items if its value is true. Its default value is false.

<List items={["one", "two", "three", "four", "five", "six", "seven"]} itemsHeight={160} sortable={true} />

Virtualization

To enable virtualization, make sure one of these two conditions is met:

  1. You set the itemsHeight property. In this case, the list height will be fixed and equal to that value.
  2. You set the display: flex style for the list parent container. In this case, the list will adapt to the container width.

External management

Sometimes, you may want to manage the activity of items from the keyboard by maintaining the focus on an external item. The onKeyDown event forwarding to a list may help you here. Likewise, you can forward onFocus and onBlur if you need to repeat the behavior when an active item is lost.

Filter

The filter property provides the filter value used with external sorting.

PropTypes

NameDescriptionTypeDefault
itemsList of itemsArray[]
itemHeightItem height in px or a function that returns the height value for an item: (item: any) => number.Number/Function28
itemsHeightItem list height or a function that returns the height value for a list. It can be helpful when setting the list height dynamically: (items: []) => number.Number/Function
renderItemRender function with an item received as an input and a React node returned: (item: any, isItemActive: bool, itemIndex: number) => React.ReactNode.Function
filterItemFiltering function that receives a specified string as a search or filter input and returns a function that receives an item as an input and outputs boolean: (filter: string) => (item: any) => boolean.Function
filterableFlag that enables a filter field.Booleantrue
filterPlaceholderPlaceholder for a filter field.String
filterFilter value (in case external sorting is used).String
filterClassNameClass for filter input styles.String
onChangeFilterFilter change handler (in case external sorting is used): (filter: string) => void.Function
onFilterEndFunction invoked after internal filtering is complete: ({items}: {items: T[]}) => voidFunction
emptyPlaceholderPlaceholder for an empty list.React.ReactNode
sortableFlag that enables list sorting.Boolean
sortHandleAlignSorting indicator alignment (left or right).left right
onSortEndSorting event handler: ({oldIndex: number, newIndex: number}) => void.Function
virtualizedFlag that enables virtualization. If inactive, all items are rendered at once.Booleantrue
onItemClickItem click handler: (item: any, index: number, fromKeyboard?: bool) => void.Function
deactivateOnLeaveIf this flag is set, the item selection is deactivated once the cursor leaves the item or the list loses its focus. If not set, the last selected item will always be selected.Booleantrue
activeItemIndexIf a value is set, an item with this index is rendered as active.Number
selectedItemIndexIf a value is set, an item with this index is rendered as selected (the background color is taken from --g-color-base-selection).Number/Array
itemClassNameCustom class name to add to an item container.String
itemsClassNameCustom class name to add to an item list.String
rolerole HTML attributeStringlist
idid HTML attributestring
onChangeActiveFires when the index of an option in the listbox that is visually highlighted as having keyboard focus is changed: (index?: number) => void.Function