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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * React {@code Component} responsible for fetching and displaying dial-out
  24. * country codes, as well as dialing a phone number.
  25. *
  26. * @extends Component
  27. */
  28. class DialOutNumbersForm extends Component {
  29. /**
  30. * {@code DialOutNumbersForm}'s property types.
  31. *
  32. * @static
  33. */
  34. static propTypes = {
  35. /**
  36. * The redux state representing the list of dial-out codes.
  37. */
  38. _dialOutCodes: React.PropTypes.array,
  39. /**
  40. * The function called on every dial input change.
  41. */
  42. onChange: React.PropTypes.func,
  43. /**
  44. * Invoked to obtain translated strings.
  45. */
  46. t: React.PropTypes.func,
  47. /**
  48. * Invoked to send an ajax request for dial-out codes.
  49. */
  50. updateDialOutCodes: React.PropTypes.func
  51. };
  52. /**
  53. * Initializes a new {@code DialOutNumbersForm} instance.
  54. *
  55. * @param {Object} props - The read-only properties with which the new
  56. * instance is to be initialized.
  57. */
  58. constructor(props) {
  59. super(props);
  60. this.state = {
  61. dialInput: '',
  62. /**
  63. * Whether or not the dropdown should be open.
  64. *
  65. * @type {boolean}
  66. */
  67. isDropdownOpen: false,
  68. /**
  69. * The selected country.
  70. *
  71. * @type {Object}
  72. */
  73. selectedCountry: DEFAULT_COUNTRY
  74. };
  75. /**
  76. * The internal reference to the DOM/HTML element backing the React
  77. * {@code Component} text input.
  78. *
  79. * @private
  80. * @type {HTMLInputElement}
  81. */
  82. this._dialInputElem = null;
  83. // Bind event handlers so they are only bound once for every instance.
  84. this._onInputChange = this._onInputChange.bind(this);
  85. this._onOpenChange = this._onOpenChange.bind(this);
  86. this._onSelect = this._onSelect.bind(this);
  87. this._setDialInputElement = this._setDialInputElement.bind(this);
  88. }
  89. /**
  90. * Dispatches a request for dial out codes if not already present in the
  91. * redux store. If dial out codes are present, sets a default code to
  92. * display in the dropdown trigger.
  93. *
  94. * @inheritdoc
  95. * @returns {void}
  96. */
  97. componentDidMount() {
  98. const dialOutCodes = this.props._dialOutCodes;
  99. if (dialOutCodes) {
  100. this._setDefaultCode(dialOutCodes);
  101. } else {
  102. this.props.updateDialOutCodes();
  103. }
  104. }
  105. /**
  106. * Monitors for dial out code updates and sets a default code to display in
  107. * the dropdown trigger if not already set.
  108. *
  109. * @inheritdoc
  110. * @returns {void}
  111. */
  112. componentWillReceiveProps(nextProps) {
  113. if (!this.state.selectedCountry && nextProps._dialOutCodes) {
  114. this._setDefaultCode(nextProps._dialOutCodes);
  115. }
  116. }
  117. /**
  118. * Implements React's {@link Component#render()}.
  119. *
  120. * @inheritdoc
  121. * @returns {ReactElement}
  122. */
  123. render() {
  124. const { t, _dialOutCodes } = this.props;
  125. const items
  126. = _dialOutCodes ? this._formatCountryCodes(_dialOutCodes) : [];
  127. return (
  128. <div className = 'form-control'>
  129. { this._createDropdownMenu(items) }
  130. <div className = 'dial-out-input'>
  131. <input
  132. autoFocus = { true }
  133. className = 'input-control'
  134. onChange = { this._onInputChange }
  135. placeholder = { t('dialOut.enterPhone') }
  136. ref = { this._setDialInputElement }
  137. type = 'text' />
  138. </div>
  139. </div>
  140. );
  141. }
  142. /**
  143. * Creates a {@code StatelessDropdownMenu} instance.
  144. *
  145. * @param {Array} items - The content to display within the dropdown.
  146. * @returns {ReactElement}
  147. */
  148. _createDropdownMenu(items) {
  149. const { code, dialCode } = this.state.selectedCountry;
  150. return (
  151. <StatelessDropdownMenu
  152. isOpen = { this.state.isDropdownOpen }
  153. items = { [ { items } ] }
  154. onItemActivated = { this._onSelect }
  155. onOpenChange = { this._onOpenChange }
  156. shouldFitContainer = { true }>
  157. { this._createDropdownTrigger(dialCode, code) }
  158. </StatelessDropdownMenu>
  159. );
  160. }
  161. /**
  162. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  163. * trigger for displaying the dropdown menu. The {@code Component} will also
  164. * display the currently selected number.
  165. *
  166. * @param {string} dialCode - The +xx dial code.
  167. * @param {string} countryCode - The country 2 letter code.
  168. * @private
  169. * @returns {ReactElement}
  170. */
  171. _createDropdownTrigger(dialCode, countryCode) {
  172. return (
  173. <div className = 'dropdown'>
  174. <CountryIcon
  175. className = 'dial-out-flag-icon'
  176. countryCode = { `${countryCode}` } />
  177. <input
  178. className = 'input-control dial-out-code'
  179. readOnly = { true }
  180. type = 'text'
  181. value = { dialCode || '' } />
  182. <span className = 'dropdown-trigger-icon'>
  183. <ExpandIcon
  184. label = 'expand'
  185. size = 'medium' />
  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. country,
  207. elemBefore: countryElement
  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(
  309. connect(_mapStateToProps, { updateDialOutCodes })(DialOutNumbersForm));