Skip to content

Commit

Permalink
Merge pull request #551 from adevinta/refacto/switch/space
Browse files Browse the repository at this point in the history
[Fix] Switch: Remove the space when there is no text
  • Loading branch information
robergro authored Oct 27, 2023
2 parents 2b28332 + 1321c0e commit da1fe9d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ enum SwitchSubviewType {
case text
case toggle

// MARK: - Properties
// MARK: - Methods

static var leftAlignmentCases: [Self] {
return [.toggle, .space, .text]
}
static func allCases(isLeftAlignment: Bool, showSpace: Bool) -> [Self] {
var cases: [Self]

// Left or right alignement ?
if isLeftAlignment {
cases = [.toggle, .text]
} else {
cases = [.text, .toggle]
}

// Add spaces ?
if showSpace {
cases.insert(.space, at: 1)
}

static var rightAlignmentCases: [Self] {
return [.text, .space, .toggle]
return cases
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ final class SwitchSubviewTypeTests: XCTestCase {

// MARK: - Tests

func test_leftAlignmentCases() {
func test_allCases_when_isLeftAlignment_is_true_and_showSpace_is_false() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.leftAlignmentCases
let allCases = SwitchSubviewType.allCases(
isLeftAlignment: true,
showSpace: false
)

// THEN
XCTAssertEqual(
allCases,
[.toggle, .text]
)
}

func test_allCases_when_isLeftAlignment_is_true_and_showSpace_is_true() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.allCases(
isLeftAlignment: true,
showSpace: true
)

// THEN
XCTAssertEqual(
Expand All @@ -24,9 +41,26 @@ final class SwitchSubviewTypeTests: XCTestCase {
)
}

func test_rightAlignmentCases() {
func test_allCases_when_isLeftAlignment_is_false_and_showSpace_is_false() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.rightAlignmentCases
let allCases = SwitchSubviewType.allCases(
isLeftAlignment: false,
showSpace: false
)

// THEN
XCTAssertEqual(
allCases,
[.text, .toggle]
)
}

func test_allCases_when_isLeftAlignment_is_false_and_showSpace_is_true() {
// GIVEN / WHEN
let allCases = SwitchSubviewType.allCases(
isLeftAlignment: false,
showSpace: true
)

// THEN
XCTAssertEqual(
Expand Down
7 changes: 5 additions & 2 deletions core/Sources/Components/Switch/View/SwiftUI/SwitchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct SwitchView: View {
}

// MARK: - View

public var body: some View {
HStack(alignment: .top) {
ForEach(self.subviewsTypes(), id: \.self) {
Expand All @@ -68,7 +68,10 @@ public struct SwitchView: View {
// MARK: - Subview Maker

private func subviewsTypes() -> [SwitchSubviewType] {
return (self.viewModel.isToggleOnLeft == true) ? SwitchSubviewType.leftAlignmentCases : SwitchSubviewType.rightAlignmentCases
return SwitchSubviewType.allCases(
isLeftAlignment: self.viewModel.isToggleOnLeft == true,
showSpace: self.viewModel.horizontalSpacing ?? 0 > 0
)
}

@ViewBuilder
Expand Down

0 comments on commit da1fe9d

Please sign in to comment.