Skip to content

Commit 46dfc02

Browse files
committed
customization: templates: namespace feature as item.feature
by moving the actual feature data one level down we can add metadata to the top level of the object, such as `active`, `index`, and `searchTerm`, so the template can use ${item.feature.properties} ${item.active} that way the meta properties don’t conflict with the geo data itself
1 parent 90193d0 commit 46dfc02

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ If you want to customize how a feature is turned into a string for rendering (in
153153
```html
154154
<ge-autocomplete api_key="">
155155
<template string>
156-
${feature.properties.name} (${feature.properties.id}, ${feature.properties.source})
156+
${item.feature.properties.name} (${item.feature.properties.id}, ${item.feature.properties.source})
157157
</template>
158158
</ge-autocomplete>
159159
```
@@ -167,15 +167,15 @@ Similar to the string template mentioned above, you can use the row template to
167167
```html
168168
<ge-autocomplete api_key="">
169169
<template row>
170-
<div class="custom-row ${feature.active ? 'custom-row--active' : null}">
171-
<img src="/flags/${feature.properties.country_a.png}" alt="${feature.properties.country_a}">
172-
<span>${feature.properties.label}</span>
170+
<div class="custom-row ${item.active ? 'custom-row--active' : null}">
171+
<img src="/flags/${item.feature.properties.country_a.png}" alt="${item.feature.properties.country_a}">
172+
<span>${item.feature.properties.label}</span>
173173
</div>
174174
</template>
175175
</ge-autocomplete>
176176
```
177177

178-
**Pro Tip™:** Use the `active` property to check if the current row is being hovered over or activated via arrow keys.
178+
**Pro Tip™:** Use the `item.active` property to check if the current row is being hovered over or activated via arrow keys.
179179

180180
The example above could render a little flag icon for the result’s country, for example. You can customize the styling by defining custom classes in the same way you would customize the CSS variables. It’s best to prefix your classes to avoid conflicts with internal classnames of the element.
181181

src/autocomplete/autocomplete.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ export default ({
107107
}
108108

109109
// turns an autocomplete result (feature) into a string
110-
const itemToString = (feature) => {
110+
const itemToString = (feature, extra) => {
111111
if (typeof stringTemplate === 'function') {
112-
return stringTemplate(escape(feature))
112+
return stringTemplate({feature, ...extra})
113113
}
114114

115115
return feature.properties.label
@@ -166,12 +166,15 @@ export default ({
166166
return <li
167167
key={item.properties.id}
168168
{...getItemProps({ item, index })}
169-
dangerouslySetInnerHTML={{ __html: rowTemplate(escape({
170-
...item,
171-
active: highlightedIndex === index
172-
})) }}
173-
/>
174-
} else {
169+
dangerouslySetInnerHTML={{
170+
__html: rowTemplate({
171+
feature: escape(item),
172+
active: highlightedIndex === index,
173+
searchTerm: inputRef.current.value,
174+
index
175+
})}}
176+
/>
177+
} else {
175178
return <li
176179
className={
177180
highlightedIndex === index
@@ -182,7 +185,11 @@ export default ({
182185
{...getItemProps({ item, index })}
183186
>
184187
<LocationMarker className='result-item-icon' />
185-
{itemToString(item)}
188+
{itemToString(item, {
189+
active: highlightedIndex === index,
190+
searchTerm: inputRef.current.value,
191+
index
192+
})}
186193
</li>
187194
}
188195
})}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class GEAutocomplete extends HTMLElement {
164164

165165
this[k] = _template(
166166
_unescape(tmpl.innerHTML.trim()), // unescape is important for `<%` etc. lodash tags
167-
{ variable: 'feature' } // namespace the passed in Feature as `feature` so missing keys don’t throw
167+
{ variable: 'item' } // namespace the passed in item so missing keys don’t throw
168168
)
169169

170170
// contrary to the way custom styles are handled above we remove the <template> when we’re done

0 commit comments

Comments
 (0)