Capgemini / dcx-react-library

React Library UI/UX agnostic
https://main--6069a6f47f4b9f002171f8e1.chromatic.com
MIT License
113 stars 7 forks source link

Provide a prop for sorting results within the Autocomplete #422

Closed Ibabalola closed 1 year ago

Ibabalola commented 1 year ago

Within the autocomplete compenent, I would like to control the way that results are ordered by providing the autocomplete component with an optional search function.

Currently results are ordered in the same order that they are given within the options prop, however I would like to have the results ordered by specified ranking function that is passed to the autocomplete as an optional prop.

// we need a way to sort the array of strings within filtered, 
// based on a provided search function passed in as a prop 
const filtered = options.filter((optionsName) =>
 optionsName.toLowerCase().includes(value.toLowerCase())
);

for example:

  quickSearch = (value: string) => {
    const queryStr = value.toLowerCase();
    options.toLowerCase().includes(queryStr);

    const filtered: string[] = options
      .filter((optionsName: string) => optionsName.toLowerCase().includes(value.toLowerCase()))
      .sort((a: string, b: string) => {
        // code that will sort the valid results
    });

    return filtered;
  };

  <Autocomplete 
    .. // usualy props
    search={this.quickSearch}
  />

If a search function is not provided the autocomplete should display results as it is currently doing.

daniele-zurico commented 1 year ago

Closed by #423