-
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
1 parent
f468c0f
commit 596ae17
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
Sources/ValidatorUI/Classes/Extensions/View+EraseToAnyView.swift
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 @@ | ||
// | ||
// Validator | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
func eraseToAnyView() -> AnyView { | ||
AnyView(self) | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
Sources/ValidatorUI/Classes/SUI/ViewModifiers/ValidationViewModifier.swift
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,94 @@ | ||
// | ||
// Validator | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import ValidatorCore | ||
|
||
/// A validation view modifier. | ||
/// | ||
/// The validation view modifier automatically tracks validation errors, | ||
/// uses the content view builder to construct an error view, and displays | ||
/// it to the user. | ||
/// | ||
/// ``` | ||
/// struct ContentView: View { | ||
/// @State private var text: String = "Text" | ||
/// | ||
/// var body: some View { | ||
/// VStack { | ||
/// TextField("Title", text: $text) | ||
/// .modifier( | ||
/// ValidationViewModifier( | ||
/// item: $text, | ||
/// rules: [ | ||
/// LengthValidationRule(max: 10, error: "The error message"), | ||
/// ], | ||
/// content: { errors in | ||
/// Text(errors.map { $0.message }.joined(separator: " ")) | ||
/// } | ||
/// ) | ||
/// ) | ||
/// Spacer() | ||
/// } | ||
/// .padding() | ||
/// } | ||
/// } | ||
/// ``` | ||
public struct ValidationViewModifier<T, ErrorView: View>: ViewModifier { | ||
// MARK: Properties | ||
|
||
/// The result of the validation. | ||
@State private var validationResult: ValidationResult = .valid | ||
|
||
/// The binding item to validate. | ||
@Binding private var item: T | ||
|
||
/// A custom parameter attribute that constructs views from closures. | ||
@ViewBuilder private let content: ([any IValidationError]) -> ErrorView | ||
|
||
/// The array of validation rules to apply to the item's value. | ||
public let rules: [any IValidationRule<T>] | ||
|
||
/// Creates a new instance of the `ValidationViewModifier`. | ||
/// | ||
/// - Parameters: | ||
/// - item: The binding item to validate. | ||
/// - rules: The array of validation rules to apply to the item's value. | ||
/// - content: A custom parameter attribute that constructs an error view from closures. | ||
public init( | ||
item: Binding<T>, | ||
rules: [any IValidationRule<T>], | ||
@ViewBuilder content: @escaping ([any IValidationError]) -> ErrorView | ||
) { | ||
_item = item | ||
self.rules = rules | ||
self.content = content | ||
} | ||
|
||
// MARK: ViewModifier | ||
|
||
public func body(content: Content) -> some View { | ||
VStack(alignment: .leading) { | ||
content | ||
.validation($item, rules: rules) { result in | ||
DispatchQueue.main.async { | ||
self.validationResult = result | ||
} | ||
} | ||
validationMessageView | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private var validationMessageView: some View { | ||
switch validationResult { | ||
case .valid: | ||
return EmptyView().eraseToAnyView() | ||
case let .invalid(errors): | ||
return content(errors).eraseToAnyView() | ||
} | ||
} | ||
} |