API для подсказок при вводе адреса
Как лучше реализовать ввод пользователем адреса доставки? Использую React. Адрес вводится в input. Но можно сделать подсказку. Когда вводишь символ в input предлагаются сразу варианты, чтобы человеку не нужно было долго писать. Как это осуществить? Каким инструментом пользоваться? Что посоветуете?
Ответы (2 шт):
Автор решения: Sheridan
→ Ссылка
На событие ввода символа (а лучше спустя небольшой таймаут) вешаешь обработчик, который спрашивает у сервера об api варианты значений, подходящих под введённые символы. Результат рендеришь в "выпадающем меню".
Ну а вообще, можно конечно воспользоваться вот этим: https://kladr-api.ru/examples
Автор решения: Enokin
→ Ссылка
Вот хорошая библиотека React Autosuggest Ссылка на рабочий пример codepen
const languages = [
{
name: 'C',
year: 1972
},
{
name: 'C#',
year: 2000
},
{
name: 'C++',
year: 1983
},
{
name: 'Clojure',
year: 2007
},
{
name: 'Elm',
year: 2012
},
{
name: 'Go',
year: 2009
},
{
name: 'Haskell',
year: 1990
},
{
name: 'Java',
year: 1995
},
{
name: 'Javascript',
year: 1995
},
{
name: 'Perl',
year: 1987
},
{
name: 'PHP',
year: 1995
},
{
name: 'Python',
year: 1991
},
{
name: 'Ruby',
year: 1995
},
{
name: 'Scala',
year: 2003
}
];
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
const escapeRegexCharacters = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const getSuggestions = value => {
const escapedValue = escapeRegexCharacters(value.trim());
if (escapedValue === '') {
return [];
}
const regex = new RegExp('^' + escapedValue, 'i');
const suggestions = languages.filter(language => regex.test(language.name));
if (suggestions.length === 0) {
return [
{ isAddNew: true }
];
}
return suggestions;
}
class App extends React.Component {
constructor() {
super();
this.state = {
value: '',
suggestions: []
};
}
onChange = (event, { newValue, method }) => {
this.setState({
value: newValue
});
};
getSuggestionValue = suggestion => {
if (suggestion.isAddNew) {
return this.state.value;
}
return suggestion.name;
};
renderSuggestion = suggestion => {
if (suggestion.isAddNew) {
return (
<span>
[+] Add new: <strong>{this.state.value}</strong>
</span>
);
}
return suggestion.name;
};
onSuggestionsFetchRequested = ({ value }) => {
this.setState({
suggestions: getSuggestions(value)
});
};
onSuggestionsClearRequested = () => {
this.setState({
suggestions: []
});
};
onSuggestionSelected = (event, { suggestion }) => {
if (suggestion.isAddNew) {
console.log('Add new:', this.state.value);
}
};
render() {
const { value, suggestions } = this.state;
const inputProps = {
placeholder: "Type 'c'",
value,
onChange: this.onChange
};
return (
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
getSuggestionValue={this.getSuggestionValue}
renderSuggestion={this.renderSuggestion}
onSuggestionSelected={this.onSuggestionSelected}
inputProps={inputProps}
/>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
body {
font-family: Helvetica, sans-serif;
}
.react-autosuggest__container {
position: relative;
}
.react-autosuggest__input {
width: 240px;
height: 30px;
padding: 10px 20px;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border: 1px solid #aaa;
border-radius: 4px;
}
.react-autosuggest__input--focused {
outline: none;
}
.react-autosuggest__input--open {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
.react-autosuggest__suggestions-container {
display: none;
}
.react-autosuggest__suggestions-container--open {
display: block;
position: absolute;
top: 51px;
width: 280px;
border: 1px solid #aaa;
background-color: #fff;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
z-index: 2;
}
.react-autosuggest__suggestions-list {
margin: 0;
padding: 0;
list-style-type: none;
}
.react-autosuggest__suggestion {
cursor: pointer;
padding: 10px 20px;
}
.react-autosuggest__suggestion--highlighted {
background-color: #ddd;
}
.footer {
margin: 9px 20px;
font-size: 12px;
color: #777;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.4.2/react-dom.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/standalone/autosuggest.js"></script>
<h2>Add new option</h2>
<div id="app"></div>