AllenYuan

Untitled

Jun 13th, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. List,
  4. ListItem,
  5. ListItemIcon,
  6. ListItemText,
  7. Collapse,
  8. } from '@material-ui/core';
  9. import { ExpandMore, ExpandLess, Translate } from '@material-ui/icons';
  10. import styled from 'styled-components';
  11. import { langs } from '../../lang/index';
  12. import constants from '../constants';
  13. import { useStrings } from '../../hooks/useStrings.hook';
  14.  
  15. const StyledListItem = styled(ListItem)`
  16. color: ${constants.primaryTextColor} !important;
  17. `;
  18.  
  19. const StyledListItemIcon = styled(ListItemIcon)`
  20. color: ${constants.primaryTextColor} !important;
  21. `;
  22.  
  23. // set localization language, page reloads and fires getStrings action to get localized strings
  24. const setLocalization = (event, key, payload) => {
  25. window.localStorage.setItem('localization', payload.value);
  26. window.location.reload();
  27. };
  28.  
  29. const LocalizationMenu = React.forwardRef(() => {
  30. const strings = useStrings();
  31. const [open, setOpen] = React.useState(false);
  32.  
  33. // toggle open/closed status
  34. const handleOnClick = React.useCallback(
  35. (event) => {
  36. event.preventDefault();
  37. event.stopPropagation();
  38. setOpen(!open);
  39. },
  40. [open, setOpen],
  41. );
  42.  
  43. return (
  44. <div style={{ minWidth: '200px' }}>
  45. <StyledListItem button onClick={handleOnClick}>
  46. <StyledListItemIcon>
  47. <Translate />
  48. </StyledListItemIcon>
  49. <ListItemText primary={strings.app_language} />
  50. {open ? <ExpandLess /> : <ExpandMore />}
  51. </StyledListItem>
  52. {/* list of languages */}
  53. <List component="div">
  54. <Collapse
  55. in={open}
  56. timeout="auto"
  57. unmountOnExit
  58. style={{ maxHeight: 300, overflow: 'auto' }}
  59. >
  60. <List component="div" disablePadding>
  61. {langs.map((lang) => (
  62. <StyledListItem
  63. button
  64. onClick={() => setLocalization(null, null, lang)}
  65. key={lang.value}
  66. >
  67. <ListItemText primary={lang.native} />
  68. </StyledListItem>
  69. ))}
  70. </List>
  71. </Collapse>
  72. </List>
  73. </div>
  74. );
  75. });
  76.  
  77. export default LocalizationMenu;
Advertisement
Add Comment
Please, Sign In to add comment