Skip to content

Commit 74e4952

Browse files
committed
events: add onFeature event
1 parent 907155e commit 74e4952

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The element also emits **events** as a user interacts with it. This is how you c
113113
|---------------|--------------|-----------|
114114
|`select` |`Feature` |Dispatched when a user selects a suggested item from the list.|
115115
|`change` |`string` |Dispatched with every keystroke as the user types (not debounced).|
116+
|`features` |`[]Feature` |Dispatched when the GeoJSON features backing the UI change.|
116117
|`error` |`Error` |Dispatched if an error occures during the request (for example if the rate limit was exceeded.)|
117118

118119

example/index.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717
<script>
1818
const el = document.querySelector('ge-autocomplete')
1919

20+
el.addEventListener('change', ({ detail, currentTarget }) => {
21+
console.log('change', currentTarget)
22+
console.log(detail)
23+
})
24+
25+
el.addEventListener('features', ({ detail, currentTarget }) => {
26+
console.log('features', currentTarget)
27+
console.log(detail)
28+
})
29+
2030
el.addEventListener('select', ({ detail, currentTarget }) => {
21-
console.log(currentTarget)
31+
console.log('select', currentTarget)
2232
console.log(detail)
2333

2434
document.querySelector('#result').innerHTML = JSON.stringify(detail, undefined, 4)
2535
})
2636

2737
el.addEventListener('error', ({ detail, currentTarget }) => {
28-
console.log(currentTarget)
38+
console.log('error', currentTarget)
2939
console.error(detail)
3040
})
3141
</script>

src/autocomplete/autocomplete.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default ({
2222
throttle: throttleWait = 200,
2323
onSelect: userOnSelectItem,
2424
onChange: userOnChange,
25+
onFeatures: userOnFeatures,
2526
onError: userOnError,
2627
environment = window,
2728
rowTemplate,
@@ -31,6 +32,13 @@ export default ({
3132
const [isLoading, setIsLoading] = useState(false)
3233
const inputRef = useRef()
3334

35+
// call user-supplied onFeatures callback
36+
useEffect(() => {
37+
if (typeof userOnFeatures === 'function') {
38+
userOnFeatures(results.features)
39+
}
40+
}, [results])
41+
3442
// setting params & options as state so they can be passed to useMemo as dependencies,
3543
// which doesn’t work if they’re just objects as the internal comparison fails
3644
const [apiParams, setApiParams] = useState(params)
@@ -94,6 +102,8 @@ export default ({
94102

95103
// called user-supplied callback when an item is selected
96104
const onSelectItem = ({ selectedItem }) => {
105+
setResults(emptyResults)
106+
97107
if (typeof userOnSelectItem === 'function') {
98108
userOnSelectItem(selectedItem)
99109
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const WebComponent = ({ host, ...autocompleteProps }) => {
3131
// dispatch custom events on the host (custom element)
3232
const dispatchEvent = (name, detail) => host.dispatchEvent(new CustomEvent(name, { detail }))
3333
const onSelect = (item) => dispatchEvent('select', item)
34+
const onFeatures = (items) => dispatchEvent('features', items)
3435
const onError = (error) => dispatchEvent('error', error)
3536
const onChange = (text) => {
3637
// reflect the value of the input field on the element by setting the `value` attribute
@@ -43,6 +44,7 @@ const WebComponent = ({ host, ...autocompleteProps }) => {
4344
onSelect={onSelect}
4445
onError={onError}
4546
onChange={onChange}
47+
onFeatures={onFeatures}
4648
environment={environment}
4749
/>
4850
}

0 commit comments

Comments
 (0)