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.

DialOutNumbersForm.web.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import ExpandIcon from '@atlaskit/icon/glyph/expand';
  4. import { StatelessDropdownMenu } from '@atlaskit/dropdown-menu';
  5. import { translate } from '../../base/i18n';
  6. import CountryIcon from './CountryIcon';
  7. import { updateDialOutCodes } from '../actions';
  8. /**
  9. * The expand icon of the dropdown menu.
  10. *
  11. * @type {XML}
  12. */
  13. const EXPAND_ICON = <ExpandIcon label = 'expand' />;
  14. /**
  15. * The default value of the country if the fetch service is unavailable.
  16. *
  17. * @type {{name: string, dialCode: string, code: string}}
  18. */
  19. const DEFAULT_COUNTRY = {
  20. name: 'United States',
  21. dialCode: '+1',
  22. code: 'US'
  23. };
  24. /**
  25. * React {@code Component} responsible for fetching and displaying dial-out
  26. * country codes, as well as dialing a phone number.
  27. *
  28. * @extends Component
  29. */
  30. class DialOutNumbersForm extends Component {
  31. /**
  32. * {@code DialOutNumbersForm}'s property types.
  33. *
  34. * @static
  35. */
  36. static propTypes = {
  37. /**
  38. * The redux state representing the list of dial-out codes.
  39. */
  40. _dialOutCodes: React.PropTypes.array,
  41. /**
  42. * The function called on every dial input change.
  43. */
  44. onChange: React.PropTypes.func,
  45. /**
  46. * Invoked to obtain translated strings.
  47. */
  48. t: React.PropTypes.func,
  49. /**
  50. * Invoked to send an ajax request for dial-out codes.
  51. */
  52. updateDialOutCodes: React.PropTypes.func
  53. };
  54. /**
  55. * Initializes a new {@code DialOutNumbersForm} instance.
  56. *
  57. * @param {Object} props - The read-only properties with which the new
  58. * instance is to be initialized.
  59. */
  60. constructor(props) {
  61. super(props);
  62. this.state = {
  63. dialInput: '',
  64. /**
  65. * Whether or not the dropdown should be open.
  66. *
  67. * @type {boolean}
  68. */
  69. isDropdownOpen: false,
  70. /**
  71. * The selected country.
  72. *
  73. * @type {Object}
  74. */
  75. selectedCountry: DEFAULT_COUNTRY
  76. };
  77. /**
  78. * The internal reference to the DOM/HTML element backing the React
  79. * {@code Component} text input.
  80. *
  81. * @private
  82. * @type {HTMLInputElement}
  83. */
  84. this._dialInputElem = null;
  85. // Bind event handlers so they are only bound once for every instance.
  86. this._onInputChange = this._onInputChange.bind(this);
  87. this._onOpenChange = this._onOpenChange.bind(this);
  88. this._onSelect = this._onSelect.bind(this);
  89. this._setDialInputElement = this._setDialInputElement.bind(this);
  90. }
  91. /**
  92. * Dispatches a request for dial out codes if not already present in the
  93. * redux store. If dial out codes are present, sets a default code to
  94. * display in the dropdown trigger.
  95. *
  96. * @inheritdoc
  97. * returns {void}
  98. */
  99. componentDidMount() {
  100. const dialOutCodes = this.props._dialOutCodes;
  101. if (dialOutCodes) {
  102. this._setDefaultCode(dialOutCodes);
  103. } else {
  104. this.props.updateDialOutCodes();
  105. }
  106. }
  107. /**
  108. * Monitors for dial out code updates and sets a default code to display in
  109. * the dropdown trigger if not already set.
  110. *
  111. * @inheritdoc
  112. * returns {void}
  113. */
  114. componentWillReceiveProps(nextProps) {
  115. if (!this.state.selectedCountry && nextProps._dialOutCodes) {
  116. this._setDefaultCode(nextProps._dialOutCodes);
  117. }
  118. }
  119. /**
  120. * Implements React's {@link Component#render()}.
  121. *
  122. * @inheritdoc
  123. * @returns {ReactElement}
  124. */
  125. render() {
  126. const { t, _dialOutCodes } = this.props;
  127. const items
  128. = _dialOutCodes ? this._formatCountryCodes(_dialOutCodes) : [];
  129. return (
  130. <div className = 'form-control'>
  131. { this._createDropdownMenu(items) }
  132. <div className = 'dial-out-input'>
  133. <input
  134. autoFocus = { true }
  135. className = 'input-control'
  136. onChange = { this._onInputChange }
  137. placeholder = { t('dialOut.enterPhone') }
  138. ref = { this._setDialInputElement }
  139. type = 'text' />
  140. </div>
  141. </div>
  142. );
  143. }
  144. /**
  145. * Creates a {@code StatelessDropdownMenu} instance.
  146. *
  147. * @param {Array} items - The content to display within the dropdown.
  148. * @returns {ReactElement}
  149. */
  150. _createDropdownMenu(items) {
  151. const { code, dialCode } = this.state.selectedCountry;
  152. return (
  153. <StatelessDropdownMenu
  154. isOpen = { this.state.isDropdownOpen }
  155. items = { [ { items } ] }
  156. onItemActivated = { this._onSelect }
  157. onOpenChange = { this._onOpenChange }
  158. shouldFitContainer = { true }>
  159. { this._createDropdownTrigger(dialCode, code) }
  160. </StatelessDropdownMenu>
  161. );
  162. }
  163. /**
  164. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  165. * trigger for displaying the dropdown menu. The {@code Component} will also
  166. * display the currently selected number.
  167. *
  168. * @param {string} dialCode - The +xx dial code.
  169. * @param {string} countryCode - The country 2 letter code.
  170. * @private
  171. * @returns {ReactElement}
  172. */
  173. _createDropdownTrigger(dialCode, countryCode) {
  174. return (
  175. <div className = 'dropdown'>
  176. <CountryIcon
  177. className = 'dial-out-flag-icon'
  178. countryCode = { `${countryCode}` } />
  179. <input
  180. className = 'input-control dial-out-code'
  181. readOnly = { true }
  182. type = 'text'
  183. value = { dialCode || '' } />
  184. <span className = 'dropdown-trigger-icon'>
  185. { EXPAND_ICON }
  186. </span>
  187. </div>
  188. );
  189. }
  190. /**
  191. * Transforms the passed in numbers object into an array of objects that can
  192. * be parsed by {@code StatelessDropdownMenu}.
  193. *
  194. * @param {Object} countryCodes - The list of country codes.
  195. * @private
  196. * @returns {Array<Object>}
  197. */
  198. _formatCountryCodes(countryCodes) {
  199. return countryCodes.map(country => {
  200. const countryIcon
  201. = <CountryIcon countryCode = { `${country.code}` } />;
  202. const countryElement
  203. = <span>{countryIcon} { country.name }</span>;
  204. return {
  205. content: `${country.dialCode}`,
  206. elemBefore: countryElement,
  207. country
  208. };
  209. });
  210. }
  211. /**
  212. * Updates the dialNumber when changes to the dial text or code happen.
  213. *
  214. * @private
  215. * @returns {void}
  216. */
  217. _onDialNumberChange() {
  218. const { dialCode } = this.state.selectedCountry;
  219. this.props.onChange(dialCode, this.state.dialInput);
  220. }
  221. /**
  222. * Updates the dialInput state when the input changes.
  223. *
  224. * @param {Object} e - The event notifying us of the change.
  225. * @private
  226. * @returns {void}
  227. */
  228. _onInputChange(e) {
  229. this.setState({
  230. dialInput: e.target.value
  231. }, () => {
  232. this._onDialNumberChange();
  233. });
  234. }
  235. /**
  236. * Sets the internal state to either open or close the dropdown. If the
  237. * dropdown is disabled, the state will always be set to false.
  238. *
  239. * @param {Object} dropdownEvent - The even returned from clicking on the
  240. * dropdown trigger.
  241. * @private
  242. * @returns {void}
  243. */
  244. _onOpenChange(dropdownEvent) {
  245. this.setState({
  246. isDropdownOpen: dropdownEvent.isOpen
  247. });
  248. }
  249. /**
  250. * Updates the internal state of the currently selected country code.
  251. *
  252. * @param {Object} selection - Event from choosing an dropdown option.
  253. * @private
  254. * @returns {void}
  255. */
  256. _onSelect(selection) {
  257. this.setState({
  258. isDropdownOpen: false,
  259. selectedCountry: selection.item.country
  260. }, () => {
  261. this._onDialNumberChange();
  262. this._dialInputElem.focus();
  263. });
  264. }
  265. /**
  266. * Updates the internal state of the currently selected number by defaulting
  267. * to the first available number.
  268. *
  269. * @param {Object} countryCodes - The list of country codes to choose from
  270. * for setting a default code.
  271. * @private
  272. * @returns {void}
  273. */
  274. _setDefaultCode(countryCodes) {
  275. this.setState({
  276. selectedCountry: countryCodes[0]
  277. });
  278. }
  279. /**
  280. * Sets the internal reference to the DOM/HTML element backing the React
  281. * {@code Component} dial input.
  282. *
  283. * @param {HTMLInputElement} input - The DOM/HTML element for this
  284. * {@code Component}'s text input.
  285. * @private
  286. * @returns {void}
  287. */
  288. _setDialInputElement(input) {
  289. this._dialInputElem = input;
  290. }
  291. }
  292. /**
  293. * Maps (parts of) the Redux state to the associated
  294. * {@code DialOutNumbersForm}'s props.
  295. *
  296. * @param {Object} state - The Redux state.
  297. * @private
  298. * @returns {{
  299. * _dialOutCodes: React.PropTypes.object
  300. * }}
  301. */
  302. function _mapStateToProps(state) {
  303. const { dialOutCodes } = state['features/dial-out'];
  304. return {
  305. _dialOutCodes: dialOutCodes
  306. };
  307. }
  308. export default translate(connect(_mapStateToProps,
  309. { updateDialOutCodes })(DialOutNumbersForm));