List

Lists present data in one or more of rows.

Pro component

Requires all-access to use the source code

Apple
Apple
import { ListRenderItemInfo } from '@shopify/flash-list';
import { Stack } from 'expo-router';
import { View } from 'react-native';

import { Text } from '~/components/nativewindui/Text';
import { ESTIMATED_ITEM_HEIGHT, List, ListDataItem, ListItem, ListSectionHeader } from '../List';

export default function ListScreen() {
  return (
    <>
      <Stack.Screen options={{ title: 'Insets' }} />
      <List
        variant="insets"
        data={DATA}
        estimatedItemSize={ESTIMATED_ITEM_HEIGHT.titleOnly}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      />
    </>
  );
}

function renderItem<T extends ListDataItem>(info: ListRenderItemInfo<T>) {
  if (typeof info.item === 'string') {
    return <ListSectionHeader {...info} />;
  }
  return (
    <ListItem
      leftView={
        <View className="flex-1 justify-center px-4">
          <View className="aspect-square h-8 rounded bg-red-500" />
        </View>
      }
      rightView={
        <View className="flex-1 justify-center px-4">
          <Text variant="caption1" className="ios:px-0 text-muted-foreground px-2">
            100+
          </Text>
        </View>
      }
      {...info}
      onPress={() => console.log('onPress')}
    />
  );
}

function keyExtractor(item: (Omit<ListDataItem, string> & { id: string }) | string) {
  return typeof item === 'string' ? item : item.id;
}

const DATA = [
  'Header',
  {
    id: '1',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '2',
    title: 'Hello',
    subTitle: 'World',
  },

  {
    id: '3',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '4',
    title: 'Hello',
    subTitle: 'World',
  },
  'Header 2',
  {
    id: '5',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '6',
    title: 'Hello',
    subTitle: 'World',
  },

  {
    id: '7',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '8',
    title: 'Hello',
    subTitle: 'World',
  },
  'Header 3',
  {
    id: '9',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '10',
    title: 'Hello',
    subTitle: 'World',
  },

  {
    id: '11',
    title: 'Hello',
    subTitle: 'World',
  },
  {
    id: '12',
    title: 'Hello',
    subTitle: 'World',
  },
];
Logo

Unlock All Pro Screens & Components

Elevate your app with powerful, native-feeling components and templates. Build faster with beautiful ready-to-use designs.

Get all-access

Usage

import {
  ESTIMATED_ITEM_HEIGHT,
  List,
  ListItem,
} from '~/components/nativewindui/List';
<List
  data={[
    {
      id: '1',
      title: 'Hello',
      subTitle: 'World',
    },
    {
      id: '2',
      title: 'Hello',
      subTitle: 'World',
    },

    {
      id: '3',
      title: 'Hello',
      subTitle: 'World',
    },
  ]}
  estimatedItemSize={ESTIMATED_ITEM_HEIGHT.withSubTitle}
  renderItem={(info) => {
    return <ListItem {...info} />;
  }}
  keyExtractor={(item) => item.id}
/>

Props

List

Inherits all the props from @shopify's FlashList component.

PropTypeDefaultDescription
renderItemListRenderItem<T>Custom render function for list items.
variantListVariant'full-width'The layout variant for the list.
sectionHeaderAsGapbooleanfalseWhether the section header is used as a gap.
rootClassNamestringClass name for the root element.
rootStyleStyleProp<ViewStyle>Style for the root element.
contentInsetAdjustmentBehavior'automatic' | 'scrollableAxes' | 'never' | 'always''automatic'Behavior for adjusting content insets.
dataT[]Data source for the list.
refListRef<T>Reference to the FlashList instance.
contentContainerClassNamestringClass name for the content container.

ListItem

Inherits all the props from React Native's Pressable component.

PropTypeDefaultDescription
itemTThe data item for this list item.
isFirstInSectionbooleanIndicates if this is the first item in a section.
isLastInSectionbooleanIndicates if this is the last item in a section.
indexnumberThe index of the item in the list.
variantListVariantThe variant for the list item.
classNamestringAdditional class names for the list item.
androidRootClassNamestringClass name for the root element on Android.
titleClassNamestringClass name for the title text.
titleStyleStyleProp<TextStyle>Style for the title text.
textNumberOfLinesnumberNumber of lines for the title text.
subTitleStyleStyleProp<TextStyle>Style for the subtitle text.
subTitleClassNamestringClass name for the subtitle text.
subTitleNumberOfLinesnumberNumber of lines for the subtitle text.
textContentClassNamestringClass name for the content text container.
sectionHeaderAsGapbooleanWhether the section header is used as a gap.
removeSeparatorbooleanfalseWhether to remove the separator below the item.
leftViewReact.ReactNodeCustom view rendered on the left side of the item.
rightViewReact.ReactNodeCustom view rendered on the right side of the item.
disabledbooleanfalseWhether the item is disabled.
refListItemRefReference to the list item component.

Constant

ESTIMATED_ITEM_HEIGHT: The estimated height of each list item with title only and with subtle.

© Ronin Technologies LLC 2024