Skip to content

Commit 8971e10

Browse files
committed
Improve EmptyView detection
1 parent d6131c4 commit 8971e10

15 files changed

+77
-15
lines changed

Sources/Orbit/Components/Badge.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SwiftUI
66
/// They can be updated when a status changes, but they should not be actionable.
77
///
88
/// - Note: [Orbit definition](https://orbit.kiwi/components/badge/)
9-
public struct Badge<LeadingIcon: View, TrailingIcon: View>: View {
9+
public struct Badge<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {
1010

1111
@Environment(\.status) private var status
1212
@Environment(\.sizeCategory) private var sizeCategory

Sources/Orbit/Components/BadgeList.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import SwiftUI
55
/// The items in the list should all be static information, *not* actionable.
66
///
77
/// - Note: [Orbit definition](https://orbit.kiwi/components/information/badgelist/)
8-
public struct BadgeList<Icon: View, Content: View>: View {
8+
public struct BadgeList<Icon: View, Content: View>: View, PotentiallyEmptyView {
99

1010
@Environment(\.status) private var status
1111
@Environment(\.textAccentColor) private var textAccentColor

Sources/Orbit/Components/Button.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import SwiftUI
44
///
55
/// - Note: [Orbit definition](https://orbit.kiwi/components/button/)
66
/// - Important: Component expands horizontally unless prevented by `fixedSize` or `idealSize` modifier.
7-
public struct Button<LeadingIcon: View, TrailingIcon: View>: View {
7+
public struct Button<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {
88

99
@Environment(\.suppressButtonStyle) private var suppressButtonStyle
1010

Sources/Orbit/Components/ButtonLink.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftUI
33
/// Displays a single, less important action a user can take.
44
///
55
/// - Note: [Orbit definition](https://orbit.kiwi/components/buttonlink/)
6-
public struct ButtonLink<LeadingIcon: View, TrailingIcon: View>: View {
6+
public struct ButtonLink<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {
77

88
@Environment(\.suppressButtonStyle) var suppressButtonStyle
99

Sources/Orbit/Components/CountryFlag.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010
/// ```
1111
///
1212
/// - Note: [Orbit definition](https://orbit.kiwi/components/countryflag/)
13-
public struct CountryFlag: View {
13+
public struct CountryFlag: View, PotentiallyEmptyView {
1414

1515
@Environment(\.textSize) var textSize
1616
@Environment(\.iconSize) var iconSize
@@ -51,6 +51,10 @@ public struct CountryFlag: View {
5151
var size: CGFloat {
5252
(iconSize ?? textSize.map(Icon.Size.fromTextSize(size:)) ?? Icon.Size.normal.value) * sizeCategory.ratio
5353
}
54+
55+
var isEmpty: Bool {
56+
countryCode == nil
57+
}
5458
}
5559

5660
// MARK: - Inits

Sources/Orbit/Components/Icon.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import SwiftUI
2525
/// ```
2626
///
2727
/// - Note: [Orbit definition](https://orbit.kiwi/components/icon/)
28-
public struct Icon: View, TextBuildable {
28+
public struct Icon: View, TextBuildable, PotentiallyEmptyView {
2929

3030
/// Approximate size ratio between SF Symbol and Orbit icon symbol.
3131
public static let sfSymbolToOrbitSymbolSizeRatio: CGFloat = 0.75

Sources/Orbit/Components/ListChoice.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import SwiftUI
44
///
55
/// - Note: [Orbit definition](https://orbit.kiwi/components/listchoice/)
66
/// - Important: Component expands horizontally unless prevented by `fixedSize` or `idealSize` modifier.
7-
public struct ListChoice<Header: View, Icon: View, Content: View>: View {
7+
public struct ListChoice<Header: View, Icon: View, Content: View>: View, PotentiallyEmptyView {
88

99
public let verticalPadding: CGFloat = .small // = 45 height @ normal size
1010

@@ -148,7 +148,7 @@ public struct ListChoice<Header: View, Icon: View, Content: View>: View {
148148
}
149149

150150
var isEmpty: Bool {
151-
isHeaderEmpty && header is EmptyView && content is EmptyView && disclosure == .none
151+
isHeaderEmpty && header.isEmpty && content.isEmpty && disclosure == .none
152152
}
153153

154154
var isHeaderEmpty: Bool {

Sources/Orbit/Components/Select.swift

+41
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ struct SelectPreviews: PreviewProvider {
187187
static var previews: some View {
188188
PreviewWrapper {
189189
standalone
190+
customContent
190191
idealSize
191192
sizing
192193
styles
@@ -197,6 +198,9 @@ struct SelectPreviews: PreviewProvider {
197198

198199
static var standalone: some View {
199200
VStack(spacing: .medium) {
201+
Select(InputFieldPreviews.label, value: InputFieldPreviews.value) {
202+
// No action
203+
}
200204
Select(InputFieldPreviews.label, prefix: .grid, value: InputFieldPreviews.value) {
201205
// No action
202206
}
@@ -214,6 +218,43 @@ struct SelectPreviews: PreviewProvider {
214218
.previewDisplayName()
215219
}
216220

221+
static var customContent: some View {
222+
VStack(spacing: .medium) {
223+
Select(value: "Value with a very very very very very long value..") {
224+
// No action
225+
} prefix: {
226+
EmptyView()
227+
} suffix: {
228+
EmptyView()
229+
}
230+
231+
Select(value: "Value with a very very very very very long value..") {
232+
// No action
233+
} prefix: {
234+
CountryFlag("")
235+
} suffix: {
236+
CountryFlag("")
237+
}
238+
239+
// FIXME: conditional content EmptyView
240+
StateWrapper(false) { state in
241+
Select(value: "Value with a very very very very very long value..") {
242+
state.wrappedValue.toggle()
243+
} prefix: {
244+
if state.wrappedValue {
245+
CountryFlag("us")
246+
}
247+
} suffix: {
248+
if state.wrappedValue {
249+
CountryFlag("us")
250+
}
251+
}
252+
}
253+
}
254+
.padding(.medium)
255+
.previewDisplayName()
256+
}
257+
217258
static var idealSize: some View {
218259
VStack(spacing: .medium) {
219260
Select("Ideal size", prefix: .grid, value: InputFieldPreviews.value, action: {})

Sources/Orbit/Components/Tag.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import SwiftUI
44
///
55
/// - Note: [Orbit definition](https://orbit.kiwi/components/tag/)
66
/// - Important: Component can expand horizontally by using `idealSize` modifier.
7-
public struct Tag<Icon: View>: View {
7+
public struct Tag<Icon: View>: View, PotentiallyEmptyView {
88

99
@Environment(\.idealSize) private var idealSize
1010
@Environment(\.textColor) private var textColor

Sources/Orbit/Components/Text.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SwiftUI
77
///
88
/// - Note: [Orbit definition](https://orbit.kiwi/components/text/)
99
/// - Important: Component has fixed vertical size. When the content is empty, the component results in `EmptyView`.
10-
public struct Text: View, FormattedTextBuildable {
10+
public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
1111

1212
@Environment(\.multilineTextAlignment) private var multilineTextAlignment
1313
@Environment(\.lineSpacing) private var lineSpacing

Sources/Orbit/Components/TileGroup.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SwiftUI
1919
/// ```
2020
///
2121
/// - Note: [Orbit definition](https://orbit.kiwi/components/tilegroup/)
22-
public struct TileGroup<Content: View>: View {
22+
public struct TileGroup<Content: View>: View, PotentiallyEmptyView {
2323

2424
@ViewBuilder let content: Content
2525

@@ -38,7 +38,7 @@ public struct TileGroup<Content: View>: View {
3838
}
3939

4040
var isEmpty: Bool {
41-
content is EmptyView
41+
content.isEmpty
4242
}
4343

4444
/// Creates Orbit TileGroup component as a wrapper for Tile content.

Sources/Orbit/Components/Timeline.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SwiftUI
66
/// - ``TimelineItem``
77
///
88
/// - Note: [Orbit definition](https://orbit.kiwi/components/progress-indicators/timeline/)
9-
public struct Timeline<Content: View>: View {
9+
public struct Timeline<Content: View>: View, PotentiallyEmptyView {
1010

1111
@Environment(\.sizeCategory) var sizeCategory
1212
@ViewBuilder let content: Content
@@ -35,7 +35,7 @@ public struct Timeline<Content: View>: View {
3535
}
3636

3737
var isEmpty: Bool {
38-
content is EmptyView
38+
content.isEmpty
3939
}
4040

4141
func progressLineHeight(

Sources/Orbit/Support/Forms/InputContent.swift

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ struct InputContentPreviews: PreviewProvider {
134134
// No action
135135
}
136136
}
137+
138+
InputContent(state: .default) {
139+
headerPlaceholder
140+
} prefix: {
141+
CountryFlag("")
142+
} suffix: {
143+
EmptyView()
144+
}
137145
}
138146
.padding(.medium)
139147
.previewDisplayName()

Sources/Orbit/Support/Layout/IsEmpty.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import SwiftUI
33
extension View {
44

55
var isEmpty: Bool {
6-
self is EmptyView || (self as? Orbit.Icon)?.isEmpty == true || (self as? Orbit.Text)?.isEmpty == true
6+
switch self {
7+
case let view as PotentiallyEmptyView: return view.isEmpty
8+
case is EmptyView: return true
9+
default: return false
10+
}
711
}
812
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
/// A type that represents views that can optionally result in `EmptyView`.
3+
protocol PotentiallyEmptyView {
4+
var isEmpty: Bool { get }
5+
}

0 commit comments

Comments
 (0)