Skip to content

Commit

Permalink
events: add onFeature event
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Feb 1, 2022
1 parent 907155e commit 40a2224
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The element also emits **events** as a user interacts with it. This is how you c
|---------------|--------------|-----------|
|`select` |`Feature` |Dispatched when a user selects a suggested item from the list.|
|`change` |`string` |Dispatched with every keystroke as the user types (not debounced).|
|`features` |`[]Feature` |Dispatched when the GeoJSON features backing the UI change.|
|`error` |`Error` |Dispatched if an error occures during the request (for example if the rate limit was exceeded.)|


Expand Down
13 changes: 11 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@
<script>
const el = document.querySelector('ge-autocomplete')

el.addEventListener('change', ({ detail, currentTarget }) => {
console.log('change', currentTarget)
console.log(detail)
})

el.addEventListener('features', ({ detail, currentTarget }) => {
console.log('features', currentTarget)
console.log(detail)
})

el.addEventListener('select', ({ detail, currentTarget }) => {
console.log(currentTarget)
console.log(detail)

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

el.addEventListener('error', ({ detail, currentTarget }) => {
console.log(currentTarget)
console.log('error', currentTarget)
console.error(detail)
})
</script>
Expand Down
8 changes: 8 additions & 0 deletions src/autocomplete/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default ({
throttle: throttleWait = 200,
onSelect: userOnSelectItem,
onChange: userOnChange,
onFeatures: userOnFeatures,
onError: userOnError,
environment = window,
rowTemplate,
Expand All @@ -31,6 +32,13 @@ export default ({
const [isLoading, setIsLoading] = useState(false)
const inputRef = useRef()

// call user-supplied onFeatures callback
if (typeof userOnFeatures === 'function') {
useEffect(() => {
userOnFeatures(results.features)
}, [results])
}

// setting params & options as state so they can be passed to useMemo as dependencies,
// which doesn’t work if they’re just objects as the internal comparison fails
const [apiParams, setApiParams] = useState(params)
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const WebComponent = ({ host, ...autocompleteProps }) => {
// dispatch custom events on the host (custom element)
const dispatchEvent = (name, detail) => host.dispatchEvent(new CustomEvent(name, { detail }))
const onSelect = (item) => dispatchEvent('select', item)
const onFeatures = (items) => dispatchEvent('features', items)
const onError = (error) => dispatchEvent('error', error)
const onChange = (text) => {
// reflect the value of the input field on the element by setting the `value` attribute
Expand All @@ -43,6 +44,7 @@ const WebComponent = ({ host, ...autocompleteProps }) => {
onSelect={onSelect}
onError={onError}
onChange={onChange}
onFeatures={onFeatures}
environment={environment}
/>
}
Expand Down

0 comments on commit 40a2224

Please sign in to comment.