sickdyd / react-search-autocomplete

A search box that filters the provided array of objects
https://sickdyd.github.io/react-search-autocomplete
MIT License
218 stars 82 forks source link

formatResult return more data in the search #44

Closed edinsoncs closed 2 years ago

edinsoncs commented 2 years ago

Hello community, is it possible to return the name + category + date + ..keys in the view?

sickdyd commented 2 years ago

Hello @edinsoncs! Could you please add more details about what you are trying to achieve? What do you mean with "in the view"? The onSearch callback returns the full item selected. If possible create a codesandbox with the code to make it easier to help you out.

PunkSage commented 2 years ago

@sickdyd I spent some time with your code and run into the exactly a same problem.

Let's imagine that you have a list of elements that you want to search through with your autocomplete component.

const items = [ { id: 1, displayName: 'Option A', uuid: '66236926-0b7a-4970-a475-0de4a2b8a9a3' }, { id: 2, displayName: 'Option B', uuid: 'efee70f9-eae6-4acd-8fb9-8ca9a068437c' }, { id: 3, displayName: 'Option C', uuid: '48c4d60c-0dcb-4556-b5b5-c07184b1c661' } ];

Let's assume that we configured Fuse to search by 'displayName' property, but still would like to get the results that will also contain the information about the additional property like uuid for example:

Autocomplete:
[Opt...]

Results:

[Opt A (66236926-0b7a-4970-a475-0de4a2b8a9a3)] [Opt B (efee70f9-eae6-4acd-8fb9-8ca9a068437c)] [Opt C (48c4d60c-0dcb-4556-b5b5-c07184b1c661)]

What @edinsoncs is asking how to use formatResult in order to achieve this result. I don't think he can with the current codebase.

@sickdyd I hope that it clarifies what might be needed. Mention me if you need any additional explanation.

sickdyd commented 2 years ago

@edinsoncs @PunkSage Thank you for your request and explanation. I added this feature, it's now possible to format the results. I added an example on the demo (last one): https://sickdyd.github.io/react-search-autocomplete/