7.0.2 • Published 4 years ago

react-native-bpk-component-phone-input v7.0.2

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
4 years ago

react-native-bpk-component-phone-input

Backpack React Native telephone input component.

Installation

npm install react-native-bpk-component-phone-input --save-dev

Data format

Consumers are expected to provide the data that powers the two components available in this package.

Each supported country should be in the following format:

{ id: 'UK', dialingCode: '+44', name: 'United Kingdom' }

All keys are required and should have non-null/empty values.

For BpkDialingCodeList a list of objects with this format should be used. Sorting should be done beforehand as the component does not perform any sorting itself.

Optionally, you may supply a list of suggested ids which are your best guess at the user's country code. These codes will be shown at the top of the list under a custom title.

BpkDialingCodeList

Usage

import React, { Component } from 'react';
import { Image } from 'react-native';
import { BpkDialingCodeList } from 'react-native-bpk-component-phone-input';

const CODES = [
  { id: 'DZ', dialingCode: '+213', name: 'Algeria' },
  { id: 'CA', dialingCode: '+1', name: 'Canada' },
  { id: 'CD', dialingCode: '+243', name: 'Democratic Republic of the Congo' },
  { id: 'IT', dialingCode: '+39', name: 'Italy' },
  { id: 'JP', dialingCode: '+81', name: 'Japan' },
  { id: 'SE', dialingCode: '+46', name: 'Sweden' },
  { id: 'GB', dialingCode: '+44', name: 'United Kingdom' },
];

const FLAG_IMAGES = {
  'DZ': '/resources/algeria.png',
  'CA': '/resources/canada.png',
  'CD': '/resources/drcongo.png',
  'IT': '/resources/italy.png',
  'JP': '/resources/japan.png',
  'SE': '/resources/sweden.png',
  'GB': '/resources/uk.png',
};

const SUGGESTED = {
  ids: ['IT', 'GB'],  // The IDs must match the ones from dialingCodes
  title: 'Suggested', // The title shown above the suggested codes
};

export default class App extends Component {
  render() {
    return (
      <BpkDialingCodeList
        dialingCodes={CODES}
        selectedId="CD"
        onItemPress={code => console.log(code.id)}
        renderFlag={code => <Image source={require(FLAG_IMAGES[code.id])} />}
        suggested={SUGGESTED}
      />
    );
  }
}

Usage with search

You can combine the dialing code list with SectionList's search abilities to allow users to search the dialing code list.

A default filtering function - getFilteredDialingCodes - is available for you to use. It filters using the dialingCode and name properties. You can either use this or provide your own filtering logic if you need advanced functionality.

import React, { Component } from 'react';
import { Image } from 'react-native';
import {
  BpkDialingCodeList,
  getFilteredDialingCodes,
} from 'react-native-bpk-component-phone-input';
import {
  BpkSectionListNoResultsText,
  BpkSectionListSearchField,
} from 'react-native-bpk-component-section-list';

const CODES = [
  { id: 'DZ', dialingCode: '+213', name: 'Algeria' },
  { id: 'CA', dialingCode: '+1', name: 'Canada' },
  { id: 'CD', dialingCode: '+243', name: 'Democratic Republic of the Congo' },
  { id: 'IT', dialingCode: '+39', name: 'Italy' },
  { id: 'JP', dialingCode: '+81', name: 'Japan' },
  { id: 'SE', dialingCode: '+46', name: 'Sweden' },
  { id: 'GB', dialingCode: '+44', name: 'United Kingdom' },
];

const FLAG_IMAGES = {
  'DZ': '/resources/algeria.png',
  'CA': '/resources/canada.png',
  'CD': '/resources/drcongo.png',
  'IT': '/resources/italy.png',
  'JP': '/resources/japan.png',
  'SE': '/resources/sweden.png',
  'GB': '/resources/uk.png',
};

const SUGGESTED = {
  ids: ['IT', 'GB'],  // The IDs must match the ones from dialingCodes
  title: 'Suggested', // The title shown above the suggested codes
};

export default class App extends Component {
  constructor() {
    super();
    this.state = {
      codes: CODES,
    };
  }

