bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/common/locales-dropdown/component.jsx

78 lines
2.2 KiB
React
Raw Normal View History

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
const DEFAULT_VALUE = 'select';
const DEFAULT_KEY = -1;
const propTypes = {
allLocales: PropTypes.arrayOf(PropTypes.object).isRequired,
value: PropTypes.string,
handleChange: PropTypes.func.isRequired,
elementId: PropTypes.string.isRequired,
selectMessage: PropTypes.string.isRequired,
};
const defaultProps = {
value: null,
};
class LocalesDropdown extends PureComponent {
// returns an array with the base language list + variations of currently selected language
filterLocaleVariations(value) {
const { allLocales } = this.props;
if (allLocales) {
if (Meteor.settings.public.app.showAllAvailableLocales) {
return allLocales;
}
// splits value if not empty
const splitValue = (value) ? value.split('-')[0] : '';
const allLocaleCodes = [];
2021-10-30 01:10:20 +08:00
allLocales.map((item) => allLocaleCodes.push(item.locale));
/*
locales show if:
1. it is a general version of a locale with no specific locales
2. it is a specific version of a selected locale with many specific versions
3. it is a specific version of a locale with no general locale
*/
return allLocales.filter(
2021-10-30 01:10:20 +08:00
(locale) => (!locale.locale.includes('-') || locale.locale.split('-')[0] === splitValue || !allLocaleCodes.includes(locale.locale.split('-')[0])),
);
}
return [];
}
render() {
const {
2021-11-06 00:39:53 +08:00
value, handleChange, elementId, selectMessage,
} = this.props;
const defaultLocale = value || DEFAULT_VALUE;
const availableLocales = this.filterLocaleVariations(value);
return (
<select
id={elementId}
onChange={handleChange}
value={defaultLocale}
>
<option disabled key={DEFAULT_KEY} value={DEFAULT_VALUE}>
{selectMessage}
</option>
2021-10-30 01:10:20 +08:00
{availableLocales.map((localeItem) => (
<option key={localeItem.locale} value={localeItem.locale}>
{localeItem.name}
</option>
))}
</select>
);
}
}
LocalesDropdown.propTypes = propTypes;
LocalesDropdown.defaultProps = defaultProps;
export default LocalesDropdown;