Skip to content
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

[SPA-173] Add info label - Improve component #5

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ let package = Package(
url: "https://github.com/adevinta/spark-ios-theming.git",
// path: "../spark-ios-theming"
/*version*/ "0.0.1"..."999.999.999"
),
.package(
url: "https://github.com/adevinta/spark-ios-component-checkbox.git",
// path: "../spark-ios-component-checkbox"
/*version*/ "0.0.1"..."999.999.999"
),
.package(
url: "https://github.com/adevinta/spark-ios-component-radio-button.git",
// path: "../spark-ios-component-radio-button"
/*version*/ "0.0.1"..."999.999.999"
)
],
targets: [
Expand Down Expand Up @@ -103,15 +93,7 @@ let package = Package(
.product(
name: "SparkCommonSnapshotTesting",
package: "spark-ios-common"
),
.product(
name: "SparkCheckbox",
package: "spark-ios-component-checkbox"
),
.product(
name: "SparkRadioButton",
package: "spark-ios-component-radio-button"
),
)
],
path: "Tests/SnapshotTests"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public enum FormFieldAccessibilityIdentifier {
public static let formField = "spark-formfield"
public static let formFieldLabel = "spark-formfield-label"
public static let formFieldHelperMessage = "spark-formfield-helper-message"
public static let formFieldInfoMessage = "spark-formfield-secondary-helper-message"
}
3 changes: 2 additions & 1 deletion Sources/Core/Model/FormFieldColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SparkTheming

