Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PBIOS-705] Add typeahead deselected listener #522

Merged
merged 5 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Sources/Playbook/Components/Typeahead/GridInputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct GridInputField: View {
@State private var isHovering: Bool = false
@State private var clearButtonIsHovering: Bool = false
@State private var indicatorIsHovering: Bool = false
@State private var textFrame: CGRect = .zero

init(
placeholder: String = "Select",
Expand Down Expand Up @@ -61,20 +62,20 @@ public struct GridInputField: View {
}
.padding(.horizontal, Spacing.small)
.padding(.vertical, Spacing.xSmall)
.background(Color.white.opacity(0.01))
.onTapGesture {
isFocused.wrappedValue = true
if isFocused.wrappedValue {
DispatchQueue.main.async {
onViewTap?()
}
}
}
.frameReader { textFrame = $0 }
dismissIconView
indicatorView
}
.focused(isFocused)
.background(backgroundColor)
.onTapGesture {
isFocused.wrappedValue = true
if isFocused.wrappedValue {
DispatchQueue.main.async {
onViewTap?()
}
}
}
.overlay {
shape.stroke(borderColor, lineWidth: 1.0)
}
Expand Down Expand Up @@ -108,25 +109,24 @@ private extension GridInputField {
.pbFont(.body, color: textColor)
.frame(height: 24)
}
.fixedSize()
.fixedSize(horizontal: true, vertical: false)
.frame(minWidth: 60, alignment: .leading)
.overlay {
Color.white
.opacity(isFocused.wrappedValue ? 0.001 : 0)
.onTapGesture {
if isFocused.wrappedValue {
onViewTap?()
}
}
}
.clipped()
}

var systemTextField: some View {
#if os(iOS)
KeyboardTextField(text: $searchText, onDelete: { onDelete?() })
KeyboardTextField(text: $searchText, maxWidth: maxWidth, onDelete: { onDelete?() })
#elseif os(macOS)
TextField("", text: $searchText).frame(maxWidth: maxWidth)
#endif
}

@MainActor
var maxWidth: CGFloat {
#if os(iOS)
return 250
#elseif os(macOS)
TextField("", text: $searchText)
return textFrame.width
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,33 @@ import UIKit
import SwiftUI

class CustomTextField: UITextField {
var maxWidth: CGFloat?
var onDelete: (() -> Void)?

override func deleteBackward() {
onDelete?()
super.deleteBackward()
}

override var intrinsicContentSize: CGSize {
let original = super.intrinsicContentSize
let cappedWidth = min(original.width, maxWidth ?? original.width)
return CGSize(width: cappedWidth, height: original.height)
}
}

struct KeyboardTextField: UIViewRepresentable {
@Binding var text: String
var maxWidth: CGFloat
var onDelete: () -> Void

func makeUIView(context: Context) -> CustomTextField {
let textField = CustomTextField(frame: .zero)
textField.delegate = context.coordinator
textField.onDelete = onDelete
textField.maxWidth = maxWidth
textField.borderStyle = .none
textField.font = .init(name: PBFont.proximaNovaLight, size: PBFont.body.size)
return textField
}

Expand All @@ -48,7 +58,9 @@ struct KeyboardTextField: UIViewRepresentable {
}

func textFieldDidChangeSelection(_ textField: UITextField) {
parent.text = textField.text ?? ""
DispatchQueue.main.async { [weak self] in
self?.parent.text = textField.text ?? ""
}
}
}
}
Expand Down