youjinp / SwiftUIKit

A collection of missing SwiftUI components
MIT License
278 stars 32 forks source link

CurrencyTextField not showing decimals onChange #9

Closed LualdiD closed 4 years ago

LualdiD commented 4 years ago

I'm trying to update CurrencyTextField based on another textfield but it doesn't seems to work well. The problem I'm facing is when I updated hourlySalary to a decimal like 0.02 it always round to 0

Also the button in this view show the decimals in hourlySalary just on the initial state, if I start inputting something in any textFields then it just jumps from 2.0 to 0.0 without showing decimals

` import SwiftUI

struct ContentView: View {

@State var hourlySalary: Double? = 1.0
@State var montlhySalary: Double? = 0.0
@State var yearlySalary: Double? = 0.0

@State var test: Bool = false

var body: some View {
    ScrollView {
        VStack(alignment: .leading, spacing: 20) {
            Text("Hourly")
            CurrencyTextField(currencyString(amount: 0), value: $hourlySalary)
                .font(.title)

            Text("Monthly")
            CurrencyTextField(currencyString(amount: 0), value: $montlhySalary)
                .font(.title)

            Text("Yearly")
            CurrencyTextField(currencyString(amount: 0), value: $yearlySalary, tag: 10)
                .font(.title)

            Button(action: {
                if test {
                    hourlySalary = 0.5
                } else {
                    hourlySalary = 2
                }
                test.toggle()
            }, label: {
                Text("Button")
            })

        }
        .onChange(of: hourlySalary ?? 0, perform: { value in
            let hourly = value
            let monthly = (hourly * 8) * 20
            let yearly = monthly * 12
            montlhySalary = monthly
            yearlySalary = yearly
        })
        .onChange(of: montlhySalary ?? 0, perform: { value in
            let monthly = value
            let hourly = (monthly / 20) / 8
            let yearly = monthly * 12
            hourlySalary = hourly
            yearlySalary = yearly
        })
        .onChange(of: yearlySalary ?? 0, perform: { value in
            let yearly = value
            let monthly = yearly / 12
            let hourly = (monthly / 20) / 8
            hourlySalary = hourly
            montlhySalary = monthly
        })
        .padding()
        .navigationTitle("What's your salary?")
    }
}

`

youjinp commented 4 years ago

I've just tested with Xcode 12 beta 6 and iOS 14 beta 6.

Screenshot

Am I seeing something different from what you saw?

LualdiD commented 4 years ago

This is what I'm seeing:

Screen-Recording-2020-09-06-at-2

At the end of the gif I make it work if hourly already have a decimal. I'm not sure why the button works like that but anyway it's there just as a test. My main problem is that when I update the yearly field, the hourlySalary fields always gets round up to 0. For example in the GIF when I type 111 hourly salary become 0.0578125 but I still get 0 in the hourly field, and at 1,111 it should be 0.57 but it rounds it up to 1

youjinp commented 4 years ago

I fixed it in the latest release, thanks for letting me know about this issue!

youjinp commented 4 years ago

Heres the gif of it working properly: