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

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