MojtabaHs / iPhoneNumberField

Elegant SwiftUI phone number textField.
MIT License
542 stars 88 forks source link

PhoneNumberKit as init parameter #104

Open thedemonswithin opened 5 months ago

thedemonswithin commented 5 months ago

Passing PhoneNumberKit via init param, this can be useful in some cases. Also updated min version of PNK.

MojtabaHs commented 5 months ago

Thank you for your contribution! Generally, it's not a good practice to expose the initializer of the reference type, outside of the Representable. Can you please suggest some simple use-case examples of this change which is not achievable with the current API?

thedemonswithin commented 5 months ago

In my use case I need to restrict country code picker for only few countries, that allowed by rest api. This api provides only number prefixes (7, 20, 41, etc). I can import PhoneNumberKit and create another one instance to get country codes by their phone prefix. But as been stated in marmelroy's readme:

A PhoneNumberKit instance is relatively expensive to allocate (it parses the metadata and keeps it in memory for the object's lifecycle), you should try and make sure PhoneNumberKit is allocated once and deallocated when no longer needed.

And i'd prefer to create it once, pass to the view and use for another purposes, described before. Screenshot for example

Screenshot 2024-05-29 at 15 05 42
MojtabaHs commented 5 months ago

The issue is about keeping the reference in the UIViewRepresentable, How about this instead:

iPhoneNumberField(text: $text) {
    $0.phoneNumberKit = myCustomKit // 👈 Pass it in here
}

You can also customize the $0.phoneNumberKit directly without any need to pass in a custom kit.