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

128 lines
3.0 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import classnames from 'classnames';
function noop() {}
2018-03-07 14:21:55 +00:00
const scrollTo = (element, to, duration) => {
2019-01-12 03:33:27 +00:00
const requestAnimationFrame =
window.requestAnimationFrame ||
function requestAnimationFrameTimeout() {
return setTimeout(arguments[0], 10);
};
2018-03-07 14:21:55 +00:00
// jump to target if duration zero
if (duration <= 0) {
2019-01-12 03:33:27 +00:00
element.scrollTop = to;
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
requestAnimationFrame(() => {
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 = {
mixins: [BaseMixin],
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
},
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);
};
2019-01-12 03:33:27 +00:00
return (
2019-03-04 13:00:08 +00:00
<li role="button" onClick={onClick} class={cls} key={index} disabled={item.disabled}>
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 });
},
2019-01-12 03:33:27 +00:00
scrollToSelected(duration) {
// move to selected item
const select = this.$el;
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}>
<ul ref="list">{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;