Skip to content

Commit c6e4561

Browse files
committed
customization: add string template
this template allows customization of the way a Feature is converted into a string and can be used for "smaller" customization where a fully new template with custom styles would be overkill. Also this applies to the resulting string that is placed in the input field after an item is selected. this commit also refactors the way templates are passed to parse them both in a single function instead of duplicating the logic. This also allows easily adding more templates in the future.
1 parent d1d036a commit c6e4561

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/autocomplete/autocomplete.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export default ({
2424
onChange: userOnChange,
2525
onError: userOnError,
2626
environment = window,
27-
rowTemplate
27+
rowTemplate,
28+
stringTemplate
2829
}) => {
2930
const [results, setResults] = useState(emptyResults)
3031
const [isLoading, setIsLoading] = useState(false)
@@ -106,7 +107,13 @@ export default ({
106107
}
107108

108109
// turns an autocomplete result (feature) into a string
109-
const itemToString = ({ properties: { label } }) => label
110+
const itemToString = (feature) => {
111+
if (typeof stringTemplate === 'function') {
112+
return stringTemplate(escape(feature))
113+
}
114+
115+
return feature.properties.label
116+
}
110117

111118
// focus the input field if requested
112119
useEffect(() => {

src/index.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class GEAutocomplete extends HTMLElement {
133133

134134
connectedCallback () {
135135
this.importStyles()
136-
this.importRowTemplate()
136+
this.importTemplates()
137137
this.render()
138138
}
139139

@@ -149,29 +149,38 @@ class GEAutocomplete extends HTMLElement {
149149
this.shadowRoot.appendChild(styles)
150150
}
151151

152-
// importRowTemplate looks for a <template row> tag inside the custom element
153-
// and stores its contents as a lodash template, which is then passed on to
154-
// the autocomplete component
155-
importRowTemplate() {
156-
const tmpl = this.querySelector('template[row]')
157-
if (tmpl === null) return
152+
// importTemplates looks for custom <template> tags inside this custom element,
153+
// parses their content as a lodash template and stores them to be passed on
154+
// to the autocomplete component
155+
importTemplates() {
156+
const templates = {
157+
stringTemplate: this.querySelector('template[string]'),
158+
rowTemplate: this.querySelector('template[row]')
159+
}
158160

159-
this.rowTemplate = _template(
160-
_unescape(tmpl.innerHTML.trim()), // unescape is important for `<%` etc. lodash tags
161-
{ variable: 'feature' } // namespace the passed in Feature as `feature` so missing keys don’t throw
162-
)
161+
Object.keys(templates).forEach(k => {
162+
const tmpl = templates[k]
163+
if (tmpl === null) return
163164

164-
// contrary to the way custom styles are handled above we remove the <template> when we’re done
165-
// so it doesn’t hang around in the host document (not the Shadow DOM)
166-
tmpl.remove()
165+
this[k] = _template(
166+
_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
168+
)
169+
170+
// contrary to the way custom styles are handled above we remove the <template> when we’re done
171+
// so it doesn’t hang around in the host document (not the Shadow DOM)
172+
tmpl.remove()
173+
})
167174
}
168175

169176
render () {
170177
ReactDOM.render(
171178
<WebComponent
172179
{...this.props}
173180
host={this}
174-
rowTemplate={this.rowTemplate} />,
181+
stringTemplate={this.stringTemplate}
182+
rowTemplate={this.rowTemplate}
183+
/>,
175184
this.shadowRoot
176185
)
177186
}

0 commit comments

Comments
 (0)