Hi Apple-Team,
the format of a text field, in my case a percentage with one decimal place, is not applied when a button is pressed and the view is exited using dismiss. It is only used when another field receives focus. A button is not a focusable object, and this is a problem. This allows you to specify more decimal places, resulting in a saved value with unwanted decimal places.
It's even worse when you specify integer limits and the value exceeds the limit.
Do you always have to check the value before saving it? What's the point of having a format then?
Some example images:
The items in the list have a fraction limit of 8.
The field has integer limit 2 and 1 fraction.
When focusing a different field the format is applied.
Here the code I used:
struct Item: Identifiable, Hashable {
var id: Int
var value: Double
}
struct WrongFractionsListView: View {
@State private var items: [Item] = [
Item(id: 1, value: 0.225),
Item(id: 2, value: 0.377),
Item(id: 3, value: 0.241)]
enum ItemTransfer: Hashable {
case new
}
var body: some View {
List {
ForEach(items) { item in
HStack {
Text("\(item.id)")
Spacer()
Text("\(item.value, format: .percent.precision(.fractionLength(8)))")
}
}
}
.navigationDestination(for: ItemTransfer.self) { _ in
WrongFractionsEditorView(items: $items)
}
.toolbar{
ToolbarItem(placement: .topBarTrailing) {
NavigationLink(value: ItemTransfer.new) {
Image(systemName: "plus")
}
}
}
}
}
#Preview {
NavigationStack {
WrongFractionsListView()
}
}
struct WrongFractionsEditorView: View {
@Environment(\.dismiss) var dismiss
@State private var percentValue: Double = 0
@State private var testValue: String = ""
@Binding var items: [Item]
var body: some View {
Form {
TextField("(%)", value: $percentValue, format: .percent.precision(.integerAndFractionLength(integerLimits: 0...2, fractionLimits: 0...1)))
.keyboardType(.decimalPad)
TextField("Just for Focus", text: $testValue)
}
.navigationTitle("Percent Fractions")
.toolbar{
ToolbarItem(placement: .topBarTrailing) {
Button {
saveAndClose()
} label: {
Image(systemName: "checkmark")
}
}
}
}
private func saveAndClose() {
let max = items.max { $0.id < $1.id}!.id
let item: Item = .init(id: max+1, value: percentValue)
items.append(item)
dismiss()
}
}
#Preview {
@Previewable @State var items: [Item] = [
Item(id: 1, value: 0.225),
Item(id: 2, value: 0.377),
Item(id: 3, value: 0.241)]
NavigationStack {
WrongFractionsEditorView(items: $items)
}
}
How can I fix this?
Thank you
Christian