gitbay/Views/Shared/RemovableChips.swift

v1.6.2
gitbay-ios/gitbay/Views/Shared/RemovableChips.swift history · blame · raw

40 lines · 1524 bytes

 1import SwiftUI
 2
 3/// A horizontal row of chips, each with a remove button: an issue's
 4/// labels and assignees, a merge request's labels. The remove button is
 5/// identified as `<idPrefix>-remove-<item>`.
 6struct RemovableChips: View {
 7
 8    let items: [String]
 9    let idPrefix: String
10    let disabled: Bool
11    var color: (String) -> Color = { _ in .secondary }
12    let remove: (String) -> Void
13
14    var body: some View {
15        if !items.isEmpty {
16            ScrollView(.horizontal, showsIndicators: false) {
17                HStack(spacing: 6) {
18                    ForEach(items, id: \.self) { item in
19                        HStack(spacing: 3) {
20                            Text(item)
21                            Button {
22                                remove(item)
23                            } label: {
24                                Image(systemName: "xmark.circle.fill")
25                                    .foregroundStyle(.tertiary)
26                            }
27                            .disabled(disabled)
28                            .accessibilityIdentifier("\(idPrefix)-remove-\(item)")
29                        }
30                        .font(.gbSans(.caption))
31                        .padding(.horizontal, 8)
32                        .padding(.vertical, 3)
33                        .background(color(item).opacity(0.07), in: gbChipShape)
34                        .overlay(gbChipShape.stroke(color(item).opacity(0.35), lineWidth: 1))
35                    }
36                }
37            }
38        }
39    }
40}