Skip to content

Commit

Permalink
♻️ :: [#336] SMSSegmentedControl / 외부에서 selected option을 제어 가능하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
baekteun committed May 26, 2024
1 parent 0f3eac4 commit b05826e
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import SwiftUI

public struct SMSSegmentedControl: View {
@State private var selectedIndex = 0
@Namespace private var segmentNamespace
private let options: [String]
private let selectedOption: String?
private let onSelect: (String) -> Void

public init(
options: [String],
selectedOption: String? = nil,
onSelect: @escaping (String) -> Void
) {
self.options = options
self.selectedOption = selectedOption
self.onSelect = onSelect
}

public var body: some View {
HStack(spacing: 0) {
ForEach(0..<options.count, id: \.self) { index in
let isSelected = selectedIndex == index
let option = options[index]
let isSelected = options[index] == selectedOption
SMSText(options[index], font: .title2)
.frame(maxWidth: .infinity)
.padding(.vertical, 9.5)
Expand All @@ -32,12 +35,12 @@ public struct SMSSegmentedControl: View {
}
}
.foregroundStyle(
self.selectedIndex == index ? Color.sms(.system(.white)) : Color.sms(.neutral(.n40))
isSelected ? Color.sms(.system(.white)) : Color.sms(.neutral(.n40))
)
.padding(4)
.buttonWrapper {
withAnimation(.interactiveSpring(duration: 0.5)) {
self.selectedIndex = index
onSelect(option)
}
}
}
Expand Down

0 comments on commit b05826e

Please sign in to comment.