META-INF.resources.js.translate.LanguageSelector.js Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
import ClayButton from '@clayui/button';
import ClayDropDown from '@clayui/drop-down';
import ClayIcon from '@clayui/icon';
import PropTypes from 'prop-types';
import React, {useState} from 'react';
function getLanguage(id) {
const text = id.replace('_', '-');
const icon = text.toLowerCase();
return {
icon,
text,
};
}
function LanguageSelector({languageIds, onChange, selectedLanguageId}) {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const language = getLanguage(selectedLanguageId);
return (
{language.text}
}
>
{languageIds.map((id) => {
const {icon, text} = getLanguage(id);
return (
{
onChange(id);
setIsDropdownOpen(false);
}}
>
{text}{' '}
);
})}
);
}
LanguageSelector.propTypes = {
languageIds: PropTypes.arrayOf(PropTypes.string).isRequired,
onChange: PropTypes.func.isRequired,
selectedLanguageId: PropTypes.string.isRequired,
};
export default LanguageSelector;