You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CountryPicker.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // @flow
  2. import InlineDialog from '@atlaskit/inline-dialog';
  3. import React, { PureComponent } from 'react';
  4. import { connect } from '../../../base/redux';
  5. import { setDialOutCountry, setDialOutNumber } from '../../actions';
  6. import { getDialOutCountry, getDialOutNumber } from '../../functions';
  7. import { getCountryFromDialCodeText } from '../../utils';
  8. import CountryDropDown from './CountryDropdown';
  9. import CountrySelector from './CountrySelector';
  10. const PREFIX_REG = /^(00)|\+/;
  11. type Props = {
  12. /**
  13. * The country to dial out to.
  14. */
  15. dialOutCountry: { name: string, dialCode: string, code: string },
  16. /**
  17. * The number to dial out to.
  18. */
  19. dialOutNumber: string,
  20. /**
  21. * Handler used when user presses 'Enter'.
  22. */
  23. onSubmit: Function,
  24. /**
  25. * Sets the dial out number.
  26. */
  27. setDialOutNumber: Function,
  28. /**
  29. * Sets the dial out country.
  30. */
  31. setDialOutCountry: Function,
  32. };
  33. type State = {
  34. /**
  35. * If the country picker is open or not.
  36. */
  37. isOpen: boolean,
  38. /**
  39. * The value of the input.
  40. */
  41. value: string
  42. }
  43. /**
  44. * This component displays a country picker with an input for the phone number.
  45. */
  46. class CountryPicker extends PureComponent<Props, State> {
  47. /**
  48. * A React ref to the HTML element containing the {@code input} instance.
  49. */
  50. inputRef: Object;
  51. /**
  52. * Initializes a new {@code CountryPicker} instance.
  53. *
  54. * @inheritdoc
  55. */
  56. constructor(props) {
  57. super(props);
  58. this.state = {
  59. isOpen: false,
  60. value: ''
  61. };
  62. this.inputRef = React.createRef();
  63. this._onChange = this._onChange.bind(this);
  64. this._onDropdownClose = this._onDropdownClose.bind(this);
  65. this._onCountrySelectorClick = this._onCountrySelectorClick.bind(this);
  66. this._onEntryClick = this._onEntryClick.bind(this);
  67. this._onKeyPress = this._onKeyPress.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#componentDidUnmount()}.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. this.inputRef.current.focus();
  76. }
  77. /**
  78. * Implements React's {@link Component#render()}.
  79. *
  80. * @inheritdoc
  81. * @returns {ReactElement}
  82. */
  83. render() {
  84. const { dialOutCountry, dialOutNumber } = this.props;
  85. const { isOpen } = this.state;
  86. const {
  87. inputRef,
  88. _onChange,
  89. _onCountrySelectorClick,
  90. _onDropdownClose,
  91. _onKeyPress,
  92. _onEntryClick
  93. } = this;
  94. return (
  95. <div className = 'cpick-container'>
  96. <InlineDialog
  97. content = { <CountryDropDown onEntryClick = { _onEntryClick } /> }
  98. isOpen = { isOpen }
  99. onClose = { _onDropdownClose }>
  100. <div className = 'cpick'>
  101. <CountrySelector
  102. country = { dialOutCountry }
  103. onClick = { _onCountrySelectorClick } />
  104. <input
  105. className = 'cpick-input'
  106. onChange = { _onChange }
  107. onKeyPress = { _onKeyPress }
  108. ref = { inputRef }
  109. value = { dialOutNumber } />
  110. </div>
  111. </InlineDialog>
  112. </div>
  113. );
  114. }
  115. _onChange: (Object) => void;
  116. /**
  117. * Handles the input text change.
  118. * Automatically updates the country from the 'CountrySelector' if a
  119. * phone number prefix is entered (00 or +).
  120. *
  121. * @param {Object} e - The synthetic event.
  122. * @returns {void}
  123. */
  124. _onChange({ target: { value } }) {
  125. if (PREFIX_REG.test(value)) {
  126. const textWithDialCode = value.replace(PREFIX_REG, '');
  127. if (textWithDialCode.length >= 4) {
  128. const country = getCountryFromDialCodeText(textWithDialCode);
  129. if (country) {
  130. const rest = textWithDialCode.replace(country.dialCode, '');
  131. this.props.setDialOutCountry(country);
  132. this.props.setDialOutNumber(rest);
  133. return;
  134. }
  135. }
  136. }
  137. this.props.setDialOutNumber(value);
  138. }
  139. _onCountrySelectorClick: (Object) => void;
  140. /**
  141. * Click handler for country selector.
  142. *
  143. * @param {Object} e - The synthetic event.
  144. * @returns {void}
  145. */
  146. _onCountrySelectorClick(e) {
  147. e.stopPropagation();
  148. this.setState({
  149. isOpen: !this.setState.isOpen
  150. });
  151. }
  152. _onDropdownClose: () => void;
  153. /**
  154. * Closes the dropdown.
  155. *
  156. * @returns {void}
  157. */
  158. _onDropdownClose() {
  159. this.setState({
  160. isOpen: false
  161. });
  162. }
  163. _onEntryClick: (Object) => void;
  164. /**
  165. * Click handler for a single entry from the dropdown.
  166. *
  167. * @param {Object} country - The country used for dialing out.
  168. * @returns {void}
  169. */
  170. _onEntryClick(country) {
  171. this.props.setDialOutCountry(country);
  172. this._onDropdownClose();
  173. }
  174. _onKeyPress: (Object) => void;
  175. /**
  176. * Handler for key presses.
  177. *
  178. * @param {Object} e - The synthetic event.
  179. * @returns {void}
  180. */
  181. _onKeyPress(e) {
  182. if (e.key === 'Enter') {
  183. this.props.onSubmit();
  184. }
  185. }
  186. }
  187. /**
  188. * Maps (parts of) the redux state to the React {@code Component} props.
  189. *
  190. * @param {Object} state - The redux state.
  191. * @returns {Props}
  192. */
  193. function mapStateToProps(state) {
  194. return {
  195. dialOutCountry: getDialOutCountry(state),
  196. dialOutNumber: getDialOutNumber(state)
  197. };
  198. }
  199. /**
  200. * Maps redux actions to the props of the component.
  201. *
  202. * @type {{
  203. * setDialOutCountry: Function,
  204. * setDialOutNumber: Function
  205. * }}
  206. */
  207. const mapDispatchToProps = {
  208. setDialOutCountry,
  209. setDialOutNumber
  210. };
  211. export default connect(mapStateToProps, mapDispatchToProps)(CountryPicker);