Skip to content

Commit

Permalink
init project.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 10, 2023
0 parents commit 4770614
Show file tree
Hide file tree
Showing 17 changed files with 702 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
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.
10 changes: 10 additions & 0 deletions SearchField.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions SearchField.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
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 not shown.
8 changes: 8 additions & 0 deletions SearchField/.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
23 changes: 23 additions & 0 deletions SearchField/Package.swift
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"]),
]
)
66 changes: 66 additions & 0 deletions SearchField/Sources/SearchField/SearchField.swift
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)
}

}
12 changes: 12 additions & 0 deletions SearchField/Tests/SearchFieldTests/SearchFieldTests.swift
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
}
}
Loading

0 comments on commit 4770614

Please sign in to comment.