Skip to content

Commit

Permalink
[ALS-1910] Map languages dropdown (#293)
Browse files Browse the repository at this point in the history
* updated political view config to support new country codes, desctiption to be updated

* hide political view flags for windows OS, fixed map language issue for Portuguese and Chinese

* added isSupportedByPlaces flag to political view

* added isSupportedByPlaces flag to political view

* added map language dropdown

* added labels for political views and map languages

* fixes failing test issue

---------

Co-authored-by: Ahmad Azizi <[email protected]>
  • Loading branch information
wadhawh and its-aazizi authored Dec 6, 2024
1 parent 8b25965 commit 1eb4e24
Show file tree
Hide file tree
Showing 27 changed files with 551 additions and 64 deletions.
99 changes: 99 additions & 0 deletions src/atomicui/atoms/MapLanguageDropdown/MapLanguageDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { FC, useCallback, useEffect, useRef, useState } from "react";

import { Flex, Text } from "@aws-amplify/ui-react";
import { IconArrow } from "@demo/assets/svgs";
import { appConfig } from "@demo/core/constants";
import { useMap } from "@demo/hooks";
import { useTranslation } from "react-i18next";
import "./styles.scss";

const {
MAP_RESOURCES: { MAP_LANGUAGES }
} = appConfig;

interface MapLanguageDropdownProps {
bordered?: boolean;
arrowIconColor?: string;
width?: string;
disabled?: boolean;
}

const MapLanguageDropdown: FC<MapLanguageDropdownProps> = ({
bordered = false,
arrowIconColor,
width = "100%",
disabled = false
}) => {
const [open, setOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const { i18n, t } = useTranslation();
const { mapLanguage, setMapLanguage } = useMap();
const mapLanguageDir = i18n.dir(mapLanguage.value);

const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setOpen(false);
}
};

useEffect(() => {
document.addEventListener("mousedown", handleClickOutside);

return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);

const handleClick = useCallback(
(option: { value: string; label: string }) => {
setMapLanguage(option);
setOpen(false);
},
[setMapLanguage]
);

return (
<div ref={dropdownRef} className="dropdown-container" style={{ width }}>
<div
data-testid="dropdown-trigger-map-language"
style={{ cursor: disabled ? "not-allowed" : "pointer", backgroundColor: disabled ? "#ddd" : "" }}
className={bordered ? `trigger bordered dropdown-${open}` : "trigger"}
onClick={() => !disabled && setOpen(!open)}
>
<p data-testid="dropdown-label" className="dropdown-label" dir={mapLanguageDir}>
{mapLanguage.label}
</p>
<IconArrow
style={{
transform: open ? "rotate(180deg)" : "rotate(0deg)",
width: bordered ? "1rem" : "1.23rem",
height: bordered ? "" : "1.23rem",
fill: arrowIconColor ? arrowIconColor : "var(--primary-color)"
}}
/>
</div>
{open && (
<ul data-testid="dropdown-options" className={bordered ? "options bordered" : "options"}>
{MAP_LANGUAGES.map(({ value, label }) => {
return (
<li
data-testid={value}
key={value}
style={{ display: "flex", justifyContent: "start" }}
onClick={() => handleClick({ value, label })}
>
<Flex gap={0} direction="column" padding="0.46rem 1.23rem">
<Text className="bold small-text" color="var(--tertiary-color)">
{t(label)}
</Text>
</Flex>
</li>
);
})}
</ul>
)}
</div>
);
};

export default MapLanguageDropdown;
1 change: 1 addition & 0 deletions src/atomicui/atoms/MapLanguageDropdown/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as MapLanguageDropdown } from "./MapLanguageDropdown";
117 changes: 117 additions & 0 deletions src/atomicui/atoms/MapLanguageDropdown/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. */
/* SPDX-License-Identifier: MIT-0 */

