104 lines
1.8 KiB
Go
104 lines
1.8 KiB
Go
package ui
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/a-h/templ"
|
|
)
|
|
|
|
func selectPlaceholder(props SelectProps) string {
|
|
if props.Placeholder != "" {
|
|
return props.Placeholder
|
|
}
|
|
if props.Multiple {
|
|
return "Select values"
|
|
}
|
|
return "Select an option"
|
|
}
|
|
|
|
func selectNativeID(id string, name string) string {
|
|
baseID := inputID(id, name)
|
|
if baseID == "" {
|
|
return "ui-select-native"
|
|
}
|
|
return baseID + "-native"
|
|
}
|
|
|
|
func selectMenuID(id string, name string) string {
|
|
baseID := inputID(id, name)
|
|
if baseID == "" {
|
|
return "ui-select-menu"
|
|
}
|
|
return baseID + "-menu"
|
|
}
|
|
|
|
func selectBoolData(value bool) string {
|
|
if value {
|
|
return "true"
|
|
}
|
|
return "false"
|
|
}
|
|
|
|
func selectSelectedValues(props SelectProps) []string {
|
|
if props.Multiple {
|
|
return props.Values
|
|
}
|
|
if props.Value == "" {
|
|
return nil
|
|
}
|
|
return []string{props.Value}
|
|
}
|
|
|
|
func selectOptionSelected(props SelectProps, value string) bool {
|
|
for _, selected := range selectSelectedValues(props) {
|
|
if selected == value {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func selectSelectedLabels(props SelectProps) []string {
|
|
var labels []string
|
|
for _, option := range props.Options {
|
|
if selectOptionSelected(props, option.Value) {
|
|
labels = append(labels, option.Label)
|
|
}
|
|
}
|
|
return labels
|
|
}
|
|
|
|
func selectSelectedLabel(props SelectProps) string {
|
|
return strings.Join(selectSelectedLabels(props), ", ")
|
|
}
|
|
|
|
func selectMenuOptionClass(selected bool, disabled bool) string {
|
|
className := "ui-select-option"
|
|
if selected {
|
|
className += " is-selected"
|
|
}
|
|
if disabled {
|
|
className += " is-disabled"
|
|
}
|
|
return className
|
|
}
|
|
|
|
func selectIsDisabled(attrs templ.Attributes) bool {
|
|
if attrs == nil {
|
|
return false
|
|
}
|
|
|
|
value, ok := attrs["disabled"]
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
switch typed := value.(type) {
|
|
case bool:
|
|
return typed
|
|
case string:
|
|
return typed != "" && typed != "false"
|
|
default:
|
|
return true
|
|
}
|
|
}
|