Skip to content

Commit

Permalink
[SPA-174] Add a counter test - Update ReadME
Browse files Browse the repository at this point in the history
  • Loading branch information
robergro committed Jan 7, 2025
1 parent 833b0cd commit 0b37914
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ The FormField contains some public subviews :

#### Functions:

If the component inside the FormField is inherit from an UITextInput (The Spark TextField and TextEditor for example), a function to set the number of the characters is available:
If the component inside the FormField is inherit from an UITextInput (The Spark TextField and TextEditor for example), two functions to set the number of the characters are available:

```swift
// With the text
func setCounter(on text: String?, limit: Int?)

// Or with the text length
func setCounter(on textLength: Int, limit: Int?)
```

### FormFieldView
Expand All @@ -56,6 +60,18 @@ func setCounter(on text: String?, limit: Int?)
- `attributedDescription`: An option attributed string to change helper message of font or size.
- `isTitleRequired`: A bool value to add asterisk character at the end of title for specifying required field.

#### Modifiers:

Two modifier functions to set the number of the characters are available:

```swift
// With the text
func counter(on text: String, limit: Int?) -> Self

// Or with the text length
func counter(on textLength: Int, limit: Int?) -> Self
```

## Examples

### FormFieldUIView
Expand Down
11 changes: 10 additions & 1 deletion Sources/Core/View/SwiftUI/FormFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ public struct FormFieldView<Component: View>: View {
return copy
}

/// Set accessibility value for the *secondaryHelper* subview.
/// - parameter value: the accessibility value.
/// - Returns: The current view.
public func secondaryHelperAccessibilityValue(_ value: String) -> Self {
var copy = self
copy.secondaryHelperAccessibility.value = value
return copy
}

// MARK: - Counter Modifier

/// Display a counter value (X/Y) in the secondary helper label with a text and the limit.
Expand All @@ -217,7 +226,7 @@ public struct FormFieldView<Component: View>: View {
/// - parameter textLength: the text length.
/// - parameter limit: the counter limit. If the value is nil, the counter is not displayed.
/// - Returns: The current view.
public func setCounter(on textLength: Int, limit: Int?) -> Self {
public func counter(on textLength: Int, limit: Int?) -> Self {
self.viewModel.setCounter(textLength: textLength, limit: limit)
return self
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Core/ViewModel/FormFieldViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ final class FormFieldViewModel<AS: SparkAttributedString>: ObservableObject {
self.updateTitle()
}

func setCounter(text: String?, limit: Int?) {
self.setCounter(textLength: text?.count, limit: limit)
}

func setCounter(textLength: Int?, limit: Int?) {
guard let limit else {
self.secondaryHelper = nil
Expand Down
34 changes: 34 additions & 0 deletions Tests/UnitTests/ViewModel/FormFieldViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ final class FormFieldViewModelTests: XCTestCase {
XCTAssertEqual(viewModel.secondaryHelper, "0/100")
}

func test_setCounter_with_textLength() {
// Given
let viewModel = FormFieldViewModel<NSAttributedString>(
theme: self.theme,
feedbackState: .default,
title: NSAttributedString("Title"),
helper: NSAttributedString("Helper"),
isTitleRequired: true
)

// When
viewModel.setCounter(textLength: 4, limit: 100)

// Then
XCTAssertEqual(viewModel.secondaryHelper, "4/100")
}

func test_setCounter_without_textLength() {
// Given
let viewModel = FormFieldViewModel<NSAttributedString>(
theme: self.theme,
feedbackState: .default,
title: NSAttributedString("Title"),
helper: NSAttributedString("Helper"),
isTitleRequired: true
)

// When
viewModel.setCounter(textLength: nil, limit: 100)

// Then
XCTAssertEqual(viewModel.secondaryHelper, "0/100")
}

func test_setCounter_without_limit() {
// Given
let viewModel = FormFieldViewModel<NSAttributedString>(
Expand Down

0 comments on commit 0b37914

Please sign in to comment.