简体   繁体   中英

mui autocomplete - how do i sort selected options on top?

I'm using mui's useAutocomplete hook

https://mui.com/material-ui/react-autocomplete/#useautocomplete

Is it possible to sort options and put selected on top with actual js sorting not flex order or any css workaround?

I have tried using

filterOptions = (options, { inputValue })

however current selection state is notpassed here

friend: I had the same problem and this is how I solved it:

 const arrOfChechedItems = [Your checked items by onChange or what do you have];
    const sortItems = (id: string) => {
    const a = arrOfChechedItems?.find((item) => item === id);
    if (a) return -1;
    if (!a) return 1;
    return 0;
  };

Before you map the list: arr**.sort((a) => sortItems(a.id)).**map

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM