ant-design-vue/components/vc-tree-select/demo/basic.jsx

399 lines
13 KiB
Vue
Raw Normal View History

2018-07-11 17:51:20 +08:00
/* eslint react/no-multi-comp:0, no-console:0, no-alert: 0 */
2019-01-12 11:33:27 +08:00
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';
2018-07-11 17:51:20 +08:00
2019-01-12 11:33:27 +08:00
function isLeaf(value) {
2018-07-11 17:51:20 +08:00
if (!value) {
2019-01-12 11:33:27 +08:00
return false;
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
let queues = [...gData];
while (queues.length) {
// BFS
const item = queues.shift();
2018-07-11 17:51:20 +08:00
if (item.value === value) {
if (!item.children) {
2019-01-12 11:33:27 +08:00
return true;
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
return false;
2018-07-11 17:51:20 +08:00
}
if (item.children) {
2019-01-12 11:33:27 +08:00
queues = queues.concat(item.children);
2018-07-11 17:51:20 +08:00
}
}
2019-01-12 11:33:27 +08:00
return false;
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
function findPath(value, data) {
const sel = [];
function loop(selected, children) {
2018-07-11 17:51:20 +08:00
for (let i = 0; i < children.length; i++) {
2019-01-12 11:33:27 +08:00
const item = children[i];
2018-07-11 17:51:20 +08:00
if (selected === item.value) {
2019-01-12 11:33:27 +08:00
sel.push(item);
return;
2018-07-11 17:51:20 +08:00
}
if (item.children) {
2019-01-12 11:33:27 +08:00
loop(selected, item.children, item);
2018-07-11 17:51:20 +08:00
if (sel.length) {
2019-01-12 11:33:27 +08:00
sel.push(item);
return;
2018-07-11 17:51:20 +08:00
}
}
}
}
2019-01-12 11:33:27 +08:00
loop(value, data);
return sel;
2018-07-11 17:51:20 +08:00
}
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,
},
}),
2018-07-11 17:51:20 +08:00
methods: {
2019-01-12 11:33:27 +08:00
onClick() {
this.setState({
visible: true,
2019-01-12 11:33:27 +08:00
});
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onClose() {
this.setState({
visible: false,
2019-01-12 11:33:27 +08:00
});
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onSearch(value) {
console.log('Do Search:', value, arguments);
this.setState({ searchValue: value });
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onChange(value, ...rest) {
console.log('onChange', value, ...rest);
this.setState({ value });
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onChangeChildren(...args) {
console.log('onChangeChildren', ...args);
const value = args[0];
const pre = value ? this.value : undefined;
this.setState({ value: isLeaf(value) ? value : pre });
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onChangeLV(value) {
console.log('labelInValue', arguments);
2018-07-11 17:51:20 +08:00
if (!value) {
2019-01-12 11:33:27 +08:00
this.setState({ lv: undefined });
return;
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
const path = findPath(value.value, gData)
.map(i => i.label)
.reverse()
.join(' > ');
this.setState({ lv: { value: value.value, label: path } });
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onMultipleChange(value) {
console.log('onMultipleChange', arguments);
this.setState({ multipleValue: value });
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
onSelect() {
2018-07-11 17:51:20 +08:00
// use onChange instead
2019-01-12 11:33:27 +08:00
console.log(arguments);
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
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;
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
return true;
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
filterTreeNode(input, child) {
return String(child.data.props.title).indexOf(input) === 0;
2018-07-11 17:51:20 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
2018-07-11 17:51:20 +08:00
return (
<div style={{ margin: '20px' }}>
<h2>tree-select in dialog</h2>
2019-01-12 11:33:27 +08:00
<button class="btn btn-primary" onClick={this.onClick}>
show dialog
</button>
{this.visible ? (
<Dialog
visible={this.visible}
animation="zoom"
maskAnimation="fade"
onClose={this.onClose}
style={{ width: '600px', height: '400px', overflow: 'auto' }}
id="area"
>
<div style={{ height: '600px', paddingTop: '100px' }}>
<TreeSelect
getPopupContainer={triggerNode => triggerNode.parentNode}
style={{ width: '300px' }}
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
dropdownStyle={{ maxHeight: '200px', overflow: 'auto', zIndex: 1500 }}
placeholder={<i>请下拉选择</i>}
searchPlaceholder="please search"
showSearch
allowClear
treeLine
value={this.value}
treeData={gData}
treeNodeFilterProp="label"
filterTreeNode={false}
onSearch={this.onSearch}
onChange={this.onChange}
onSelect={this.onSelect}
__propsSymbol__={Symbol()}
/>
</div>
</Dialog>
) : null}
2018-07-11 17:51:20 +08:00
<h2>single select</h2>
<TreeSelect
style={{ width: '300px' }}
2019-01-12 11:33:27 +08:00
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
2018-07-11 17:51:20 +08:00
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
showSearch
allowClear
treeLine
searchValue={this.searchValue}
2018-07-11 17:51:20 +08:00
value={this.value}
treeData={gData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="label"
2018-07-11 17:51:20 +08:00
filterTreeNode={false}
onSearch={this.onSearch}
open={this.tsOpen}
onChange={(value, ...args) => {
2019-01-12 11:33:27 +08:00
console.log('onChange', value, ...args);
2018-07-11 17:51:20 +08:00
if (value === '0-0-0-0-value') {
2019-01-12 11:33:27 +08:00
this.setState({ tsOpen: true });
2018-07-11 17:51:20 +08:00
} else {
2019-01-12 11:33:27 +08:00
this.setState({ tsOpen: false });
2018-07-11 17:51:20 +08:00
}
2019-01-12 11:33:27 +08:00
this.setState({ value });
}}
2018-07-11 17:51:20 +08:00
dropdownVisibleChange={(v, info) => {
2019-01-12 11:33:27 +08:00
console.log('single onDropdownVisibleChange', v, info);
2018-07-11 17:51:20 +08:00
// document clicked
if (info.documentClickClose && this.value === '0-0-0-0-value') {
2019-01-12 11:33:27 +08:00
return false;
2018-07-11 17:51:20 +08:00
}
this.setState({
tsOpen: v,
2019-01-12 11:33:27 +08:00
});
return true;
}}
2018-07-11 17:51:20 +08:00
onSelect={this.onSelect}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>single select (just select children)</h2>
<TreeSelect
style={{ width: '300px' }}
2019-01-12 11:33:27 +08:00
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
2018-07-11 17:51:20 +08:00
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
showSearch
allowClear
treeLine
2018-07-11 17:51:20 +08:00
value={this.value}
treeData={gData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="label"
2018-07-11 17:51:20 +08:00
filterTreeNode={false}
onChange={this.onChangeChildren}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>multiple select</h2>
<TreeSelect
2018-07-11 17:51:20 +08:00
style={{ width: '300px' }}
2019-01-12 11:33:27 +08:00
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
2018-07-11 17:51:20 +08:00
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
2018-07-11 17:51:20 +08:00
multiple
value={this.multipleValue}
treeData={gData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="title"
2018-07-11 17:51:20 +08:00
onChange={this.onMultipleChange}
onSelect={this.onSelect}
allowClear
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>check select</h2>
<TreeSelect
2019-01-12 11:33:27 +08:00
class="check-select"
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
2018-07-11 17:51:20 +08:00
dropdownStyle={{ height: '200px', overflow: 'auto' }}
dropdownPopupAlign={{ overflow: { adjustY: 0, adjustX: 0 }, offset: [0, 2] }}
dropdownVisibleChange={this.onDropdownVisibleChange}
2018-07-11 17:51:20 +08:00
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
treeLine
maxTagTextLength={10}
2018-07-11 17:51:20 +08:00
value={this.value}
autoClearSearchValue
2018-07-11 17:51:20 +08:00
treeData={gData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="title"
treeCheckable
showCheckedStrategy={SHOW_PARENT}
2018-07-11 17:51:20 +08:00
onChange={this.onChange}
onSelect={this.onSelect}
maxTagCount={2}
2019-01-12 11:33:27 +08:00
maxTagPlaceholder={valueList => {
console.log('Max Tag Rest Value:', valueList);
return `${valueList.length} rest...`;
}}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>labelInValue & show path</h2>
<TreeSelect
style={{ width: '500px' }}
2019-01-12 11:33:27 +08:00
transitionName="rc-tree-select-dropdown-slide-up"
choiceTransitionName="rc-tree-select-selection__choice-zoom"
2018-07-11 17:51:20 +08:00
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
showSearch
allowClear
treeLine
value={this.lv}
labelInValue
2018-07-11 17:51:20 +08:00
treeData={gData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="label"
2018-07-11 17:51:20 +08:00
filterTreeNode={false}
onChange={this.onChangeLV}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>use treeDataSimpleMode</h2>
<TreeSelect
style={{ width: '300px' }}
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
placeholder={<i>请下拉选择</i>}
2019-01-12 11:33:27 +08:00
searchPlaceholder="please search"
treeLine
maxTagTextLength={10}
searchValue={this.simpleSearchValue}
2019-01-12 11:33:27 +08:00
onSearch={simpleSearchValue => {
this.setState({ simpleSearchValue });
}}
2018-07-11 17:51:20 +08:00
value={this.value}
treeData={this.simpleTreeData}
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="title"
2018-07-11 17:51:20 +08:00
treeDataSimpleMode={this.treeDataSimpleMode}
2019-01-12 11:33:27 +08:00
treeCheckable
showCheckedStrategy={SHOW_PARENT}
2018-07-11 17:51:20 +08:00
onChange={this.onChange}
onSelect={this.onSelect}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>Testing in extreme conditions (Boundary conditions test) </h2>
<TreeSelect
style={{ width: '200px' }}
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
2019-01-12 11:33:27 +08:00
defaultValue="leaf1"
multiple
treeCheckable
showCheckedStrategy={SHOW_PARENT}
2018-07-11 17:51:20 +08:00
treeDefaultExpandAll
treeData={[
{ key: '', value: '', label: 'empty value', children: [] },
{
2019-01-12 11:33:27 +08:00
key: '0',
value: '0',
label: '0 label',
children: [
2018-07-11 17:51:20 +08:00
{ key: '00', value: '00', label: '00 label', children: [] },
{ key: '01', value: '01', label: '01 label', children: [] },
],
},
]}
onChange={(val, ...args) => console.log(val, ...args)}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
/>
<h2>use TreeNode Component (not recommend)</h2>
<TreeSelect
style={{ width: '200px' }}
dropdownStyle={{ maxHeight: '200px', overflow: 'auto' }}
2019-01-12 11:33:27 +08:00
defaultValue="leaf1"
2018-07-11 17:51:20 +08:00
treeDefaultExpandAll
2019-01-12 11:33:27 +08:00
treeNodeFilterProp="title"
2018-07-11 17:51:20 +08:00
filterTreeNode={this.filterTreeNode}
onChange={(val, ...args) => console.log(val, ...args)}
__propsSymbol__={Symbol()}
2018-07-11 17:51:20 +08:00
>
2019-01-12 11:33:27 +08:00
<TreeNode value="" title="parent 1" key="">
<TreeNode value="parent 1-0" title="parent 1-0" key="0-1-0">
<TreeNode value="leaf1" title="my leaf" key="random" />
<TreeNode value="leaf2" title="your leaf" key="random1" disabled />
2018-07-11 17:51:20 +08:00
</TreeNode>
2019-01-12 11:33:27 +08:00
<TreeNode value="parent 1-1" title="parent 1-1" key="0-1-1">
<TreeNode
value="sss"
title={<span style={{ color: 'red' }}>sss</span>}
key="random3"
2018-07-11 17:51:20 +08:00
/>
2019-01-12 11:33:27 +08:00
<TreeNode value="same value1" title="same txtle" key="0-1-1-1">
<TreeNode
value="same value10"
title="same titlexd"
key="0-1-1-1-0"
style={{ color: 'red', background: 'green' }}
/>
2018-07-11 17:51:20 +08:00
</TreeNode>
</TreeNode>
</TreeNode>
2019-01-12 11:33:27 +08:00
<TreeNode value="same value2" title="same title" key="0-2">
<TreeNode value="2same value" title="2same title" key="0-2-0" />
2018-07-11 17:51:20 +08:00
</TreeNode>
2019-01-12 11:33:27 +08:00
<TreeNode value="same value3" title="same title" key="0-3" />
2018-07-11 17:51:20 +08:00
</TreeSelect>
</div>
2019-01-12 11:33:27 +08:00
);
2018-07-11 17:51:20 +08:00
},
2019-01-12 11:33:27 +08:00
};