Skip to content

Commit

Permalink
refactor: 💡 [JIRA:HCPSDKFIORIUIKIT-2882] TextInputField Enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyi-zhang committed Jan 9, 2025
1 parent 7b388b4 commit 498d4ee
Show file tree
Hide file tree
Showing 25 changed files with 108 additions and 256 deletions.
2 changes: 1 addition & 1 deletion Sources/FioriSwiftUICore/Models/ModelDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public protocol ActionModel: ActionComponent {}
public protocol _TextInputModel: TextInputComponent {}

/// Deprecated TextInputModel
@available(*, unavailable, renamed: "_TextInputModel", message: "Will be removed in the future release. Please create TextInput with other initializers instead.")
@available(*, unavailable, renamed: "_TextInputModel", message: "Will be removed in the future release. Please create TextInput with TextInputField instead.")
public protocol TextInputModel {}

// sourcery: generated_component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ protocol _MoreActionOverflowComponent {
// sourcery: BaseComponent
protocol _TextInputFieldComponent {
// sourcery: @Binding
// sourcery: defaultValue = ".constant("")"
var text: String { get }
// sourcery: defaultValue = """"
var prompt: String { get }
// sourcery: no_view
var onCommit: (() -> Void)? { get }
}

// sourcery: BaseComponent
Expand Down Expand Up @@ -496,15 +501,6 @@ protocol _ActivityItemsComponent {
var activityItems: [ActivityItemDataType] { get }
}

// sourcery: BaseComponent
protocol _TextInputComponent {
// sourcery: @Binding
// sourcery: defaultValue = ".constant("")"
var textInputValue: String { get }
// sourcery: no_view
var onCommit: (() -> Void)? { get }
}

