2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classnames from '../_util/classNames';
|
2020-03-07 11:45:13 +00:00
|
|
|
import raf from 'raf';
|
2020-07-27 14:36:56 +00:00
|
|
|
import { findDOMNode } from '../_util/props-util';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function noop() {}
|
2018-03-07 14:21:55 +00:00
|
|
|
const scrollTo = (element, to, duration) => {
|
|
|
|
// jump to target if duration zero
|
|
|
|
if (duration <= 0) {
|
2020-03-07 11:45:13 +00:00
|
|
|
raf(() => {
|
|
|
|
element.scrollTop = to;
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-07 14:21:55 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const difference = to - element.scrollTop;
|
|
|
|
const perTick = (difference / duration) * 10;
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
raf(() => {
|
2019-03-04 13:00:08 +00:00
|
|
|
element.scrollTop += perTick;
|
2019-01-12 03:33:27 +00:00
|
|
|
if (element.scrollTop === to) return;
|
|
|
|
scrollTo(element, to, duration - 10);
|
|
|
|
});
|
|
|
|
};
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2018-03-08 15:02:04 +00:00
|
|
|
const Select = {
|
2020-07-21 08:46:39 +00:00
|
|
|
name: 'Select',
|
2018-03-08 15:02:04 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-07-21 08:46:39 +00:00
|
|
|
inheritAttrs: false,
|
2018-03-08 15:02:04 +00:00
|
|
|
props: {
|
2018-03-07 14:21:55 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
options: PropTypes.array,
|
|
|
|
selectedIndex: PropTypes.number,
|
|
|
|
type: PropTypes.string,
|
2018-03-08 15:02:04 +00:00
|
|
|
// onSelect: PropTypes.func,
|
|
|
|
// onMouseEnter: PropTypes.func,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-03-08 15:02:04 +00:00
|
|
|
return {
|
|
|
|
active: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-03-08 15:02:04 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
// jump to selected option
|
2019-01-12 03:33:27 +00:00
|
|
|
this.scrollToSelected(0);
|
|
|
|
});
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-02-01 09:23:00 +00:00
|
|
|
selectedIndex() {
|
2018-03-08 15:02:04 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
// smooth scroll to selected option
|
2019-01-12 03:33:27 +00:00
|
|
|
this.scrollToSelected(120);
|
|
|
|
});
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onSelect(value) {
|
|
|
|
const { type } = this;
|
|
|
|
this.__emit('select', type, value);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
onEsc(e) {
|
|
|
|
this.__emit('esc', e);
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
getOptions() {
|
|
|
|
const { options, selectedIndex, prefixCls } = this;
|
2018-03-08 15:02:04 +00:00
|
|
|
return options.map((item, index) => {
|
|
|
|
const cls = classnames({
|
|
|
|
[`${prefixCls}-select-option-selected`]: selectedIndex === index,
|
|
|
|
[`${prefixCls}-select-option-disabled`]: item.disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2019-03-04 13:00:08 +00:00
|
|
|
const onClick = item.disabled
|
2019-06-27 14:00:08 +00:00
|
|
|
? noop
|
2019-05-28 03:37:38 +00:00
|
|
|
: () => {
|
|
|
|
this.onSelect(item.value);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
const onKeyDown = e => {
|
|
|
|
if (e.keyCode === 13) onClick();
|
|
|
|
else if (e.keyCode === 27) this.onEsc();
|
|
|
|
};
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
2020-03-07 11:45:13 +00:00
|
|
|
<li
|
|
|
|
role="button"
|
|
|
|
onClick={onClick}
|
|
|
|
class={cls}
|
|
|
|
key={index}
|
|
|
|
disabled={item.disabled}
|
2020-07-17 09:13:30 +00:00
|
|
|
tabindex="0"
|
2020-03-07 11:45:13 +00:00
|
|
|
onKeydown={onKeyDown}
|
|
|
|
>
|
2019-01-12 03:33:27 +00:00
|
|
|
{item.value}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
});
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-03-04 13:00:08 +00:00
|
|
|
handleMouseEnter(e) {
|
|
|
|
this.setState({ active: true });
|
|
|
|
this.__emit('mouseenter', e);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseLeave() {
|
|
|
|
this.setState({ active: false });
|
|
|
|
},
|
|
|
|
|
2020-07-21 08:46:39 +00:00
|
|
|
saveList(node) {
|
|
|
|
this.list = node;
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollToSelected(duration) {
|
|
|
|
// move to selected item
|
2020-07-27 14:36:56 +00:00
|
|
|
const select = findDOMNode(this);
|
2019-01-12 03:33:27 +00:00
|
|
|
const list = this.$refs.list;
|
2018-03-08 15:02:04 +00:00
|
|
|
if (!list) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-08 15:02:04 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
let index = this.selectedIndex;
|
2018-03-08 15:02:04 +00:00
|
|
|
if (index < 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
index = 0;
|
2018-03-08 15:02:04 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const topOption = list.children[index];
|
|
|
|
const to = topOption.offsetTop;
|
|
|
|
scrollTo(select, to, duration);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-07 14:21:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-06-27 14:00:08 +00:00
|
|
|
const { prefixCls, options, active } = this;
|
|
|
|
if (options.length === 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return null;
|
2018-03-07 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 15:02:04 +00:00
|
|
|
const cls = {
|
2018-03-07 14:21:55 +00:00
|
|
|
[`${prefixCls}-select`]: 1,
|
2019-06-27 14:00:08 +00:00
|
|
|
[`${prefixCls}-select-active`]: active,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-07 14:21:55 +00:00
|
|
|
|
|
|
|
return (
|
2019-01-12 03:33:27 +00:00
|
|
|
<div class={cls} onMouseenter={this.handleMouseEnter} onMouseleave={this.handleMouseLeave}>
|
2020-07-21 08:46:39 +00:00
|
|
|
<ul ref={this.saveList}>{this.getOptions()}</ul>
|
2018-03-07 14:21:55 +00:00
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-08 15:02:04 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default Select;
|