-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { StoryObj } from '@storybook/preact'; | ||
|
||
import LocationInputComponent from './LocationInput'; | ||
|
||
const meta = { | ||
component: LocationInputComponent, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const LocationInput: StoryObj = { | ||
render: () => ( | ||
<> | ||
<label htmlFor="custom-location-input">Where are you?</label> | ||
<locator-location-input inputId="custom-location-input"></locator-location-input> | ||
</> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Signal, signal } from '@preact/signals'; | ||
import { Component } from 'preact'; | ||
import register from 'preact-custom-element'; | ||
|
||
import config from '../../config'; | ||
|
||
interface HereMapsAutosuggestResult { | ||
items: { | ||
title: string; | ||
}[]; | ||
} | ||
|
||
interface LocationInputProps { | ||
readonly inputId?: string; | ||
} | ||
|
||
/** | ||
* An autosuggest input for locations. | ||
* The autosuggest list will appear after > 3 characters are entered. | ||
*/ | ||
export default class LocationInput extends Component<LocationInputProps> { | ||
apiKey = config.mapsPlacesKey; | ||
autosuggestEndpoint = 'https://autosuggest.search.hereapi.com/v1/autosuggest'; | ||
boundingBox = '-7.57216793459,49.959999905,1.68153079591,58.6350001085'; | ||
resultTypes = 'address,place'; | ||
locationSuggestions: Signal<string[]>; | ||
|
||
constructor(props: LocationInputProps) { | ||
super(props); | ||
this.locationSuggestions = signal<string[]>([]); | ||
} | ||
|
||
autosuggest = async (query: string): Promise<HereMapsAutosuggestResult> => { | ||
const apiKey = `apiKey=${this.apiKey}`; | ||
const bbox = `in=bbox:${this.boundingBox}`; | ||
const resultTypes = `result_types=${this.resultTypes}`; | ||
|
||
const response = await fetch( | ||
`${this.autosuggestEndpoint}?q=${query}&${apiKey}&${bbox}&${resultTypes}`, | ||
); | ||
return response.json(); | ||
}; | ||
|
||
handleInput = async (event: preact.JSX.TargetedEvent<HTMLInputElement>) => { | ||
const query = event.currentTarget.value; | ||
|
||
if (query.length <= 3) { | ||
return; | ||
} | ||
|
||
const result = await this.autosuggest(query); | ||
const locations = result.items.map((item) => item.title); | ||
this.locationSuggestions.value = locations; | ||
}; | ||
|
||
render() { | ||
const locations = this.locationSuggestions.value; | ||
const inputId = this.props.inputId ?? 'location-input'; | ||
const listId = `${inputId}-locations`; | ||
|
||
return ( | ||
<> | ||
<input | ||
type="text" | ||
id={inputId} | ||
list={listId} | ||
onInput={this.handleInput} | ||
/> | ||
<datalist id={listId}> | ||
{locations.map((location) => ( | ||
<option value={location} key={location}> | ||
{location} | ||
</option> | ||
))} | ||
</datalist> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
register(LocationInput, 'locator-location-input', ['inputId']); | ||
|
||
declare module 'preact' { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
'locator-location-input': LocationInputProps & preact.JSX.HTMLAttributes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters