/* eslint react/no-multi-comp:0, no-console:0, no-alert: 0 */
import BaseMixin from '../../_util/BaseMixin'
import '../assets/index.less'
import '../../vc-dialog/assets/index.less'
import Dialog from '../../vc-dialog'
import TreeSelect, { TreeNode, SHOW_PARENT } from '../src/index'
import { gData } from './util'
import './demo.less'
function isLeaf (value) {
if (!value) {
return false
}
let queues = [...gData]
while (queues.length) { // BFS
const item = queues.shift()
if (item.value === value) {
if (!item.children) {
return true
}
return false
}
if (item.children) {
queues = queues.concat(item.children)
}
}
return false
}
function findPath (value, data) {
const sel = []
function loop (selected, children) {
for (let i = 0; i < children.length; i++) {
const item = children[i]
if (selected === item.value) {
sel.push(item)
return
}
if (item.children) {
loop(selected, item.children, item)
if (sel.length) {
sel.push(item)
return
}
}
}
}
loop(value, data)
return sel
}
export default {
mixins: [BaseMixin],
data: () => ({
tsOpen: false,
visible: false,
searchValue: '0-0-0-label',
value: '0-0-0-value1',
// value: ['0-0-0-0-value', '0-0-0-1-value', '0-0-0-2-value'],
lv: { value: '0-0-0-value', label: 'spe label' },
multipleValue: [],
simpleSearchValue: 'test111',
simpleTreeData: [
{ key: 1, pId: 0, label: 'test1', value: 'test1' },
{ key: 121, pId: 0, label: 'test2', value: 'test2' },
{ key: 11, pId: 1, label: 'test11', value: 'test11' },
{ key: 12, pId: 1, label: 'test12', value: 'test12' },
{ key: 111, pId: 11, label: 'test111', value: 'test111' },
],
treeDataSimpleMode: {
id: 'key',
rootPId: 0,
},
}),
methods: {
onClick () {
this.setState({
visible: true,
})
},
onClose () {
this.setState({
visible: false,
})
},
onSearch (value) {
console.log('Do Search:', value, arguments)
this.setState({ searchValue: value })
},
onChange (value, ...rest) {
console.log('onChange', value, ...rest)
this.setState({ value })
},
onChangeChildren (...args) {
console.log('onChangeChildren', ...args)
const value = args[0]
const pre = value ? this.value : undefined
this.setState({ value: isLeaf(value) ? value : pre })
},
onChangeLV (value) {
console.log('labelInValue', arguments)
if (!value) {
this.setState({ lv: undefined })
return
}
const path = findPath(value.value, gData).map(i => i.label).reverse().join(' > ')
this.setState({ lv: { value: value.value, label: path }})
},
onMultipleChange (value) {
console.log('onMultipleChange', arguments)
this.setState({ multipleValue: value })
},
onSelect () {
// use onChange instead
console.log(arguments)
},
onDropdownVisibleChange (visible, info) {
console.log(visible, this.value, info)
if (Array.isArray(this.value) && this.value.length > 1 &&
this.value.length < 3) {
window.alert('please select more than two item or less than one item.')
return false
}
return true
},
filterTreeNode (input, child) {
return String(child.data.props.title).indexOf(input) === 0
},
},
render () {
return (
tree-select in dialog
{this.visible ?
: null}
single select
请下拉选择}
searchPlaceholder='please search'
showSearch allowClear treeLine
searchValue={this.searchValue}
value={this.value}
treeData={gData}
treeNodeFilterProp='label'
filterTreeNode={false}
onSearch={this.onSearch}
open={this.tsOpen}
onChange={(value, ...args) => {
console.log('onChange', value, ...args)
if (value === '0-0-0-0-value') {
this.setState({ tsOpen: true })
} else {
this.setState({ tsOpen: false })
}
this.setState({ value })
} }
dropdownVisibleChange={(v, info) => {
console.log('single onDropdownVisibleChange', v, info)
// document clicked
if (info.documentClickClose && this.value === '0-0-0-0-value') {
return false
}
this.setState({
tsOpen: v,
})
return true
} }
onSelect={this.onSelect}
__propsSymbol__={Symbol()}
/>
single select (just select children)
请下拉选择}
searchPlaceholder='please search'
showSearch allowClear treeLine
value={this.value}
treeData={gData}
treeNodeFilterProp='label'
filterTreeNode={false}
onChange={this.onChangeChildren}
__propsSymbol__={Symbol()}
/>
multiple select
请下拉选择}
searchPlaceholder='please search'
multiple
value={this.multipleValue}
treeData={gData}
treeNodeFilterProp='title'
onChange={this.onMultipleChange}
onSelect={this.onSelect}
allowClear
__propsSymbol__={Symbol()}
/>
check select
请下拉选择}
searchPlaceholder='please search'
treeLine maxTagTextLength={10}
value={this.value}
autoClearSearchValue
treeData={gData}
treeNodeFilterProp='title'
treeCheckable showCheckedStrategy={SHOW_PARENT}
onChange={this.onChange}
onSelect={this.onSelect}
maxTagCount={2}
maxTagPlaceholder={(valueList) => {
console.log('Max Tag Rest Value:', valueList)
return `${valueList.length} rest...`
}}
__propsSymbol__={Symbol()}
/>
labelInValue & show path
请下拉选择}
searchPlaceholder='please search'
showSearch allowClear treeLine
value={this.lv} labelInValue
treeData={gData}
treeNodeFilterProp='label'
filterTreeNode={false}
onChange={this.onChangeLV}
__propsSymbol__={Symbol()}
/>
use treeDataSimpleMode
请下拉选择}
searchPlaceholder='please search'
treeLine maxTagTextLength={10}
searchValue={this.simpleSearchValue}
onSearch={(simpleSearchValue) => {
this.setState({ simpleSearchValue })
}}
value={this.value}
treeData={this.simpleTreeData}
treeNodeFilterProp='title'
treeDataSimpleMode={this.treeDataSimpleMode}
treeCheckable showCheckedStrategy={SHOW_PARENT}
onChange={this.onChange}
onSelect={this.onSelect}
__propsSymbol__={Symbol()}
/>
Testing in extreme conditions (Boundary conditions test)
console.log(val, ...args)}
__propsSymbol__={Symbol()}
/>
use TreeNode Component (not recommend)
console.log(val, ...args)}
__propsSymbol__={Symbol()}
>
sss} key='random3'
/>
)
},
}