Skip to content

Commit

Permalink
feat: update My Locations to use custom dropdown [PT-188162567]
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanik committed Aug 27, 2024
1 parent 5f90c7b commit e40d70d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/components/dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.day-length-dropdown-container {
box-sizing: border-box;
font-size: 12px;
font-weight: 500;

.day-length-dropdown-label {
position: relative;
Expand Down Expand Up @@ -31,6 +32,7 @@
left: 6px;
top: 6px;
z-index: 1;
pointer-events: none;
}

.dropdown-main-button, input {
Expand Down Expand Up @@ -65,8 +67,18 @@
}

.dropdown-main-button {
background-color: white;
font-weight: 600;
cursor: pointer;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

&.iconPresent {
padding-left: 28px;
font-weight: 500;
}

&:hover {
background-color: $codap-teal-lightest;
}
Expand Down Expand Up @@ -101,6 +113,7 @@

button {
font-size: 12px;
font-weight: 500;
font-family: "Montserrat", sans-serif;
border: none;
background-color: white;
Expand Down
18 changes: 13 additions & 5 deletions src/components/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import DropdownArrow from "../assets/images/dropdown-arrow-icon.svg";

import "./dropdown.scss";

interface IOption {
export interface IOption {
name: string;
value?: string;
}

interface IDropdownProps<T extends IOption> {
value: string;
options: T[];
onSelect: (place: T) => void;
onSelect: (option: T) => void;
onSearchChange?: (value: string) => void;
label: string;
inputPlaceholder?: string;
Expand Down Expand Up @@ -65,7 +65,7 @@ export const Dropdown = <T extends IOption>({
setShowOptions(false);
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement | HTMLButtonElement>) => {
if (!showOptions) return;

switch (event.key) {
Expand All @@ -92,6 +92,8 @@ export const Dropdown = <T extends IOption>({
}
};

const iconPresent = !!icon;

return (
<div className={clsx("day-length-dropdown-container", { inline })}>
<div className="day-length-dropdown-label">
Expand All @@ -109,7 +111,13 @@ export const Dropdown = <T extends IOption>({
/>
) : (
<div className="dropdown-main-button-container">
<button className="dropdown-main-button" onClick={handleMainButtonClick}>{value}</button>
<button
className={clsx("dropdown-main-button", { iconPresent })}
onClick={handleMainButtonClick}
onKeyDown={handleKeyDown}
>
{value}
</button>
<DropdownArrow className={clsx("arrow-icon", { up: showOptions })} />
</div>
)
Expand All @@ -129,7 +137,7 @@ export const Dropdown = <T extends IOption>({
onMouseLeave={() => setFocusedOptionIndex(-1)}
className={clsx({
focused: focusedOptionIndex === index ? "focused" : "",
iconPresent: icon
iconPresent
})
}
>
Expand Down
28 changes: 16 additions & 12 deletions src/grasp-seasons/components/my-locations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { Component } from "react";
import t, { Language } from "../translation/translate";
import { ILocation } from "../../types";
import { locationsEqual } from "../../utils/daylight-utils";
import { Dropdown, IOption } from "../../components/dropdown";
import LocationIcon from "../../assets/images/icon-location.svg";

import "./my-locations.scss";

Expand All @@ -19,22 +21,20 @@ export default class MyLocations extends Component<IProps> {
this.selectChange = this.selectChange.bind(this);
}

selectChange(event: any) {
selectChange(option: IOption) {
const { onLocationChange, locations } = this.props;
const location = locations[event.target.value];
if (location.latitude && location.longitude) {
const location = locations[Number(option.value)];
if (location && location.latitude && location.longitude) {
onLocationChange(location.latitude, location.longitude, location.name);
}
}

getOptions() {
const { locations } = this.props;
const options = this.selectedLocation === "" ? [
<option key="unsaved" value="" disabled={true}>Custom Location (unsaved)</option>
] : [];
const options = [];
for (let i = 0; i < locations.length; i++) {
const loc = locations[i];
options.push(<option key={i} value={i}>{ loc.name || `(${loc.latitude}, ${loc.longitude})` }</option>);
options.push({ name: loc.name || `(${loc.latitude}, ${loc.longitude})`, value: i.toString() });
}
return options;
}
Expand All @@ -44,7 +44,7 @@ export default class MyLocations extends Component<IProps> {
const currentLocation: ILocation = { latitude: lat, longitude: long, name: "" };
for (let i = 0; i < locations.length; i++) {
if (locationsEqual(currentLocation, locations[i])) {
return i;
return locations[i].name;
}
}
return ""; // custom location
Expand All @@ -53,10 +53,14 @@ export default class MyLocations extends Component<IProps> {
render() {
return (
<div className="my-locations">
<label>{ t("~MY_LOCATIONS", this.props.lang) }</label>
<select className="form-control" value={this.selectedLocation} onChange={this.selectChange}>
{ this.getOptions() }
</select>
<Dropdown
width="250px"
value={this.selectedLocation}
options={this.getOptions()}
onSelect={this.selectChange}
label={t("~MY_LOCATIONS", this.props.lang)}
icon={<LocationIcon />}
/>
</div>
);
}
Expand Down

0 comments on commit e40d70d

Please sign in to comment.