Minors changes og line breack and spaces in tagSelector, autocomplete-dropdown and dropdown

This commit is contained in:
LautaroCesso 2020-01-14 17:09:06 -03:00
parent 179252303c
commit 780aa64d19
6 changed files with 107 additions and 103 deletions

View File

@ -13,12 +13,12 @@ const color = [
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,
name: name.toLowerCase(),
content: name,
color: color[_.random(0, color.length-1)],
}
})
});
const searchApi = (query, blacklist = []) => {
const data = countries.filter(x => !_.includes(blacklist, x.id));
@ -28,7 +28,7 @@ const searchApi = (query, blacklist = []) => {
const result = data.filter(item => _.includes(item.name, query));
res(result.slice(0, 10));
}, query == 'brazilq' ? 2000 : 100);
})
});
};
class DemoPage extends React.Component {
@ -39,7 +39,7 @@ class DemoPage extends React.Component {
{id: 48, name: '123123123', content: '123123123.', color: 'blue'},
{id: 49, name: 'hola', content: 'hola', color: 'green'},
],
selectedList2: []
selectedList2: [],
}
render() {
@ -66,7 +66,6 @@ class DemoPage extends React.Component {
<button onClick={() => {
this.setState({selectedList2: []});
}}>clear</button>
</div>
);
}

View File

