forked from pnp/sp-dev-fx-webparts
-
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.
Merge pull request pnp#2 from SPFxAppDev/feature/searchbox
Search Box Implemtation
- Loading branch information
Showing
6 changed files
with
214 additions
and
9 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
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
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
61 changes: 61 additions & 0 deletions
61
src/webparts/map/components/plugin/SearchPlugin.module.scss
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,61 @@ | ||
@import '~@microsoft/sp-office-ui-fabric-core/dist/sass/SPFabricCore.scss'; | ||
|
||
|
||
.map-plugin-search { | ||
display: block; | ||
top: 20px; | ||
right: 20px; | ||
z-index: 1000; | ||
position: absolute; | ||
|
||
button { | ||
outline: none; | ||
border-radius: 2px; | ||
height: 32px; | ||
border: 2px solid rgba(0,0,0,0.2); | ||
background-clip: padding-box; | ||
font-size: 1.4em; | ||
background: #fff; | ||
cursor: pointer; | ||
} | ||
|
||
.textbox { | ||
display: inline-block !important; | ||
border-width: 0px; | ||
padding: 0; | ||
|
||
div { | ||
border-width: 0px; | ||
} | ||
} | ||
} | ||
|
||
.suggesstion { | ||
display: block; | ||
|
||
&-item { | ||
display: flex; | ||
padding: 5px 20px; | ||
border-bottom: solid 1px $ms-color-themeLighter; | ||
border-top: solid 1px $ms-color-themeLighter; | ||
align-items: center; | ||
cursor: pointer; | ||
|
||
&:first-child { | ||
border-top-width: 0; | ||
} | ||
|
||
&:last-child { | ||
border-bottom-width: 0; | ||
} | ||
|
||
i { | ||
padding-right: 20px; | ||
font-size: 2em; | ||
} | ||
|
||
span { | ||
font-size: 1em; | ||
} | ||
} | ||
} |
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,106 @@ | ||
import { isFunction, isNullOrEmpty } from '@spfxappdev/utility'; | ||
import { Autocomplete } from '@src/components/autocomplete/Autocomplete'; | ||
import { Icon } from 'office-ui-fabric-react'; | ||
import * as React from 'react'; | ||
import styles from './SearchPlugin.module.scss'; | ||
|
||
export interface ISearchPluginProps { | ||
nominatimUrl?: string; | ||
resultLimit?: number; | ||
onLocationSelected?(latitude: number, longitude: number): void; | ||
} | ||
|
||
interface ISearchResult { | ||
place_id: number; | ||
display_name: string; | ||
lat: string; | ||
lon: string; | ||
} | ||
|
||
interface ISearchPluginState { | ||
searchResult: Array<ISearchResult>; | ||
isSearchBoxVisible: boolean; | ||
} | ||
|
||
export default class SearchPlugin extends React.Component<ISearchPluginProps, ISearchPluginState> { | ||
|
||
public state: ISearchPluginState = { | ||
searchResult: [], | ||
isSearchBoxVisible: false | ||
}; | ||
|
||
public static defaultProps: ISearchPluginProps = { | ||
nominatimUrl: "https://nominatim.openstreetmap.org/search", | ||
resultLimit: 3 | ||
}; | ||
|
||
|
||
public render(): React.ReactElement<ISearchPluginProps> { | ||
return ( | ||
<div className={styles["map-plugin-search"]}> | ||
{this.state.isSearchBoxVisible && | ||
<Autocomplete | ||
className={styles['textbox']} | ||
onRenderSuggestions={(searchTerm: string) => { | ||
return this.renderSuggesstionsFlyout(searchTerm); | ||
}} | ||
onChange={async (ev: any, searchTerm: string) => { | ||
const result = await this.makeSearchRequest(searchTerm); | ||
this.setState({ | ||
searchResult: result | ||
}); | ||
}} /> | ||
} | ||
|
||
<button type="button" onClick={() => { | ||
const isVisible: boolean = this.state.isSearchBoxVisible ? false : true; | ||
|
||
this.setState({ | ||
isSearchBoxVisible: isVisible | ||
}); | ||
}}> | ||
<Icon iconName="Search" /> | ||
</button> | ||
</div>); | ||
} | ||
|
||
private renderSuggesstionsFlyout(searchTerm: string): JSX.Element { | ||
|
||
const results = this.state.searchResult; | ||
|
||
if(isNullOrEmpty(results)) { | ||
return (<> | ||
No results | ||
</>); | ||
} | ||
|
||
return (<div className={styles["suggesstion"]}> | ||
{results.map((location: ISearchResult, index: number): JSX.Element => { | ||
return (<div | ||
key={`Icon_${index}_${location.place_id}`} | ||
onClick={() => { | ||
|
||
if(isFunction(this.props.onLocationSelected)) { | ||
this.props.onLocationSelected(parseFloat(location.lat), parseFloat(location.lon)); | ||
} | ||
|
||
this.setState({ | ||
isSearchBoxVisible: false | ||
}); | ||
}} | ||
className={styles["suggesstion-item"]}> | ||
{location.display_name} | ||
</div>); | ||
})} | ||
</div>); | ||
|
||
} | ||
|
||
private async makeSearchRequest(searchTerm: string): Promise<ISearchResult[]> { | ||
|
||
const response = await fetch(`${this.props.nominatimUrl}?format=json&limit=${this.props.resultLimit}&q=${searchTerm}`); | ||
const responseJson = await response.json(); | ||
console.log("SSC", responseJson); | ||
return responseJson; | ||
} | ||
} |