Free type with auto-complete?

Is there an option for the Autocomplete box to allow the user to override with a free-type option?

It would be really useful for offering suggestions (eg. category names that have been used before), but allow new ones to be used as well.

Bumping instead of posting a new thread as I came looking for the answer and apparently I’d already asked last year :grin:

The ability to override a UDAutoComplete by typing your own value would be really useful. :pray:

You should be able to achieve this with the OnLoadOptions event handler. Looking at the docs just now I realize they are bad… but you could just include the $Body in the list of items.

https://docs.powershelluniversal.com/dashboard/components/inputs/automcomplete#autocomplete-with-an-onchange-script-block

New-UDAutocomplete -OnLoadOptions { 
    @('Test', 'Test2', 'Test3', 'Test4', $Body) | Where-Object { $_ -match $Body } | ConvertTo-Json
} -OnChange {
    Show-UDToast $Body 
}
1 Like

Just mocked this up - first try ended with a React error on my main dashboard. Took it out on its own so I wasn’t chasing some other bug accidentally - works like a charm - thank you!

Out of interest - what’s the Where-Object block doing? Seems to work without it - is it saving me from some problem I haven’t thought about?

Ah - cracked the react error. I was adding $body to my options array wrong apparently. The below works if you’ve already got an array called $options your passing to -options.

New-UDAutocomplete -OnLoadOptions { 
    $options + $body | Where-Object {$_ -match $body} | ConvertTo-Json
} -OnChange {
    Show-UDToast $Body
}

Would make a great switch for New-UDAutoComplete though… specify -freetype or something and it adds $body to whatever array has been passed to -options?

Actually, no I haven’t cracked it.

The issue comes in when the options are coming from a text file.

$options = @('one','two','three')

New-UDAutocomplete -OnLoadOptions { 
    $options + $body | Where-Object { $_ -match $Body } | ConvertTo-Json
} -OnChange {
    Show-UDToast $Body   
}

This works fine.

But if I put
One
Two
Three
into a text file then do

$options = Get-Content -path "options.txt"

New-UDAutocomplete -OnLoadOptions { 
    $options + $body | Where-Object { $_ -match $Body } | ConvertTo-Json
} -OnChange {
    Show-UDToast $Body   
}

It gets a React Error if I type something that gets a match. If I don’t hit any matches, it works.

Importing a text file like this works fine with -options - I’m trying to work out what the difference is in the objects I must be sending through. Both just show up as arrays of strings as far as I’ve been able to dig.

I can loop through $options and spit out UDTypography’s with the options, so it’s importing just fine.

I’ve even tried looping through the results of Get-Content to build a fresh new array… no dice.

Aha!

Edit: It wasn’t the Where-Object and now I’ve given it my full collection I realise what it’s there for :laughing:

You need to declare the variable taking the result of Get-Content as a collection of strings. [string[]] - if I don’t do strongly type as string at all, I get the React Error - if I strongly type as a single [string] I end up with my whole text file as a single option.

Then the React errors at least disappear.

Second Edit: Removed a red herring. Thought there was something going on when I combined the arrays on the same line - nope,that was fine.

This works:

[string[]]$options = Get-Content -path "options.txt"
New-UDAutocomplete -OnLoadOptions {
    $options  + $body | Where-Object {$_ -match $Body} | ConvertTo-Json
} -OnChange {
    Show-UDToast $Body 
}
1 Like