@ -1,31 +1,37 @@
import React, { createRef }from 'react';
import React from 'react';
import _ from 'lodash';
import keyCode from 'keycode';
import DropDown from 'core-components/drop-down';
import Menu from 'core-components/menu';
import Tag from 'core-components/tag';
const ItemsSchema = React.PropTypes.arrayOf(React.PropTypes.shape({
id: React.PropTypes.number,
name: React.PropTypes.string,
content: React.PropTypes.string,
color: React.PropTypes.string,
}));
class AutocompleteDropDown extends React.Component {
static propTypes = {
//items: Menu.propTypes.items,
items: ItemsSchema,
onChange: React.PropTypes.func,
values: React.PropTypes.arrayOf(React.PropTypes.shape({})),
values: ItemsSchema,
onRemoveClick: React.PropTypes.func,
onTagSelected: React.PropTypes.func,
//getItemListFromQuery: React.propTypes.func,
getItemListFromQuery: React.PropTypes.func,
disabled: React.PropTypes.bool,
};
id = 1;
state = {
itemsSelected: [],
selectedItems: [],
inputValue: "",
opened: false,
highlightedIndex: 0,
items2: [],
itemsFromQuery: [],
loading: false,
};
@ -35,11 +41,11 @@ class AutocompleteDropDown extends React.Component {
this.setTimeout = _.throttle((query) => {
let id = ++this.id;
getItemListFromQuery(query, this.getValue().map(item => item.id))
.then(res => {
getItemListFromQuery(query, this.getSelectedItems().map(item => item.id))
.then(result => {
if(id === this.id)
this.setState({
items2: res,
itemsFromQuery: result,
loading: false,
});
})
@ -49,24 +55,23 @@ class AutocompleteDropDown extends React.Component {
}, 300, {leading: false});
this.searchApi("");
}
render() {
let inputWith = 0;
let inputWidth = 0;
if(this.span) {
this.span.style.display = 'inline';
this.span.textContent = this.state.inputValue;
inputWith = Math.ceil(this.span.getBoundingClientRect().width) + 20
inputWidth = Math.ceil(this.span.getBoundingClientRect().width)-31;
this.span.style.display = 'none';
}
return (
<div className="tag-selector">
<label onClick={(e) => e.stopPropagation()}>
<div className="autocomplete">
<label className="autocomplete__label" onClick={(e) => e.stopPropagation()}>
<DropDown
className="tag-selector__drop-down"
className="autocomplete__drop-down"
items={this.getDropdownList()}
size="large"
onChange={e => this.onChangeDropDown(e)}
@ -84,9 +89,8 @@ class AutocompleteDropDown extends React.Component {
value={this.state.inputValue}
onKeyDown={e => this.onKeyDown(e)}
onChange={e => this.onChangeInput(e.target.value)}
style={this.span ? {width: inputWith} : {}}
/>
<span className="sizer" ref={span => this.span = span} />
style={this.span ? {width: inputWidth} : {}} />
<span className="sizer" ref={span => this.span = span} />
</DropDown>
</label>
</div>
@ -94,7 +98,7 @@ class AutocompleteDropDown extends React.Component {
}
renderSelectedItems() {
return this.getValue().map(item => this.renderSelectedItem(item));
return this.getSelectedItems().map(item => this.renderSelectedItem(item));
}
renderSelectedItem(item) {
@ -103,37 +107,34 @@ class AutocompleteDropDown extends React.Component {
color={item.color}
showDeleteButton
onRemoveClick={this.onRemoveClick.bind(this,item.id)}
key={item.id}/>
key={item.id} />
}
getDropdownList() {
const {
items,
} = this.props;
let resp = [];
let dropdownList = [];
if (items !== undefined) {
const list = this.getUnselectedList(items, this.getValue());
resp = list.filter(s => _.includes(s.name, this.state.inputValue));
if(items !== undefined) {
const list = this.getUnselectedList(items, this.getSelectedItems());
dropdownList = list.filter(s => _.includes(s.name, this.state.inputValue));
} else {
resp = this.getUnselectedList(this.state.items2, this.getValue());
dropdownList = this.getUnselectedList(this.state.itemsFromQuery, this.getSelectedItems());
}
return resp;
return dropdownList;
}
getUnselectedList(list, selectedList) {
return list.filter(item => !_.some(selectedList, item));
}
getValue() {
getSelectedItems() {
const { values, } = this.props;
return (values !== undefined) ? values : this.state.itemsSelected;
return (values !== undefined) ? values : this.state.selectedItems;
}
onRemoveClick(itemId, event) {
@ -141,13 +142,11 @@ class AutocompleteDropDown extends React.Component {
onChange,
onRemoveClick,
} = this.props;
const newList = this.getValue().filter(item => item.id != itemId);
const newList = this.getSelectedItems().filter(item => item.id != itemId);
event.preventDefault();
this.setState({
itemsSelected: newList,
selectedItems: newList,
opened: false,
highlightedIndex: 0,
});
@ -157,18 +156,18 @@ class AutocompleteDropDown extends React.Component {
this.searchApi("", newList);
}
onChangeDropDown(e){
onChangeDropDown(e) {
const {
onChange,
onTagSelected,
} = this.props;
if (this.getDropdownList().length) {
if(this.getDropdownList().length) {
const itemSelected = this.getDropdownList()[e.index];
const newList = [...this.getValue(), itemSelected];
const newList = [...this.getSelectedItems(), itemSelected];
this.setState({
itemsSelected: newList,
selectedItems: newList,
inputValue: "",
highlightedIndex: 0,
});
@ -176,12 +175,10 @@ class AutocompleteDropDown extends React.Component {
onChange && onChange(newList);
onTagSelected && onTagSelected(itemSelected.id);
this.searchApi("", newList);
}
}
onChangeInput(str){
onChangeInput(str) {
const { getItemListFromQuery, } = this.props;
this.setState({
@ -190,30 +187,28 @@ class AutocompleteDropDown extends React.Component {
highlightedIndex: 0,
});
if (getItemListFromQuery !== undefined) {
if(getItemListFromQuery !== undefined) {
this.setState({
loading: true,
});
this.setTimeout(str);
}
}
onMenuToggle(b){
onMenuToggle(b) {
this.setState({
opened: b,
});
}
onHighlightedIndexChange(n){
onHighlightedIndexChange(n) {
this.setState({
highlightedIndex: n,
});
}
onKeyDown(event){
onKeyDown(event) {
const {
onChange,
onRemoveClick,
@ -224,57 +219,49 @@ class AutocompleteDropDown extends React.Component {
event.preventDefault();
return;
}
if (keyCode(event) === "space"){
if(keyCode(event) === "space") {
event.stopPropagation();
}
if (keyCode(event) === "backspace" && this.state.inputValue === ""){
const newList = this.getValue().slice(0,this.getValue().length-1);
if(keyCode(event) === "backspace" && this.state.inputValue === "") {
const lastSelectedItemsIndex = this.getSelectedItems().length-1;
const newList = this.getSelectedItems().slice(0, lastSelectedItemsIndex);
this.setState({
itemsSelected: newList,
selectedItems: newList,
highlightedIndex: 0,
});
onChange && onChange(newList);
if (this.getValue().length) {
const itemId = this.getValue()[this.getValue().length-1].id;
if(this.getSelectedItems().length) {
const itemId = this.getSelectedItems()[lastSelectedItemsIndex].id;
onRemoveClick && onRemoveClick(itemId);
}
this.searchApi("", newList);
}
}
searchApi(query, blacklist=this.getValue()) {
searchApi(query, blacklist=this.getSelectedItems()) {
const { getItemListFromQuery, } = this.props;
if (getItemListFromQuery !== undefined) {
if(getItemListFromQuery !== undefined) {
getItemListFromQuery(query, blacklist.map(item => item.id))
.then(res => {
.then(result => {
this.setState({
items2: res,
itemsFromQuery: result,
loading: false,
});
})
.catch(() => {
this.setState({
loading: false,
})
})
});
});
}
}
}
export default AutocompleteDropDown;

View File

@ -1,15 +1,36 @@
@import "../scss/vars";
.autocomplete {
margin-bottom: 30px;
text-align: left;
&__drop-down {
.drop-down__current-item {
cursor: text;
background-color: $very-light-grey;
border: 1px solid $grey;
min-height: 38px;
&:focus {
outline: none;
border-color: $primary-blue;
}
}
}
&__label {
display: inline-block;
}
&__input {
display: inline-block;
border: 0;
background: transparent;
outline: none;
padding-left: 5px;
width: 20px;
width: 10px;
max-width: 100%;
min-width: 20px;
min-width: 10px;
}
.sizer {

View File

@ -6,7 +6,6 @@ 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 {
@ -17,7 +16,6 @@ class DropDown extends React.Component {
items: Menu.propTypes.items,
onChange: React.PropTypes.func,
size: React.PropTypes.oneOf(['small', 'medium', 'large']),
highlightedIndex: React.PropTypes.number,
onHighlightedIndexChange: React.PropTypes.func,
opened: React.PropTypes.bool,
@ -63,8 +61,11 @@ class DropDown extends React.Component {
<div {...this.getCurrentItemProps()}>
{this.props.children ? this.props.children : this.renderCurrentItem()}
</div>
<Motion defaultStyle={animation.defaultStyle} style={animation.style} onRest={this.onAnimationFinished.bind(this)}>
{this.renderList.bind(this)}
<Motion
defaultStyle={animation.defaultStyle}
style={animation.style}
onRest={this.onAnimationFinished.bind(this)}>
{this.renderList.bind(this)}
</Motion>
</div>
);
@ -167,26 +168,26 @@ class DropDown extends React.Component {
return {
'up': () => {
if (opened) {
const newHighlightedIndex = this.modulo(highlightedIndex - 1, itemsQuantity);
event.preventDefault();
this.setState({
highlightedIndex: this.modulo(highlightedIndex - 1, itemsQuantity)
highlightedIndex: newHighlightedIndex,
});
onHighlightedIndexChange && onHighlightedIndexChange(this.modulo(highlightedIndex - 1, itemsQuantity));
onHighlightedIndexChange && onHighlightedIndexChange(newHighlightedIndex);
}
},
'down': () => {
if (opened) {
const newHighlightedIndex = this.modulo(highlightedIndex + 1, itemsQuantity);
event.preventDefault();
this.setState({
highlightedIndex: this.modulo(highlightedIndex + 1, itemsQuantity)
highlightedIndex: newHighlightedIndex,
});
onHighlightedIndexChange && onHighlightedIndexChange(this.modulo(highlightedIndex + 1, itemsQuantity));
onHighlightedIndexChange && onHighlightedIndexChange(newHighlightedIndex);
}
},
'enter': () => {
@ -198,7 +199,6 @@ class DropDown extends React.Component {
});
onMenuToggle && onMenuToggle(true);
}
},
'space': () => {
@ -209,7 +209,6 @@ class DropDown extends React.Component {
});
onMenuToggle && onMenuToggle(true);
},
'esc': () => {
this.setState({
@ -217,14 +216,12 @@ class DropDown extends React.Component {
});
onMenuToggle && onMenuToggle(false);
},
'tab': () => {
if (this.getOpen()) {
event.preventDefault();
onHighlightedIndexChange && this.onIndexSelected(highlightedIndex);
}
}
};
@ -238,7 +235,6 @@ class DropDown extends React.Component {
});
onMenuToggle && onMenuToggle(false);
}
handleClick() {
@ -249,7 +245,6 @@ class DropDown extends React.Component {
});
onMenuToggle && onMenuToggle(!this.getOpen());
}
handleItemClick(index) {
@ -268,13 +263,12 @@ class DropDown extends React.Component {
selectedIndex: index,
highlightedIndex: index
});
onHighlightedIndexChange && onHighlightedIndexChange(index);
onMenuToggle && onMenuToggle(false);
onChange && onChange({ index });
}
handleListMouseDown(event) {
@ -291,23 +285,26 @@ class DropDown extends React.Component {
});
onHighlightedIndexChange && onHighlightedIndexChange(this.getSelectedIndex());
}
}
getSelectedIndex() {
return (this.props.selectedIndex !== undefined) ? this.props.selectedIndex : this.state.selectedIndex;
const { selectedIndex, } = this.props;
return (selectedIndex !== undefined) ? selectedIndex : this.state.selectedIndex;
}
modulo(number, mod) {
return ((number % mod) + mod) % mod;
}
getOpen(){
return (this.props.opened !== undefined) ? this.props.opened : this.state.opened;
const { opened, } = this.props;
return (opened !== undefined) ? opened : this.state.opened;
}
getHighlightedIndex() {
return (this.props.highlightedIndex !== undefined) ? this.props.highlightedIndex : this.state.highlightedIndex;
const { highlightedIndex, } = this.props;
return (highlightedIndex !== undefined) ? highlightedIndex : this.state.highlightedIndex;
}
}

View File

@ -34,11 +34,11 @@
&__loading {
padding-top: 12.5%;
.loading__icon {
border-color: rgba(150, 150, 150, 0.2);
border-left-color: $primary-red;
}
}
&__empty-list {

View File

@ -17,7 +17,7 @@ class TagSelector extends React.Component {
render() {
const items = this.props.items.map(tag => ({...tag, content: this.renderTagOption(tag)}));
const values = items.filter(item => _.includes(this.props.values, item.name ));
const values = items.filter(item => _.includes(this.props.values, item.name));
return (
<div className="tag-selector">