mirror of
https://github.com/jlengrand/leaflet-geosearch.git
synced 2026-03-10 08:31:26 +00:00
feat: add option for custom "not found message" (#284)
This commit is contained in:
committed by
GitHub
parent
339b0bd65c
commit
8f509ca127
@@ -212,6 +212,10 @@
|
|||||||
border-right: none;
|
border-right: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.leaflet-bar-notfound {
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
.leaflet-control-geosearch a.reset {
|
.leaflet-control-geosearch a.reset {
|
||||||
color: black;
|
color: black;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
|||||||
@@ -44,6 +44,29 @@ const searchControl = new SearchControl({
|
|||||||
map.addControl(searchControl);
|
map.addControl(searchControl);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Message if address not found
|
||||||
|
|
||||||
|
<Playground>
|
||||||
|
<Map
|
||||||
|
provider="OpenStreetMap"
|
||||||
|
controlOptions={{
|
||||||
|
notFoundMessage: 'Sorry, that address could not be found.',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Playground>
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { SearchControl, OpenStreetMapProvider } from 'leaflet-geosearch';
|
||||||
|
|
||||||
|
const searchControl = new SearchControl({
|
||||||
|
notFoundMessage: 'Sorry, that address could not be found.',
|
||||||
|
provider: new OpenStreetMapProvider(),
|
||||||
|
});
|
||||||
|
|
||||||
|
map.addControl(searchControl);
|
||||||
|
``
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
<Props of={SearchControl} />
|
<Props of={SearchControl} />
|
||||||
|
```
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ const defaultOptions: Omit<SearchControlProps, 'provider'> = {
|
|||||||
retainZoomLevel: false,
|
retainZoomLevel: false,
|
||||||
animateZoom: true,
|
animateZoom: true,
|
||||||
searchLabel: 'Enter address',
|
searchLabel: 'Enter address',
|
||||||
notFoundMessage: 'Sorry, that address could not be found.',
|
notFoundMessage: '',
|
||||||
messageHideDelay: 3000,
|
messageHideDelay: 3000,
|
||||||
zoomLevel: 18,
|
zoomLevel: 18,
|
||||||
classNames: {
|
classNames: {
|
||||||
@@ -45,6 +45,9 @@ const defaultOptions: Omit<SearchControlProps, 'provider'> = {
|
|||||||
msgbox: 'leaflet-bar message',
|
msgbox: 'leaflet-bar message',
|
||||||
form: '',
|
form: '',
|
||||||
input: '',
|
input: '',
|
||||||
|
resultlist: '',
|
||||||
|
item: '',
|
||||||
|
notfound: 'leaflet-bar-notfound',
|
||||||
},
|
},
|
||||||
autoComplete: true,
|
autoComplete: true,
|
||||||
autoCompleteDelay: 250,
|
autoCompleteDelay: 250,
|
||||||
@@ -93,6 +96,9 @@ interface SearchControlProps {
|
|||||||
msgbox: string;
|
msgbox: string;
|
||||||
form: string;
|
form: string;
|
||||||
input: string;
|
input: string;
|
||||||
|
resultlist: string;
|
||||||
|
item: string;
|
||||||
|
notfound: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
autoComplete: boolean;
|
autoComplete: boolean;
|
||||||
@@ -205,6 +211,12 @@ const Control: SearchControl = {
|
|||||||
this.searchElement.input.value = result.label;
|
this.searchElement.input.value = result.label;
|
||||||
this.onSubmit({ query: result.label, data: result });
|
this.onSubmit({ query: result.label, data: result });
|
||||||
},
|
},
|
||||||
|
classNames: {
|
||||||
|
resultlist: this.classNames.resultlist,
|
||||||
|
item: this.classNames.item,
|
||||||
|
notfound: this.classNames.notfound,
|
||||||
|
},
|
||||||
|
notFoundMessage: this.options.notFoundMessage,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.searchElement.form.appendChild(this.resultList.container);
|
this.searchElement.form.appendChild(this.resultList.container);
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import { SearchResult } from './providers/provider';
|
|||||||
interface ResultListProps {
|
interface ResultListProps {
|
||||||
handleClick: (args: { result: SearchResult }) => void;
|
handleClick: (args: { result: SearchResult }) => void;
|
||||||
classNames?: {
|
classNames?: {
|
||||||
container?: string;
|
resultlist?: string;
|
||||||
item?: string;
|
item?: string;
|
||||||
|
notfound?: string;
|
||||||
};
|
};
|
||||||
|
notFoundMessage?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class ResultList {
|
export default class ResultList {
|
||||||
@@ -16,13 +18,26 @@ export default class ResultList {
|
|||||||
|
|
||||||
container: HTMLDivElement;
|
container: HTMLDivElement;
|
||||||
resultItem: HTMLDivElement;
|
resultItem: HTMLDivElement;
|
||||||
|
notFoundMessage?: HTMLDivElement;
|
||||||
|
|
||||||
constructor({ handleClick, classNames = {} }: ResultListProps) {
|
constructor({
|
||||||
|
handleClick,
|
||||||
|
classNames = {},
|
||||||
|
notFoundMessage,
|
||||||
|
}: ResultListProps) {
|
||||||
this.handleClick = handleClick;
|
this.handleClick = handleClick;
|
||||||
|
this.notFoundMessage = !!notFoundMessage
|
||||||
|
? createElement<HTMLDivElement>(
|
||||||
|
'div',
|
||||||
|
cx(classNames.notfound),
|
||||||
|
undefined,
|
||||||
|
{ html: notFoundMessage! },
|
||||||
|
)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
this.container = createElement<HTMLDivElement>(
|
this.container = createElement<HTMLDivElement>(
|
||||||
'div',
|
'div',
|
||||||
cx('results', classNames.container),
|
cx('results', classNames.resultlist),
|
||||||
);
|
);
|
||||||
this.container.addEventListener('click', this.onClick, true);
|
this.container.addEventListener('click', this.onClick, true);
|
||||||
|
|
||||||
@@ -42,6 +57,9 @@ export default class ResultList {
|
|||||||
if (results.length > 0) {
|
if (results.length > 0) {
|
||||||
addClassName(this.container.parentElement, 'open');
|
addClassName(this.container.parentElement, 'open');
|
||||||
addClassName(this.container, 'active');
|
addClassName(this.container, 'active');
|
||||||
|
} else if (!!this.notFoundMessage) {
|
||||||
|
this.container.appendChild(this.notFoundMessage);
|
||||||
|
addClassName(this.container.parentElement, 'open');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.results = results;
|
this.results = results;
|
||||||
|
|||||||
Reference in New Issue
Block a user