WIP: Adds database field to delegate airflow to another room, adds select to room form.
Some checks failed
CI / Linux Tests (push) Failing after 6m39s
Some checks failed
CI / Linux Tests (push) Failing after 6m39s
This commit is contained in:
61
Sources/Styleguide/Select.swift
Normal file
61
Sources/Styleguide/Select.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
import Elementary
|
||||
import Foundation
|
||||
|
||||
/// NOTE: This does not have the 'select' class added to it, because it's generally
|
||||
/// added to the label of the field.
|
||||
public struct Select<Label, Element>: HTML where Label: HTML {
|
||||
|
||||
let label: @Sendable (Element) -> Label
|
||||
let value: @Sendable (Element) -> String
|
||||
let selected: @Sendable (Element) -> Bool
|
||||
let items: [Element]
|
||||
let placeholder: String?
|
||||
|
||||
public init(
|
||||
_ items: [Element],
|
||||
placeholder: String? = nil,
|
||||
value: @escaping @Sendable (Element) -> String,
|
||||
selected: @escaping @Sendable (Element) -> Bool = { _ in false },
|
||||
@HTMLBuilder label: @escaping @Sendable (Element) -> Label
|
||||
) {
|
||||
self.label = label
|
||||
self.items = items
|
||||
self.placeholder = placeholder
|
||||
self.selected = selected
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public var body: some HTML<HTMLTag.select> {
|
||||
select {
|
||||
if let placeholder {
|
||||
option(.selected, .disabled) { placeholder }
|
||||
}
|
||||
for item in items {
|
||||
option(.value(value(item))) { label(item) }
|
||||
.attributes(.selected, when: selected(item))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Select: Sendable where Element: Sendable, Label: Sendable {}
|
||||
|
||||
extension Select where Element: Identifiable, Element.ID == UUID, Element: Sendable {
|
||||
|
||||
public init(
|
||||
_ items: [Element],
|
||||
placeholder: String? = nil,
|
||||
selected: @escaping @Sendable (Element) -> Bool = { _ in false },
|
||||
@HTMLBuilder label: @escaping @Sendable (Element) -> Label
|
||||
) {
|
||||
self.init(
|
||||
items,
|
||||
placeholder: placeholder,
|
||||
value: { $0.id.uuidString },
|
||||
selected: selected,
|
||||
label: label
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user