Skip to content

Commit

Permalink
doc: update document.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 10, 2023
1 parent 4770614 commit 157b949
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ SearchField

The search input box component can be placed in a non-specific location and is consistent with the default search input box style.

![SearchField](https://github.com/jaywcjlove/swiftui-searchfield/assets/1680273/43a01ac1-d5bc-42a8-a13b-716aa9eaaf20)

## Installation

You can add MarkdownUI to an Xcode project by adding it as a package dependency.
Expand All @@ -29,8 +31,9 @@ struct ContentView: View {
var body: some View {
SearchField(searchText, textFieldChanged: { value in
print("value\(value)")
searchText = value
})
.padding()
Text(searchText)
}
}
```
Expand Down
Binary file not shown.
35 changes: 26 additions & 9 deletions SearchField/Sources/SearchField/SearchField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,47 @@ import SwiftUI

@available(macOS 10.15, *)
public struct SearchField: View {
@State private var text: String = ""
// A binding to a string value
@Binding private var text: String?
// A closure that takes a string as input and returns void
var textFieldChanged: ((String) -> Void)
var value: String

// Initializes a SearchField with a default value and a closure to be called when the text field changes
public init(_ defaultValue: String?, textFieldChanged: @escaping ((String) -> Void)) {
self.value = defaultValue ?? ""
_text = .constant(defaultValue ?? "")
self.textFieldChanged = textFieldChanged
}
// Initializes a SearchField with a binding to a string value and a closure to be called when the text field changes
public init(_ defaultValue: Binding<String?>, textFieldChanged: @escaping ((String) -> Void)) {
_text = defaultValue
self.textFieldChanged = textFieldChanged
}
// Defines the body of the SearchField view
public var body: some View {
let binding = Binding<String>(
get: { self.text },
get: { self.text ?? "" }, // Retrieves the value of the binding
set: {
// Sets the value of the binding to the new value
self.text = $0;
// Calls the closure with the new value
self.textFieldChanged($0)
}
)
return SearchFieldView(search: binding).onAppear {
text = value
}
// Returns a SearchFieldView with the created binding
return SearchFieldView(search: binding)
}
}

@available(macOS 10.15, *)
private struct SearchFieldView: NSViewRepresentable {
// A binding to a string value
@Binding var search: String
class Coordinator: NSObject, NSSearchFieldDelegate {
var parent: SearchFieldView
init(_ parent: SearchFieldView) {
self.parent = parent
}
// Called when the text in the search field changes
func controlTextDidChange(_ notification: Notification) {
// let searchField = notification.object as! NSSearchField
// self.parent.search = searchField.stringValue
Expand All @@ -42,23 +54,28 @@ private struct SearchFieldView: NSViewRepresentable {
print("Unexpected control in update notification")
return
}
// Sets the binding value to the new search field value
self.parent.search = searchField.stringValue
}

}

// Creates a new NSView instance
func makeNSView(context: NSViewRepresentableContext<SearchFieldView>) -> NSSearchField {
// Creates a new NSSearchField
let tf = NSSearchField(frame: .zero)
// tf.focusRingType = .none
tf.drawFocusRingMask()
return tf
}

func updateNSView(_ searchField: NSSearchField, context: NSViewRepresentableContext<SearchFieldView>) {
// Updates the search field's value with the binding value
searchField.stringValue = search
// Sets the coordinator as the search field's delegate
searchField.delegate = context.coordinator
}

// 创建一个协调器实例以与 NSView 协调
// Creates a coordinator instance to coordinate with the NSView
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
Expand Down
3 changes: 3 additions & 0 deletions SearchFieldExample/SearchFieldExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SearchField

struct ContentView: View {
@State private var searchText = ""

var body: some View {
VStack {
Image(systemName: "globe")
Expand All @@ -18,7 +19,9 @@ struct ContentView: View {
Text("Hello, Search Field!")
SearchField(searchText, textFieldChanged: { value in
print("value\(value)")
searchText = value
})
Text(searchText)
}
.padding()
}
Expand Down

0 comments on commit 157b949

Please sign in to comment.