item[prop.value]).join(',') }>
{ sels.length === 0 && { config.tips }
}
this.bodyView = ref}>
{ Body }
{ disabled && }
);
}
//组件完成挂载
componentDidMount(){
this.prepare = true;
//监听键盘事件
this.base.addEventListener('keydown', e => {
let keyCode = e.keyCode;
if(keyCode === 13){
this.onClick()
}
});
//表单验证
this.input = this.base.querySelector('.xm-select-default');
//监听class的变化, 然后进行边框变色处理, 或者更多的处理
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
MutationObserver && new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type == "attributes") {
let attr = mutation.attributeName;
if(attr === 'class'){
if(this.input.className.indexOf('layui-form-danger') !== -1){
this.input.className = 'xm-select-default';
this.base.style.borderColor = this.props.theme.maxColor;
//这里可以自己新增一个回调, 也许看到源码的你能够看到
// this.base.scrollIntoView({ behavior: "smooth" });
}
}
}
});
}).observe(this.input, { attributes: true });
//监听form的重置按钮
let dom = this.base;
while(dom){
if(dom.tagName === 'FORM'){
let resetBtn = dom.querySelector('button[type="reset"]')
resetBtn && resetBtn.addEventListener('click', e => {
this.init(this.props, true);
});
break;
}
dom = dom.parentElement;
}
}
//此时页面又被重新渲染了
componentDidUpdate(){
let { direction, model } = this.props;
if(model.type === 'relative'){
return ;
}
let rect = this.base.getBoundingClientRect();
if(direction === 'auto'){
//用于控制js获取下拉框的高度
this.bodyView.style.display = 'block';
this.bodyView.style.visibility = 'hidden';
//获取下拉元素的高度
let bodyViewRect = this.bodyView.getBoundingClientRect();
let bodyViewHeight = bodyViewRect.height;
//还原控制效果
this.bodyView.style.display = '';
this.bodyView.style.visibility = '';
//确定下拉框是朝上还是朝下
let y = rect.y || rect.top || 0;
let clientHeight = document.documentElement.clientHeight;
let diff = clientHeight - y - rect.height - 20;
direction = diff > bodyViewHeight || y < diff ? 'down' : 'up';
}
if(direction == 'down'){
this.bodyView.style.top = rect.height + 4 + 'px';
this.bodyView.style.bottom = 'auto';
}else{
this.bodyView.style.top = 'auto';
this.bodyView.style.bottom = rect.height + 4 + 'px';
}
}
}
export default Framework;