You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClub/PadelClub/Views/Components/StepperView.swift

90 lines
2.3 KiB

//
// StepperView.swift
// PadelClub
//
// Created by Laurent Morvillier on 14/02/2024.
//
import SwiftUI
struct StepperView: View {
var title: String? = nil
@Binding var count: Int
var minimum: Int? = nil
var maximum: Int? = nil
var body: some View {
VStack(spacing: 0) {
HStack(spacing: 16) {
Button(action: {
self._subtract()
}, label: {
Image(systemName: "minus.circle")
.resizable()
.scaledToFit()
.frame(width: 24)
})
.disabled(_minusIsDisabled())
.buttonStyle(.borderless)
TextField("00", value: $count, format: .number)
.keyboardType(.numberPad)
.fixedSize()
.font(.title2)
.monospacedDigit()
.onSubmit {
if let minimum, count < minimum {
count = minimum
} else if let maximum, count > maximum {
count = maximum
}
}
Button(action: {
self._add()
}, label: {
Image(systemName: "plus.circle")
.resizable()
.scaledToFit()
.frame(width: 24)
})
.disabled(_plusIsDisabled())
.buttonStyle(.borderless)
}
if let title {
Text(title + count.pluralSuffix).font(.caption)
}
}
.multilineTextAlignment(.trailing)
}
fileprivate func _minusIsDisabled() -> Bool {
count <= (minimum ?? 0)
}
fileprivate func _plusIsDisabled() -> Bool {
count >= (maximum ?? 100_000)
}
fileprivate func _add() {
if let maximum, self.count + 1 > maximum {
return
}
self.count += 1
}
fileprivate func _subtract() {
if let minimum, self.count - 1 < minimum {
return
}
self.count -= 1
}
}
#Preview {
StepperView(title: "poule", count: .constant(1))
}