Skip to content

Commit 2786bcc

Browse files
authored
fix: table and tooltip title casing fix (#2386)
1 parent 5fc5945 commit 2786bcc

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

packages/core/components/DataTable/helpers/mapCellMatrix.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ type MapRowsProps = DataTableProps & {
1212
rows: string[]
1313
}
1414

15-
const getGeoLabel = (config, row, formatLegendLocation, displayGeoName) => {
15+
const getGeoLabel = (config, row, formatLegendLocation, displayGeoName, runtimeData = null) => {
1616
const { geoType, type } = config.general
17+
1718
let labelValue
1819
if (!['single-state', 'us-county'].includes(geoType) || type === 'us-geocode') {
20+
// Use the row (UID) for lookup - this allows "US-AL" to become "Alabama"
1921
labelValue = displayGeoName(row)
22+
23+
// If displayGeoName returned the same value (not found in lookups), use the raw imported data
24+
if (labelValue === row && runtimeData && config.columns?.geo?.name) {
25+
const rawGeoValue = runtimeData[row]?.[config.columns.geo.name]
26+
if (rawGeoValue && rawGeoValue !== row) {
27+
labelValue = rawGeoValue
28+
}
29+
}
30+
2031
labelValue = String(labelValue).startsWith('region') ? _.capitalize(labelValue) : labelValue
2132
} else {
2233
labelValue = formatLegendLocation(row)
@@ -58,7 +69,7 @@ export const getMapRowData = (
5869
].map(column => {
5970
const label = columns[column]?.label || columns[column]?.name || column
6071
if (column === 'geo') {
61-
dataRow[label] = getGeoLabel(config, row, formatLegendLocation, displayGeoName)
72+
dataRow[label] = getGeoLabel(config, row, formatLegendLocation, displayGeoName, runtimeData)
6273
} else if (filterColumns.includes(column)) {
6374
dataRow[label] = runtimeData[row][column]
6475
} else {
@@ -100,7 +111,7 @@ const mapCellArray = ({
100111
if (!legendColor) {
101112
console.error('No legend color found') // eslint-disable-line no-console
102113
}
103-
const labelValue = getGeoLabel(config, row, formatLegendLocation, displayGeoName)
114+
const labelValue = getGeoLabel(config, row, formatLegendLocation, displayGeoName, runtimeData)
104115
const mapZoomHandler =
105116
type === 'bubble' && allowMapZoom && geoType === 'world' ? () => setFilteredCountryCode(row) : undefined
106117

packages/map/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
<body>
3232
<!-- DEFAULT EXAMPLES -->
33-
<div class="react-container" data-config="/examples/example-city-state.json"></div>
33+
<!-- <div class="react-container" data-config="/examples/example-city-state.json"></div> -->
34+
<div class="react-container" data-config="/examples/private/measles.json"></div>
3435

3536
<noscript>You need to enable JavaScript to run this app.</noscript>
3637
<script type="module" src="./src/index.jsx"></script>

packages/map/src/helpers/displayGeoName.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
supportedTerritories,
55
supportedCountries,
66
supportedCounties,
7+
supportedCities,
78
stateKeys,
89
territoryKeys,
910
countryKeys,
10-
countyKeys
11+
countyKeys,
12+
cityKeys
1113
} from '../data/supported-geos'
1214

1315
/**
@@ -20,25 +22,35 @@ import {
2022
export const displayGeoName = (key: string, convertFipsCodes = true): string => {
2123
if (!convertFipsCodes) return key
2224
let value = key
25+
let wasLookedUp = false
2326

2427
// Map to first item in values array which is the preferred label
2528
if (stateKeys.includes(value)) {
2629
value = titleCase(supportedStates[key][0])
30+
wasLookedUp = true
2731
}
2832

2933
if (territoryKeys.includes(value)) {
3034
value = titleCase(supportedTerritories[key][0])
35+
wasLookedUp = true
3136
if (value === 'U.s. Virgin Islands') {
3237
value = 'U.S. Virgin Islands'
3338
}
3439
}
3540

3641
if (countryKeys.includes(value)) {
3742
value = titleCase(supportedCountries[key][0])
43+
wasLookedUp = true
3844
}
3945

4046
if (countyKeys.includes(value)) {
4147
value = titleCase(supportedCounties[key])
48+
wasLookedUp = true
49+
}
50+
51+
if (cityKeys.includes(value)) {
52+
value = titleCase(String(value) || '')
53+
wasLookedUp = true
4254
}
4355

4456
const dict = {
@@ -51,11 +63,14 @@ export const displayGeoName = (key: string, convertFipsCodes = true): string =>
5163

5264
if (Object.keys(dict).includes(value)) {
5365
value = dict[value]
66+
wasLookedUp = true
5467
}
55-
// if you get here and it's 2 letters then DONT titleCase state abbreviations like "AL"
56-
if (value?.length === 2 || value === 'U.S. Virgin Islands') {
68+
69+
// If value was looked up from our dictionaries and needs formatting, or if it's a 2-letter abbreviation, return as-is
70+
if (value?.length === 2 || value === 'U.S. Virgin Islands' || wasLookedUp) {
5771
return value
5872
} else {
73+
// Value is user data that wasn't in our lookups - return as-is to preserve original formatting
5974
return value
6075
}
6176
}

0 commit comments

Comments
 (0)