-
Notifications
You must be signed in to change notification settings - Fork 3
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
1d0a554
commit 494dba4
Showing
6 changed files
with
95 additions
and
26 deletions.
There are no files selected for viewing
File renamed without changes.
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,20 @@ | ||
// | ||
// File.swift | ||
// Core | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public extension View { | ||
@ViewBuilder | ||
func `if`<Content: View>(_ conditional: Bool, content: (Self) -> Content) -> some View { | ||
if conditional { | ||
content(self) | ||
} else { | ||
self | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
sources/Features/Sources/Features/Setup/Extensions/String+Extensions.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,16 @@ | ||
// | ||
// File.swift | ||
// Features | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
var isValidURL: Bool { | ||
let regEx = "((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?" | ||
let predicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [regEx]) | ||
return predicate.evaluate(with: 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
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
54 changes: 54 additions & 0 deletions
54
sources/Features/Sources/Features/Setup/Views/FormField.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,54 @@ | ||
// | ||
// File.swift | ||
// Features | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct FormField: View { | ||
enum FieldType { | ||
case string(required: Bool) | ||
case url(required: Bool) | ||
|
||
var isURL: Bool { | ||
switch self { | ||
case .url: | ||
return true | ||
case .string: | ||
return false | ||
} | ||
} | ||
|
||
func validate(text: String) -> Bool { | ||
switch self { | ||
case let .string(required): | ||
return !required || !text.isEmpty | ||
case let .url(required): | ||
return !required || text.isValidURL | ||
} | ||
} | ||
} | ||
|
||
let type: FieldType | ||
@Binding var text: String | ||
@State var isValid = true | ||
|
||
var body: some View { | ||
TextField("", text: $text, onEditingChanged: { isEditing in | ||
if isEditing { | ||
isValid = true | ||
} else { | ||
isValid = type.validate(text: text) | ||
} | ||
}) | ||
.foregroundColor(isValid ? .primary : .red) | ||
.if(type.isURL) { | ||
$0.keyboardType(.URL) | ||
.disableAutocorrection(true) | ||
.autocapitalization(.none) | ||
} | ||
} | ||
} |