ant-design-vue/components/vc-time-picker/Select.jsx

127 lines
3.0 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import classnames from 'classnames';
function noop() {}
2018-03-07 22:21:55 +08:00
const scrollTo = (element, to, duration) => {
2019-01-12 11:33:27 +08:00
const requestAnimationFrame =
window.requestAnimationFrame ||
function requestAnimationFrameTimeout() {
return setTimeout(arguments[0], 10);
};
2018-03-07 22:21:55 +08:00
// jump to target if duration zero
if (duration <= 0) {
2019-01-12 11:33:27 +08:00
element.scrollTop = to;
return;
2018-03-07 22:21:55 +08:00
}
2019-01-12 11:33:27 +08:00
const difference = to - element.scrollTop;
const perTick = (difference / duration) * 10;
2018-03-07 22:21:55 +08:00
requestAnimationFrame(() => {
2019-01-12 11:33:27 +08:00
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
});
};
2018-03-07 22:21:55 +08:00
2018-03-08 23:02:04 +08:00
const Select = {
mixins: [BaseMixin],
props: {
2018-03-07 22:21:55 +08:00
prefixCls: PropTypes.string,
options: PropTypes.array,
selectedIndex: PropTypes.number,
type: PropTypes.string,
2018-03-08 23:02:04 +08:00
// onSelect: PropTypes.func,
// onMouseEnter: PropTypes.func,
},
2019-01-12 11:33:27 +08:00
data() {
2018-03-08 23:02:04 +08:00
return {
active: false,
2019-01-12 11:33:27 +08:00
};
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
mounted() {
2018-03-08 23:02:04 +08:00
this.$nextTick(() => {
// jump to selected option
2019-01-12 11:33:27 +08:00
this.scrollToSelected(0);
});
2018-03-08 23:02:04 +08:00
},
watch: {
2019-02-01 17:23:00 +08:00
selectedIndex() {
2018-03-08 23:02:04 +08:00
this.$nextTick(() => {
// smooth scroll to selected option
2019-01-12 11:33:27 +08:00
this.scrollToSelected(120);
});
2018-03-08 23:02:04 +08:00
},
},
methods: {
2019-01-12 11:33:27 +08:00
onSelect(value) {
const { type } = this;
this.__emit('select', type, value);
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
getOptions() {
const { options, selectedIndex, prefixCls } = this;
2018-03-08 23:02:04 +08:00
return options.map((item, index) => {
const cls = classnames({
[`${prefixCls}-select-option-selected`]: selectedIndex === index,
[`${prefixCls}-select-option-disabled`]: item.disabled,
2019-01-12 11:33:27 +08:00
});
let onClick = noop;
2018-03-08 23:02:04 +08:00
if (!item.disabled) {
2019-01-12 11:33:27 +08:00
onClick = this.onSelect.bind(this, item.value);
2018-03-08 23:02:04 +08:00
}
2019-01-12 11:33:27 +08:00
return (
<li class={cls} key={index} onClick={onClick} disabled={item.disabled}>
{item.value}
</li>
);
});
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
scrollToSelected(duration) {
// move to selected item
const select = this.$el;
const list = this.$refs.list;
2018-03-08 23:02:04 +08:00
if (!list) {
2019-01-12 11:33:27 +08:00
return;
2018-03-08 23:02:04 +08:00
}
2019-01-12 11:33:27 +08:00
let index = this.selectedIndex;
2018-03-08 23:02:04 +08:00
if (index < 0) {
2019-01-12 11:33:27 +08:00
index = 0;
2018-03-08 23:02:04 +08:00
}
2019-01-12 11:33:27 +08:00
const topOption = list.children[index];
const to = topOption.offsetTop;
scrollTo(select, to, duration);
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
handleMouseEnter(e) {
this.setState({ active: true });
this.__emit('mouseenter', e);
2018-03-08 23:02:04 +08:00
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
handleMouseLeave() {
this.setState({ active: false });
2018-03-08 23:02:04 +08:00
},
},
2018-03-07 22:21:55 +08:00
2019-01-12 11:33:27 +08:00
render() {
2018-03-08 23:02:04 +08:00
if (this.options.length === 0) {
2019-01-12 11:33:27 +08:00
return null;
2018-03-07 22:21:55 +08:00
}
2019-01-12 11:33:27 +08:00
const { prefixCls } = this;
2018-03-08 23:02:04 +08:00
const cls = {
2018-03-07 22:21:55 +08:00
[`${prefixCls}-select`]: 1,
2018-03-08 23:02:04 +08:00
[`${prefixCls}-select-active`]: this.active,
2019-01-12 11:33:27 +08:00
};
2018-03-07 22:21:55 +08:00
return (
2019-01-12 11:33:27 +08:00
<div class={cls} onMouseenter={this.handleMouseEnter} onMouseleave={this.handleMouseLeave}>
<ul ref="list">{this.getOptions()}</ul>
2018-03-07 22:21:55 +08:00
</div>
2019-01-12 11:33:27 +08:00
);
2018-03-08 23:02:04 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-03-19 10:16:27 +08:00
2019-01-12 11:33:27 +08:00
export default Select;