import SwiftUI

struct IntegerField: View {

    private let titleKey: LocalizedStringKey

    @Binding
    private var number: Int

    @State
    private var text: String = ""

    init(_ titleKey: LocalizedStringKey, number: Binding<Int>) {
        self.titleKey = titleKey
        self._number = number
    }

    var body: some View {
        TextField(titleKey, text: $text)
            .onChange(of: text) { _, newValue in
                if let intValue = Int(newValue) {
                    number = intValue
                } else {
                    text = "\(number)"
                }
            }
            .onAppear {
                text = "\(number)"
            }
    }
}
