LivePhone (live_phone v0.4.0) View Source

LivePhone

A Phoenix LiveView Component for phone number input fields, basically a intl-tel-input for Phoenix LiveView.

Based on ISO and ex_phone_number, which in turn is based on libphonenumber.

Installation

If available in Hex, the package can be installed by adding live_phone to your list of dependencies in mix.exs:

def deps do
  [
    {:live_phone, "~> 0.4"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/live_phone.

To your assets/package.json file add:

"live_phone": "file:../deps/live_phone",

To your app.js add something like:

import LivePhone from "live_phone"
let Hooks = {}
Hooks.LivePhone = LivePhone

And finally to your CSS add:

@import "../../deps/live_phone/assets/live_phone";

Example

In the example/ directory you will find a very minimal Phoenix application to demonstrate LivePhone in usage.

Browser Tests (chromedriver)

To run the browser tests you need to install chromedriver (brew install chromedriver on MacOS) and it has to be running already. The tests are excluded by default, but you can include them with --include browser. See below:

$ chromedriver # hound assumes default port 9515)
$ mix test --include browser

Link to this section Summary

Functions

Parses the given country_code into an emoji, but I should note that the emoji is not validated so it might return an invalid emoji (this will also depend on the unicode version supported by your operating system, and which flags are included.)

This is used to try and get a Country for a given phone number.

This is used to verify a given phone number and see if it is a valid number according to ExPhoneNumber.

This is used to normalize a given phone number to E.164 format, and returns a tuple with {:ok, formatted_phone} for valid numbers and {:error, unformatted_phone} for invalid numbers.

This is used to normalize a given phone number to E.164 format, and immediately return the value whether it is formatted or not.

Link to this section Functions

Link to this function

emoji_for_country(country_code)

View Source

Specs

emoji_for_country(String.t() | nil) :: String.t()

Parses the given country_code into an emoji, but I should note that the emoji is not validated so it might return an invalid emoji (this will also depend on the unicode version supported by your operating system, and which flags are included.)

Examples

iex> LivePhone.emoji_for_country(nil)
""

iex> LivePhone.emoji_for_country("US")
"πŸ‡ΊπŸ‡Έ"

Specs

get_country(String.t()) ::
  {:ok, LivePhone.Countries.Country.t()} | {:error, :invalid_number}

This is used to try and get a Country for a given phone number.

Examples

iex> LivePhone.get_country("")
{:error, :invalid_number}

iex> LivePhone.get_country("+1555")
{:error, :invalid_number}

iex> LivePhone.get_country("+1555")
{:error, :invalid_number}

iex> LivePhone.get_country("+1 (555) 555-1234")
{:error, :invalid_number}

iex> LivePhone.get_country("+1 (555) 555-1234")
{:error, :invalid_number}

iex> LivePhone.get_country("+1 (650) 253-0000")
{:ok, %LivePhone.Country{code: "US", flag_emoji: "πŸ‡ΊπŸ‡Έ", name: "United States of America (the)", preferred: false, region_code: "1"}}

iex> LivePhone.get_country("+16502530000")
{:ok, %LivePhone.Country{code: "US", flag_emoji: "πŸ‡ΊπŸ‡Έ", name: "United States of America (the)", preferred: false, region_code: "1"}}

Specs

is_valid?(String.t()) :: boolean()

This is used to verify a given phone number and see if it is a valid number according to ExPhoneNumber.

Examples

iex> LivePhone.is_valid?("")
false

iex> LivePhone.is_valid?("+1555")
false

iex> LivePhone.is_valid?("+1555")
false

iex> LivePhone.is_valid?("+1 (555) 555-1234")
false

iex> LivePhone.is_valid?("+1 (555) 555-1234")
false

iex> LivePhone.is_valid?("+1 (650) 253-0000")
true

iex> LivePhone.is_valid?("+16502530000")
true
Link to this function

normalize(phone, country)

View Source

Specs

normalize(String.t(), String.t()) :: {:ok, String.t()} | {:error, String.t()}

This is used to normalize a given phone number to E.164 format, and returns a tuple with {:ok, formatted_phone} for valid numbers and {:error, unformatted_phone} for invalid numbers.

Examples

iex> LivePhone.normalize("1234", nil)
{:error, "1234"}

iex> LivePhone.normalize("+1 (650) 253-0000", "US")
{:ok, "+16502530000"}
Link to this function

normalize!(phone, country)

View Source

Specs

normalize!(String.t(), String.t()) :: String.t()

This is used to normalize a given phone number to E.164 format, and immediately return the value whether it is formatted or not.

Examples

iex> LivePhone.normalize!("1234", nil)
"1234"

iex> LivePhone.normalize!("+1 (650) 253-0000", "US")
"+16502530000"