Skip to content

Commit

Permalink
Change configuration from boolean to other first, instead to array of…
Browse files Browse the repository at this point in the history
… geocoder options
  • Loading branch information
amy-corson-ibigroup committed Sep 30, 2024
1 parent 048c899 commit 03c02ac
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 84 deletions.
115 changes: 67 additions & 48 deletions packages/location-field/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const LocationField = ({
GeocodedOptionIconComponent = GeocodedOptionIcon,
geocoderConfig,
getCurrentPosition,
geocoderResultsOrder = ["STATIONS", "STOPS", "OTHER"],
hideExistingValue = false,
initialSearchResults = null,
inputPlaceholder = null,
Expand All @@ -174,7 +175,6 @@ const LocationField = ({
onTextInputClick = null,
operatorIconMap = {},
preferredLayers = [],
renderOtherFirst = false,
sessionOptionIcon = <Search size={ICON_SIZE} />,
sessionSearches = [],
showClearButton = true,
Expand Down Expand Up @@ -645,57 +645,76 @@ const LocationField = ({
const transitFeaturesPresent =
stopFeatures.length > 0 || stationFeatures.length > 0;

const OtherFeaturesHeader = () => (
<S.MenuGroupHeader as={headingType} bgColor="#333" key="other-header">
<FormattedMessage
description="Text for header above the 'other' category of geocoder results"
id="otpUi.LocationField.other"
/>
</S.MenuGroupHeader>
);

const otherFeaturesElements = otherFeatures.map(feature =>
renderFeature(itemIndex++, feature)
);

// Iterate through the geocoder results
menuItems = menuItems.concat(
renderOtherFirst &&
transitFeaturesPresent &&
otherFeatures.length > 0 && <OtherFeaturesHeader />,
renderOtherFirst && otherFeaturesElements,
stationFeatures.length > 0 && (
<S.MenuGroupHeader
as={headingType}
bgColor={layerColorMap.stations}
key="gtfs-stations-header"
>
const FeaturesElements = ({
bgColor,
key,
titleId,
featuresArray
}: {
bgColor: string;
key: string;
titleId: string;
featuresArray: JSX.Element[];
}) => {
const Header = () => (
<S.MenuGroupHeader as={headingType} bgColor={bgColor} key={key}>
<FormattedMessage
description="Text for header above Stations"
id="otpUi.LocationField.stations"
description="Text for header above the 'other' category of geocoder results"
id={`otpUi.LocationField.${titleId}`}
/>
</S.MenuGroupHeader>
),
stationFeatures.map(feature => renderFeature(itemIndex++, feature)),

stopFeatures.length > 0 && (
<S.MenuGroupHeader
as={headingType}
bgColor={layerColorMap.stops}
key="gtfs-stops-header"
>
<FormattedMessage
description="Text for header above Stops"
id="otpUi.LocationField.stops"
);
return (
<>
{/* Only include the header if there are features to show */}
{titleId === "other" ? (
<Header />
) : (
transitFeaturesPresent && <Header />
)}
{featuresArray.map(feature => renderFeature(itemIndex++, feature))}
</>
);
};

// Create an array of results to display based on the geocoderResultsOrder
const featuresElementsArray = geocoderResultsOrder.map(result => {
let Element;
if (result === "OTHER") {
Element = (
<FeaturesElements
bgColor="#333"
key="other-header"
featuresArray={otherFeatures}
titleId="other"
/>
</S.MenuGroupHeader>
),
stopFeatures.map(feature => renderFeature(itemIndex++, feature)),
!renderOtherFirst &&
transitFeaturesPresent &&
otherFeatures.length > 0 && <OtherFeaturesHeader />,
!renderOtherFirst && otherFeaturesElements
);
);
}
if (result === "STATIONS") {
Element = (
<FeaturesElements
bgColor={layerColorMap.stations}
key="gtfs-stations-header"
featuresArray={stationFeatures}
titleId="stations"
/>
);
}
if (result === "STOPS") {
Element = (
<FeaturesElements
bgColor={layerColorMap.stops}
key="gtfs-stops-header"
featuresArray={stopFeatures}
titleId="stops"
/>
);
}
return Element;
});

// Iterate through the geocoder results
menuItems = menuItems.concat(featuresElementsArray);
}

/* 2) Process nearby transit stop options */
Expand Down
51 changes: 19 additions & 32 deletions packages/location-field/src/stories/Desktop.story.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React from "react";
import { ComponentMeta } from "@storybook/react";

import { Ship } from "@styled-icons/fa-solid/Ship";
Expand Down Expand Up @@ -252,34 +252,21 @@ export const WithUserSettings = (): JSX.Element => (
/>
);

export const WithOtherFirst = (): JSX.Element => {
const [otherControl, setOtherControl] = useState(false);
return (
<>
<label htmlFor="other-input">
renderOtherFirst?
<input
id="other-input"
type="checkbox"
checked={otherControl}
onChange={() => setOtherControl(!otherControl)}
/>
</label>
<LocationField
currentPosition={currentPosition}
geocoderConfig={geocoderConfig}
getCurrentPosition={getCurrentPosition}
preferredLayers={["example_layer"]}
initialSearchResults={mockedGeocoderResponse.features}
inputPlaceholder="Select from location"
layerColorMap={layerColorMap}
locationType="from"
onLocationSelected={onLocationSelected}
renderOtherFirst={otherControl}
sortByDistance
style={{ fontFamily: "sans-serif" }}
/>
</>
);
};
WithOtherFirst.parameters = a11yOverrideParameters;
export const WithCustomResultsOrder = (): JSX.Element => (
<LocationField
currentPosition={currentPosition}
geocoderConfig={geocoderConfig}
getCurrentPosition={getCurrentPosition}
preferredLayers={["example_layer"]}
initialSearchResults={mockedGeocoderResponse.features}
inputPlaceholder="Select from location"
layerColorMap={layerColorMap}
locationType="from"
onLocationSelected={onLocationSelected}
geocoderResultsOrder={["OTHER", "STATIONS", "STOPS"]}
sortByDistance
style={{ fontFamily: "sans-serif" }}
/>
);

WithCustomResultsOrder.parameters = a11yOverrideParameters;
6 changes: 2 additions & 4 deletions packages/location-field/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export interface LocationFieldProps {
maxNearbyStops?: number;
type: string;
};
/** Order of geocoder results to display */
geocoderResultsOrder?: Array<"STATIONS" | "STOPS" | "OTHER">;
/**
* This is dispatched when the current position is null. This indicates that
* the user has requested to use the current position, but that the current
Expand Down Expand Up @@ -214,10 +216,6 @@ export interface LocationFieldProps {
* Results are sorted by distance, but favored layers will always appear first.
*/
preferredLayers?: string[];
/**
* If true, make "Other" category (addresses, POIs, etc.) first in the results container.
*/
renderOtherFirst?: boolean;
/**
* A slot for the icon to display for an option that was used during the
* current session.
Expand Down

0 comments on commit 03c02ac

Please sign in to comment.