https://github.com/p-x9/editvalueview
Library that makes easy to display property edit screens for SwiftUI.
https://github.com/p-x9/editvalueview
edit swift-package-manager swiftui
Last synced: 3 months ago
JSON representation
Library that makes easy to display property edit screens for SwiftUI.
- Host: GitHub
- URL: https://github.com/p-x9/editvalueview
- Owner: p-x9
- License: mit
- Created: 2022-10-18T17:34:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T16:19:57.000Z (over 1 year ago)
- Last Synced: 2025-03-17T13:17:33.143Z (4 months ago)
- Topics: edit, swift-package-manager, swiftui
- Language: Swift
- Homepage:
- Size: 138 KB
- Stars: 15
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EditValueView
Library that makes easy to display property edit screens for SwiftUI.
## Demo
| String | Bool | Int |
| ---- | ---- | ---- |
|  |  |  || Double | Date | Color |
| ---- | ---- | ---- |
|  |  |  || Image | UI/NSImage |
| ---- | ---- |
|  |  || Array | Dictionary |
| ---- | ---- |
|  |  || Enum(CaseIterable) | Enum(CaseIterable & RawRepresentable) |
| ---- | ---- |
|  |  || Codable |
| ---- |
|  |## Supported types
- String
- Bool
- any Numerics
- Date
- Color/UIColor/NSColor/CGColor/CIColor
- Image/UIImage/CGImage/CIImage (iOS Only)
- Array(Codable)
- Dictionary(Codable)
- CaseIterable
- CaseIterable & RawRepresentable
- Codable## Usage
> **Note**
> If you want to use the camera for editing images, you must add a key named `NSCameraUsageDescription` to the info.plist file.### SwiftUI
#### Initialize
- Initialize with key and initial value
```swift
var name = ""
EditValueView(key: "name", value: name)
.onUpdate { newValue in
name = newValue
}
```
- Initialize with keyPath
```swift
EditValueView(target, key: "name", keyPath: \Item.name)
.onUpdate { newValue in
target[keyPath: \.name] = newValue
}
```
- Initialize with binding
```swift
@State var name: String = ""
EditValueView(key: "name", binding: $name)
```#### Update Handler
You can receive an edit callback when you press the `save` button.
```swift
EditValueView(target, key: "name", keyPath: \Item.name)
.onUpdate { newValue in
// update
}
```#### Input Validation
You can validate input values.
```swift
EditValueView(target, key: "name", keyPath: \Item.name)
.validate { newValue -> Bool in
// input validation
return !name.isEmpty
}
```### UIKit
```swift
let vc = EditValueViewController(target, key: "name", keyPath: \Item.name)
vc.onUpdate = { target, newValue in
// update
}
vc.validate = { target, newValue -> Bool in
// input validation
}
```### Protocol
When using optional types, type hints for Codable cannot be displayed when nil is used.
To avoid such problems, provide a default value in accordance with the protocol named `DefaultRepresentable`.```swift
struct Item: Codable {
var name: String
var date: Date
}struct Message: Codable {
var content: String
var item: Item?
}
```
```swift
// Confirm to `DefaultRepresentable` protocol
extension Item: DefaultRepresentable {
static var defaultValue: Self {
.init(name: "name", date: Date())
}
}
```
```swift
// give default value
EditValueView(target, key: "item", keyPath: \Message.item, defaultValue: .init(name: "name", date: Date()))
```## License
EditValueView is released under the MIT License. See [LICENSE](./LICENSE)