  doSearch = (searchText) => {
    const codes = getFilteredDialingCodes(searchText, CODES);
    this.setState({ codes });
  }

  render() {
    return (
      <BpkDialingCodeList
        dialingCodes={this.state.codes}
        selectedId="CD"
        onItemPress={code => console.log(code.id)}
        renderFlag={code => <Image source={require(FLAG_IMAGES[code.id])} />}
        suggested={SUGGESTED}
        ListHeaderComponent={
          <BpkSectionListSearchField placeholder="Search" onChangeText={this.doSearch} />
        }
        ListEmptyComponent={
          <BpkSectionListNoResultsText>No results</BpkSectionListNoResultsText>
        }
      />
    );
  }
}

Props

Is an instance of React Native's SectionList component so it accepts the same props, as well as the following:

PropertyPropTypeRequiredDefault Value
dialingCodesarrayOf({id, dialingCode, name})true-
onItemPressfunctrue-
renderFlagfunctrue-
selectedIdstringfalsenull
suggested{ ids, title }falsenull

BpkPhoneNumberInput

Usage

import React, { Component } from 'react';
import { Image } from 'react-native';
import { BpkPhoneNumberInput } from 'react-native-bpk-component-phone-input';

const CODES = [
  { id: 'DZ', dialingCode: '+213', name: 'Algeria' },
];

const FLAG_IMAGES = {
  'DZ': '/resources/algeria.png',
};

export default class App extends Component {
  render() {
    return (
      <BpkPhoneNumberInput
        label="Phone number"
        value=""
        dialingCode={CODES[0]}
        onDialingCodePress={() => presentDialingCodeList()}
        renderFlag={code => <Image source={require(FLAG_IMAGES[code.id])} />}
      />
    );
  }
}

Props

Inherits all props from BpkTextInput except accessoryView.

PropertyPropTypeRequiredDefault Value
dialingCode{id, dialingCode, name}true-
onDialingCodePressfunctrue-
renderFlagfunctrue-

BpkFlag

Usage

import React, { Component } from 'react';
import { Image } from 'react-native';
import { BpkFlag } from 'react-native-bpk-component-phone-input';

export default class App extends Component {
  render() {
    return (
      <BpkFlag
        flag={<Image source={require('/resources/algeria.png')} />}
      />
    );
  }
}

Props

Inherits all props from View.

PropertyPropTypeRequiredDefault Value
flagelementfalsenull
widthnumberfalsespacingLg
7.0.2

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

6.1.6

4 years ago

6.1.5

4 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.13

4 years ago

5.0.12

4 years ago

5.0.11

4 years ago

5.0.10

4 years ago

5.0.10-alpha.1

4 years ago

5.0.9

4 years ago

5.0.8

4 years ago

5.0.7

4 years ago

5.0.6

4 years ago

5.0.5

4 years ago

5.0.4

5 years ago

5.0.3

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.8

5 years ago

4.0.7

5 years ago

4.0.6

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.0

5 years ago

3.0.38

5 years ago

3.0.37

5 years ago

3.0.36

5 years ago

3.0.35

5 years ago

3.0.34

5 years ago

3.0.33

5 years ago

3.0.32

5 years ago

3.0.31

5 years ago

3.0.30

5 years ago

3.0.29

5 years ago

3.0.28

5 years ago

3.0.27

5 years ago

3.0.26

5 years ago

3.0.25

5 years ago

3.0.24

5 years ago

3.0.23

5 years ago

3.0.22

5 years ago

3.0.21

5 years ago

3.0.20

5 years ago

3.0.19

5 years ago

3.0.18

5 years ago

3.0.17

5 years ago

3.0.16

5 years ago

3.0.15

5 years ago

3.0.14

5 years ago

3.0.13

5 years ago

3.0.12

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

5 years ago

3.0.8

5 years ago

3.0.7

5 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.5-beta.1

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.50

6 years ago

1.0.49

6 years ago

1.0.48

6 years ago

1.0.46

6 years ago

1.0.45

6 years ago

1.0.44

6 years ago

1.0.43

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.36

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.29

6 years ago

1.0.28

6 years ago

1.0.27

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago