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

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