Add loading, empty list message. (autocompplete component)

This commit is contained in:
LautaroCesso 2020-01-13 22:00:51 -03:00
parent bbe66b4dea
commit b267a29d06
4 changed files with 132 additions and 55 deletions

View File

@ -3,18 +3,31 @@ import AutocompleteDropDown from 'core-components/autocomplete-dropdown';
const React = require('react');
const _ = require('lodash');
const searchApi = (query) => {
const data = [
{id: 11, name: 'ivan', content: 'Ivan.', color: 'red',},
{id: 12, name: 'lautaro', content: 'Lautaro.', color: 'indigo',},
{id: 13, name: 'javier', content: 'Javier.', color: 'cyan',},
{id: 14, name: 'guillermo', content: 'Guillermo.', color: 'violet',},
]
const color = [
'red',
'cyan',
'blue',
'green',
];
let countries = ["Afghanistan","Åland Islands","Albania","Algeria","American Samoa","AndorrA","Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory","Brunei Darussalam","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Christmas Island","Cocos (Keeling) Islands","Colombia","Comoros","Congo","Congo, The Democratic Republic of the","Cook Islands","Costa Rica","Cote D'Ivoire","Croatia","Cuba","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands (Malvinas)","Faroe Islands","Fiji","Finland","France","French Guiana","French Polynesia","French Southern Territories","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guernsey","Guinea","Guinea-Bissau","Guyana","Haiti","Heard Island and Mcdonald Islands","Holy See (Vatican City State)","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran, Islamic Republic Of","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Korea, Democratic People'S Republic of","Korea, Republic of","Kuwait","Kyrgyzstan","Lao People'S Democratic Republic","Latvia","Lebanon","Lesotho","Liberia","Libyan Arab Jamahiriya","Liechtenstein","Lithuania","Luxembourg","Macao","Macedonia, The Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia, Federated States of","Moldova, Republic of","Monaco","Mongolia","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauru","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","Northern Mariana Islands","Norway","Oman","Pakistan","Palau","Palestinian Territory, Occupied","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcairn","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russian Federation","RWANDA","Saint Helena","Saint Kitts and Nevis","Saint Lucia","Saint Pierre and Miquelon","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia and Montenegro","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Georgia and the South Sandwich Islands","Spain","Sri Lanka","Sudan","Suriname","Svalbard and Jan Mayen","Swaziland","Sweden","Switzerland","Syrian Arab Republic","Taiwan, Province of China","Tajikistan","Tanzania, United Republic of","Thailand","Timor-Leste","Togo","Tokelau","Tonga","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan","Turks and Caicos Islands","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States","United States Minor Outlying Islands","Uruguay","Uzbekistan","Vanuatu","Venezuela","Viet Nam","Virgin Islands, British","Virgin Islands, U.S.","Wallis and Futuna","Western Sahara","Yemen","Zambia","Zimbabwe"];
countries = countries.map((name, index) => {
return {
name: name.toLowerCase(),
id: index,
content: name,
color: color[_.random(0, color.length-1)],
}
})
const searchApi = (query, blacklist = []) => {
const data = countries.filter(x => !_.includes(blacklist, x.id));
return new Promise((res,rej) => {
setTimeout(function () {
res(data.filter(item => _.includes(item.name, query)));
}, 500);
const result = data.filter(item => _.includes(item.name, query));
res(result.slice(0, 10));
}, query == 'brazilq' ? 2000 : 100);
})
};
@ -43,14 +56,16 @@ class DemoPage extends React.Component {
<AutocompleteDropDown
items={itemsList}
values={this.state.selectedList}
onChange={selectedList => this.setState({selectedList: selectedList})}/>
onChange={selectedList => this.setState({selectedList: selectedList})} />
<button onClick={() => this.setState({selectedList: []})}>clear</button>
<AutocompleteDropDown
values={this.state.selectedList2}
getItemListFromQuery={searchApi}
onChange={selectedList => this.setState({selectedList2: selectedList})}/>
<button onClick={() => this.setState({selectedList2: []})}>clear</button>
onChange={selectedList => this.setState({selectedList2: selectedList})} />
<button onClick={() => {
this.setState({selectedList2: []});
}}>clear</button>
</div>
);

View File