// sourcery: BaseComponent
protocol _LowerThumbComponent {
// sourcery: @ViewBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import SwiftUI
public struct TextInputFieldBaseStyle: TextInputFieldStyle {
@ViewBuilder
public func makeBody(_ configuration: TextInputFieldConfiguration) -> some View {
TextField("", text: configuration.$text)
TextField(configuration.prompt,
text: configuration.$text,
onCommit: configuration.onCommit ?? {})
}
}

Expand Down
26 changes: 0 additions & 26 deletions Sources/FioriSwiftUICore/_FioriStyles/TextInputStyle.fiori.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ import SwiftUI

public struct PlaceholderTextField {
@Binding var text: String
let prompt: String
let onCommit: (() -> Void)?
let placeholder: any View

@Environment(\.placeholderTextFieldStyle) var style

fileprivate var _shouldApplyDefaultStyle = true

public init(text: Binding<String>,
public init(text: Binding<String> = .constant(""),
prompt: String = "",
onCommit: (() -> Void)? = nil,
@ViewBuilder placeholder: () -> any View = { EmptyView() })
{
self._text = text
self.prompt = prompt
self.onCommit = onCommit
self.placeholder = Placeholder(placeholder: placeholder)
}
}

public extension PlaceholderTextField {
init(text: Binding<String>,
prompt: String = "",
onCommit: (() -> Void)? = nil,
placeholder: AttributedString? = nil)
{
self.init(text: text, placeholder: { OptionalText(placeholder) })
self.init(text: text, prompt: prompt, onCommit: onCommit, placeholder: { OptionalText(placeholder) })
}
}

Expand All @@ -34,6 +42,8 @@ public extension PlaceholderTextField {

internal init(_ configuration: PlaceholderTextFieldConfiguration, shouldApplyDefaultStyle: Bool) {
self._text = configuration.$text
self.prompt = configuration.prompt
self.onCommit = configuration.onCommit
self.placeholder = configuration.placeholder
self._shouldApplyDefaultStyle = shouldApplyDefaultStyle
}
Expand All @@ -44,7 +54,7 @@ extension PlaceholderTextField: View {
if self._shouldApplyDefaultStyle {
self.defaultStyle()
} else {
self.style.resolve(configuration: .init(text: self.$text, placeholder: .init(self.placeholder))).typeErased
self.style.resolve(configuration: .init(text: self.$text, prompt: self.prompt, onCommit: self.onCommit, placeholder: .init(self.placeholder))).typeErased
.transformEnvironment(\.placeholderTextFieldStyleStack) { stack in
if !stack.isEmpty {
stack.removeLast()
Expand All @@ -62,7 +72,7 @@ private extension PlaceholderTextField {
}

func defaultStyle() -> some View {
PlaceholderTextField(.init(text: self.$text, placeholder: .init(self.placeholder)))
PlaceholderTextField(.init(text: self.$text, prompt: self.prompt, onCommit: self.onCommit, placeholder: .init(self.placeholder)))
.shouldApplyDefaultStyle(false)
.placeholderTextFieldStyle(PlaceholderTextFieldFioriStyle.ContentFioriStyle())
.typeErased
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct AnyPlaceholderTextFieldStyle: PlaceholderTextFieldStyle {

public struct PlaceholderTextFieldConfiguration {
@Binding public var text: String
public let prompt: String
public let onCommit: (() -> Void)?
public let placeholder: Placeholder

public typealias Placeholder = ConfigurationViewWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import FioriThemeManager
public struct StepperField {
let decrementAction: any View
@Binding var text: String
let prompt: String
let onCommit: (() -> Void)?
let incrementAction: any View
/// The step value
let step: Double
Expand All @@ -21,14 +23,18 @@ public struct StepperField {
fileprivate var _shouldApplyDefaultStyle = true

public init(@ViewBuilder decrementAction: () -> any View = { FioriButton { _ in FioriIcon.actions.less } },
text: Binding<String>,
text: Binding<String> = .constant(""),
prompt: String = "",
onCommit: (() -> Void)? = nil,
@ViewBuilder incrementAction: () -> any View = { FioriButton { _ in FioriIcon.actions.add } },
step: Double = 1,
stepRange: ClosedRange<Double>,
isDecimalSupported: Bool = false)
{
self.decrementAction = DecrementAction(decrementAction: decrementAction)
self._text = text
self.prompt = prompt
self.onCommit = onCommit
self.incrementAction = IncrementAction(incrementAction: incrementAction)
self.step = step
self.stepRange = stepRange
Expand All @@ -39,12 +45,14 @@ public struct StepperField {
public extension StepperField {
init(decrementAction: FioriButton? = FioriButton { _ in FioriIcon.actions.less },
text: Binding<String>,
prompt: String = "",
onCommit: (() -> Void)? = nil,
incrementAction: FioriButton? = FioriButton { _ in FioriIcon.actions.add },
step: Double = 1,
stepRange: ClosedRange<Double>,
isDecimalSupported: Bool = false)
{
self.init(decrementAction: { decrementAction }, text: text, incrementAction: { incrementAction }, step: step, stepRange: stepRange, isDecimalSupported: isDecimalSupported)
self.init(decrementAction: { decrementAction }, text: text, prompt: prompt, onCommit: onCommit, incrementAction: { incrementAction }, step: step, stepRange: stepRange, isDecimalSupported: isDecimalSupported)
}
}

Expand All @@ -56,6 +64,8 @@ public extension StepperField {
internal init(_ configuration: StepperFieldConfiguration, shouldApplyDefaultStyle: Bool) {
self.decrementAction = configuration.decrementAction
self._text = configuration.$text
self.prompt = configuration.prompt
self.onCommit = configuration.onCommit
self.incrementAction = configuration.incrementAction
self.step = configuration.step
self.stepRange = configuration.stepRange
Expand All @@ -69,7 +79,7 @@ extension StepperField: View {
if self._shouldApplyDefaultStyle {
self.defaultStyle()
} else {
self.style.resolve(configuration: .init(decrementAction: .init(self.decrementAction), text: self.$text, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported)).typeErased
self.style.resolve(configuration: .init(decrementAction: .init(self.decrementAction), text: self.$text, prompt: self.prompt, onCommit: self.onCommit, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported)).typeErased
.transformEnvironment(\.stepperFieldStyleStack) { stack in
if !stack.isEmpty {
stack.removeLast()
Expand All @@ -87,7 +97,7 @@ private extension StepperField {
}

func defaultStyle() -> some View {
StepperField(.init(decrementAction: .init(self.decrementAction), text: self.$text, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported))
StepperField(.init(decrementAction: .init(self.decrementAction), text: self.$text, prompt: self.prompt, onCommit: self.onCommit, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported))
.shouldApplyDefaultStyle(false)
.stepperFieldStyle(StepperFieldFioriStyle.ContentFioriStyle())
.typeErased
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct AnyStepperFieldStyle: StepperFieldStyle {
public struct StepperFieldConfiguration {
public let decrementAction: DecrementAction
@Binding public var text: String
public let prompt: String
public let onCommit: (() -> Void)?
public let incrementAction: IncrementAction
public let step: Double
public let stepRange: ClosedRange<Double>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public struct StepperView {
let title: any View
let decrementAction: any View
@Binding var text: String
let prompt: String
let onCommit: (() -> Void)?
let incrementAction: any View
/// The step value
let step: Double
Expand All @@ -25,7 +27,9 @@ public struct StepperView {

public init(@ViewBuilder title: () -> any View,
@ViewBuilder decrementAction: () -> any View = { FioriButton { _ in FioriIcon.actions.less } },
text: Binding<String>,
text: Binding<String> = .constant(""),
prompt: String = "",
onCommit: (() -> Void)? = nil,
@ViewBuilder incrementAction: () -> any View = { FioriButton { _ in FioriIcon.actions.add } },
step: Double = 1,
stepRange: ClosedRange<Double>,
Expand All @@ -36,6 +40,8 @@ public struct StepperView {
self.title = Title(title: title)
self.decrementAction = DecrementAction(decrementAction: decrementAction)
self._text = text
self.prompt = prompt
self.onCommit = onCommit
self.incrementAction = IncrementAction(incrementAction: incrementAction)
self.step = step
self.stepRange = stepRange
Expand All @@ -49,14 +55,16 @@ public extension StepperView {
init(title: AttributedString,
decrementAction: FioriButton? = FioriButton { _ in FioriIcon.actions.less },
text: Binding<String>,
prompt: String = "",
onCommit: (() -> Void)? = nil,
incrementAction: FioriButton? = FioriButton { _ in FioriIcon.actions.add },
step: Double = 1,
stepRange: ClosedRange<Double>,
isDecimalSupported: Bool = false,
icon: Image? = nil,
description: AttributedString? = nil)
{
self.init(title: { Text(title) }, decrementAction: { decrementAction }, text: text, incrementAction: { incrementAction }, step: step, stepRange: stepRange, isDecimalSupported: isDecimalSupported, icon: { icon }, description: { OptionalText(description) })
self.init(title: { Text(title) }, decrementAction: { decrementAction }, text: text, prompt: prompt, onCommit: onCommit, incrementAction: { incrementAction }, step: step, stepRange: stepRange, isDecimalSupported: isDecimalSupported, icon: { icon }, description: { OptionalText(description) })
}
}

Expand All @@ -69,6 +77,8 @@ public extension StepperView {
self.title = configuration.title
self.decrementAction = configuration.decrementAction
self._text = configuration.$text
self.prompt = configuration.prompt
self.onCommit = configuration.onCommit
self.incrementAction = configuration.incrementAction
self.step = configuration.step
self.stepRange = configuration.stepRange
Expand All @@ -84,7 +94,7 @@ extension StepperView: View {
if self._shouldApplyDefaultStyle {
self.defaultStyle()
} else {
self.style.resolve(configuration: .init(title: .init(self.title), decrementAction: .init(self.decrementAction), text: self.$text, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported, icon: .init(self.icon), description: .init(self.description))).typeErased
self.style.resolve(configuration: .init(title: .init(self.title), decrementAction: .init(self.decrementAction), text: self.$text, prompt: self.prompt, onCommit: self.onCommit, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported, icon: .init(self.icon), description: .init(self.description))).typeErased
.transformEnvironment(\.stepperViewStyleStack) { stack in
if !stack.isEmpty {
stack.removeLast()
Expand All @@ -102,7 +112,7 @@ private extension StepperView {
}

func defaultStyle() -> some View {
StepperView(.init(title: .init(self.title), decrementAction: .init(self.decrementAction), text: self.$text, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported, icon: .init(self.icon), description: .init(self.description)))
StepperView(.init(title: .init(self.title), decrementAction: .init(self.decrementAction), text: self.$text, prompt: self.prompt, onCommit: self.onCommit, incrementAction: .init(self.incrementAction), step: self.step, stepRange: self.stepRange, isDecimalSupported: self.isDecimalSupported, icon: .init(self.icon), description: .init(self.description)))
.shouldApplyDefaultStyle(false)
.stepperViewStyle(StepperViewFioriStyle.ContentFioriStyle())
.typeErased
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public struct StepperViewConfiguration {
public let title: Title
public let decrementAction: DecrementAction
@Binding public var text: String
public let prompt: String
public let onCommit: (() -> Void)?
public let incrementAction: IncrementAction
public let step: Double
public let stepRange: ClosedRange<Double>
Expand Down
Loading

0 comments on commit 498d4ee

Please sign in to comment.