Checkbox to show autocomplete element

Product: PowerShell Universal
Version: 5.0.7

I’m sure this is a fairly simple thing, but I can’t find an example in the docs or the forums that seems applicable enough.

I just want to show a new Autocomplete element when a check box is changed.

I can have it display the toast message no problem (just for proving the if statement is working correctly), but getting a new element to show is difficult (at least for this PSU novice). It might be a limitation, and if it is… does anybody have any clever workarounds?

This app is for running a user termination, by default it should set the email forward to their manager, but occasionally they want it to go to someone else. I’d like to present the option to the user via the checkbox and the resulting autocomplete element so they can choose a different recipient.

New-UDCheckbox -Id 'chkFwdToMgr' -Label 'Forward Email to Manager?' -OnChange {
    $chkFwdElmnt = Get-UDElement -id 'chkFwdToMgr'
    if ($chkFwdElmnt.checked) {
        New-UDAutocomplete -id "acFwdTo" -Label 'Forward to who?' -OnLoadOptions {
            get-aduser -filter "Name -like '*$body*' -and Enabled -eq 'True'" | Select-Object -ExpandProperty Name | convertto-json
        }
        Show-UDToast "$chkFwdElmnt"
    }
}

You can use a dynamic region to do this.

    New-UDCheckbox -Id 'chkFwdToMgr' -Label 'Forward Email to Manager?' -OnChange {
        Sync-UDElement -Id 'autoComplete'
    }
    New-UDDynamic -Id 'autoComplete' -Content {
        $chkFwdElmnt = Get-UDElement -id 'chkFwdToMgr'
        if ($chkFwdElmnt.checked) {
            New-UDAutocomplete -id "acFwdTo" -Label 'Forward to who?' -OnLoadOptions {
                @("1", "2,", "#")
            }
            Show-UDToast "$chkFwdElmnt"
        }
    }

Thanks! I’ve marked it as the solution. Can confirm that works.

Out of curiosity as I explore things, is there a way to do the inverse? Like having the checkbox checked when the page is loaded, and show the new autocomplete field when the box is unchecked? if not, no worries.

I tried by adding the -Checked $true parameter to the checkbox element and change the if statement to $chkFwdElmnt.unchecked, and it didn’t work.

Try using if (!($chkFwdElmnt.checked)) to invert the conditional (the more “PowerShell way” to do it is If (-not $chkFwdElmnt.checked) but it’s personal taste; they evaluate the same).