Skip to content

Commit 25cbfb4

Browse files
Merge pull request #304 from opentripplanner/houston-location-field-fix
fix(packages/location-field): generate better labels for all fields where locations are shown
2 parents b7453e0 + e82d415 commit 25cbfb4

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

__snapshots__/storybook.test.ts.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77326,7 +77326,9 @@ exports[`Storyshots LocationField/Mobile Context With Custom Icons 2`] = `
7732677326
123 Main St
7732777327
<span
7732877328
className="c19"
77329-
/>
77329+
>
77330+
77331+
</span>
7733077332
</div>
7733177333
</div>
7733277334
</a>
@@ -78349,7 +78351,9 @@ exports[`Storyshots LocationField/Mobile Context With Session Searches 2`] = `
7834978351
123 Main St
7835078352
<span
7835178353
className="c14"
78352-
/>
78354+
>
78355+
78356+
</span>
7835378357
</div>
7835478358
</div>
7835578359
</a>

packages/location-field/src/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
UserLocationIcon
2020
} from "./options";
2121
import * as S from "./styled";
22-
import { generateLabel } from "./utils";
22+
import { generateLabel, getCombinedLabel } from "./utils";
2323

2424
// FIXME have a better key generator for options
2525
let optionKey = 0;
@@ -98,7 +98,8 @@ class LocationField extends Component {
9898
/* FIXME only disabled this because it'd take longer to refactor */
9999
/* eslint-disable-next-line */
100100
this.setState({
101-
value: location !== null ? location.name : "",
101+
// location could be null if none is set
102+
value: location?.name || "",
102103
geocodedFeatures: []
103104
});
104105
}
@@ -114,7 +115,8 @@ class LocationField extends Component {
114115
*/
115116
getValueFromLocation = () => {
116117
const { hideExistingValue, location } = this.props;
117-
return location && !hideExistingValue ? location.name : "";
118+
const label = location?.name || "";
119+
return location && !hideExistingValue ? label : "";
118120
};
119121

120122
setLocation(location, resultType) {
@@ -326,11 +328,19 @@ class LocationField extends Component {
326328
operatorIconMap
327329
} = this.props;
328330
const { activeIndex } = this.state;
331+
332+
// generate the friendly labels for this feature
333+
const { main, secondary } = generateLabel(feature.properties);
334+
329335
// Create the selection handler
330336
const locationSelected = () => {
331337
getGeocoder(geocoderConfig)
332338
.getLocationFromGeocodedFeature(feature)
333339
.then(geocodedLocation => {
340+
// add the friendly location labels for use later on
341+
geocodedLocation.main = main;
342+
geocodedLocation.secondary = secondary;
343+
geocodedLocation.name = getCombinedLabel(feature.properties);
334344
// Set the current location
335345
this.setLocation(geocodedLocation, "GEOCODE");
336346
// Add to the location search history. This is intended to
@@ -360,7 +370,6 @@ class LocationField extends Component {
360370
classNames.push(`layer-${layer}`);
361371

362372
// Create and return the option menu item
363-
const { main, secondary } = generateLabel(feature.properties);
364373
return (
365374
<Option
366375
classes={classNames.join(" ")}
@@ -539,13 +548,14 @@ class LocationField extends Component {
539548

540549
// Add to the selection handler lookup (for use in onKeyDown)
541550
this.locationSelectedLookup[itemIndex] = locationSelected;
542-
543551
// Create and return the option menu item
544552
const option = (
545553
<Option
546554
icon={sessionOptionIcon}
547555
key={optionKey++}
548-
title={sessionLocation.name}
556+
// just use the name if there is no main/secondary field
557+
title={sessionLocation.main || sessionLocation.name}
558+
subTitle={sessionLocation.secondary || ""}
549559
onClick={locationSelected}
550560
isActive={itemIndex === activeIndex}
551561
/>
@@ -856,7 +866,9 @@ LocationField.propTypes = {
856866
location: PropTypes.shape({
857867
lat: PropTypes.number,
858868
lon: PropTypes.number,
859-
name: PropTypes.string
869+
name: PropTypes.string,
870+
main: PropTypes.string,
871+
secondary: PropTypes.string
860872
}),
861873
/**
862874
* A custom component for rendering the icon displayed to the left of the text

packages/location-field/src/utils.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ const layerDisplayMap = {
4242
* to generate an appropriate title subtitle pair, or return the label if the layer is
4343
* unknown.
4444
*/
45-
// TODO: Remove this exception once more utils are added
46-
// eslint-disable-next-line import/prefer-default-export
4745
export const generateLabel = properties => {
4846
const labelGenerator = layerDisplayMap[properties.layer];
4947
if (!labelGenerator) return { main: properties.label };
5048

5149
return labelGenerator(properties);
5250
};
51+
52+
/**
53+
* Generates a combined label from main and secondary for display in the main input field
54+
*/
55+
export const getCombinedLabel = properties => {
56+
const { main, secondary } = generateLabel(properties);
57+
if (main && secondary) {
58+
return `${main}, ${secondary}`;
59+
}
60+
return properties?.label || "";
61+
};

0 commit comments

Comments
 (0)