Skip to content

Commit

Permalink
ADD: Optional Selection Parameter for TabView
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercen-Lee committed Oct 17, 2023
1 parent 0f99200 commit d01eb94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Sources/OpenTDS/Component/Example/ExampleView.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import SwiftUI

struct ExampleView: View {
@State var selection: Int = 0
var body: some View {
TossTabView {
TossScrollView("a") {
Text("View1")
Button {
selection = 1
} label: {
Text("btn")
}
}
.toolbarButton {
TossToolbarButton(Image(systemName: "house.fill"), action: {
Expand Down
19 changes: 17 additions & 2 deletions Sources/OpenTDS/Component/TabView/TabView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Combine

/**
Animated Tab View of Toss.
Expand All @@ -16,12 +17,16 @@ import SwiftUI
@available(macOS 11, iOS 14, *)
public struct TossTabView: View {

@State var selected: Int = 0
@State var selection: Binding<Int>?
@State var selected: Int

let content: [TossTabItem]

public init(haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
public init(selection: Binding<Int>? = nil,
haptic: UIImpactFeedbackGenerator.FeedbackStyle? = .light,
@TossTabViewBuilder content: () -> [TossTabItem]) {
self.selection = selection
self.selected = selection?.wrappedValue ?? 0
self.content = content()
}

Expand Down Expand Up @@ -52,6 +57,9 @@ public struct TossTabView: View {
TossTabViewButton(content[idx].title,
content[idx].image,
idx == selected) {
if selection != nil {
selection?.wrappedValue = idx
}
if selected != idx {
withAnimation(.easeInOut(duration: 0.2)) {
selected = idx
Expand All @@ -71,5 +79,12 @@ public struct TossTabView: View {
}
}
.background(TossColor.background.ignoresSafeArea())
.onReceive(Just(selection)) { newValue in
if let newValue, selected != newValue.wrappedValue {
withAnimation(.easeInOut(duration: 0.2)) {
selected = newValue.wrappedValue
}
}
}
}
}

0 comments on commit d01eb94

Please sign in to comment.