Stephanevg / PSHTML

Cross platform Powershell module to generate HTML markup language
https://pshtml.readthedocs.io/en/latest/
Other
168 stars 43 forks source link

Create Abastraction for DropDown Lists #201

Closed Stephanevg closed 5 years ago

Stephanevg commented 5 years ago

PSHTML code

SelectTag {
            option -value "Citroen" -Content "Citroen"
            option -value "Renault" -Content "Renault"
            option -value "Peugeot" -Content "Peugeot"
            option -value "DS" -Content "DS"
        }
Stephanevg commented 5 years ago

Ideally, I would see something around the lines:

New-PSHTMLDropDownList -Items$

The $Items elements could be created indenpendently as followed


New-PSHTMLDropDownListItem -Value "Green" -Content "Green"

Scaling it out


$AllItems = @()

Foreach($FavoriteColor in $Colors) {
   $AllItems += New-PSHTMLDropDownListItem -Value $FavoriteColor -Content $FavoriteColor
}

It will be combined as followed:

    New-PSHTMLDropDownList -Items $AllItems
LxLeChat commented 5 years ago

Made this two small functions ... tell me if this suits you, or if you were looking at something different ! Edit: Added Pipeline support for New-PSHTMLDropDownList. If you pass an array of string it'll automatically transform it to dropdown items. You can also directly pipe an array of already well formated drop down elements.

function New-PSHTMLDropDownList {
    <#
    .SYNOPSIS
        Short description
    .DESCRIPTION
        Long description
    .EXAMPLE
        PS C:\> $items = 'apples','oranges','tomatoes','blueberries'
        PS C:\> $items | New-PSHTMLDropDownList
        Create new simple dropdownlist, array based, from the pipeline. Also work with an array of drop down options
    .EXAMPLE
        PS C:\> $items = 'apples','oranges','tomatoes','blueberries'
        PS C:\> New-PSHTMLDropDownList -Items $Items
        Create new simple dropdownlist, array based
    .EXAMPLE
        PS C:\> $ArrayOfDropDownOptions = @()
        PS C:\> $items = 'apples','oranges','tomatoes','blueberries'
        PS C:\> Foreach ( $item in $items ) { $ArrayOfDropDownOptions += New-PSHTMLDropDownListItem -value $item -content $item }
        PS C:\> New-PSHTMLDropDownList -Items $ArrayOfDropDownOptions
        Create new simple dropdownlist, array based
    .INPUTS
        Inputs (if any)
    .OUTPUTS
        Output (if any)
    .NOTES
        General notes
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $False,ValueFromPipeline=$True)]
        [AllowNull()]
        [Array]
        $items,,
        [AllowEmptyString()]
        [AllowNull()]
        [String]$Class,
        [String]$Id,
        [Hashtable]$Attributes
    )

    begin {
        $Option = @()
    }

    process {

        If ( $null -ne $items ) {

            ## Assuming its coming from New-PSHTMLDropDownListItem 
            If ( $items[0] -match '^<option') {
                $Option += $items
            } Else {
                Foreach ( $item in $items ) {
                    $Option += New-PSHTMLDropDownListItem -value $item -Content $item
                }
            }
        }

    }

    end {
        selecttag -Content {
            $option
        } -Class $Class -Id $Id -Attributes $Attributes
    }
}

function New-PSHTMLDropDownListItem {
    <#
    .SYNOPSIS
        Short description
    .DESCRIPTION
        Long description
    .EXAMPLE
        PS C:\> New-PSHTMLDropDownListItem -value 'aaaa' -content 'aaaaaaa'
        Create a new dropdown option
    .INPUTS
        Inputs (if any)
    .OUTPUTS
        Output (if any)
    .NOTES
        General notes
    #>
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        [AllowEmptyString()]
        [AllowNull()]
        $Content,
        [string]$value,
        [string]$label,
        [Switch]$Disabled,
        [Switch]$Selected,
        [AllowEmptyString()]
        [AllowNull()]
        [String]$Class = "",
        [String]$Id,
        [AllowEmptyString()]
        [AllowNull()]
        [String]$Style,
        [String]$title,
        [Hashtable]$Attributes

    )

    begin {
    }

    process {
        option @PSBoundParameters
    }

    end {
    }
}
Stephanevg commented 5 years ago

Awesome @LxLeChat ! thank you so much for your PR man!!