F
Filament2mo ago
sven

Autocomplete with custom values?

Hey! If I read it correctly, the TextInput still allows custom values, but completes with a predefined array ( https://filamentphp.com/docs/3.x/forms/fields/text-input#autocompleting-text-with-a-datalist ) whereas the selectinput allows compley callbacks for providing the suggestions, but does not allow custom values. Is there something that does both? The TagsInput seems a great fit, but always allow multiple values. I am looking for an autocomplete that accepts a single value, which can also be custom :D
6 Replies
Patrick Boivin
Patrick Boivin2mo ago
You want something that works like TextInput with datalist, but also filters the suggestions as you type. Am I understanding correctly?
sven
sven2mo ago
Exactly Or something that behaves like Select but also allows custom inputs.. I think selects UX is a bit nicer, but both works (:
Select::make($name)
->searchable()
->getSearchResultsUsing(
fn(string $search): array => collect(
self::myFancySearch($search)
)
->concat([$search])
->toArray()
)
Select::make($name)
->searchable()
->getSearchResultsUsing(
fn(string $search): array => collect(
self::myFancySearch($search)
)
->concat([$search])
->toArray()
)
The other 'solution' I found is adding the searchterm to the searchResults ^^'' but that seems super hacky
Patrick Boivin
Patrick Boivin2mo ago
Just an idea: If TagsInput does what you want, could you add a validation rule to ensure that only one tag can be selected? Not perfect, but simple to implement.
sven
sven2mo ago
ah, true! That could also be a way. Then I'd still need to cast that array to a string though. The reason I was asking in the first place was, that I felt I am just blind as this looked like a common requirement, but maybe this is worth writing a plugin
Patrick Boivin
Patrick Boivin2mo ago
I'm thinking that it would be worth adding an option to the TagsInput actually. To be able to restrict to a single item.
sven
sven2mo ago
Yup. That would be a worthwile PR / Feature-Request maybe I guess I will just give it a shot and draft a PR on the weekend