Anchor Design System
Form

Phone3.0

The Phone Input is meant to let the user input their phone number, and letting them chose a dial code by the name of their country or flag.

Installation

npm i @tryg/ui-library

Usage

Individual only imports the desired component, and only needs to be included once within the application. Which is beneficial to keep the application size small.

Webcomponent
<script type="module">
  import { defineCustomElement } from '@tryg/ui-library/dist/components/anchor-phone';
  import { defaultCountriesForPhone } from '@tryg/ui-library';
  defineCustomElement();
  document.querySelector('anchor-phone').setAttribute('options', JSON.stringify(defaultCountriesForPhone));
</script>
<anchor-phone>Your phone number</anchor-phone>

Global only needs to be defined once in the application, but it will import all components even the ones that are not in use..

Webcomponent
<script type="module">
  import { defineCustomElements } from '@tryg/ui-library/dist/loader';
  import { defaultCountriesForPhone } from '@tryg/ui-library';
  defineCustomElements();
  document.querySelector('anchor-phone').setAttribute('options', JSON.stringify(defaultCountriesForPhone));
</script>
<anchor-phone>Your phone number</anchor-phone>

With value

  <anchor-phone value="12121212">Your phone number</anchor-phone>

Denmark first

  <script type="module">
    document.querySelector('anchor-phone').setAttribute('options', JSON.stringify(rearrangeArrayByCountrycode(defaultCountriesForPhone, 'dk')));
  </script>
  <anchor-phone value="12121212">Your phone number</anchor-phone>

Norway first

  <script type="module">
    document.querySelector('anchor-phone').setAttribute('options', JSON.stringify(rearrangeArrayByCountrycode(defaultCountriesForPhone, 'no')));
  </script>
  <anchor-phone value="12121212">Your phone number</anchor-phone>

Disabled

  <anchor-phone value="12121212" disabled>Your phone number</anchor-phone>

Filled

  <anchor-phone value="12121212" filled>Your phone number</anchor-phone>

Placeholder

  <anchor-phone placeholder="Write your magic numbers">Your phone number</anchor-phone>

Pattern

  <anchor-phone pattern="[A-Za-z]{3}"">Your phone number</anchor-phone>

With error

  <anchor-phone error="You didn't write your magic numbers">Your phone number</anchor-phone>

Help text

  <anchor-phone help-text="Write your magic numbers">Your phone number</anchor-phone>

Country list details

Introduction

To populate the Phone component with a list of countries to chose from, you need to provide the component with an array containing an object for each country. Remember to stringify the array

You can also use the default list of countries and their data that Anchor provides.

import { defaultCountriesForPhone } from '@tryg/ui-library';
document.querySelector('anchor-phone').setAttribute('options', JSON.stringify(defaultCountriesForPhone));

The array of countries needs to follow this format:

[
  {
    "name": string,
    "iso2": string,
    "dialCode": number,
    "priority": number,
    "areaCodes": [number]
  }
]

Manipulate country list

If you want to make sure that a certain country is the first choice without writing your own array of countries, you can do something like this:

function rearrangeArrayByCountrycode(array, country) {
  const newArray = [...array];
  const foundIndex = newArray.findIndex(obj => obj.iso2 === country);

  if (foundIndex > -1) {
    const removedArray = newArray.splice(foundIndex, 1)[0];
    newArray.unshift(removedArray);
  }

  return newArray;
}

The array in this example is the defaultCountriesForPhone array. The country is the iso2 code for a given country.

Pass the re-arranged array in as options for the Phone component like this:

<anchor-phone options={rearrangeArrayByCountrycode(defaultCountriesForPhone, 'dk')}></anchor-phone>

API

anchor-phone

Properties

PropertyAttributeDescriptionTypeDefault
ariaCountrySelectLabelaria-country-select-labelText to be displayed if when value is missing an native form validation is usedstring'Select country'
disableddisabledDisable the number fieldbooleanfalse
errorerrorShow an error messagestring''
filledfilledShow a checkmark icon when the input field has been succesfully filledbooleanfalse
helpTexthelp-textShow a help textstring''
labellabelShow the label (you can't currently disable the label)booleantrue
labelPlacementlabel-placementPlacement of the label"left" | "top"'top'
maxLengthmax-lengthSet a max width in characters for the number field.numberundefined
namenameThe name of the number fieldstring''
optionsoptionsPass an array of countries to the phone fieldCountry[] | stringundefined
patternpatternstringnull
placeholderplaceholderPlaceholder text is shown until the user interacts with the number fieldstring''
readonlyreadonlySet the field to be read onlybooleanfalse
requiredrequiredMark the field as requiredbooleanfalse
selectedCountryselected-countrynumber0
sizesizeSet a fixed width in characters for the number field.numbernull
spinnersspinners"big" | "none" | "small"'none'
tooltipTexttooltip-textAdd an info icon with a tooltip to the right of the labelstring''
uuiduuidGive this instance of phone a custom ID, or keep the default.stringuuidv4()
validityMissingValueTextvalidity-missing-value-textText to be displayed if when value is missing an native form validation is usedstringundefined
valuevalueThe value in the number field. Use this to set a valuestring''
warningwarningShow a warning messagestring''

Events

EventDescriptionType
blureventEvent emitted when the user blurs the input fieldCustomEvent<any>
cleareventEvent emitted when the user clears the input fieldCustomEvent<any>
countrySelectedEvent emitted when a country is selectedCustomEvent<any>
focuseventEvent emitted when the user is focusing the input fieldCustomEvent<any>
inputChangeEvent emitted when user inputs something into the fieldCustomEvent<EventDetail>
inputeventEvent emitted when the user is inputting something into the input fieldCustomEvent<EventDetail>
phoneNumberInvalidEvent emitted when the input does not match the patternCustomEvent<any>
phoneNumberValidEvent emitted when the input matches the patternCustomEvent<boolean>

Methods

checkValidPhoneNumber(value: any) => Promise<CustomEvent<any>>

Parameters

NameTypeDescription
valueany

Returns

Type: Promise<CustomEvent<any>>

CSS Custom Properties

NameDescription
--phone-input-border-colorSet the border color of the phone input.
--phone-input-border-color-errorSet the border color of the phone input for error states.
--phone-input-border-color-hoverSet the border color of the phone input on hover.
--phone-input-border-widthSet the border width of the phone input.
--phone-input-line-heightSet the line height of the phone input.
--phone-input-list-countries-lengthSet the number of countries shown in the list when it is open.
--phone-input-list-heightSet the height of the list in the phone input.
--phone-input-margin-bottomSet the bottom margin of the phone input.
--phone-input-margin-leftSet the left margin of the phone input.
--phone-input-margin-rightSet the right margin of the phone input.
--phone-input-margin-topSet the top margin of the phone input.
--phone-input-max-widthSet the max width of the phone input.
--phone-input-padding-bottomSet the bottom padding of the phone input.
--phone-input-padding-leftSet the left padding of the phone input.
--phone-input-padding-rightSet the right padding of the phone input.
--phone-input-padding-topSet the top padding of the phone input.
--phone-input-widthSet the width of the phone input.
--room-for-selectGive the Input extra padding to make room for the Select.

Dependencies

Depends on

Graph


Built with StencilJS

On this page