-
Notifications
You must be signed in to change notification settings - Fork 33
/
BPKNavigationTabGroup.swift
112 lines (98 loc) · 3.23 KB
/
BPKNavigationTabGroup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2018 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import SwiftUI
/// A Group of tabs that allows a single tab to be selected at a time.
public struct BPKNavigationTabGroup: View {
private let tabs: [Item]
private let style: BPKNavigationTabGroup.Style
private let onItemClick: (_ index: Int) -> Void
@Binding private var selectedIndex: Int
public init(
tabs: [Item],
style: BPKNavigationTabGroup.Style = .default,
selectedIndex: Binding<Int>,
onItemClick: @escaping (_ index: Int) -> Void
) {
self.tabs = tabs
self.style = style
self._selectedIndex = selectedIndex
self.onItemClick = onItemClick
}
public var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: .md) {
ForEach(Array(tabs.enumerated()), id: \.element) { index, item in
tab(for: item, index: index)
}
}
.padding(1) // to account for chip outlines
}
}
@ViewBuilder
private func tab(for tab: Item, index: Int) -> some View {
BPKNavigationTab(
tab.text,
icon: tab.icon,
selected: selectedIndex == index,
style: style
) {
onItemClick(index)
}
}
}
public extension BPKNavigationTabGroup {
struct Item: Hashable {
let text: String
let icon: BPKIcon?
public init(text: String, icon: BPKIcon? = nil) {
self.text = text
self.icon = icon
}
public static func == (lhs: Item, rhs: Item) -> Bool {
lhs.text == rhs.text && lhs.icon?.name == rhs.icon?.name
}
public func hash(into hasher: inout Hasher) {
hasher.combine(text)
hasher.combine(icon?.name)
}
}
}
struct BPKNavigationTabGroup_Previews: PreviewProvider {
static let tabs: [BPKNavigationTabGroup.Item] = [
.init(text: "Explore", icon: .explore),
.init(text: "Flights", icon: .flight),
.init(text: "Hotels", icon: .hotels),
.init(text: "Car Hire", icon: .cars)
]
static var previews: some View {
VStack {
BPKNavigationTabGroup(
tabs: tabs,
selectedIndex: .constant(0)
) { _ in }
.padding()
BPKNavigationTabGroup(
tabs: tabs,
style: .onDark,
selectedIndex: .constant(0)
) { _ in }
.padding()
.background(.surfaceContrastColor)
}
}
}