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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { DropdownMenuStateless as DropdownMenu } from '@atlaskit/dropdown-menu';
  2. import { FieldTextStateless as TextField } from '@atlaskit/field-text';
  3. import ChevronDownIcon from '@atlaskit/icon/glyph/chevron-down';
  4. import PropTypes from 'prop-types';
  5. import React, { Component } from 'react';
  6. import { connect } from 'react-redux';
  7. import { translate } from '../../base/i18n';
  8. import { updateDialOutCodes } from '../actions';
  9. import CountryIcon from './CountryIcon';
  10. /**
  11. * The default value of the country if the fetch service is unavailable.
  12. *
  13. * @type {{
  14. * code: string,
  15. * dialCode: string,
  16. * name: string
  17. * }}
  18. */
  19. const DEFAULT_COUNTRY = {
  20. code: 'US',
  21. dialCode: '+1',
  22. name: 'United States'
  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: PropTypes.array,
  41. /**
  42. * The function called on every dial input change.
  43. */
  44. onChange: PropTypes.func,
  45. /**
  46. * Invoked to obtain translated strings.
  47. */
  48. t: PropTypes.func,
  49. /**
  50. * Invoked to send an ajax request for dial-out codes.
  51. */
  52. updateDialOutCodes: 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._onDropdownTriggerInputChange
  87. = this._onDropdownTriggerInputChange.bind(this);
  88. this._onInputChange = this._onInputChange.bind(this);
  89. this._onOpenChange = this._onOpenChange.bind(this);
  90. this._onSelect = this._onSelect.bind(this);
  91. this._setDialInputElement = this._setDialInputElement.bind(this);
  92. }
  93. /**
  94. * Dispatches a request for dial out codes if not already present in the
  95. * redux store. If dial out codes are present, sets a default code to
  96. * display in the dropdown trigger.
  97. *
  98. * @inheritdoc
  99. * @returns {void}
  100. */
  101. componentDidMount() {
  102. const dialOutCodes = this.props._dialOutCodes;
  103. if (dialOutCodes) {
  104. this._setDefaultCode(dialOutCodes);
  105. } else {
  106. this.props.updateDialOutCodes();
  107. }
  108. }
  109. /**
  110. * Monitors for dial out code updates and sets a default code to display in
  111. * the dropdown trigger if not already set.
  112. *
  113. * @inheritdoc
  114. * @returns {void}
  115. */
  116. componentWillReceiveProps(nextProps) {
  117. if (!this.state.selectedCountry && nextProps._dialOutCodes) {
  118. this._setDefaultCode(nextProps._dialOutCodes);
  119. }
  120. }
  121. /**
  122. * Implements React's {@link Component#render()}.
  123. *
  124. * @inheritdoc
  125. * @returns {ReactElement}
  126. */
  127. render() {
  128. const { t, _dialOutCodes } = this.props;
  129. return (
  130. <div className = 'form-control'>
  131. { _dialOutCodes ? this._createDropdownMenu(
  132. this._formatCountryCodes(_dialOutCodes)) : null }
  133. <div className = 'dial-out-input'>
  134. <TextField
  135. autoFocus = { true }
  136. isLabelHidden = { true }
  137. label = { 'dial-out-input-field' }
  138. onChange = { this._onInputChange }
  139. placeholder = { t('dialOut.enterPhone') }
  140. ref = { this._setDialInputElement }
  141. shouldFitContainer = { true }
  142. value = { this.state.dialInput } />
  143. </div>
  144. </div>
  145. );
  146. }
  147. /**
  148. * Creates a {@code DropdownMenu} instance.
  149. *
  150. * @param {Array} items - The content to display within the dropdown.
  151. * @returns {ReactElement}
  152. */
  153. _createDropdownMenu(items) {
  154. const { code, dialCode } = this.state.selectedCountry;
  155. return (
  156. <div className = 'dropdown-container'>
  157. <DropdownMenu
  158. isOpen = { this.state.isDropdownOpen }
  159. items = { [ { items } ] }
  160. onItemActivated = { this._onSelect }
  161. onOpenChange = { this._onOpenChange }
  162. shouldFitContainer = { false }>
  163. { this._createDropdownTrigger(dialCode, code) }
  164. </DropdownMenu>
  165. </div>
  166. );
  167. }
  168. /**
  169. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  170. * trigger for displaying the dropdown menu. The {@code Component} will also
  171. * display the currently selected number.
  172. *
  173. * @param {string} dialCode - The +xx dial code.
  174. * @param {string} countryCode - The country 2 letter code.
  175. * @private
  176. * @returns {ReactElement}
  177. */
  178. _createDropdownTrigger(dialCode, countryCode) {
  179. return (
  180. <div className = 'dropdown'>
  181. <CountryIcon
  182. className = 'dial-out-flag-icon'
  183. countryCode = { `${countryCode}` } />
  184. { /**
  185. * FIXME Replace TextField with AtlasKit Button when an issue
  186. * with icons shrinking due to button text is fixed.
  187. */ }
  188. <TextField
  189. className = 'input-control dial-out-code'
  190. isLabelHidden = { true }
  191. isReadOnly = { true }
  192. label = 'dial-out-code'
  193. onChange = { this._onDropdownTriggerInputChange }
  194. type = 'text'
  195. value = { dialCode || '' } />
  196. <span className = 'dropdown-trigger-icon'>
  197. <ChevronDownIcon
  198. label = 'expand'
  199. size = 'small' />
  200. </span>
  201. </div>
  202. );
  203. }
  204. /**
  205. * Transforms the passed in numbers object into an array of objects that can
  206. * be parsed by {@code DropdownMenu}.
  207. *
  208. * @param {Object} countryCodes - The list of country codes.
  209. * @private
  210. * @returns {Array<Object>}
  211. */
  212. _formatCountryCodes(countryCodes) {
  213. return countryCodes.map(country => {
  214. const countryIcon
  215. = <CountryIcon countryCode = { `${country.code}` } />;
  216. const countryElement
  217. = <span>{countryIcon} { country.name }</span>;
  218. return {
  219. content: `${country.dialCode}`,
  220. country,
  221. elemBefore: countryElement
  222. };
  223. });
  224. }
  225. /**
  226. * Updates the dialNumber when changes to the dial text or code happen.
  227. *
  228. * @private
  229. * @returns {void}
  230. */
  231. _onDialNumberChange() {
  232. const { dialCode } = this.state.selectedCountry;
  233. this.props.onChange(dialCode, this.state.dialInput);
  234. }
  235. /**
  236. * This is a no-op function used to stub out TextField's onChange in order
  237. * to prevent TextField from printing prop type validation errors. TextField
  238. * is used as a trigger for the dropdown in {@code DialOutNumbersForm} to
  239. * get the desired AtlasKit input look for the UI.
  240. *
  241. * @returns {void}
  242. */
  243. _onDropdownTriggerInputChange() {
  244. // Intentionally left empty.
  245. }
  246. /**
  247. * Updates the dialInput state when the input changes.
  248. *
  249. * @param {Object} e - The event notifying us of the change.
  250. * @private
  251. * @returns {void}
  252. */
  253. _onInputChange(e) {
  254. this.setState({
  255. dialInput: e.target.value
  256. }, () => {
  257. this._onDialNumberChange();
  258. });
  259. }
  260. /**
  261. * Sets the internal state to either open or close the dropdown. If the
  262. * dropdown is disabled, the state will always be set to false.
  263. *
  264. * @param {Object} dropdownEvent - The even returned from clicking on the
  265. * dropdown trigger.
  266. * @private
  267. * @returns {void}
  268. */
  269. _onOpenChange(dropdownEvent) {
  270. this.setState({
  271. isDropdownOpen: dropdownEvent.isOpen
  272. });
  273. }
  274. /**
  275. * Updates the internal state of the currently selected country code.
  276. *
  277. * @param {Object} selection - Event from choosing an dropdown option.
  278. * @private
  279. * @returns {void}
  280. */
  281. _onSelect(selection) {
  282. this.setState({
  283. isDropdownOpen: false,
  284. selectedCountry: selection.item.country
  285. }, () => {
  286. this._onDialNumberChange();
  287. this._dialInputElem.focus();
  288. });
  289. }
  290. /**
  291. * Updates the internal state of the currently selected number by defaulting
  292. * to the first available number.
  293. *
  294. * @param {Object} countryCodes - The list of country codes to choose from
  295. * for setting a default code.
  296. * @private
  297. * @returns {void}
  298. */
  299. _setDefaultCode(countryCodes) {
  300. this.setState({
  301. selectedCountry: countryCodes[0]
  302. });
  303. }
  304. /**
  305. * Sets the internal reference to the DOM/HTML element backing the React
  306. * {@code Component} dial input.
  307. *
  308. * @param {HTMLInputElement} input - The DOM/HTML element for this
  309. * {@code Component}'s text input.
  310. * @private
  311. * @returns {void}
  312. */
  313. _setDialInputElement(input) {
  314. this._dialInputElem = input;
  315. }
  316. }
  317. /**
  318. * Maps (parts of) the Redux state to the associated
  319. * {@code DialOutNumbersForm}'s props.
  320. *
  321. * @param {Object} state - The Redux state.
  322. * @private
  323. * @returns {{
  324. * _dialOutCodes: Object
  325. * }}
  326. */
  327. function _mapStateToProps(state) {
  328. const { dialOutCodes } = state['features/dial-out'];
  329. return {
  330. _dialOutCodes: dialOutCodes
  331. };
  332. }
  333. export default translate(
  334. connect(_mapStateToProps, { updateDialOutCodes })(DialOutNumbersForm));