| 12 | type FilterFunction<T extends OptionLike> = (search: string, opt: T) => boolean; |
| 13 | |
| 14 | function getCursorForValue<T extends OptionLike>( |
| 15 | selected: T['value'] | undefined, |
| 16 | items: T[] |
| 17 | ): number { |
| 18 | if (selected === undefined) { |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | const currLength = items.length; |
| 23 | |
| 24 | // If filtering changed the available options, update cursor |
| 25 | if (currLength === 0) { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | // Try to maintain the same selected item |
| 30 | const index = items.findIndex((item) => item.value === selected); |
| 31 | return index !== -1 ? index : 0; |
| 32 | } |
| 33 | |
| 34 | function defaultFilter<T extends OptionLike>(input: string, option: T): boolean { |
| 35 | const label = option.label ?? String(option.value); |