The Fluxbit::Form::TelephoneComponent is a specialized telephone input component that extends Fluxbit::Form::TextFieldComponent. It provides a telephone number input with an integrated country code selector that displays country flags and international dialing codes. The component includes automatic phone number masking that adapts to the selected countryโs phone format.
Attention: This component extends TextFieldComponent and inherits all of its functionality.
To start using the telephone field you can use the default way to call the component:
<%= render Fluxbit::Form::TelephoneComponent.new(name: "phone", label: "Phone Number") %>
<!-- or -->
<%= render Fluxbit::Form::TelephoneComponent.new(name: "phone", label: "Phone Number") do %>
<% end %>
The result is:
Features
- Country Selector: Dropdown with country flags and dial codes (๐ง๐ท +55, ๐บ๐ธ +1, etc.)
- Automatic Masking: Phone numbers are automatically formatted based on the selected country
- 15 Countries Supported: Brazil, USA, Canada, UK, Germany, France, Spain, Italy, Portugal, Argentina, Mexico, Japan, China, India, and Australia
- Responsive Sizing: Three size options (Small, Medium, Large) with consistent styling
- Form Builder Support: Works seamlessly with Rails form builders
- Validation States: Supports all validation colors (success, danger, warning, info)
Options
| Param | Default | Description |
|---|---|---|
| name: | ย | Field name (required unless using a form builder) |
| label: | ย | Text label above the field |
| value: | ย | Value for the phone number input |
| placeholder: | ย | Placeholder text shown when empty |
| default_country: | โBRโ | Default country code (ISO 3166-1 alpha-2: โBRโ, โUSโ, โGBโ, etc.) |
| country: | ย | Attribute name for the country field when using form builder (e.g., :phone_country) |
| country_field_name: | ย | Custom name for the country select field (standalone mode, defaults to {name}_country) |
| color: | :default | State: :default, :success, :danger, :warning, :info |
| help_text: | ย | Help or error text below the field |
| helper_popover: | ย | Content for a popover helper |
| sizing: | 1 | Field size: 0 (Small), 1 (Medium), 2 (Large) |
| disabled: | false | Disables both the country select and phone input |
| readonly: | false | Makes the input readonly |
| required: | false | Marks the field as required |
| wrapper_html: | {} | Additional HTML attributes for the wrapper div |
| **props | ย | Any other HTML attribute for the <input> |
Supported Countries
The component supports 31 countries by default, with their respective masks:
Latin America (19 countries):
- ๐ง๐ท Brazil (+55):
(##) #####-#### - ๐ฆ๐ท Argentina (+54):
## ####-#### - ๐ฒ๐ฝ Mexico (+52):
## #### #### - ๐จ๐ด Colombia (+57):
### ### #### - ๐จ๐ฑ Chile (+56):
# #### #### - ๐ต๐ช Peru (+51):
### ### ### - ๐ป๐ช Venezuela (+58):
###-###-#### - ๐ช๐จ Ecuador (+593):
## ### #### - ๐ง๐ด Bolivia (+591):
# ### #### - ๐ต๐พ Paraguay (+595):
### ### ### - ๐บ๐พ Uruguay (+598):
# ### ## ## - ๐จ๐ท Costa Rica (+506):
#### #### - ๐ต๐ฆ Panama (+507):
####-#### - ๐จ๐บ Cuba (+53):
# ### #### - ๐ฉ๐ด Dominican Republic (+1):
(###) ###-#### - ๐ฌ๐น Guatemala (+502):
#### #### - ๐ญ๐ณ Honduras (+504):
####-#### - ๐ธ๐ป El Salvador (+503):
####-#### - ๐ณ๐ฎ Nicaragua (+505):
#### ####
Other Regions (12 countries):
- ๐บ๐ธ United States (+1):
(###) ###-#### - ๐จ๐ฆ Canada (+1):
(###) ###-#### - ๐ช๐ธ Spain (+34):
### ## ## ## - ๐ต๐น Portugal (+351):
### ### ### - ๐ฌ๐ง United Kingdom (+44):
#### ### #### - ๐ฉ๐ช Germany (+49):
### ######### - ๐ซ๐ท France (+33):
# ## ## ## ## - ๐ฎ๐น Italy (+39):
### ### #### - ๐ฏ๐ต Japan (+81):
##-####-#### - ๐จ๐ณ China (+86):
### #### #### - ๐ฎ๐ณ India (+91):
##### ##### - ๐ฆ๐บ Australia (+61):
### ### ###
Slots
This component does not define any named slots. The field content is determined by the form data and input value.
Examples
Basic telephone field
Different countries
Validation states
Different sizes
Disabled and readonly
Required field
With form builder
Usage with Forms
Standalone (without form builder)
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
label: "Phone Number",
default_country: "US",
placeholder: "Enter your phone number",
help_text: "We'll never share your phone number"
) %>
With Rails form builder
<%= form_with model: @user do |form| %>
<%= render Fluxbit::Form::TelephoneComponent.new(
form: form,
attribute: :phone,
label: "Contact Phone",
default_country: "BR",
required: true
) %>
<% end %>
With form builder and separate country attribute
When you want the country code stored in a separate database column:
<%= form_with model: @user do |form| %>
<%= form.fx_telephone :phone,
country: :phone_country,
label: "Contact Phone",
required: true %>
<% end %>
This will create two fields:
user[phone]- The phone numberuser[phone_country]- The country code (e.g., โBRโ, โUSโ)
With validation errors
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
label: "Phone Number",
value: @user.phone,
color: @user.errors[:phone].any? ? :danger : :default,
help_text: @user.errors[:phone].first
) %>
JavaScript Behavior
The component uses the fx-telephone Stimulus controller to handle:
- Automatic masking: Phone numbers are formatted as you type based on the selected country
- Dynamic mask updates: Changing the country automatically updates the mask and reformats the number
- Smart backspace: Handles deletion of mask characters intelligently
- Number-only input: Only numeric characters are accepted
The mask uses # as a placeholder for digits. For example, the Brazilian mask (##) #####-#### formats 11999998888 as (11) 99999-8888.
Customization
Custom country field name
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
country_field_name: "phone_country_code",
label: "Phone Number"
) %>
With specific sizing
<!-- Small -->
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
sizing: 0
) %>
<!-- Medium (default) -->
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
sizing: 1
) %>
<!-- Large -->
<%= render Fluxbit::Form::TelephoneComponent.new(
name: "phone",
sizing: 2
) %>
Configuration
The component can be configured globally through an initializer. All settings have sensible defaults but can be customized to fit your needs.
Available Configuration Options
Create or edit config/initializers/fluxbit.rb:
# Configure TelephoneComponent defaults
Fluxbit::Config::Form::TelephoneComponent.default_country = "US"
Fluxbit::Config::Form::TelephoneComponent.default_sizing = 1
# Add custom countries
Fluxbit::Config::Form::TelephoneComponent.countries << {
code: "NZ",
name: "New Zealand",
dial_code: "+64",
flag: "๐ณ๐ฟ",
mask: "## ### ####"
}
# Replace all countries with your own list
Fluxbit::Config::Form::TelephoneComponent.countries = [
{ code: "BR", name: "Brasil", dial_code: "+55", flag: "๐ง๐ท", mask: "(##) #####-####" },
{ code: "US", name: "USA", dial_code: "+1", flag: "๐บ๐ธ", mask: "(###) ###-####" }
# ... your countries
]
# Customize styles
Fluxbit::Config::Form::TelephoneComponent.styles[:country_select][:width] = "w-32"
Configuration Reference
| Option | Type | Default | Description |
|---|---|---|---|
default_country | String | "BR" | Default country code (ISO 3166-1 alpha-2) |
default_sizing | Integer | 1 | Default size: 0 (Small), 1 (Medium), 2 (Large) |
countries | Array | 31 countries | List of supported countries with masks |
styles | Hash | Tailwind classes | Styling configuration for select and input |
Country Object Structure
Each country in the countries array should have:
{
code: "BR", # ISO 3166-1 alpha-2 country code
name: "Brasil", # Country name
dial_code: "+55", # International dial code
flag: "๐ง๐ท", # Country flag emoji
mask: "(##) #####-####" # Phone number mask (# = digit)
}
Customizing Styles
You can customize the appearance by modifying the styles configuration:
# Change select width
Fluxbit::Config::Form::TelephoneComponent.styles[:country_select][:width] = "w-32"
# Customize colors
Fluxbit::Config::Form::TelephoneComponent.styles[:country_select][:colors][:default] =
"text-blue-900 bg-blue-50 border-blue-300"
# Adjust sizes
Fluxbit::Config::Form::TelephoneComponent.styles[:country_select][:sizes][1] = {
padding: "p-3",
text: "text-base"
}
Notes
- The component automatically forces the input type to
:telfor better mobile experience - The default sizing is 1 (Medium) instead of 0 (Small) for better usability
- Country selection and phone input are submitted as separate fields
- The mask is applied client-side using Stimulus; server-side validation should handle raw numbers
- All TextFieldComponent options (except
type,icon,addon,multiline) are supported - All configuration is optional - the component works with sensible defaults out of the box