import { h, Component, render } from '@/components/preact' import { isFunction, isArray, safety, deepMerge, mergeArr, IEVersion } from '@/components/common/util' /** * 普通的多选渲染 */ class General extends Component{ constructor(options){ super(options); this.setState({ filterValue: '', remote: true, loading: false, pageIndex: 1, pageSize: 10, }); this.searchCid = 0; this.inputOver = true; this.__value = ''; } optionClick(item, selected, disabled, e){ this.props.ck(item, selected, disabled); //阻止父组件上的事件冒泡 this.blockClick(e); } groupClick(item, e){ const { click, children, disabled } = this.props.prop; let m = item[click], arr = item[children].filter(opt => !opt[disabled]); if(m === 'SELECT'){ this.props.onReset(arr, 'append'); }else if(m === 'CLEAR'){ this.props.onReset(arr, 'delete'); }else if(m === 'AUTO'){ this.props.onReset(arr, 'auto'); }else if(isFunction(m)){ m(item); } //阻止父组件上的事件冒泡 this.blockClick(e); } blockClick(e){ e.stopPropagation(); } pagePrevClick(e){ let index = this.state.pageIndex; if(index <= 1){ return ; } this.changePageIndex(index - 1); } pageNextClick(e, size){ let index = this.state.pageIndex; if(index >= size){ return ; } this.changePageIndex(index + 1); } changePageIndex(index){ this.setState({ pageIndex: index }) } searchInput(e){ let v = e.target.value; if(v === this.__value){ return ; } clearTimeout(this.searchCid); if(this.inputOver){ //保证输入框内的值是实时的 this.__value = v; //让搜索变成异步的 this.searchCid = setTimeout(() => { this.callback = true; this.setState({ filterValue: this.__value, remote: true }) }, this.props.delay); } } focus(){ this.searchInputRef && this.searchInputRef.focus(); } blur(){ this.searchInputRef && this.searchInputRef.blur(); } handleComposition(e){ let type = e.type; if(type === 'compositionstart'){ this.inputOver = false; clearTimeout(this.searchCid); }else if(type === 'compositionend'){ this.inputOver = true; this.searchInput(e); } } componentWillReceiveProps(props){ if(this.props.show != props.show){ if(!props.show){ //清空输入框的值 this.setState({ filterValue: '' }); this.__value = ''; this.searchInputRef && (this.searchInputRef.value = ''); }else{ //聚焦输入框 setTimeout(() => this.focus(), 0); } } } componentDidUpdate(){ if(this.callback){ this.callback = false; let done = this.props.filterDone; if(isFunction(done)){ done(this.state.filterValue, this.tempData || []); } } } render(config) { let { data, prop, template, theme, radio, sels, empty, filterable, filterMethod, remoteSearch, remoteMethod, delay, searchTips } = config const { name, value, disabled, children, optgroup } = prop; let arr = deepMerge([], data); //是否开启了搜索 if(filterable){ if(remoteSearch){//是否进行远程搜索 if(this.state.remote){ this.callback = false; this.setState({ loading: true, remote: false }); //让输入框失去焦点 this.blur(); remoteMethod(this.state.filterValue, result => { //回调后可以重新聚焦 this.focus(); this.callback = true; this.setState({ loading: false }); this.props.onReset(result, 'data'); }, this.props.show); } }else{ const filterData = (item, index) => { const isGroup = item[optgroup]; if(isGroup){ delete item.__del; return true; } return filterMethod(this.state.filterValue, item, index, prop); } arr = arr.filter(filterData); for(let i = 0; i < arr.length - 1; i++){ let a = arr[i]; let b = arr[i + 1]; if(a[optgroup] && b[optgroup]){ arr[i].__del = true; } } if(arr.length && arr[arr.length - 1][optgroup]){ arr[arr.length - 1].__del = true; } arr = arr.filter(item => !item.__del); } } const search = (