'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require('create-react-class');
var PropTypes = require('prop-types');
var Infinite = require('react-infinite');
var classNames = require('classnames');
var ClassNameMixin = require('./mixins/ClassNameMixin');
var Dropdown = require('./Dropdown');
var Icon = require('./Icon');
var Input = require('./Input');
var cloneDeep = require('lodash/cloneDeep');
var InfiniteSelected = createReactClass({
mixins: [ClassNameMixin],
propTypes: {
classprefix: PropTypes.string,
data: PropTypes.array.isRequired,
placeholder: PropTypes.string,
value: PropTypes.string,
multiple: PropTypes.bool,
searchBox: PropTypes.bool,
name: PropTypes.string,
onChange: PropTypes.func,
optionFilter: PropTypes.func, // (filterText, option) => {}
dropup: PropTypes.bool,
btnWidth: PropTypes.number,
btnStyle: PropTypes.string,
btnSize: PropTypes.string,
maxHeight: PropTypes.number,
sortSelected: PropTypes.bool,
limit: PropTypes.number,
onValidateValue: PropTypes.func, // 判断value是否有效
// delimiter to use to join multiple values
delimiter: PropTypes.string
},
getDefaultProps: function () {
return {
sortSelected: true,
classprefix: 'selected',
placeholder: '点击选择...',
onChange: function () { },
onValidateValue: function (value) { return true },
value: '',
delimiter: ',',
optionFilter: function optionFilter(filterText, option) {
return filterText ? option.label.toLowerCase().indexOf(filterText) > -1 : true;
}
};
},
getInitialState: function () {
return {
value: this.props.value,
dropdownWidth: null,
filterText: null,
selectedLabel: [],
isInfiniteLoading: false,
items: [],
data: [],
loadNum: 50,
};
},
componentDidMount: function () {
this.setDropdownWidth();
},
componentWillUpdate: function componentWillUpdate(nextProps, nextState) {
if (nextState.filterText !== this.state.filterText) {
this.setItems(nextState.data, nextState.filterText);
}
},
componentWillReceiveProps: function (nextProps) {
var sortedData = this.showSelectedFirst(nextProps.value, cloneDeep(nextProps.data));
this.setState({
value: nextProps.value,
data: sortedData,
selectedLabel: this.getSelectedLabel(nextProps.value, sortedData),
lastSearchTime: new Date().getTime(),
});
this.setItems(sortedData, this.state.filterText);
},
setItems: function (sortedData, filterText) {
var filterData = filterText ? this.getFilterData(filterText) : sortedData;
var items = this.buildElements(filterData, 0, this.state.loadNum);
this.setState({
items: items,
itemsLength: items.length,
filterData: filterData,
filterText: filterText
});
},
buildElements: function (data, start, end) {
var groupHeader;
var items = [];
var temp = new Date().getTime();
data && data.slice(start, end).forEach(function (option, i) {
var checked = this.hasValue(option.value);
var checkedClass = checked ? this.setClassNamespace('checked') : null;
var checkedIcon = checked ? : null;
// add group header
if (option.group && groupHeader !== option.group) {
groupHeader = option.group;
items.push(
{groupHeader}
);
}
items.push(
{option.label}
{checkedIcon}
);
}.bind(this));
return items;
},
handleInfiniteLoad: function () {
var that = this;
this.setState({
isInfiniteLoading: true
});
setTimeout(function () {
var elemLength = that.state.itemsLength;
var dataLength = that.state.data.length;
if (elemLength >= dataLength) {
that.setState({
isInfiniteLoading: false
});
return false;
}
var newElements = that.buildElements(that.state.filterData, elemLength, elemLength + that.state.loadNum >= dataLength ? dataLength : elemLength + that.state.loadNum);
that.setState({
isInfiniteLoading: false,
items: that.state.items.concat(newElements),
itemsLength: newElements.length + elemLength
});
}, 2500);
},
elementInfiniteLoad: function () {
return
Loading...
;
},
getSelectedLabel: function (value, data) {
if (!data || data.length <= 0) {
return [];
}
var label = [];
var valueArr = value ? value.split(this.props.delimiter) : [];
data.forEach(function (option, i) {
if (valueArr.indexOf(option.value) > -1) {
label.push(option.label);
} else {
return;
}
})
return label;
},
getFilterData: function (filterText) {
if (!filterText) {
return [];
}
var data = [];
var that = this;
this.state.data.forEach(function (option, i) {
if (that.props.optionFilter(filterText, option)) {
data.push(option);
}
})
return data;
},
setDropdownWidth: function () {
if (this.isMounted()) {
var that = this;
setTimeout(function() {
var toggleButton = ReactDOM.findDOMNode(that.refs.dropdown.refs.dropdownToggle);
toggleButton && that.setState({ dropdownWidth: toggleButton.offsetWidth });
}, 300);
}
},
getValueArray: function () {
return this.state.value ? this.state.value.split(this.props.delimiter) : [];
},
hasValue: function (value) {
return this.getValueArray().indexOf(value) > -1;
},
setValue: function (value, callback) {
if (this.props.onValidateValue(value)) {
this.setState({
value: value
}, function () {
this.props.onChange(value);
callback && callback();
});
}
},
handleCheck: function (option, e) {
e.preventDefault();
var clickedValue = option.value;
// multiple select
if (this.props.multiple) {
var values = this.getValueArray();
if (this.hasValue(clickedValue)) {
values.splice(values.indexOf(clickedValue), 1);
} else if (!this.props.limit || values.length < this.props.limit) {
values.push(clickedValue);
}
this.setValue(values.join(this.props.delimiter));
} else {
this.setValue(clickedValue);
this.refs.dropdown.setDropdown(false);
}
},
handleUserInput: function (e) {
e.preventDefault();
var now = new Date().getTime();
var change = { lastSearchTime: now };
if (now - this.state.lastSearchTime > 300) {
change = Object.assign(change,
{
filterText: ReactDOM.findDOMNode(this.refs.filterInput).value,
timeout: this.getTimeout(this.handleTimeout)
})
}
this.setState(change);
},
getTimeout: function (callback) {
if (this.state.timeout) {
clearTimeout(this.state.timeout);
}
return setTimeout(callback, 300);
},
handleTimeout: function () {
//clear timeout and check filterText
if (this.state.timeout) {
clearTimeout(this.state.timeout);
}
var _filterText = ReactDOM.findDOMNode(this.refs.filterInput).value;
if (_filterText !== this.state.filterText) {
this.setItems(this.state.data, _filterText);
}
},
// clear filter
clearFilterInput: function () {
if (this.props.multiple && this.props.searchBox) {
this.setState({
filterText: null
});
ReactDOM.findDOMNode(this.refs.filterInput).value = null;
}
},
// API for getting component value
getValue: function () {
return this.state.value;
},
showSelectedFirst(value, data) {
let that = this;
if (Object.prototype.toString.call(data) != '[object Array]' || data.length == 0) {
return [];
}
var arr = value && value.split(',') || [],
temp = [];
arr.forEach(_value => {
//从data中找到_value, 并弹出
let index = that.findIndex(data, _value);
if (index == -1) {
return;
}
temp = temp.concat(data.splice(index, 1))
});
return temp.concat(data);
},
findIndex(data, value) {
// var value = '4.0.0.195(4.0.0.195)';
for (var i = 0, l = data.length; i < l; i++) {
if (data[i].value == value) {
return i;
}
}
return -1;
},
render: function () {
var classSet = this.getClassSet();
var status = (
{this.state.selectedLabel.length ? this.state.selectedLabel.join(', ') : (
{this.props.placeholder}
)}
);
var optionsStyle = {};
if (this.props.maxHeight) {
optionsStyle = {
maxHeight: this.props.maxHeight,
overflowY: 'scroll'
};
}
let searchTip = this.props.searchBox && this.props.searchTip || null;
return (
{searchTip}
{this.props.searchBox ? (
) : null}
);
}
});
module.exports = InfiniteSelected;