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

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