diff --git a/apps/climatemappedafrica/src/components/DropdownSearch/DownloadSearch.js b/apps/climatemappedafrica/src/components/DropdownSearch/DownloadSearch.js new file mode 100644 index 000000000..dc9f31362 --- /dev/null +++ b/apps/climatemappedafrica/src/components/DropdownSearch/DownloadSearch.js @@ -0,0 +1,186 @@ +import { + IconButton, + InputBase, + Typography, + List, + ListItem, + SvgIcon, + Box, +} from "@mui/material"; +import { useRouter } from "next/router"; +import PropTypes from "prop-types"; +import React, { useEffect, useState } from "react"; + +import SearchIcon from "@/climatemappedafrica/assets/icons/search.svg"; +import Link from "@/climatemappedafrica/components/Link"; + +function DropdownSearch({ + href: hrefProp = "/explore", + label = "Search for a location", + locations, + onClick, + icon: IconProp = SearchIcon, + placeholder, + variant, + ...props +}) { + const router = useRouter(); + const [query, setQuery] = useState(""); + const [selectedLocation, setSelectedLocation] = useState(null); + const [suggestions, setSuggestions] = useState([]); + + const handleChange = (e) => { + setQuery(e.target.value); + setSelectedLocation(null); + }; + + const handleSelect = (code, name) => { + setQuery(name.toLowerCase()); + setSelectedLocation(code); + if (code && hrefProp?.length) { + router.push(`${hrefProp}/${code}`); + } + }; + + useEffect(() => { + if (query?.length > 0 && !selectedLocation) { + const matchedGeo = locations?.filter(({ name }) => + name.toLowerCase()?.startsWith(query.toLowerCase()), + ); + setSuggestions(matchedGeo); + } else { + setSuggestions([]); + } + }, [locations, selectedLocation, query]); + + const handleClickSearch = () => { + if (onClick) { + onClick(selectedLocation); + } else if (selectedLocation) { + const href = `${hrefProp}/${selectedLocation}`; + router.push(href); + } else if (query) { + router.push("/404"); + } + }; + + let iconComponent = SearchIcon; + let iconBorder; + if (variant === "explore") { + iconComponent = IconProp; + iconBorder = { + borderRadius: "50%", + border: "2px solid #fff", + }; + } + const searchIconButton = ( + ({ + padding: 0, + ml: 2, + })} + > + + + ); + + return ( + + ({ + color: palette.text.primary, + marginBottom: typography.pxToRem(10), + })} + > + {label} + + ({ + borderRadius: typography.pxToRem(10), + color: palette.primary.main, + border: `2px solid ${palette.text.hint}`, + width: typography.pxToRem(278), + backgroundColor: "inherit", + height: typography.pxToRem(48), + padding: `0 ${typography.pxToRem(20)}`, + "&.MuiInputBase-input": { + backgroundColor: "inherit", + height: typography.pxToRem(48), + borderRadius: typography.pxToRem(10), + padding: `0 ${typography.pxToRem(20)}`, + textTransform: "capitalize", + }, + "&.Mui-focused": { + border: `2px solid ${palette.primary.main}`, + }, + ...props.sx, + })} + endAdornment={variant === "explore" ? searchIconButton : null} + /> + {variant !== "explore" && searchIconButton} + + + {suggestions?.length > 0 && ( + ({ + width: typography.pxToRem(278), + position: "absolute", + marginTop: typography.pxToRem(5), + zIndex: 10, + background: palette.background.default, + border: `2px solid ${palette.grey.main}`, + borderRadius: typography.pxToRem(10), + padding: 0, + textTransform: "capitalize", + })} + > + {suggestions.map(({ name, code }) => ( + handleSelect(code, name)} + sx={({ typography, palette }) => ({ + paddingLeft: typography.pxToRem(20), + color: palette.text.hint, + })} + key={code} + > + {name.toLowerCase()} + + ))} + + )} + + + ); +} + +DropdownSearch.propTypes = { + label: PropTypes.string, + href: PropTypes.string, + onClick: PropTypes.func, + icon: PropTypes.elementType, + locations: PropTypes.arrayOf(PropTypes.shape({})), + variant: PropTypes.string, + placeholder: PropTypes.string, +}; + +export default DropdownSearch; diff --git a/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.snap.js b/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.snap.js new file mode 100644 index 000000000..d8dd3412a --- /dev/null +++ b/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.snap.js @@ -0,0 +1,46 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders unchanged 1`] = ` +
+ +`; diff --git a/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.test.js b/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.test.js new file mode 100644 index 000000000..c50350da4 --- /dev/null +++ b/apps/climatemappedafrica/src/components/DropdownSearch/DropdownSearch.test.js @@ -0,0 +1,25 @@ +import { createRender } from "@commons-ui/testing-library"; +import React from "react"; + +import DropdownSearch from "./DownloadSearch"; + +import theme from "@/climatemappedafrica/theme"; + +// eslint-disable-next-line testing-library/render-result-naming-convention +const render = createRender({ theme }); + +const defaultProps = { + href: "/explore", + label: "Search for a location", + locations: [], + icon: null, + placeholder: "Search for a location", + variant: "explore", +}; + +describe("", () => { + it("renders unchanged", () => { + const { container } = render(); + expect(container).toMatchSnapshot(); + }); +}); diff --git a/apps/climatemappedafrica/src/components/DropdownSearch/index.js b/apps/climatemappedafrica/src/components/DropdownSearch/index.js index dfb33c3f3..958811b94 100644 --- a/apps/climatemappedafrica/src/components/DropdownSearch/index.js +++ b/apps/climatemappedafrica/src/components/DropdownSearch/index.js @@ -1,195 +1,3 @@ -import { - IconButton, - InputBase, - Typography, - List, - ListItem, - SvgIcon, -} from "@mui/material"; -import makeStyles from "@mui/styles/makeStyles"; -import { useRouter } from "next/router"; -import PropTypes from "prop-types"; -import React, { useEffect, useState } from "react"; - -import SearchIcon from "@/climatemappedafrica/assets/icons/search.svg"; -import Link from "@/climatemappedafrica/components/Link"; - -const useStyles = makeStyles(({ palette, typography }) => ({ - root: {}, - inputRoot: { - borderRadius: typography.pxToRem(10), - color: palette.primary.main, - border: `2px solid ${palette.text.hint}`, - width: typography.pxToRem(278), - }, - focused: { - border: `2px solid ${palette.primary.main}`, - }, - label: { - color: palette.text.secondary, - marginBottom: typography.pxToRem(10), - }, - button: { - padding: 0, - marginLeft: typography.pxToRem(15), - }, - input: { - backgroundColor: "inherit", - height: typography.pxToRem(48), - borderRadius: typography.pxToRem(10), - padding: `0 ${typography.pxToRem(20)}`, - textTransform: "capitalize", - }, - suggestions: { - position: "relative", - }, - selectMenu: { - width: typography.pxToRem(278), - position: "absolute", - marginTop: typography.pxToRem(5), - zIndex: 10, - background: palette.background.default, - border: `2px solid ${palette.grey.main}`, - borderRadius: typography.pxToRem(10), - padding: 0, - textTransform: "capitalize", - }, - menuList: {}, - menuItem: { - paddingLeft: typography.pxToRem(20), - color: palette.text.hint, - }, -})); - -function DropdownSearch({ - href: hrefProp = "/explore", - label = "Search for a location", - locations, - onClick, - icon: IconProp = SearchIcon, - placeholder, - variant, - ...props -}) { - const classes = useStyles(props); - const router = useRouter(); - const [query, setQuery] = useState(""); - const [selectedLocation, setSelectedLocation] = useState(null); - const [suggestions, setSuggestions] = useState([]); - - const handleChange = (e) => { - setQuery(e.target.value); - setSelectedLocation(null); - }; - - const handleSelect = (code, name) => { - setQuery(name.toLowerCase()); - setSelectedLocation(code); - if (code && hrefProp?.length) { - router.push(`${hrefProp}/${code}`); - } - }; - - useEffect(() => { - if (query?.length > 0 && !selectedLocation) { - const matchedGeo = locations?.filter(({ name }) => - name.toLowerCase()?.startsWith(query.toLowerCase()), - ); - setSuggestions(matchedGeo); - } else { - setSuggestions([]); - } - }, [locations, selectedLocation, query]); - - const handleClickSearch = () => { - if (onClick) { - onClick(selectedLocation); - } else if (selectedLocation) { - const href = `${hrefProp}/${selectedLocation}`; - router.push(href); - } else if (query) { - router.push("/404"); - } - }; - - let iconComponent = SearchIcon; - let iconBorder; - if (variant === "explore") { - iconComponent = IconProp; - iconBorder = { - borderRadius: "50%", - border: "2px solid #fff", - }; - } - const searchIconButton = ( - - - - ); - - return ( - - ); -} - -DropdownSearch.propTypes = { - label: PropTypes.string, - href: PropTypes.string, - onClick: PropTypes.func, - icon: PropTypes.elementType, - locations: PropTypes.arrayOf(PropTypes.shape({})), - variant: PropTypes.string, - placeholder: PropTypes.string, -}; +import DropdownSearch from "./DownloadSearch"; export default DropdownSearch; diff --git a/apps/climatemappedafrica/src/components/DropdownSearch/index.stories.js b/apps/climatemappedafrica/src/components/DropdownSearch/index.stories.js deleted file mode 100644 index eabebcb54..000000000 --- a/apps/climatemappedafrica/src/components/DropdownSearch/index.stories.js +++ /dev/null @@ -1,43 +0,0 @@ -import React from "react"; - -import DropdownSearch from "."; - -export default { - title: "Components/DropdownSearch", - argTypes: { - title: { - control: { - type: "string", - }, - }, - counties: { - control: { - type: "object", - }, - }, - }, -}; - -function Template({ ...args }) { - return ; -} - -export const Default = Template.bind({}); - -Default.args = { - label: "Search for Location", - counties: [ - { - name: "Nairobi", - code: 47, - }, - { - name: "Marsabit", - code: 10, - }, - { - name: "Meru", - code: 6, - }, - ], -}; diff --git a/apps/climatemappedafrica/src/components/Hero/Hero.js b/apps/climatemappedafrica/src/components/Hero/Hero.js index 8c687dbc8..21de5f19f 100644 --- a/apps/climatemappedafrica/src/components/Hero/Hero.js +++ b/apps/climatemappedafrica/src/components/Hero/Hero.js @@ -49,24 +49,16 @@ function Hero({
- renders unchanged 1`] = ` />
-
@@ -32,7 +29,7 @@ exports[` renders unchanged 1`] = ` class="MuiBox-root css-0" >

renders unchanged 1`] = `