-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking βSign up for GitHubβ, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IOS-10813 Make Forms component in SwiftUI #419
Merged
+223
β4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
76 changes: 76 additions & 0 deletions
76
MisticaCatalog/Source/Catalog/MisticaSwiftUI/Components/FormViewCatalog.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,76 @@ | ||
// | ||
// FormViewCatalog.swift | ||
// | ||
// Made with β€οΈ by Novum | ||
// | ||
// Copyright Β© Telefonica. All rights reserved. | ||
// | ||
|
||
import MisticaSwiftUI | ||
import SwiftUI | ||
|
||
public struct FormViewCatalog: View { | ||
struct FormFieldState: Identifiable { | ||
let id = UUID() | ||
let placeholder: String | ||
var text: String = "" | ||
var assistiveText: String = "" | ||
var state: InputField.ValidationState = .normal | ||
let style: InputField.Style | ||
|
||
func createInputField(binding: Binding<FormFieldState>) -> InputField { | ||
InputField( | ||
placeholder: placeholder, | ||
text: binding.text, | ||
assistiveText: binding.assistiveText, | ||
state: binding.state, | ||
nonOptionalFieldFailureMessage: "This field is required" | ||
) | ||
.style(style) | ||
} | ||
} | ||
|
||
@State private var fields: [FormFieldState] = [ | ||
FormFieldState(placeholder: "Name", style: .text), | ||
FormFieldState(placeholder: "Surname (Optional)", style: .text), | ||
FormFieldState(placeholder: "Email", style: .email), | ||
FormFieldState(placeholder: "Password", style: .secure), | ||
FormFieldState(placeholder: "Phone", style: .phone(code: "+34")) | ||
] | ||
|
||
public var body: some View { | ||
FormView( | ||
inputFields: fields.indices.map { index in | ||
fields[index].createInputField(binding: $fields[index]) | ||
}, | ||
headerView: AnyView( | ||
Text("Header View") | ||
.font(.headline) | ||
), | ||
detailView: AnyView( | ||
Text("Detail View") | ||
.font(.subheadline) | ||
), | ||
footerView: AnyView( | ||
Text("Footer View") | ||
.font(.footnote) | ||
), | ||
buttonTitle: "Save", | ||
onButtonTap: { _ in | ||
validateForm() | ||
} | ||
) | ||
} | ||
|
||
private func validateForm() { | ||
for index in fields.indices { | ||
fields[index].createInputField(binding: $fields[index]).validate() | ||
} | ||
} | ||
} | ||
|
||
struct CatalogFormView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
FormViewCatalog() | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...stica/Utils/Extensions/String+Utils.swift β ...sticaCommon/Utils/String+InputField.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// String+Utils.swift | ||
// String+InputField.swift | ||
// | ||
// Made with β€οΈ by Novum | ||
// | ||
|
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,105 @@ | ||
// | ||
// FormView.swift | ||
// | ||
// Made with β€οΈ by Novum | ||
// | ||
// Copyright Β© Telefonica. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct FormView: View { | ||
@State public var inputFields: [InputField] | ||
@State private var isButtonEnabled: Bool = true | ||
@State private var isValid: Bool = true | ||
|
||
var headerView: AnyView? | ||
var detailView: AnyView? | ||
var footerView: AnyView? | ||
var buttonTitle: String | ||
var onButtonTap: ((Bool) -> Void)? | ||
|
||
public init( | ||
inputFields: [InputField], | ||
headerView: AnyView? = nil, | ||
detailView: AnyView? = nil, | ||
footerView: AnyView? = nil, | ||
buttonTitle: String, | ||
onButtonTap: ((Bool) -> Void)? = nil | ||
) { | ||
self.inputFields = inputFields | ||
self.headerView = headerView | ||
self.detailView = detailView | ||
self.footerView = footerView | ||
self.buttonTitle = buttonTitle | ||
self.onButtonTap = onButtonTap | ||
} | ||
|
||
public var body: some View { | ||
ScrollView { | ||
VStack(alignment: .leading, spacing: 16) { | ||
if let headerView = headerView { | ||
headerView | ||
} | ||
|
||
ForEach(inputFields) { inputField in | ||
inputField | ||
} | ||
|
||
if let detailView = detailView { | ||
detailView | ||
} | ||
|
||
Button(action: validateAndSubmit) { | ||
Text(buttonTitle) | ||
.frame(maxWidth: .infinity) | ||
} | ||
.buttonStyle(.misticaPrimary()) | ||
.disabled(!isButtonEnabled) | ||
|
||
if let footerView = footerView { | ||
footerView | ||
} | ||
} | ||
.padding(16) | ||
} | ||
} | ||
|
||
private func validateAndSubmit() { | ||
onButtonTap?(isValid) | ||
} | ||
} | ||
|
||
struct FormView_Previews: PreviewProvider { | ||
@State static var text1 = "" | ||
@State static var text2 = "" | ||
@State static var text3 = "" | ||
@State static var text4 = "" | ||
|
||
@State static var assistiveText1 = "This field is required" | ||
|
||
static var previews: some View { | ||
FormView( | ||
inputFields: [ | ||
InputField(placeholder: "Name", text: $text1) | ||
.style(.text), | ||
InputField(placeholder: "Surname", text: $text2, assistiveText: $assistiveText1, state: .constant(.invalid)) | ||
.style(.text), | ||
InputField(placeholder: "Email", text: $text3) | ||
.style(.email), | ||
InputField(placeholder: "Phone", text: $text4) | ||
.style(.phone(code: "+34")) | ||
], | ||
headerView: AnyView(Text("Header view") | ||
.font(.headline)), | ||
detailView: AnyView(Text("Detail view") | ||
.font(.subheadline)), | ||
footerView: AnyView(Text("Footer view") | ||
.font(.footnote)), | ||
buttonTitle: "Save", | ||
onButtonTap: { isValid in | ||
print("Form Submitted. Is valid: \(isValid)") | ||
} | ||
) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well seen π΅οΈ