Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

region-code only country initializer #8

Closed
Rashidium opened this issue Dec 26, 2021 · 2 comments · Fixed by #9
Closed

region-code only country initializer #8

Rashidium opened this issue Dec 26, 2021 · 2 comments · Fixed by #9
Assignees

Comments

@Rashidium
Copy link
Contributor

@Rashidium What I meant was the set a default selected country for the picker.

Right now, I have set selectedCountry to TR which sets a green background in the picker view but this is not enough to pre-select the value.

Simulator.Screen.Recording.-.iPhone.11.Pro.Max.-.2021-12-26.at.13.33.06.mp4
import SwiftUI
import CountryPicker

struct ContentView: View {
    @State private var country: Country?
    @State private var showCountryPicker = false
    
    var body: some View {
        VStack {
            Button {
                showCountryPicker = true
            } label: {
                Text("Select Country")
            }.sheet(isPresented: $showCountryPicker) {
                CountryPicker(country: $country)
            }
            
            Text("Selected Country: \(country?.localizedName ?? "N/A")")
        }
    }
}

Since with iOS, I can only get the Locale region, I thought it would be better to have a initializer in Country struct to set this default value.

public init(regionCode: String) {
        self.isoCode = regionCode
        
        if let country = CountryManager.shared.getCountries().first(where: { $0.isoCode == regionCode }) {
            self.phoneCode = country.phoneCode
        } else {
            self.phoneCode = ""
        }
    }

This way I can set @State private var country = Country(regionCode: Locale.current.regionCode ?? "TR")

Originally posted by @hcancelik in #6 (comment)

@Rashidium
Copy link
Contributor Author

@karazeynep @osmanavnikocoglu guys need your support for the issue 🤟🏻

@Rashidium
Copy link
Contributor Author

@karazeynep @osmanavnikocoglu 👀

@Rashidium Rashidium linked a pull request Feb 28, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants