Skip to content

Commit

Permalink
Merge pull request #2675 from owid/grapher-redesign-entity-selector-e…
Browse files Browse the repository at this point in the history
…mpty-state

Grapher redesign: Add feedback for empty search result in entity selector
  • Loading branch information
sophiamersmann authored Sep 29, 2023
2 parents cb42c88 + 789ae0d commit acae113
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,10 @@ export class Grapher
)
}

@computed get entitiesAreCountryLike(): boolean {
return !!this.entityType.match(/\bcountry\b/i)
}

@computed get startSelectingWhenLineClicked(): boolean {
return this.showAddEntityButton
}
Expand Down
11 changes: 4 additions & 7 deletions packages/@ourworldindata/grapher/src/dataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface DataTableManager {
startTime?: Time
dataTableSlugs?: ColumnSlug[]
showSelectionOnlyInDataTable?: boolean
entitiesAreCountryLike?: boolean
isSmall?: boolean
isMedium?: boolean
isNarrow?: boolean
Expand Down Expand Up @@ -164,10 +165,6 @@ export class DataTable extends React.Component<{
return this.manager.entityType ?? "Country/area"
}

@computed private get entitiesAreCountryLike(): boolean {
return !!this.entityType.match(/\bcountry\b/i)
}

@computed private get sortValueMapper(): (
row: DataTableRow
) => number | string {
Expand Down Expand Up @@ -893,18 +890,18 @@ export class DataTable extends React.Component<{
}

@computed private get displayEntityRows(): DataTableRow[] {
if (!this.entitiesAreCountryLike) return this.displayRows
if (!this.manager.entitiesAreCountryLike) return this.displayRows
return this.displayRows.filter((row) => isCountryName(row.entityName))
}

@computed private get displayAggregateRows(): DataTableRow[] {
if (!this.entitiesAreCountryLike) return []
if (!this.manager.entitiesAreCountryLike) return []
return this.displayRows.filter((row) => !isCountryName(row.entityName))
}

@computed private get showTitleRows(): boolean {
return (
this.entitiesAreCountryLike &&
!!this.manager.entitiesAreCountryLike &&
this.displayEntityRows.length > 0 &&
this.displayAggregateRows.length > 0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
}
}

.searchResults .empty {
font-size: 0.8125em;
}

&.EntitySelectorSingle {
ul {
margin-top: 8px;
Expand Down Expand Up @@ -109,6 +113,10 @@
font-weight: 900;
}
}

.searchResults .empty {
margin-top: 1.5em;
}
}

&.EntitySelectorMulti {
Expand Down
37 changes: 24 additions & 13 deletions packages/@ourworldindata/grapher/src/modal/EntitySelectorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DEFAULT_BOUNDS,
isTouchDevice,
sortBy,
isCountryName,
} from "@ourworldindata/utils"
import { Checkbox } from "../controls/Checkbox"
import { FuzzySearch } from "../controls/FuzzySearch"
Expand All @@ -24,6 +25,7 @@ export interface EntitySelectorModalManager {
tabBounds?: Bounds
entityType?: string
entityTypePlural?: string
entitiesAreCountryLike?: boolean
}

interface SearchableEntity {
Expand Down Expand Up @@ -228,19 +230,28 @@ export class EntitySelectorModal extends React.Component<{

<div className="entities">
<div className="searchResults">
<ul>
{searchResults.map((result) => (
<EntitySearchResult
key={result.name}
result={result}
isMulti={this.isMulti}
isChecked={selectionArray.selectedSet.has(
result.name
)}
onSelect={this.onSelect}
/>
))}
</ul>
{searchResults.length > 0 ? (
<ul>
{searchResults.map((result) => (
<EntitySearchResult
key={result.name}
result={result}
isMulti={this.isMulti}
isChecked={selectionArray.selectedSet.has(
result.name
)}
onSelect={this.onSelect}
/>
))}
</ul>
) : (
<div className="empty">
{this.manager.entitiesAreCountryLike &&
isCountryName(this.searchInput)
? "There is no data for the country, region or group you are looking for."
: "Nothing turned up. You may want to try using different keywords or checking for typos."}
</div>
)}
</div>
{this.renderSelectedData()}
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/utils/src/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ const countriesBySlug: Record<string, Country> = Object.fromEntries(

const currentAndHistoricalCountryNames = regions
.filter(({ regionType }) => regionType === "country")
.map(({ name }) => name)
.map(({ name }) => name.toLowerCase())

export const isCountryName = (name: string): boolean =>
currentAndHistoricalCountryNames.includes(name)
currentAndHistoricalCountryNames.includes(name.toLowerCase())

export const getCountryBySlug = (slug: string): Country | undefined =>
countriesBySlug[slug]

0 comments on commit acae113

Please sign in to comment.