2020-08-30 20:33:21 +08:00
|
|
|
import { computed, watch } from 'vue'
|
2020-09-05 15:38:55 +08:00
|
|
|
import { TransferPanelProps, TransferPanelState, Key } from './transfer'
|
2020-08-30 20:33:21 +08:00
|
|
|
|
2020-12-01 10:28:42 +08:00
|
|
|
export const CHECKED_CHANGE_EVENT = 'checked-change'
|
|
|
|
|
2020-08-30 20:33:21 +08:00
|
|
|
|
2020-09-05 15:38:55 +08:00
|
|
|
export const useCheck = (props: TransferPanelProps, panelState: TransferPanelState, emit) => {
|
2020-08-30 20:33:21 +08:00
|
|
|
|
|
|
|
const labelProp = computed(() => props.props.label || 'label')
|
|
|
|
|
|
|
|
const keyProp = computed(() => props.props.key || 'key')
|
|
|
|
|
|
|
|
const disabledProp = computed(() => props.props.disabled || 'disabled')
|
|
|
|
|
|
|
|
|
|
|
|
const filteredData = computed(() => {
|
|
|
|
return props.data.filter(item => {
|
|
|
|
if (typeof props.filterMethod === 'function') {
|
2020-09-05 15:38:55 +08:00
|
|
|
return props.filterMethod(panelState.query, item)
|
2020-08-30 20:33:21 +08:00
|
|
|
} else {
|
|
|
|
const label = item[labelProp.value] || item[keyProp.value].toString()
|
2020-09-05 15:38:55 +08:00
|
|
|
return label.toLowerCase().includes(panelState.query.toLowerCase())
|
2020-08-30 20:33:21 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-09-05 15:38:55 +08:00
|
|
|
const checkableData = computed(() => {
|
|
|
|
return filteredData.value.filter(item => !item[disabledProp.value])
|
|
|
|
})
|
2020-08-30 20:33:21 +08:00
|
|
|
|
|
|
|
const checkedSummary = computed(() => {
|
2020-09-05 15:38:55 +08:00
|
|
|
const checkedLength = panelState.checked.length
|
2020-08-30 20:33:21 +08:00
|
|
|
const dataLength = props.data.length
|
|
|
|
const { noChecked, hasChecked } = props.format
|
|
|
|
|
|
|
|
if (noChecked && hasChecked) {
|
|
|
|
return checkedLength > 0
|
|
|
|
? hasChecked
|
|
|
|
.replace(/\${checked}/g, checkedLength.toString())
|
|
|
|
.replace(/\${total}/g, dataLength.toString())
|
|
|
|
: noChecked.replace(/\${total}/g, dataLength.toString())
|
|
|
|
} else {
|
|
|
|
return `${ checkedLength }/${ dataLength }`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const isIndeterminate = computed(() => {
|
2020-09-05 15:38:55 +08:00
|
|
|
const checkedLength = panelState.checked.length
|
2020-08-30 20:33:21 +08:00
|
|
|
return checkedLength > 0 && checkedLength < checkableData.value.length
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const updateAllChecked = () => {
|
|
|
|
const checkableDataKeys = checkableData.value.map(item => item[keyProp.value])
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.allChecked = checkableDataKeys.length > 0 && checkableDataKeys.every(item => panelState.checked.includes(item))
|
2020-08-30 20:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleAllCheckedChange = (value: Key[]) => {
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.checked = value ? checkableData.value.map(item => item[keyProp.value]) : []
|
2020-08-30 20:33:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 15:38:55 +08:00
|
|
|
watch(() => panelState.checked, (val, oldVal) => {
|
2020-08-30 20:33:21 +08:00
|
|
|
updateAllChecked()
|
|
|
|
|
2020-09-05 15:38:55 +08:00
|
|
|
if (panelState.checkChangeByUser) {
|
2020-08-30 20:33:21 +08:00
|
|
|
const movedKeys = val
|
|
|
|
.concat(oldVal)
|
|
|
|
.filter(v => !val.includes(v) || !oldVal.includes(v))
|
|
|
|
emit(CHECKED_CHANGE_EVENT, val, movedKeys)
|
|
|
|
} else {
|
|
|
|
emit(CHECKED_CHANGE_EVENT, val)
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.checkChangeByUser = true
|
2020-08-30 20:33:21 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(checkableData, () => {
|
|
|
|
updateAllChecked()
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.data, () => {
|
|
|
|
const checked = []
|
|
|
|
const filteredDataKeys = filteredData.value.map(item => item[keyProp.value])
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.checked.forEach(item => {
|
2020-08-30 20:33:21 +08:00
|
|
|
if (filteredDataKeys.includes(item)) {
|
|
|
|
checked.push(item)
|
|
|
|
}
|
|
|
|
})
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.checkChangeByUser = false
|
|
|
|
panelState.checked = checked
|
2020-08-30 20:33:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
watch(() => props.defaultChecked, (val, oldVal) => {
|
|
|
|
if (oldVal && val.length === oldVal.length && val.every(item => oldVal.includes(item))) return
|
|
|
|
|
|
|
|
const checked = []
|
|
|
|
const checkableDataKeys = checkableData.value.map(item => item[keyProp.value])
|
|
|
|
|
|
|
|
val.forEach(item => {
|
|
|
|
if (checkableDataKeys.includes(item)) {
|
|
|
|
checked.push(item)
|
|
|
|
}
|
|
|
|
})
|
2020-09-05 15:38:55 +08:00
|
|
|
panelState.checkChangeByUser = false
|
|
|
|
panelState.checked = checked
|
2020-08-30 20:33:21 +08:00
|
|
|
}, {
|
|
|
|
immediate: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
labelProp,
|
|
|
|
keyProp,
|
|
|
|
disabledProp,
|
|
|
|
filteredData,
|
|
|
|
checkableData,
|
|
|
|
checkedSummary,
|
|
|
|
isIndeterminate,
|
|
|
|
updateAllChecked,
|
|
|
|
handleAllCheckedChange,
|
|
|
|
}
|
|
|
|
}
|