PhoneInput
HkPhoneInput is the phone-number field with a country dial-code chip riding its left edge. The chip opens a searchable country list, the number itself is the national part only, and the field sanitizes what it accepts: digits, spaces and dashes survive, everything else (a pasted +86, parentheses, stray letters) is dropped. Blurring the field emits the composed E.164 form.
- For an address, an ID or any other free text, use HkInput.
- For a six-digit verification code sent to a phone, use HkOtpInput.
- If the country is fixed by the deployment, HkInput with a +86 prefix is enough.
Import
;
use *; // PhoneInput, DialCodeEntry, DIAL_CODES
use *; // rsx!, VNode
The Rust form is a plain function, PhoneInput(PhoneInputProps { … }). Its country catalog is DIAL_CODES unless countries replaces it, and its chip label is dial_code.
Form rows
modelValue is the national number only — never the dial code — so a stored value stays valid when the country changes. dialCode accepts any shape ("+86", "86", "0086") and is canonicalized only for matching, so a v-model round-trip keeps the caller's spelling. label, hint and error come straight from HkInput, whose shell this field nests.
A code arrives by SMS.
That number is not reachable.
Sizes, readonly and disabled
size rides the nested field: sm / md / lg. disabled and readonly reach both the field and the chip — a read-only receiver shows a number that must be copied verbatim, and its country cannot be switched either.
Copied from the order.
The country catalog
countries scopes the list — a mainland-only deployment passes a single row, and the bundled catalog (DIAL_CODES, China first) is the default. The chip shows the resolved country's canonical +… code plus a caret; picking a row emits the dial code, emits the composed change and, in Vue, refocuses the number field so pick-then-type needs no second tap. The Rust mirror cannot move focus, so it reports the request through the controlled open / on_open_change pair instead.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| modelValue | String | "" | — |
| dialCode | String | "+86" | — |
| countries | Array as PropType<readonly DialCodeEntry[]> | () => DIAL_CODES | — |
| placeholder | String | "" | — |
| disabled | Boolean | false | — |
| readonly | Boolean | false | — |
| size | <code>String as PropType<"sm" | "md" | "lg"></code> | "md" | — |
| label | String | undefined | — |
| error | String | undefined | — |
| hint | String | undefined | — |
| required | Boolean | false | — |
| name | String | undefined | — |
| submitOnEnter | Function as PropType<() => void> | undefined | — |
Emits
| Event | Payload |
|---|---|
| update:modelValue | string |
| update:dialCode | string |
| dialchange | string |
| change | string |
| focus | FocusEvent |
| blur | FocusEvent |
Rust
Props — PhoneInput
| Prop | Type | Default | Description |
|---|---|---|---|
| model_value | String | "" | The national number only — never the dial code (Vue: modelValue). |
| dial_code | String | "+86".to_string() | Dial code in any shape; kept as given, canonicalized only for matching (Vue: dialCode, default "+86"). |
| countries | Option<Vec<DialCodeEntry>> | None | Country catalog; None uses the bundled DIAL_CODES (Vue: countries, defaulting to the same table). |
| placeholder | Option<String> | None | Native placeholder for the number field. |
| disabled | bool | false | — |
| readonly | bool | false | — |
| size | InputSize | InputSize::Medium | Field size step, carried into the nested HkInput. |
| label | Option<String> | None | Caption above the box. |
| error | Option<String> | None | Error ink + message line. |
| hint | Option<String> | None | Secondary message line, shown only when there is no error. |
| required | bool | false | — |
| name | Option<String> | None | Native name for the number field. |
| submit_on_enter | Option<EventHandler<()>> | None | Submit intent for Enter inside the number field. |
| open | bool | false | The picker's disclosure state (Vue owns this internally; the mirror is controlled — see the header delta). |
| on_open_change | Option<EventHandler<bool>> | None | Fired with the requested disclosure state when the chip is pressed. |
| on_input | Option<EventHandler<String>> | None | Vue's update:modelValue: the sanitized national number. |
| on_dial_change | Option<EventHandler<String>> | None | Vue's update:dialCode + dialchange (same payload, collapsed): the canonical "+86" shape. |
| on_change | Option<EventHandler<String>> | None | Vue's change: the composed E.164 on blur ("" when the number is empty) — validate or submit without recomputing phone_format_e164. |
| on_focus | Option<EventHandler<FocusEvent>> | None | Vue's focus emit. |
| on_blur | Option<EventHandler<FocusEvent>> | None | Vue's blur emit. |
| class | String | "" | Host classes, appended to the root wrapper. |
| style | String | "" | Host style, applied to the root wrapper. |
Accessibility
- The chip is a real <button type="button"> that publishes aria-haspopup="listbox" and aria-expanded, and it is named by the country and the localized "Choose country code" — the visible text is only the code. In the Rust mirror the panel is an inline listbox (role="listbox" with one role="option" per row, each carrying aria-selected); the Vue form opens the shared affix picker instead.
- The number field is the nested HkInput, so it keeps that field's caption binding, error / hint line and Enter handling. In Vue the field also carries inputmode="tel" (raising the dial pad on touch devices) and maxlength="18"; the Rust mirror leaves both to the host.
- readonly disables the chip as well as the field: a number that cannot be edited must not be re-prefixed from a different country.
- The composed E.164 arrives on change on blur — validate or submit from that payload instead of re-deriving it, and keep the national value for your storage.
- Scoping too aggressively has a cost: a field limited to mainland China silently rejects a +1 number a user pastes in. Keep every dispatch path phone-capable.
Related components
- HkInput — the field shell this control nests.
- HkSearchInput — the bare search field with a clear button.
- HkNumberInput — the numeric field with a stepper column.
- HkOtpInput — the one-time code the SMS delivers.