index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. useEffect,
  3. useState,
  4. type FC
  5. } from "react";
  6. import type ILocaleSwitcherProps from "./type";
  7. import type {
  8. LocaleSwitchVariants
  9. } from "./type";
  10. import {
  11. NCoreUIKitLocalize
  12. } from "../../core/hooks";
  13. import type {
  14. NCoreUIKitIcon
  15. } from "../../types";
  16. import Button from "../button";
  17. import {
  18. EnUsIcon,
  19. TRTRIcon
  20. } from "../../assets/svg";
  21. import HighlightButton from "../highlightButton";
  22. const PaletteSwitcher: FC<ILocaleSwitcherProps> = ({
  23. variants: variantsProps = [],
  24. isWorkWithHighlightButton,
  25. isWorkWithNextShowSystem,
  26. customLocalize,
  27. color,
  28. style,
  29. ...props
  30. }) => {
  31. const {
  32. activeLocale,
  33. localeKeys
  34. } = NCoreUIKitLocalize.useContext(customLocalize);
  35. const [
  36. currentIndex,
  37. setCurrentIndex
  38. ] = useState(localeKeys.findIndex(t => t === activeLocale));
  39. useEffect(() => {
  40. const cI = localeKeys.findIndex(tI => tI === activeLocale);
  41. const nextCI = isWorkWithNextShowSystem ? cI + 1 > localeKeys.length - 1 ? 0 : cI + 1 : cI;
  42. if(currentIndex !== nextCI) {
  43. setCurrentIndex(nextCI);
  44. }
  45. }, [activeLocale]);
  46. const variantsPre: LocaleSwitchVariants = variantsProps;
  47. const isHaveNCTRTR = variantsProps.findIndex(iV => iV.localeKey === "tr-TR") !== -1;
  48. const isHaveNCENUS = variantsProps.findIndex(iV => iV.localeKey === "en-US") !== -1;
  49. if(!isHaveNCTRTR) {
  50. variantsPre.unshift({
  51. borderColor: "transparent",
  52. backgroundColor: "subtle",
  53. iconColor: "turkish",
  54. localeKey: "tr-TR",
  55. icon: ({
  56. size
  57. }) => {
  58. return <TRTRIcon
  59. size={size + 5}
  60. />;
  61. }
  62. });
  63. }
  64. if(!isHaveNCENUS) {
  65. variantsPre.unshift({
  66. borderColor: "transparent",
  67. backgroundColor: "subtle",
  68. iconColor: "english",
  69. localeKey: "en-US",
  70. icon: ({
  71. size
  72. }) => {
  73. return <EnUsIcon
  74. size={size + 5}
  75. />;
  76. }
  77. });
  78. }
  79. const variants = [...variantsPre].sort((a, b) => {
  80. return localeKeys.indexOf(a.localeKey) - localeKeys.indexOf(b.localeKey);
  81. });
  82. const currentVariantIndex = variants.findIndex(e => e.localeKey === activeLocale);
  83. if(currentVariantIndex === -1) {
  84. return null;
  85. }
  86. const nextVariantIndex = currentVariantIndex + 1;
  87. const viewVariantIndex = nextVariantIndex > variants.length - 1 ? 0 : nextVariantIndex;
  88. const currentVariant = variants[isWorkWithNextShowSystem ? viewVariantIndex : currentVariantIndex];
  89. const allProps = {
  90. ...props,
  91. customBorderColor: currentVariant && currentVariant.borderColor ?
  92. currentVariant.borderColor
  93. :
  94. color,
  95. customColor: currentVariant && currentVariant.backgroundColor ?
  96. currentVariant.backgroundColor
  97. :
  98. color,
  99. icon: ({
  100. color,
  101. size
  102. }: {
  103. color: keyof NCoreUIKit.IconContentColors;
  104. size: number;
  105. }) => {
  106. const Icon = currentVariant?.icon as NCoreUIKitIcon;
  107. return <Icon
  108. customColor={currentVariant && currentVariant.iconColor ? currentVariant.iconColor : undefined}
  109. color={color}
  110. size={size}
  111. />;
  112. },
  113. onPress: () => {
  114. NCoreUIKitLocalize.switch({
  115. localeKey: localeKeys[viewVariantIndex] as keyof NCoreUIKit.LocaleKey
  116. });
  117. },
  118. style: [
  119. style
  120. ]
  121. };
  122. if(isWorkWithHighlightButton) {
  123. return <HighlightButton
  124. {...allProps}
  125. />;
  126. }
  127. return <Button
  128. {...allProps}
  129. />;
  130. };
  131. export default PaletteSwitcher;