Add support for text fields
Some checks failed
Deploy Docs / publish (push) Successful in 34m23s
SwiftLint / SwiftLint (push) Failing after 5s

This commit is contained in:
david-swift 2025-01-31 17:31:54 +01:00
parent 319797a68d
commit 27c30dfd79
2 changed files with 46 additions and 0 deletions

View File

@ -20,6 +20,8 @@ public struct Alert: SwiftUIWidget {
var actions: [Action] = []
/// The wrapped view.
var child: Meta.AnyView
/// An additional view in the alert, for example a text field.
var textField: (String, Meta.Binding<String>)?
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
@ -101,6 +103,9 @@ public struct Alert: SwiftUIWidget {
public static func view(properties: Self) -> some SwiftUI.View {
MacBackendView(.mainContent)
.alert(properties.title, isPresented: properties.isPresented.swiftUI) {
if let field = properties.textField {
SwiftUI.TextField(field.0, text: field.1.swiftUI)
}
SwiftUI.ForEach(Array(properties.actions.enumerated()), id: \.offset) { action in
action.element.button
}
@ -125,6 +130,13 @@ public struct Alert: SwiftUIWidget {
}
}
public func textField(
_ label: String,
text: Meta.Binding<String>
) -> Self {
modify { $0.textField = (label, text) }
}
/// Add a cancel button to the alert.
/// - Parameters:
/// - label: The button's label.

View File

@ -0,0 +1,34 @@
//
// TextField.swift
// MacBackend
//
// Created by david-swift on 31.01.2025.
//
import SwiftUI
/// A text field.
public struct TextField: SwiftUIWidget {
/// The text.
@Meta.Binding var text: String
/// The field's label.
var label: String
/// Initialize the text field.
/// - Parameters:
/// - label: The picker's label.
/// - text: The current text.
public init(_ label: String, text: Meta.Binding<String>) {
self.label = label
self._text = text
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
SwiftUI.TextField(properties.label, text: properties._text.swiftUI)
}
}