FlurSchnaps-iOS/FlurSchnaps/TextEntryField.swift

60 lines
1.7 KiB
Swift
Raw Normal View History

2022-06-07 11:26:32 +02:00
import SwiftUI
import SFSafeSymbols
struct TextEntryField: View {
let name: String
let placeholder: LocalizedStringKey
let symbol: SFSymbol
let showClearButton: Bool
@Binding
var text: String
init(_ name: String, placeholder: LocalizedStringKey, symbol: SFSymbol, showClearButton: Bool = false, text: Binding<String>) {
self.name = name
self.placeholder = placeholder
self.symbol = symbol
self.showClearButton = showClearButton
self._text = text
}
var body: some View {
TextField(name, text: $text, prompt: Text(placeholder))
.padding(7)
.padding(.horizontal, 25)
.background(Color(.systemGray5))
.cornerRadius(8)
.overlay(
HStack {
Image(systemSymbol: symbol)
.foregroundColor(.gray)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding(.leading, 8)
if showClearButton && text != "" {
Button(action: {
self.text = ""
}) {
Image(systemSymbol: .multiplyCircleFill)
.foregroundColor(.gray)
.padding(.trailing, 8)
}
}
}
)
}
}
struct TextEntryField_Previews: PreviewProvider {
static var previews: some View {
TextEntryField("Test",
placeholder: "Enter text...",
symbol: .textformat,
text: .constant(""))
.previewLayout(.fixed(width: 375, height: 50))
}
}