Skip to content

SwiftUI CurrencyFormater binding

Latest
Compare
Choose a tag to compare
@marinofelipe marinofelipe released this 09 Jun 19:26
· 14 commits to main since this release
32c6f68

Breaking

Change CurrencyTextFieldConfiguration.formatter to be a @Binding property, which aims to propagate state changes when the formatter is updated, like on formatter.currency changes.

When bumping to this release, users will have to update their CurrencyTextField usages passing a projected value for their formatters, prefixed with $.

CurrencyTextField(
    configuration: .init(
    // ...
    formatter: $currencyFormatter

In order to update the bound formatter, it must be re-initialized with the desired properties, since the view update is only triggered when the Binding changes, in that case the formatter instance itself:

CurrencyTextField(
Picker(
    "Change currency",
    selection: $currency
) {
    ForEach(
       [
           Currency.euro,
           Currency.dollar,
           Currency.brazilianReal,
           Currency.yen
       ],
       id: \.self
    ) {
        Text($0.rawValue).tag($0)
    }
}
.pickerStyle(.segmented)
.onChange(of: currency) { newValue in
     currencyFormatter = .init {
         $0.currency = newValue
         // other props
     }
}

For a full example, one case check the SwiftUIExampleView.swift on the Example app target, available in the CurrencyText.xcworkspace.