@ -18,28 +18,38 @@ class AutocompleteDropDown extends React.Component {
disabled: React.PropTypes.bool,
};
id = 1;
state = {
itemsSelected: [],
inputValue: "",
opened: false,
highlightedIndex: 0,
items2: [],
loading: false,
};
componentDidMount() {
const { getItemListFromQuery, } = this.props;
if (this.state.items2.length === 0){
if(getItemListFromQuery !== undefined) {
getItemListFromQuery("")
.then(res => {
this.setState({
items2: res,
});
});
}
this.setTimeout = _.throttle((query) => {
let id = ++this.id;
getItemListFromQuery(query, this.getValue().map(item => item.id))
.then(res => {
if(id === this.id)
this.setState({
items2: res,
loading: false,
});
})
.catch(() => this.setState({
loading: false,
}));
}, 300, {leading: false});
this.searchApi("");
}
}
render() {
@ -64,6 +74,7 @@ class AutocompleteDropDown extends React.Component {
opened={this.state.opened}
onHighlightedIndexChange={n => this.onHighlightedIndexChange(n)}
highlightedIndex={this.state.highlightedIndex}
loading={this.state.loading}
>
{this.renderSelectedItems()}
<input
@ -87,7 +98,12 @@ class AutocompleteDropDown extends React.Component {
}
renderSelectedItem(item) {
return <Tag name={item.name} color={item.color} showDeleteButton onRemoveClick={this.onRemoveClick.bind(this,item.id)} key={item.id}/>
return <Tag
name={item.name}
color={item.color}
showDeleteButton
onRemoveClick={this.onRemoveClick.bind(this,item.id)}
key={item.id}/>
}
getDropdownList() {
@ -120,57 +136,46 @@ class AutocompleteDropDown extends React.Component {
return (values !== undefined) ? values : this.state.itemsSelected;
}
getDeletingItemLoading() {
const { deletingItemLoading, } = this.props;
return (deletingItemLoading !== undefined) ? deletingItemLoading : false;
}
onRemoveClick(itemId, event) {
const {
onChange,
onRemoveClick,
} = this.props;
const newList = this.getValue().filter(item => item.id != itemId);
event.preventDefault();
this.setState({
itemsSelected: this.getValue().filter(item => item.id != itemId),
itemsSelected: newList,
opened: false,
highlightedIndex: 0,
});
onChange && onChange(this.getValue().filter(item => item.id != itemId));
onChange && onChange(newList);
onRemoveClick && onRemoveClick(itemId);
this.searchApi("", newList);
}
onChangeDropDown(e){
const {
onChange,
onTagSelected,
getItemListFromQuery,
} = this.props;
if (this.getDropdownList().length) {
const itemSelected = this.getDropdownList()[e.index];
const newList = [...this.getValue(), itemSelected];
this.setState({
itemsSelected: [...this.getValue(), this.getDropdownList()[e.index]],
itemsSelected: newList,
inputValue: "",
highlightedIndex: 0,
});
if (getItemListFromQuery !== undefined) {
getItemListFromQuery("")
.then(res => {
this.setState({
items2: res,
});
})
}
onChange && onChange([...this.getValue(), this.getDropdownList()[e.index]]);
onTagSelected && onTagSelected(this.getDropdownList()[e.index].id);
onChange && onChange(newList);
onTagSelected && onTagSelected(itemSelected.id);
this.searchApi("", newList);
}
@ -186,13 +191,14 @@ class AutocompleteDropDown extends React.Component {
});
if (getItemListFromQuery !== undefined) {
getItemListFromQuery(str)
.then(res => {
this.setState({
items2: res,
});
})
this.setState({
loading: true,
});
this.setTimeout(str);
}
}
onMenuToggle(b){
@ -216,26 +222,57 @@ class AutocompleteDropDown extends React.Component {
if(this.props.disabled) {
event.stopPropagation();
event.preventDefault();
return;
}
if (keyCode(event) === "space"){
event.stopPropagation();
}
if (keyCode(event) === "backspace" && this.state.inputValue === ""){
const newList = this.getValue().slice(0,this.getValue().length-1);
this.setState({
itemsSelected: this.getValue().slice(0,this.getValue().length-1),
itemsSelected: newList,
highlightedIndex: 0,
});
onChange && onChange(this.getValue().slice(0,this.getValue().length-1));
onChange && onChange(newList);
if (this.getValue().length) {
const itemId = this.getValue()[this.getValue().length-1].id;
onRemoveClick && onRemoveClick(itemId);
}
this.searchApi("", newList);
}
}
searchApi(query, blacklist=this.getValue()) {
const { getItemListFromQuery, } = this.props;
if (getItemListFromQuery !== undefined) {
getItemListFromQuery(query, blacklist.map(item => item.id))
.then(res => {
this.setState({
items2: res,
loading: false,
});
})
.catch(() => {
this.setState({
loading: false,
})
})
}
}
}

View File

@ -7,6 +7,8 @@ import keyCode from 'keycode';
import Menu from 'core-components/menu';
import Icon from 'core-components/icon';
import Loading from 'core-components/loading'
class DropDown extends React.Component {
static propTypes = {
@ -76,7 +78,11 @@ class DropDown extends React.Component {
return (
<div className="drop-down__list-container" style={style}>
<Menu {...this.getMenuProps()} />
{this.props.loading ?
<div><Loading className='drop-down__loading' /></div> :
this.props.items.length ?
<Menu {...this.getMenuProps()} /> :
<div className='drop-down__empty-list'>Empty</div>}
</div>
);
}

View File

@ -28,6 +28,25 @@
position: absolute;
width: 150px;
z-index: 100;
background-color: white;
height: 40px;
}
&__loading {
padding-top: 12.5%;
.loading__icon {
border-color: rgba(150, 150, 150, 0.2);
border-left-color: $primary-red;
}
}
&__empty-list {
padding: 10px;
background-color: white;
color: $dark-grey;
font-style: italic;
cursor: default;
}
&_closed {