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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import { StatelessDropdownMenu } from '@atlaskit/dropdown-menu';
  2. import AKFieldText, { FieldText } from '@atlaskit/field-text';
  3. import ExpandIcon from '@atlaskit/icon/glyph/expand';
  4. import { noop as _onNoop } from 'lodash';
  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: 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. return (
  128. <div className = 'form-control'>
  129. { _dialOutCodes ? this._createDropdownMenu(
  130. this._formatCountryCodes(_dialOutCodes)) : null }
  131. <div className = 'dial-out-input'>
  132. <AKFieldText
  133. autoFocus = { true }
  134. isLabelHidden = { true }
  135. label = { 'dial-out-input-field' }
  136. onChange = { this._onInputChange }
  137. placeholder = { t('dialOut.enterPhone') }
  138. ref = { this._setDialInputElement }
  139. shouldFitContainer = { true }
  140. value = { this.state.dialInput } />
  141. </div>
  142. </div>
  143. );
  144. }
  145. /**
  146. * Creates a {@code StatelessDropdownMenu} instance.
  147. *
  148. * @param {Array} items - The content to display within the dropdown.
  149. * @returns {ReactElement}
  150. */
  151. _createDropdownMenu(items) {
  152. const { code, dialCode } = this.state.selectedCountry;
  153. return (
  154. <div className = 'dropdown-container'>
  155. <StatelessDropdownMenu
  156. isOpen = { this.state.isDropdownOpen }
  157. items = { [ { items } ] }
  158. onItemActivated = { this._onSelect }
  159. onOpenChange = { this._onOpenChange }
  160. shouldFitContainer = { false }>
  161. { this._createDropdownTrigger(dialCode, code) }
  162. </StatelessDropdownMenu>
  163. </div>
  164. );
  165. }
  166. /**
  167. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  168. * trigger for displaying the dropdown menu. The {@code Component} will also
  169. * display the currently selected number.
  170. *
  171. * @param {string} dialCode - The +xx dial code.
  172. * @param {string} countryCode - The country 2 letter code.
  173. * @private
  174. * @returns {ReactElement}
  175. */
  176. _createDropdownTrigger(dialCode, countryCode) {
  177. return (
  178. <div className = 'dropdown'>
  179. <CountryIcon
  180. className = 'dial-out-flag-icon'
  181. countryCode = { `${countryCode}` } />
  182. { /**
  183. * FIXME Replace FieldText with AtlasKit Button when an issue
  184. * with icons shrinking due to button text is fixed.
  185. */ }
  186. <FieldText
  187. className = 'input-control dial-out-code'
  188. isLabelHidden = { true }
  189. isReadOnly = { true }
  190. label = 'dial-out-code'
  191. onChange = { _onNoop }
  192. type = 'text'
  193. value = { dialCode || '' } />
  194. <span className = 'dropdown-trigger-icon'>
  195. <ExpandIcon
  196. label = 'expand'
  197. size = 'medium' />
  198. </span>
  199. </div>
  200. );
  201. }
  202. /**
  203. * Transforms the passed in numbers object into an array of objects that can
  204. * be parsed by {@code StatelessDropdownMenu}.
  205. *
  206. * @param {Object} countryCodes - The list of country codes.
  207. * @private
  208. * @returns {Array<Object>}
  209. */
  210. _formatCountryCodes(countryCodes) {
  211. return countryCodes.map(country => {
  212. const countryIcon
  213. = <CountryIcon countryCode = { `${country.code}` } />;
  214. const countryElement
  215. = <span>{countryIcon} { country.name }</span>;
  216. return {
  217. content: `${country.dialCode}`,
  218. country,
  219. elemBefore: countryElement
  220. };
  221. });
  222. }
  223. /**
  224. * Updates the dialNumber when changes to the dial text or code happen.
  225. *
  226. * @private
  227. * @returns {void}
  228. */
  229. _onDialNumberChange() {
  230. const { dialCode } = this.state.selectedCountry;
  231. this.props.onChange(dialCode, this.state.dialInput);
  232. }
  233. /**
  234. * Updates the dialInput state when the input changes.
  235. *
  236. * @param {Object} e - The event notifying us of the change.
  237. * @private
  238. * @returns {void}
  239. */
  240. _onInputChange(e) {
  241. this.setState({
  242. dialInput: e.target.value
  243. }, () => {
  244. this._onDialNumberChange();
  245. });
  246. }
  247. /**
  248. * Sets the internal state to either open or close the dropdown. If the
  249. * dropdown is disabled, the state will always be set to false.
  250. *
  251. * @param {Object} dropdownEvent - The even returned from clicking on the
  252. * dropdown trigger.
  253. * @private
  254. * @returns {void}
  255. */
  256. _onOpenChange(dropdownEvent) {
  257. this.setState({
  258. isDropdownOpen: dropdownEvent.isOpen
  259. });
  260. }
  261. /**
  262. * Updates the internal state of the currently selected country code.
  263. *
  264. * @param {Object} selection - Event from choosing an dropdown option.
  265. * @private
  266. * @returns {void}
  267. */
  268. _onSelect(selection) {
  269. this.setState({
  270. isDropdownOpen: false,
  271. selectedCountry: selection.item.country
  272. }, () => {
  273. this._onDialNumberChange();
  274. this._dialInputElem.focus();
  275. });
  276. }
  277. /**
  278. * Updates the internal state of the currently selected number by defaulting
  279. * to the first available number.
  280. *
  281. * @param {Object} countryCodes - The list of country codes to choose from
  282. * for setting a default code.
  283. * @private
  284. * @returns {void}
  285. */
  286. _setDefaultCode(countryCodes) {
  287. this.setState({
  288. selectedCountry: countryCodes[0]
  289. });
  290. }
  291. /**
  292. * Sets the internal reference to the DOM/HTML element backing the React
  293. * {@code Component} dial input.
  294. *
  295. * @param {HTMLInputElement} input - The DOM/HTML element for this
  296. * {@code Component}'s text input.
  297. * @private
  298. * @returns {void}
  299. */
  300. _setDialInputElement(input) {
  301. this._dialInputElem = input;
  302. }
  303. }
  304. /**
  305. * Maps (parts of) the Redux state to the associated
  306. * {@code DialOutNumbersForm}'s props.
  307. *
  308. * @param {Object} state - The Redux state.
  309. * @private
  310. * @returns {{
  311. * _dialOutCodes: React.PropTypes.object
  312. * }}
  313. */
  314. function _mapStateToProps(state) {
  315. const { dialOutCodes } = state['features/dial-out'];
  316. return {
  317. _dialOutCodes: dialOutCodes
  318. };
  319. }
  320. export default translate(
  321. connect(_mapStateToProps, { updateDialOutCodes })(DialOutNumbersForm));