struct FormFieldColors {
let title: any ColorToken
let description: any ColorToken
let helper: any ColorToken
let asterisk: any ColorToken
let secondaryHelper: any ColorToken
}
11 changes: 7 additions & 4 deletions Sources/Core/UseCase/FormFieldColorsUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ protocol FormFieldColorsUseCaseable {
struct FormFieldColorsUseCase: FormFieldColorsUseCaseable {

func execute(from theme: Theme, feedback state: FormFieldFeedbackState) -> FormFieldColors {
let commonColor = theme.colors.base.onSurface.opacity(theme.dims.dim1)
switch state {
case .default:
return FormFieldColors(
title: theme.colors.base.onSurface,
description: theme.colors.base.onSurface.opacity(theme.dims.dim1),
asterisk: theme.colors.base.onSurface.opacity(theme.dims.dim1)
helper: commonColor,
asterisk: commonColor,
secondaryHelper: commonColor
)
case .error:
return FormFieldColors(
title: theme.colors.base.onSurface,
description: theme.colors.feedback.error,
asterisk: theme.colors.base.onSurface.opacity(theme.dims.dim1)
helper: theme.colors.feedback.error,
asterisk: commonColor,
secondaryHelper: commonColor
)
}
}
Expand Down
88 changes: 75 additions & 13 deletions Sources/Core/View/SwiftUI/FormFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,70 @@ import SparkTheming

public struct FormFieldView<Component: View>: View {

// MARK: - Properties

@ObservedObject private var viewModel: FormFieldViewModel<AttributedString>
@ScaledMetric private var spacing: CGFloat
private let component: Component

/// Initialize a new checkbox UIKit-view.
// MARK: - Initialization

/// Initialize a formField.
/// - Parameters:
/// - theme: The current Spark-Theme.
/// - component: The component is covered by formfield.
/// - feedbackState: The formfield feedback state. 'Default' or 'Error'.
/// - title: The formfield title.
/// - description: The formfield helper message.
/// - helper: The formfield helper message.
/// - isTitleRequired: The asterisk symbol at the end of title.
public init(
theme: Theme,
@ViewBuilder component: @escaping () -> Component,
feedbackState: FormFieldFeedbackState = .default,
title: String? = nil,
description: String? = nil,
helper: String? = nil,
isTitleRequired: Bool = false
) {
let attributedTitle: AttributedString? = title.map(AttributedString.init)
let attributedDescription: AttributedString? = description.map(AttributedString.init)
let attributedHelper: AttributedString? = helper.map(AttributedString.init)
self.init(
theme: theme,
component: component,
feedbackState: feedbackState,
attributedTitle: attributedTitle,
attributedDescription: attributedDescription,
attributedHelper: attributedHelper,
isTitleRequired: isTitleRequired
)
}

/// Initialize a formField.
/// - Parameters:
/// - theme: The current Spark-Theme.
/// - component: The component is covered by formfield.
/// - feedbackState: The formfield feedback state. 'Default' or 'Error'.
/// - title: The formfield title.
/// - description: The formfield helper message.
/// - isTitleRequired: The asterisk symbol at the end of title.
@available(*, deprecated, message: "Replaced by the init with the helper String since the 0.1.1.")
public init(
theme: Theme,
@ViewBuilder component: @escaping () -> Component,
feedbackState: FormFieldFeedbackState = .default,
title: String? = nil,
description: String? = nil,
isTitleRequired: Bool = false
) {
self.init(
theme: theme,
component: component,
feedbackState: feedbackState,
title: title,
helper: description,
isTitleRequired: isTitleRequired
)
}

/// Initialize a new checkbox UIKit-view.
/// Initialize a formField.
/// - Parameters:
/// - theme: The current Spark-Theme.
/// - component: The component is covered by formfield.
Expand All @@ -56,14 +87,14 @@ public struct FormFieldView<Component: View>: View {
@ViewBuilder component: @escaping () -> Component,
feedbackState: FormFieldFeedbackState = .default,
attributedTitle: AttributedString? = nil,
attributedDescription: AttributedString? = nil,
attributedHelper: AttributedString? = nil,
isTitleRequired: Bool = false
) {
let viewModel = FormFieldViewModel<AttributedString>(
theme: theme,
feedbackState: feedbackState,
title: attributedTitle,
description: attributedDescription,
helper: attributedHelper,
isTitleRequired: isTitleRequired
)

Expand All @@ -72,6 +103,35 @@ public struct FormFieldView<Component: View>: View {
self.component = component()
}

/// Initialize a formField.
/// - Parameters:
/// - theme: The current Spark-Theme.
/// - component: The component is covered by formfield.
/// - feedbackState: The formfield feedback state. 'Default' or 'Error'.
/// - attributedTitle: The formfield attributedTitle.
/// - attributedDescription: The formfield attributed helper message.
/// - isTitleRequired: The asterisk symbol at the end of title.
@available(*, deprecated, message: "Replaced by the init with the helper String since the 0.1.1.")
public init(
theme: Theme,
@ViewBuilder component: @escaping () -> Component,
feedbackState: FormFieldFeedbackState = .default,
attributedTitle: AttributedString? = nil,
attributedDescription: AttributedString? = nil,
isTitleRequired: Bool = false
) {
self.init(
theme: theme,
component: component,
feedbackState: feedbackState,
attributedTitle: attributedTitle,
attributedHelper: attributedDescription,
isTitleRequired: isTitleRequired
)
}

// MARK: - View

public var body: some View {
VStack(alignment: .leading, spacing: self.spacing) {

Expand All @@ -83,11 +143,13 @@ public struct FormFieldView<Component: View>: View {
}
self.component

if let description = self.viewModel.description {
Text(description)
.font(self.viewModel.descriptionFont.font)
.foregroundStyle(self.viewModel.descriptionColor.color)
.accessibilityIdentifier(FormFieldAccessibilityIdentifier.formFieldHelperMessage)
HStack(alignment: .top, spacing: self.spacing) {
if let helper = self.viewModel.helper {
Text(helper)
.font(self.viewModel.helperFont.font)
.foregroundStyle(self.viewModel.helperColor.color)
.accessibilityIdentifier(FormFieldAccessibilityIdentifier.formFieldHelperMessage)
}
}
}
.accessibilityElement(children: .contain)
Expand Down
Loading
Loading