2017-10-06 16:14:45 +00:00
|
|
|
import { DropdownMenuStateless as DropdownMenu } from '@atlaskit/dropdown-menu';
|
|
|
|
import { FieldTextStateless as TextField } from '@atlaskit/field-text';
|
|
|
|
import ChevronDownIcon from '@atlaskit/icon/glyph/chevron-down';
|
2017-09-27 21:23:31 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-19 15:12:24 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import { translate } from '../../base/i18n';
|
|
|
|
|
2017-06-07 16:45:04 +00:00
|
|
|
import { updateDialOutCodes } from '../actions';
|
|
|
|
import CountryIcon from './CountryIcon';
|
2017-05-19 15:12:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default value of the country if the fetch service is unavailable.
|
|
|
|
*
|
2017-06-07 16:45:04 +00:00
|
|
|
* @type {{
|
|
|
|
* code: string,
|
|
|
|
* dialCode: string,
|
|
|
|
* name: string
|
|
|
|
* }}
|
2017-05-19 15:12:24 +00:00
|
|
|
*/
|
|
|
|
const DEFAULT_COUNTRY = {
|
2017-06-07 16:45:04 +00:00
|
|
|
code: 'US',
|
2017-05-19 15:12:24 +00:00
|
|
|
dialCode: '+1',
|
2017-06-07 16:45:04 +00:00
|
|
|
name: 'United States'
|
2017-05-19 15:12:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React {@code Component} responsible for fetching and displaying dial-out
|
|
|
|
* country codes, as well as dialing a phone number.
|
|
|
|
*
|
|
|
|
* @extends Component
|
|
|
|
*/
|
|
|
|
class DialOutNumbersForm extends Component {
|
|
|
|
/**
|
|
|
|
* {@code DialOutNumbersForm}'s property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
|
|
|
/**
|
|
|
|
* The redux state representing the list of dial-out codes.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
_dialOutCodes: PropTypes.array,
|
2017-05-19 15:12:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The function called on every dial input change.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
onChange: PropTypes.func,
|
2017-05-19 15:12:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
t: PropTypes.func,
|
2017-05-19 15:12:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to send an ajax request for dial-out codes.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
updateDialOutCodes: PropTypes.func
|
2017-06-02 02:01:50 +00:00
|
|
|
};
|
2017-05-19 15:12:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code DialOutNumbersForm} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
dialInput: '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the dropdown should be open.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
isDropdownOpen: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The selected country.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
selectedCountry: DEFAULT_COUNTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal reference to the DOM/HTML element backing the React
|
|
|
|
* {@code Component} text input.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {HTMLInputElement}
|
|
|
|
*/
|
|
|
|
this._dialInputElem = null;
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
2017-08-15 19:30:57 +00:00
|
|
|
this._onDropdownTriggerInputChange
|
|
|
|
= this._onDropdownTriggerInputChange.bind(this);
|
2017-05-19 15:12:24 +00:00
|
|
|
this._onInputChange = this._onInputChange.bind(this);
|
|
|
|
this._onOpenChange = this._onOpenChange.bind(this);
|
|
|
|
this._onSelect = this._onSelect.bind(this);
|
|
|
|
this._setDialInputElement = this._setDialInputElement.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches a request for dial out codes if not already present in the
|
|
|
|
* redux store. If dial out codes are present, sets a default code to
|
|
|
|
* display in the dropdown trigger.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
2017-06-07 16:45:04 +00:00
|
|
|
* @returns {void}
|
2017-05-19 15:12:24 +00:00
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
const dialOutCodes = this.props._dialOutCodes;
|
|
|
|
|
|
|
|
if (dialOutCodes) {
|
|
|
|
this._setDefaultCode(dialOutCodes);
|
|
|
|
} else {
|
|
|
|
this.props.updateDialOutCodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Monitors for dial out code updates and sets a default code to display in
|
|
|
|
* the dropdown trigger if not already set.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
2017-06-07 16:45:04 +00:00
|
|
|
* @returns {void}
|
2017-05-19 15:12:24 +00:00
|
|
|
*/
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (!this.state.selectedCountry && nextProps._dialOutCodes) {
|
|
|
|
this._setDefaultCode(nextProps._dialOutCodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const { t, _dialOutCodes } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className = 'form-control'>
|
2017-07-31 19:44:50 +00:00
|
|
|
{ _dialOutCodes ? this._createDropdownMenu(
|
|
|
|
this._formatCountryCodes(_dialOutCodes)) : null }
|
2017-05-19 15:12:24 +00:00
|
|
|
<div className = 'dial-out-input'>
|
2017-10-06 16:14:45 +00:00
|
|
|
<TextField
|
2017-05-19 15:12:24 +00:00
|
|
|
autoFocus = { true }
|
2017-07-28 20:35:04 +00:00
|
|
|
isLabelHidden = { true }
|
|
|
|
label = { 'dial-out-input-field' }
|
2017-05-19 15:12:24 +00:00
|
|
|
onChange = { this._onInputChange }
|
|
|
|
placeholder = { t('dialOut.enterPhone') }
|
|
|
|
ref = { this._setDialInputElement }
|
2017-07-31 19:44:50 +00:00
|
|
|
shouldFitContainer = { true }
|
|
|
|
value = { this.state.dialInput } />
|
2017-05-19 15:12:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 16:14:45 +00:00
|
|
|
* Creates a {@code DropdownMenu} instance.
|
2017-05-19 15:12:24 +00:00
|
|
|
*
|
|
|
|
* @param {Array} items - The content to display within the dropdown.
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_createDropdownMenu(items) {
|
|
|
|
const { code, dialCode } = this.state.selectedCountry;
|
|
|
|
|
|
|
|
return (
|
2017-07-28 20:35:04 +00:00
|
|
|
<div className = 'dropdown-container'>
|
2017-10-06 16:14:45 +00:00
|
|
|
<DropdownMenu
|
2017-07-28 20:35:04 +00:00
|
|
|
isOpen = { this.state.isDropdownOpen }
|
|
|
|
items = { [ { items } ] }
|
|
|
|
onItemActivated = { this._onSelect }
|
|
|
|
onOpenChange = { this._onOpenChange }
|
|
|
|
shouldFitContainer = { false }>
|
|
|
|
{ this._createDropdownTrigger(dialCode, code) }
|
2017-10-06 16:14:45 +00:00
|
|
|
</DropdownMenu>
|
2017-07-28 20:35:04 +00:00
|
|
|
</div>
|
2017-05-19 15:12:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a React {@code Component} with a readonly HTMLInputElement as a
|
|
|
|
* trigger for displaying the dropdown menu. The {@code Component} will also
|
|
|
|
* display the currently selected number.
|
|
|
|
*
|
|
|
|
* @param {string} dialCode - The +xx dial code.
|
|
|
|
* @param {string} countryCode - The country 2 letter code.
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_createDropdownTrigger(dialCode, countryCode) {
|
|
|
|
return (
|
|
|
|
<div className = 'dropdown'>
|
|
|
|
<CountryIcon
|
|
|
|
className = 'dial-out-flag-icon'
|
|
|
|
countryCode = { `${countryCode}` } />
|
2017-07-28 20:35:04 +00:00
|
|
|
{ /**
|
2017-10-06 16:14:45 +00:00
|
|
|
* FIXME Replace TextField with AtlasKit Button when an issue
|
2017-07-28 20:35:04 +00:00
|
|
|
* with icons shrinking due to button text is fixed.
|
|
|
|
*/ }
|
2017-10-06 16:14:45 +00:00
|
|
|
<TextField
|
2017-05-19 15:12:24 +00:00
|
|
|
className = 'input-control dial-out-code'
|
2017-07-28 20:35:04 +00:00
|
|
|
isLabelHidden = { true }
|
|
|
|
isReadOnly = { true }
|
|
|
|
label = 'dial-out-code'
|
2017-08-15 19:30:57 +00:00
|
|
|
onChange = { this._onDropdownTriggerInputChange }
|
2017-05-19 15:12:24 +00:00
|
|
|
type = 'text'
|
|
|
|
value = { dialCode || '' } />
|
|
|
|
<span className = 'dropdown-trigger-icon'>
|
2017-10-06 16:14:45 +00:00
|
|
|
<ChevronDownIcon
|
2017-07-21 18:43:13 +00:00
|
|
|
label = 'expand'
|
2017-10-06 16:14:45 +00:00
|
|
|
size = 'small' />
|
2017-05-19 15:12:24 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms the passed in numbers object into an array of objects that can
|
2017-10-06 16:14:45 +00:00
|
|
|
* be parsed by {@code DropdownMenu}.
|
2017-05-19 15:12:24 +00:00
|
|
|
*
|
|
|
|
* @param {Object} countryCodes - The list of country codes.
|
|
|
|
* @private
|
|
|
|
* @returns {Array<Object>}
|
|
|
|
*/
|
|
|
|
_formatCountryCodes(countryCodes) {
|
|
|
|
return countryCodes.map(country => {
|
|
|
|
const countryIcon
|
|
|
|
= <CountryIcon countryCode = { `${country.code}` } />;
|
|
|
|
const countryElement
|
|
|
|
= <span>{countryIcon} { country.name }</span>;
|
|
|
|
|
|
|
|
return {
|
|
|
|
content: `${country.dialCode}`,
|
2017-06-07 16:45:04 +00:00
|
|
|
country,
|
|
|
|
elemBefore: countryElement
|
2017-05-19 15:12:24 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the dialNumber when changes to the dial text or code happen.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDialNumberChange() {
|
|
|
|
const { dialCode } = this.state.selectedCountry;
|
|
|
|
|
|
|
|
this.props.onChange(dialCode, this.state.dialInput);
|
|
|
|
}
|
|
|
|
|
2017-08-15 19:30:57 +00:00
|
|
|
/**
|
2017-10-06 16:14:45 +00:00
|
|
|
* This is a no-op function used to stub out TextField's onChange in order
|
|
|
|
* to prevent TextField from printing prop type validation errors. TextField
|
2017-08-15 19:30:57 +00:00
|
|
|
* is used as a trigger for the dropdown in {@code DialOutNumbersForm} to
|
|
|
|
* get the desired AtlasKit input look for the UI.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDropdownTriggerInputChange() {
|
|
|
|
// Intentionally left empty.
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:12:24 +00:00
|
|
|
/**
|
|
|
|
* Updates the dialInput state when the input changes.
|
|
|
|
*
|
|
|
|
* @param {Object} e - The event notifying us of the change.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onInputChange(e) {
|
|
|
|
this.setState({
|
|
|
|
dialInput: e.target.value
|
|
|
|
}, () => {
|
|
|
|
this._onDialNumberChange();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal state to either open or close the dropdown. If the
|
|
|
|
* dropdown is disabled, the state will always be set to false.
|
|
|
|
*
|
|
|
|
* @param {Object} dropdownEvent - The even returned from clicking on the
|
|
|
|
* dropdown trigger.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onOpenChange(dropdownEvent) {
|
|
|
|
this.setState({
|
|
|
|
isDropdownOpen: dropdownEvent.isOpen
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the internal state of the currently selected country code.
|
|
|
|
*
|
|
|
|
* @param {Object} selection - Event from choosing an dropdown option.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onSelect(selection) {
|
|
|
|
this.setState({
|
|
|
|
isDropdownOpen: false,
|
|
|
|
selectedCountry: selection.item.country
|
|
|
|
}, () => {
|
|
|
|
this._onDialNumberChange();
|
|
|
|
|
|
|
|
this._dialInputElem.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the internal state of the currently selected number by defaulting
|
|
|
|
* to the first available number.
|
|
|
|
*
|
|
|
|
* @param {Object} countryCodes - The list of country codes to choose from
|
|
|
|
* for setting a default code.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setDefaultCode(countryCodes) {
|
|
|
|
this.setState({
|
|
|
|
selectedCountry: countryCodes[0]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the internal reference to the DOM/HTML element backing the React
|
|
|
|
* {@code Component} dial input.
|
|
|
|
*
|
|
|
|
* @param {HTMLInputElement} input - The DOM/HTML element for this
|
|
|
|
* {@code Component}'s text input.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setDialInputElement(input) {
|
|
|
|
this._dialInputElem = input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps (parts of) the Redux state to the associated
|
|
|
|
* {@code DialOutNumbersForm}'s props.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
2017-09-27 21:23:31 +00:00
|
|
|
* _dialOutCodes: Object
|
2017-05-19 15:12:24 +00:00
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state) {
|
|
|
|
const { dialOutCodes } = state['features/dial-out'];
|
|
|
|
|
|
|
|
return {
|
|
|
|
_dialOutCodes: dialOutCodes
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:45:04 +00:00
|
|
|
export default translate(
|
|
|
|
connect(_mapStateToProps, { updateDialOutCodes })(DialOutNumbersForm));
|