Scripts/New-UDSelector.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<#
.SYNOPSIS
    Sample control for UniversalDashboard.
.DESCRIPTION
    Sample control function for UniversalDashboard. This function must have an ID and return a hash table.
.PARAMETER Id
    An id for the component default value will be generated by new-guid.
.EXAMPLE
    PS C:\> <example usage>
    Explanation of what the example does
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    General notes
#>

function New-UDSelector {
    param(
        [Parameter()]
        [string]$Id = (New-Guid).ToString(),
        [Parameter()]
        [scriptblock]$Options,
        [Parameter()]
        [boolean]$CloseOnSelect = $true,
        [Parameter()]
        [scriptblock]$Selected,
        [Parameter()]
        [string]$PlaceHolder = "Select Items..."

    )

    End {

        @{
            # The AssetID of the main JS File
            assetId           = $AssetId
            # Tell UD this is a plugin
            isPlugin          = $true
            # This ID must be the same as the one used in the JavaScript to register the control with UD
            type              = "UD-Selector"
            # An ID is mandatory
            id                = $Id

            # This is where you can put any other properties. They are passed to the React control's props
            # The keys are case-sensitive in JS.
            options           = [array]$Options.invoke()
            closeMenuOnSelect = $CloseOnSelect
            selectedOption = [array]$Selected.invoke()
            placeholder       = $PlaceHolder
        }

    }
}

function New-UDSelectorItem {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$Value,
        [Parameter()]
        [string]$Label,
        [Parameter()]
        [string]$HexColor,
        [Parameter(
            ParameterSetName = "Disabled"
        )]
        [switch]$isDisabled
    )
    Begin {
        $out = @{ };
    }

    Process {
        if ($null -eq $Label) {
            $Label = $Value
        }

        $out.label = $Label
        $out.value = $Value

        if ($HexColor) {
            $out.color = $HexColor
        }

        if ($isDisabled) {
            $out.isDisabled = $true
        }
    }

    End {
        return $out
    }
}