.dropdown-container {
position: relative;
display: inline-block;

.trigger {
display: flex;
justify-content: center;
align-items: center;
max-width: 18.15rem;
height: 2.46rem;
outline: none;
cursor: pointer;
background-color: transparent;
padding: 0.62rem 1rem;

@media only screen and (max-width: 1023px) {
max-width: unset;
}

&.bordered {
padding: 12px 16px;
min-width: 100%;
border: 1px solid var(--grey-color-3);
border-radius: 7px;
height: 40px;
justify-content: space-between;

.dropdown-label {
color: var(--grey-color);
font-family: "AmazonEmber-Regular";
font-weight: 400;
font-size: 1.08rem;
width: 90%;
}
}

&.bordered.dropdown-true {
border-color: var(--primary-color);
}

&.bordered.dropdown-false {
background-color: var(--grey-color-4);
border: 1px solid var(--grey-color-3);

p {
color: var(--tertiary-color);
}
}

p {
font-family: "AmazonEmber-Bold";
font-size: 1.08rem;
color: var(--primary-color);
margin-right: 0.5rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}

.options {
position: absolute;
right: 0;
display: flex;
flex-direction: column;
width: 21.5rem;
height: 16rem;
overflow: auto;
list-style: none;
background-color: var(--white-color);
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.202633);
border-radius: 0.62rem;
margin-top: 0.62rem;
padding: 0;
z-index: 1;

@media only screen and (max-width: 562px) {
width: 12rem;
left: -10%;
}

&.bordered {
left: 0;
height: auto;
width: 100%;
border-radius: 4px;
max-height: calc(100vh - 40rem);
box-shadow: none;
border: 1px solid var(--grey-color-5);

li {
padding: 0;

&:hover {
background-color: var(--primary-alpha-light-color);
}
}
}

li {
cursor: pointer;
padding: 8px 16px;
user-select: none;

&:hover {
background-color: var(--primary-alpha-light-color);

@media only screen and (min-width: 1023px) {
background-color: rgba(0, 130, 150, 0.08);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Flex, Text } from "@aws-amplify/ui-react";
import { IconArrow } from "@demo/assets/svgs";
import { appConfig } from "@demo/core/constants";
import { useMap } from "@demo/hooks";
import { getFlagEmoji } from "@demo/utils";
import { getFlagEmoji, isUserDeviceIsWin } from "@demo/utils";
import { useTranslation } from "react-i18next";
import "./styles.scss";
import { Tooltip } from "react-tooltip";
Expand Down Expand Up @@ -48,7 +48,7 @@ const PoliticalViewDropdown: FC<PoliticalViewDropdownProps> = ({
}, []);

const handleClick = useCallback(
(option: { alpha2: string; alpha3: string; desc: string }) => {
(option: { alpha2: string; alpha3: string; desc: string; isSupportedByPlaces: boolean }) => {
setMapPoliticalView(option);
setOpen(false);
},
Expand Down Expand Up @@ -83,21 +83,23 @@ const PoliticalViewDropdown: FC<PoliticalViewDropdownProps> = ({
{disabled && <Tooltip id="dropdown-trigger-political-view" />}
{open && (
<ul data-testid="dropdown-options" className={bordered ? "options bordered" : "options"}>
{MAP_POLITICAL_VIEWS.map(({ alpha2, alpha3, desc }) => {
{MAP_POLITICAL_VIEWS.map(({ alpha2, alpha3, desc, isSupportedByPlaces }) => {
return (
<li
data-testid={desc}
key={desc}
style={{ display: "flex", justifyContent: "start", direction: langDir }}
onClick={() => handleClick({ alpha2, alpha3, desc })}
onClick={() => handleClick({ alpha2, alpha3, desc, isSupportedByPlaces })}
>
<Flex gap={0} direction="column" padding="0.46rem 1.23rem">
{!!alpha2 && !!alpha3 ? (
<>
<Flex gap={0}>
<Flex gap={0} justifyContent="center" margin="0.07rem 0.3rem 0 0">
{getFlagEmoji(alpha2)}
</Flex>
{!isUserDeviceIsWin() && (
<Flex gap={0} justifyContent="center" margin="0.07rem 0.3rem 0 0">
{getFlagEmoji(alpha2)}
</Flex>
)}
<Text className="bold small-text" color="var(--tertiary-color)">
{alpha3}
</Text>
Expand Down
1 change: 1 addition & 0 deletions src/atomicui/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./ExploreButton";
export * from "./NLSearchLoader";
export * from "./ToggleSwitch";
export * from "./PoliticalViewDropdown";
export * from "./MapLanguageDropdown";
3 changes: 2 additions & 1 deletion src/atomicui/molecules/MapButtons/MapButtons.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const mockUseMapData = {
alpha2: "",
alpha3: "",
desc: faker.random.word()
}
},
mapLanguage: { value: "en", label: "English" }
};

const mockUseGeofenceData = {
Expand Down
21 changes: 14 additions & 7 deletions src/atomicui/molecules/MapButtons/MapButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FC, memo, useCallback, useEffect, useMemo, useRef, useState } from "rea

import { Card, Divider, Flex, Placeholder, Text } from "@aws-amplify/ui-react";
import { IconClose, IconDark, IconGeofencePlusSolid, IconLight, IconMapSolid, IconRadar } from "@demo/assets/svgs";
import { PoliticalViewDropdown } from "@demo/atomicui/atoms";
import { MapLanguageDropdown, PoliticalViewDropdown } from "@demo/atomicui/atoms";
import { appConfig } from "@demo/core/constants";
import { useAuth, useGeofence, useMap, useUnauthSimulation } from "@demo/hooks";
import useBottomSheet from "@demo/hooks/useBottomSheet";
Expand Down Expand Up @@ -154,7 +154,7 @@ const MapButtons: FC<MapButtonsProps> = ({
if (mapStyle !== style) {
onShowGridLoader();
style === MapStyleEnum.SATELLITE &&
setMapPoliticalView({ alpha2: "", alpha3: "", desc: "no_political_view.text" });
setMapPoliticalView({ alpha2: "", alpha3: "", desc: "no_political_view.text", isSupportedByPlaces: false });
setMapStyle(style);
record([
{
Expand Down Expand Up @@ -230,7 +230,7 @@ const MapButtons: FC<MapButtonsProps> = ({
</Flex>
))}
</Flex>
<Flex gap={0} padding="0 1rem">
<Flex gap={0} padding="1rem">
<Flex
gap={0}
borderRadius="0.61rem"
Expand Down Expand Up @@ -289,10 +289,17 @@ const MapButtons: FC<MapButtonsProps> = ({
))}
</Flex>
</Flex>
<Flex gap={0} padding="2rem 1rem 0rem 1rem" direction="column">
<Flex gap={0} padding="1rem 0rem">
<PoliticalViewDropdown bordered disabled={mapStyle === MapStyleEnum.SATELLITE} />
</Flex>
<Flex gap={0} padding="1rem" direction="column">
<Text className="bold small-text" marginBottom="0.4rem">
{t("political_views.text")}
</Text>
<PoliticalViewDropdown bordered disabled={mapStyle === MapStyleEnum.SATELLITE} />
</Flex>
<Flex gap={0} padding="1rem" direction="column">
<Text className="bold small-text" marginBottom="0.4rem">
{t("map_languages.text")}
</Text>
<MapLanguageDropdown bordered />
</Flex>
</Flex>
</Flex>
Expand Down
Loading

0 comments on commit 1eb4e24

Please sign in to comment.