2018-04-07 00:20:45 +08:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import PropTypes from '../_util/vue-types'
|
|
|
|
import { isValidElement, initDefaultProps } from '../_util/props-util'
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps'
|
|
|
|
import Checkbox from '../checkbox'
|
|
|
|
import Search from './search'
|
|
|
|
import Item from './item'
|
|
|
|
import triggerEvent from '../_util/triggerEvent'
|
2018-09-05 21:28:54 +08:00
|
|
|
import addEventListener from '../_util/Dom/addEventListener'
|
|
|
|
|
|
|
|
function isIEorEDGE () {
|
|
|
|
return document.documentMode || /Edge/.test(navigator.userAgent)
|
|
|
|
}
|
2018-04-07 00:20:45 +08:00
|
|
|
|
2018-04-07 10:32:13 +08:00
|
|
|
function noop () {
|
|
|
|
}
|
|
|
|
|
|
|
|
const TransferItem = {
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
title: PropTypes.string.isRequired,
|
2018-04-07 00:20:45 +08:00
|
|
|
description: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRenderResultPlainObject (result) {
|
|
|
|
return result && !isValidElement(result) &&
|
|
|
|
Object.prototype.toString.call(result) === '[object Object]'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TransferListProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
titleText: PropTypes.string,
|
2018-04-07 10:32:13 +08:00
|
|
|
dataSource: PropTypes.arrayOf(PropTypes.shape(TransferItem).loose),
|
2018-04-07 00:20:45 +08:00
|
|
|
filter: PropTypes.string,
|
|
|
|
filterOption: PropTypes.func,
|
|
|
|
checkedKeys: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
handleFilter: PropTypes.func,
|
|
|
|
handleSelect: PropTypes.func,
|
|
|
|
handleSelectAll: PropTypes.func,
|
|
|
|
handleClear: PropTypes.func,
|
|
|
|
renderItem: PropTypes.func,
|
|
|
|
showSearch: PropTypes.bool,
|
|
|
|
searchPlaceholder: PropTypes.string,
|
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
itemUnit: PropTypes.string,
|
|
|
|
itemsUnit: PropTypes.string,
|
|
|
|
body: PropTypes.any,
|
|
|
|
footer: PropTypes.any,
|
|
|
|
lazy: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled: PropTypes.bool,
|
2018-04-07 00:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'TransferList',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: initDefaultProps(TransferListProps, {
|
|
|
|
dataSource: [],
|
|
|
|
titleText: '',
|
|
|
|
showSearch: false,
|
|
|
|
renderItem: noop,
|
|
|
|
lazy: {},
|
|
|
|
}),
|
|
|
|
data () {
|
|
|
|
this.timer = null
|
|
|
|
this.triggerScrollTimer = null
|
|
|
|
return {
|
|
|
|
mounted: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted () {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.timer = setTimeout(() => {
|
2018-04-07 00:20:45 +08:00
|
|
|
this.setState({
|
|
|
|
mounted: true,
|
|
|
|
})
|
|
|
|
}, 0)
|
2018-09-05 21:28:54 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.$refs.listContentWrapper) {
|
|
|
|
const listContentWrapperDom = this.$refs.listContentWrapper.$el
|
|
|
|
this.scrollEvent = addEventListener(listContentWrapperDom, 'scroll', this.handleScroll)
|
|
|
|
}
|
|
|
|
})
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
beforeDestroy () {
|
|
|
|
clearTimeout(this.timer)
|
|
|
|
clearTimeout(this.triggerScrollTimer)
|
2018-09-05 21:28:54 +08:00
|
|
|
clearTimeout(this.fixIERepaintTimer)
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updated () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove()
|
|
|
|
}
|
|
|
|
if (this.$refs.listContentWrapper) {
|
|
|
|
const listContentWrapperDom = this.$refs.listContentWrapper.$el
|
|
|
|
this.scrollEvent = addEventListener(listContentWrapperDom, 'scroll', this.handleScroll)
|
|
|
|
}
|
|
|
|
})
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2018-09-05 21:28:54 +08:00
|
|
|
handleScroll (e) {
|
|
|
|
this.$emit('scroll', e)
|
|
|
|
},
|
2018-04-07 00:20:45 +08:00
|
|
|
getCheckStatus (filteredDataSource) {
|
|
|
|
const { checkedKeys } = this.$props
|
|
|
|
if (checkedKeys.length === 0) {
|
|
|
|
return 'none'
|
|
|
|
} else if (filteredDataSource.every(item => checkedKeys.indexOf(item.key) >= 0)) {
|
|
|
|
return 'all'
|
|
|
|
}
|
|
|
|
return 'part'
|
|
|
|
},
|
|
|
|
_handleSelect (selectedItem) {
|
|
|
|
const { checkedKeys } = this.$props
|
|
|
|
const result = checkedKeys.some((key) => key === selectedItem.key)
|
|
|
|
this.handleSelect(selectedItem, !result)
|
|
|
|
},
|
|
|
|
_handleFilter (e) {
|
2018-09-05 21:28:54 +08:00
|
|
|
this.handleFilter(e)
|
2018-04-07 00:20:45 +08:00
|
|
|
if (!e.target.value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Manually trigger scroll event for lazy search bug
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5631
|
2018-09-05 21:28:54 +08:00
|
|
|
this.triggerScrollTimer = setTimeout(() => {
|
|
|
|
const transferNode = this.$el
|
|
|
|
const listNode = transferNode.querySelectorAll('.ant-transfer-list-content')[0]
|
2018-04-07 00:20:45 +08:00
|
|
|
if (listNode) {
|
|
|
|
triggerEvent(listNode, 'scroll')
|
|
|
|
}
|
|
|
|
}, 0)
|
2018-09-05 21:28:54 +08:00
|
|
|
this.fixIERepaint()
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
_handleClear (e) {
|
|
|
|
this.handleClear(e)
|
2018-09-05 21:28:54 +08:00
|
|
|
this.fixIERepaint()
|
2018-04-07 00:20:45 +08:00
|
|
|
},
|
|
|
|
matchFilter (text, item) {
|
|
|
|
const { filter, filterOption } = this.$props
|
|
|
|
if (filterOption) {
|
|
|
|
return filterOption(filter, item)
|
|
|
|
}
|
|
|
|
return text.indexOf(filter) >= 0
|
|
|
|
},
|
|
|
|
renderItemHtml (item) {
|
|
|
|
const { renderItem = noop } = this.$props
|
|
|
|
const renderResult = renderItem(item)
|
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult)
|
|
|
|
return {
|
|
|
|
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
|
|
|
|
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
// Fix IE/Edge repaint
|
|
|
|
// https://github.com/ant-design/ant-design/issues/9697
|
|
|
|
// https://stackoverflow.com/q/27947912/3040605
|
|
|
|
fixIERepaint () {
|
|
|
|
if (!isIEorEDGE()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.fixIERepaintTimer = window.setTimeout(() => {
|
|
|
|
if (this.$refs.notFoundNode) {
|
|
|
|
this.$refs.notFoundNode.className = this.$refs.notFoundNode.className
|
|
|
|
}
|
|
|
|
}, 0)
|
|
|
|
},
|
2018-04-07 00:20:45 +08:00
|
|
|
filterNull (arr) {
|
|
|
|
return arr.filter((item) => {
|
|
|
|
return item !== null
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {
|
2018-12-05 20:00:13 +08:00
|
|
|
prefixCls, dataSource, titleText, checkedKeys, lazy, disabled,
|
|
|
|
body, footer, showSearch, filter,
|
2018-04-07 00:20:45 +08:00
|
|
|
searchPlaceholder, notFoundContent, itemUnit, itemsUnit,
|
|
|
|
} = this.$props
|
|
|
|
|
|
|
|
// Custom Layout
|
2018-12-05 20:00:13 +08:00
|
|
|
const footerDom = footer && footer({ ...this.$props })
|
|
|
|
const bodyDom = body && body({ ...this.$props })
|
2018-04-07 00:20:45 +08:00
|
|
|
|
|
|
|
const listCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-with-footer`]: !!footerDom,
|
|
|
|
})
|
|
|
|
|
|
|
|
const filteredDataSource = []
|
|
|
|
const totalDataSource = []
|
|
|
|
|
|
|
|
const showItems = dataSource.map((item) => {
|
|
|
|
const { renderedText, renderedEl } = this.renderItemHtml(item)
|
|
|
|
if (filter && filter.trim() && !this.matchFilter(renderedText, item)) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// all show items
|
|
|
|
totalDataSource.push(item)
|
|
|
|
if (!item.disabled) {
|
|
|
|
// response to checkAll items
|
|
|
|
filteredDataSource.push(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
const checked = checkedKeys.indexOf(item.key) >= 0
|
|
|
|
return (
|
|
|
|
<Item
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled={disabled}
|
2018-04-07 00:20:45 +08:00
|
|
|
key={item.key}
|
|
|
|
item={item}
|
|
|
|
lazy={lazy}
|
|
|
|
renderedText={renderedText}
|
|
|
|
renderedEl={renderedEl}
|
|
|
|
checked={checked}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
onClick={this._handleSelect}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
const unit = dataSource.length > 1 ? itemsUnit : itemUnit
|
|
|
|
|
|
|
|
const search = showSearch ? (
|
|
|
|
<div class={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
2018-09-05 21:28:54 +08:00
|
|
|
onChange={this._handleFilter}
|
2018-04-07 00:20:45 +08:00
|
|
|
handleClear={this.handleClear}
|
|
|
|
placeholder={searchPlaceholder}
|
|
|
|
value={filter}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
const transitionName = this.mounted ? `${prefixCls}-content-item-highlight` : ''
|
|
|
|
const transitionProps = getTransitionProps(transitionName, {
|
|
|
|
leave: noop,
|
|
|
|
})
|
|
|
|
const listBody = bodyDom || (
|
|
|
|
<div
|
2018-12-05 20:00:13 +08:00
|
|
|
class={classNames(showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`)}
|
2018-04-07 00:20:45 +08:00
|
|
|
>
|
|
|
|
{search}
|
|
|
|
<transition-group
|
|
|
|
{...transitionProps}
|
2018-09-05 21:28:54 +08:00
|
|
|
tag='ul'
|
|
|
|
class={`${prefixCls}-content`}
|
|
|
|
ref='listContentWrapper'
|
2018-04-07 00:20:45 +08:00
|
|
|
>
|
2018-09-05 21:28:54 +08:00
|
|
|
{showItems}
|
2018-04-07 00:20:45 +08:00
|
|
|
</transition-group>
|
2018-09-05 21:28:54 +08:00
|
|
|
<div class={`${prefixCls}-body-not-found`} ref='notFoundNode'>
|
2018-04-07 00:20:45 +08:00
|
|
|
{notFoundContent}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
|
|
|
const listFooter = footerDom ? (
|
|
|
|
<div class={`${prefixCls}-footer`}>
|
|
|
|
{footerDom}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
|
|
|
|
const checkStatus = this.getCheckStatus(filteredDataSource)
|
|
|
|
const checkedAll = checkStatus === 'all'
|
|
|
|
const checkAllCheckbox = (
|
|
|
|
<Checkbox
|
|
|
|
ref='checkbox'
|
2018-12-05 20:00:13 +08:00
|
|
|
disabled={disabled}
|
2018-04-07 00:20:45 +08:00
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus === 'part'}
|
|
|
|
onChange={() => {
|
|
|
|
this.handleSelectAll(filteredDataSource, checkedAll)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={listCls}>
|
|
|
|
<div class={`${prefixCls}-header`}>
|
|
|
|
{checkAllCheckbox}
|
|
|
|
<span class={`${prefixCls}-header-selected`}>
|
|
|
|
<span>
|
|
|
|
{(checkedKeys.length > 0 ? `${checkedKeys.length}/` : '') + totalDataSource.length} {unit}
|
|
|
|
</span>
|
|
|
|
<span class={`${prefixCls}-header-title`}>
|
|
|
|
{titleText}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|