youjinp / SwiftUIKit

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

How do I initialise this textField with a pre-populated value? #11

Closed PlasmaSoftUK closed 3 years ago

PlasmaSoftUK commented 3 years ago

Looking at the demo app in CurrencyTextFieldDemoView if I change the value from 0.0 to 4.5

@State private var value: Double? = 4.5

I would expect the app to launch with a value of £4.50 in the text filed however, when run, the field still only shows the Placeholder of 'Amount'. If I type nothing but click the "Print" button with it still showing the Placeholder it then prints the value it was init'd with:

Optional(4.5)

So the value is there, its just not being displayed.

The other behaviour I think is odd, is that if I set the field to say 2.1 when I dismiss the keyboard I would expect that to re-format as £2.10 however its still displays as £2.1 which is obviously an improper currency format.

Thanks Plasma

youjinp commented 3 years ago

Fixed them both. Thanks!

PlasmaSoftUK commented 3 years ago

Excellent, thank you for the quick fix.

The only other question I have is that I would like for it to always display in the $0.00 format, however if the value is a whole number it drops the decimal points. Is there an option to force it to always display in the same format. I'm looking at a list of values and it makes them look odd if some are $0 format and others are $0.00 format.

in the mean time I have changed the currencyFormat extension to this which forces it to always be $0.00 format:

        let formatter = Formatter.currency
        formatter.maximumFractionDigits = 2
        return formatter.string(from: NSNumber(value: self)) 
youjinp commented 3 years ago

Checkout the latest release, have added an option alwaysShowFractions. Also included an example in the demo!

PlasmaSoftUK commented 3 years ago

Nice thanks for the quick turn around again! 👍