hossein-zare / react-native-dropdown-picker

A single / multiple, categorizable, customizable, localizable and searchable item picker (drop-down) component for react native which supports both Android & iOS.
https://hossein-zare.github.io/react-native-dropdown-picker-website/
MIT License
970 stars 294 forks source link

API Callbacks onChangeSearchText - No data showing even there's a data #684

Open mashwishi opened 11 months ago

mashwishi commented 11 months ago

What I am trying here is to show the output of my search results

this is my reference for the search callback https://hossein-zare.github.io/react-native-dropdown-picker-website/docs/advanced/search#onchangesearchtext

This is my component

import React, { useState } from 'react';
import { View } from 'react-native';
import DropDownPicker from 'react-native-dropdown-picker';
import useContent2Store from '../../../store/useContent2Store';

const SearchBarMenu = ({}) => {

  const { ProductsStatus, fetchProducts } = useContent2Store((state) => state);

  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(null);
  const [items, setItems] = useState([]);

  return (
    <View className="z-50 flex-row items-center rounded-lg px-2 pt-4 pb-2">
      <DropDownPicker
        open={open}
        value={value}
        items={items}
        setOpen={setOpen}
        setValue={setValue}
        categorySelectable={true}
        schema={{
          label: 'name',
          value: 'id',
          parent: 'context',
        }}
        loading={ProductsStatus?.loading}
        searchable={true}
        disableLocalSearch={true}
        searchPlaceholder="Search..."
        onChangeSearchText={(text) => {
          if (text && text?.length > 2) {
            fetchProducts({ searchTerm: text })
              .then((res) => {
                if (res.isSuccessful) {
                  setItems(res.searchResults)
                  console.log(res.searchResults)
                } else {
                  console.log(res.error)
                }
              })
              .catch((err) => {
                console.log(err)
              })
          }
        }}
      />
    </View>
  )
};

export default SearchBarMenu;

This is the sample output from res.searchResults:

[
  {
    "appImageUrl": "https://dummyimage.com/200x200/C1C1C1/C1C1C1.jpg",
    "context": "Category A",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "id": 29,
    "name": "Test 2",
    "price": 0,
    "tag": "Pages"
  },
  {
    "appImageUrl": "https://dummyimage.com/200x200/C1C1C1/C1C1C1.jpg",
    "context": "Category A",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "id": 32,
    "name": "Test 1",
    "price": 0,
    "tag": "Pages"
  },
  {
    "appImageUrl": null,
    "context": "Category B",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ",
    "id": 2330,
    "name": "Test 3",
    "price": 0,
    "tag": "Products"
  }
]

As you can see that I used schema and this is my reference: https://hossein-zare.github.io/react-native-dropdown-picker-website/docs/item-schema#customize-the-schema

Preview: Aug-09-2023 20-02-29

Since I am already here asking, i would like to additionally add this question: