2020-07-19 14:40:35 +00:00
|
|
|
import { TransitionGroup } from 'vue';
|
2020-03-07 11:45:13 +00:00
|
|
|
import raf from '../_util/raf';
|
|
|
|
import ListItem from './ListItem';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
2020-07-27 14:36:56 +00:00
|
|
|
import { findDOMNode } from '../_util/props-util';
|
2020-03-07 11:45:13 +00:00
|
|
|
function noop() {}
|
|
|
|
const ListBody = {
|
|
|
|
name: 'ListBody',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
filteredRenderItems: PropTypes.array.def([]),
|
2020-10-10 10:16:28 +00:00
|
|
|
lazy: PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object]),
|
2020-03-07 11:45:13 +00:00
|
|
|
selectedKeys: PropTypes.array,
|
2020-10-10 10:16:28 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2020-08-05 09:17:07 +00:00
|
|
|
onItemSelect: PropTypes.func,
|
|
|
|
onItemSelectAll: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
mounted: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
itemsLength() {
|
|
|
|
return this.filteredRenderItems ? this.filteredRenderItems.length : 0;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
itemsLength() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
const { lazy } = this.$props;
|
|
|
|
if (lazy !== false) {
|
2020-07-27 14:36:56 +00:00
|
|
|
const container = findDOMNode(this);
|
2020-03-07 11:45:13 +00:00
|
|
|
raf.cancel(this.lazyId);
|
|
|
|
this.lazyId = raf(() => {
|
|
|
|
if (container) {
|
|
|
|
const scrollEvent = new Event('scroll', { bubbles: true });
|
|
|
|
container.dispatchEvent(scrollEvent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.mountId = raf(() => {
|
|
|
|
this.mounted = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2020-03-07 11:45:13 +00:00
|
|
|
raf.cancel(this.mountId);
|
|
|
|
raf.cancel(this.lazyId);
|
|
|
|
},
|
|
|
|
methods: {
|
2020-08-05 09:17:07 +00:00
|
|
|
handleItemSelect(item) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { selectedKeys } = this.$props;
|
|
|
|
const checked = selectedKeys.indexOf(item.key) >= 0;
|
|
|
|
this.$emit('itemSelect', item.key, !checked);
|
|
|
|
},
|
2020-08-05 09:17:07 +00:00
|
|
|
handleScroll(e) {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('scroll', e);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const { mounted } = this.$data;
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
filteredRenderItems,
|
|
|
|
lazy,
|
|
|
|
selectedKeys,
|
|
|
|
disabled: globalDisabled,
|
|
|
|
} = this.$props;
|
|
|
|
const items = filteredRenderItems.map(({ renderedEl, renderedText, item }) => {
|
|
|
|
const { disabled } = item;
|
|
|
|
const checked = selectedKeys.indexOf(item.key) >= 0;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ListItem
|
|
|
|
disabled={globalDisabled || disabled}
|
|
|
|
key={item.key}
|
|
|
|
item={item}
|
|
|
|
lazy={lazy}
|
|
|
|
renderedText={renderedText}
|
|
|
|
renderedEl={renderedEl}
|
|
|
|
checked={checked}
|
|
|
|
prefixCls={prefixCls}
|
2020-08-05 09:17:07 +00:00
|
|
|
onClick={this.handleItemSelect}
|
2020-03-07 11:45:13 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const transitionProps = getTransitionProps(
|
|
|
|
mounted ? `${prefixCls}-content-item-highlight` : '',
|
|
|
|
{
|
|
|
|
tag: 'ul',
|
2020-08-05 09:17:07 +00:00
|
|
|
onScroll: this.handleScroll,
|
2020-07-19 14:40:35 +00:00
|
|
|
onLeave: noop,
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return (
|
2020-07-19 14:40:35 +00:00
|
|
|
<TransitionGroup class={`${prefixCls}-content`} {...transitionProps}>
|
2020-03-07 11:45:13 +00:00
|
|
|
{items}
|
2020-07-19 14:40:35 +00:00
|
|
|
</TransitionGroup>
|
2020-03-07 11:45:13 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-19 14:40:35 +00:00
|
|
|
export default props => <ListBody {...props} />;
|