-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4770614
Showing
17 changed files
with
702 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
SearchField | ||
=== | ||
|
||
The search input box component can be placed in a non-specific location and is consistent with the default search input box style. | ||
|
||
## Installation | ||
|
||
You can add MarkdownUI to an Xcode project by adding it as a package dependency. | ||
|
||
1. From the File menu, select Add Packages… | ||
2. Enter https://github.com/jaywcjlove/swiftui-searchfield the Search or Enter Package URL search field | ||
3. Link `Markdown` to your application target | ||
|
||
Or add the following to `Package.swift`: | ||
|
||
```swift | ||
.package(url: "https://github.com/jaywcjlove/swiftui-searchfield", from: "1.0.0") | ||
``` | ||
|
||
Or [add the package in Xcode](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app). | ||
|
||
## Usage | ||
|
||
```swift | ||
import SearchField | ||
|
||
struct ContentView: View { | ||
@State private var searchText = "" | ||
var body: some View { | ||
SearchField(searchText, textFieldChanged: { value in | ||
print("value\(value)") | ||
}) | ||
.padding() | ||
} | ||
} | ||
``` | ||
|
||
## License | ||
|
||
Licensed under the MIT License. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
SearchField.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
Binary file added
BIN
+26.3 KB
SearchField.xcworkspace/xcuserdata/wangchujiang.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SearchField", | ||
products: [ | ||
// Products define the executables and libraries a package produces, making them visible to other packages. | ||
.library( | ||
name: "SearchField", | ||
targets: ["SearchField"]), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
.target( | ||
name: "SearchField"), | ||
.testTarget( | ||
name: "SearchFieldTests", | ||
dependencies: ["SearchField"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// The Swift Programming Language | ||
// https://docs.swift.org/swift-book | ||
|
||
import SwiftUI | ||
|
||
@available(macOS 10.15, *) | ||
public struct SearchField: View { | ||
@State private var text: String = "" | ||
var textFieldChanged: ((String) -> Void) | ||
var value: String | ||
public init(_ defaultValue: String?, textFieldChanged: @escaping ((String) -> Void)) { | ||
self.value = defaultValue ?? "" | ||
self.textFieldChanged = textFieldChanged | ||
} | ||
public var body: some View { | ||
let binding = Binding<String>( | ||
get: { self.text }, | ||
set: { | ||
self.text = $0; | ||
self.textFieldChanged($0) | ||
} | ||
) | ||
return SearchFieldView(search: binding).onAppear { | ||
text = value | ||
} | ||
} | ||
} | ||
|
||
@available(macOS 10.15, *) | ||
private struct SearchFieldView: NSViewRepresentable { | ||
@Binding var search: String | ||
class Coordinator: NSObject, NSSearchFieldDelegate { | ||
var parent: SearchFieldView | ||
init(_ parent: SearchFieldView) { | ||
self.parent = parent | ||
} | ||
func controlTextDidChange(_ notification: Notification) { | ||
// let searchField = notification.object as! NSSearchField | ||
// self.parent.search = searchField.stringValue | ||
|
||
guard let searchField = notification.object as? NSSearchField else { | ||
print("Unexpected control in update notification") | ||
return | ||
} | ||
self.parent.search = searchField.stringValue | ||
} | ||
|
||
} | ||
|
||
func makeNSView(context: NSViewRepresentableContext<SearchFieldView>) -> NSSearchField { | ||
let tf = NSSearchField(frame: .zero) | ||
// tf.focusRingType = .none | ||
tf.drawFocusRingMask() | ||
return tf | ||
} | ||
|
||
func updateNSView(_ searchField: NSSearchField, context: NSViewRepresentableContext<SearchFieldView>) { | ||
searchField.stringValue = search | ||
searchField.delegate = context.coordinator | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
return Coordinator(self) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import XCTest | ||
@testable import SearchField | ||
|
||
final class SearchFieldTests: XCTestCase { | ||
func testExample() throws { | ||
// XCTest Documentation | ||
// https://developer.apple.com/documentation/xctest | ||
|
||
// Defining Test Cases and Test Methods | ||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods | ||
} | ||
} |
Oops, something went wrong.