2017-03-14 14:50:13 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2020-05-12 00:04:18 +03:00
|
|
|
import {COUNTRIES, getEmojiFlag} from '../../../phonenumber';
|
2019-06-01 06:25:13 +03:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2019-12-15 18:04:57 +03:00
|
|
|
import { _t } from "../../../languageHandler";
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2019-01-24 03:32:36 +03:00
|
|
|
const COUNTRIES_BY_ISO2 = {};
|
2017-03-14 14:50:13 +03:00
|
|
|
for (const c of COUNTRIES) {
|
|
|
|
COUNTRIES_BY_ISO2[c.iso2] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
function countryMatchesSearchQuery(query, country) {
|
2017-05-18 19:01:40 +03:00
|
|
|
// Remove '+' if present (when searching for a prefix)
|
|
|
|
if (query[0] === '+') {
|
|
|
|
query = query.slice(1);
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:50:13 +03:00
|
|
|
if (country.name.toUpperCase().indexOf(query.toUpperCase()) == 0) return true;
|
|
|
|
if (country.iso2 == query.toUpperCase()) return true;
|
2017-05-18 19:01:40 +03:00
|
|
|
if (country.prefix.indexOf(query) !== -1) return true;
|
2017-03-14 14:50:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class CountryDropdown extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this._onSearchChange = this._onSearchChange.bind(this);
|
2017-04-25 13:21:47 +03:00
|
|
|
this._onOptionChange = this._onOptionChange.bind(this);
|
2017-05-17 15:04:06 +03:00
|
|
|
this._getShortOption = this._getShortOption.bind(this);
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2019-06-01 06:25:13 +03:00
|
|
|
let defaultCountry = COUNTRIES[0];
|
2019-06-01 18:12:09 +03:00
|
|
|
const defaultCountryCode = SdkConfig.get()["defaultCountryCode"];
|
|
|
|
if (defaultCountryCode) {
|
|
|
|
const country = COUNTRIES.find(c => c.iso2 === defaultCountryCode.toUpperCase());
|
2019-06-01 06:25:13 +03:00
|
|
|
if (country) defaultCountry = country;
|
|
|
|
}
|
2019-06-01 06:26:22 +03:00
|
|
|
|
2017-03-14 14:50:13 +03:00
|
|
|
this.state = {
|
|
|
|
searchQuery: '',
|
2019-06-01 06:25:13 +03:00
|
|
|
defaultCountry,
|
2017-05-18 13:45:23 +03:00
|
|
|
};
|
2017-03-29 17:05:49 +03:00
|
|
|
}
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2020-03-31 23:14:17 +03:00
|
|
|
componentDidMount() {
|
2017-03-29 17:05:49 +03:00
|
|
|
if (!this.props.value) {
|
2019-06-01 06:25:13 +03:00
|
|
|
// If no value is given, we start with the default
|
2017-03-14 14:50:13 +03:00
|
|
|
// country selected, but our parent component
|
|
|
|
// doesn't know this, therefore we do this.
|
2019-06-01 06:25:13 +03:00
|
|
|
this.props.onOptionChange(this.state.defaultCountry);
|
2017-03-14 14:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchChange(search) {
|
|
|
|
this.setState({
|
|
|
|
searchQuery: search,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-25 13:21:47 +03:00
|
|
|
_onOptionChange(iso2) {
|
|
|
|
this.props.onOptionChange(COUNTRIES_BY_ISO2[iso2]);
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:50:13 +03:00
|
|
|
_flagImgForIso2(iso2) {
|
2020-05-12 00:04:18 +03:00
|
|
|
return <div className="mx_Dropdown_option_emoji">{ getEmojiFlag(iso2) }</div>;
|
2017-03-14 14:50:13 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:04:06 +03:00
|
|
|
_getShortOption(iso2) {
|
|
|
|
if (!this.props.isSmall) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let countryPrefix;
|
|
|
|
if (this.props.showPrefix) {
|
|
|
|
countryPrefix = '+' + COUNTRIES_BY_ISO2[iso2].prefix;
|
|
|
|
}
|
2019-01-30 01:04:44 +03:00
|
|
|
return <span className="mx_CountryDropdown_shortOption">
|
2017-05-17 15:04:06 +03:00
|
|
|
{ this._flagImgForIso2(iso2) }
|
|
|
|
{ countryPrefix }
|
|
|
|
</span>;
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:50:13 +03:00
|
|
|
render() {
|
|
|
|
const Dropdown = sdk.getComponent('elements.Dropdown');
|
|
|
|
|
|
|
|
let displayedCountries;
|
|
|
|
if (this.state.searchQuery) {
|
|
|
|
displayedCountries = COUNTRIES.filter(
|
|
|
|
countryMatchesSearchQuery.bind(this, this.state.searchQuery),
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
this.state.searchQuery.length == 2 &&
|
|
|
|
COUNTRIES_BY_ISO2[this.state.searchQuery.toUpperCase()]
|
|
|
|
) {
|
|
|
|
// exact ISO2 country name match: make the first result the matches ISO2
|
|
|
|
const matched = COUNTRIES_BY_ISO2[this.state.searchQuery.toUpperCase()];
|
|
|
|
displayedCountries = displayedCountries.filter((c) => {
|
|
|
|
return c.iso2 != matched.iso2;
|
|
|
|
});
|
|
|
|
displayedCountries.unshift(matched);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
displayedCountries = COUNTRIES;
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = displayedCountries.map((country) => {
|
2019-01-30 01:04:44 +03:00
|
|
|
return <div className="mx_CountryDropdown_option" key={country.iso2}>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this._flagImgForIso2(country.iso2) }
|
2019-03-06 21:00:05 +03:00
|
|
|
{ country.name } (+{ country.prefix })
|
2017-03-14 14:50:13 +03:00
|
|
|
</div>;
|
|
|
|
});
|
|
|
|
|
|
|
|
// default value here too, otherwise we need to handle null / undefined
|
|
|
|
// values between mounting and the initial value propgating
|
2019-06-01 06:25:13 +03:00
|
|
|
const value = this.props.value || this.state.defaultCountry.iso2;
|
2017-03-14 14:50:13 +03:00
|
|
|
|
2019-12-15 18:04:57 +03:00
|
|
|
return <Dropdown
|
|
|
|
id="mx_CountryDropdown"
|
|
|
|
className={this.props.className + " mx_CountryDropdown"}
|
|
|
|
onOptionChange={this._onOptionChange}
|
|
|
|
onSearchChange={this._onSearchChange}
|
|
|
|
menuWidth={298}
|
|
|
|
getShortOption={this._getShortOption}
|
|
|
|
value={value}
|
|
|
|
searchEnabled={true}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
label={_t("Country Dropdown")}
|
2017-03-14 14:50:13 +03:00
|
|
|
>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ options }
|
2017-05-18 13:45:23 +03:00
|
|
|
</Dropdown>;
|
2017-03-14 14:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CountryDropdown.propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
className: PropTypes.string,
|
|
|
|
isSmall: PropTypes.bool,
|
2017-05-17 15:04:06 +03:00
|
|
|
// if isSmall, show +44 in the selected value
|
2017-12-26 04:03:18 +03:00
|
|
|
showPrefix: PropTypes.bool,
|
|
|
|
onOptionChange: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
2017-03-14 14:50:13 +03:00
|
|
|
};
|