ci eslint
This commit is contained in:
@@ -124,8 +124,7 @@ function handleTitleClick(node: TreeData) {
|
||||
handleChange(checked, node);
|
||||
}
|
||||
"
|
||||
>
|
||||
</LayCheckbox>
|
||||
/>
|
||||
<span
|
||||
:class="{
|
||||
'layui-tree-txt': true,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { App } from 'vue'
|
||||
import Component from './index.vue'
|
||||
import type { IDefineComponent } from '../type/index'
|
||||
import type { App } from "vue";
|
||||
import Component from "./index.vue";
|
||||
import type { IDefineComponent } from "../type/index";
|
||||
|
||||
Component.install = (app: App) => {
|
||||
app.component(Component.name || 'LayTree', Component)
|
||||
}
|
||||
app.component(Component.name || "LayTree", Component);
|
||||
};
|
||||
|
||||
export default Component as IDefineComponent
|
||||
export default Component as IDefineComponent;
|
||||
|
||||
@@ -94,6 +94,6 @@ function handleClick(node: TreeData) {
|
||||
:show-line="showLine"
|
||||
:only-icon-control="onlyIconControl"
|
||||
@node-click="handleClick"
|
||||
></tree-node>
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -1,82 +1,82 @@
|
||||
import { OriginalTreeData, StringOrNumber } from './tree.type'
|
||||
import { Nullable } from '../type'
|
||||
import { Ref, ref } from 'vue'
|
||||
import { OriginalTreeData, StringOrNumber } from "./tree.type";
|
||||
import { Nullable } from "../type";
|
||||
import { Ref, ref } from "vue";
|
||||
|
||||
type CustomKey = string | number
|
||||
type CustomString = (() => string) | string
|
||||
type CustomKey = string | number;
|
||||
type CustomString = (() => string) | string;
|
||||
|
||||
export interface TreeData {
|
||||
id: CustomKey
|
||||
title: CustomString
|
||||
children: TreeData[]
|
||||
parentKey: Nullable<StringOrNumber>
|
||||
isRoot: boolean
|
||||
isChecked: Ref<boolean>
|
||||
isDisabled: Ref<boolean>
|
||||
isLeaf: Ref<boolean>
|
||||
hasNextSibling: boolean
|
||||
parentNode: Nullable<TreeData>
|
||||
id: CustomKey;
|
||||
title: CustomString;
|
||||
children: TreeData[];
|
||||
parentKey: Nullable<StringOrNumber>;
|
||||
isRoot: boolean;
|
||||
isChecked: Ref<boolean>;
|
||||
isDisabled: Ref<boolean>;
|
||||
isLeaf: Ref<boolean>;
|
||||
hasNextSibling: boolean;
|
||||
parentNode: Nullable<TreeData>;
|
||||
}
|
||||
|
||||
interface ReplaceFields {
|
||||
id: string
|
||||
title: string
|
||||
children: string
|
||||
id: string;
|
||||
title: string;
|
||||
children: string;
|
||||
}
|
||||
|
||||
interface TreeConfig {
|
||||
showCheckbox: boolean
|
||||
checkedKeys: StringOrNumber[]
|
||||
expandKeys: StringOrNumber[]
|
||||
nodeMap: Map<StringOrNumber, TreeData>
|
||||
originMap: Map<StringOrNumber, OriginalTreeData>
|
||||
replaceFields: ReplaceFields
|
||||
showCheckbox: boolean;
|
||||
checkedKeys: StringOrNumber[];
|
||||
expandKeys: StringOrNumber[];
|
||||
nodeMap: Map<StringOrNumber, TreeData>;
|
||||
originMap: Map<StringOrNumber, OriginalTreeData>;
|
||||
replaceFields: ReplaceFields;
|
||||
}
|
||||
|
||||
class Tree {
|
||||
protected config: TreeConfig
|
||||
protected treeData: TreeData[]
|
||||
protected config: TreeConfig;
|
||||
protected treeData: TreeData[];
|
||||
|
||||
constructor(
|
||||
config: TreeConfig,
|
||||
origin: OriginalTreeData | OriginalTreeData[]
|
||||
) {
|
||||
this.config = config
|
||||
this.treeData = []
|
||||
this.init(origin)
|
||||
this.config = config;
|
||||
this.treeData = [];
|
||||
this.init(origin);
|
||||
}
|
||||
|
||||
init(origin: OriginalTreeData | OriginalTreeData[]): void {
|
||||
const tree = this.createTree(origin)
|
||||
this.treeData = tree
|
||||
const tree = this.createTree(origin);
|
||||
this.treeData = tree;
|
||||
}
|
||||
|
||||
createTree(
|
||||
origin: OriginalTreeData | OriginalTreeData[],
|
||||
parentKey: StringOrNumber = ''
|
||||
parentKey: StringOrNumber = ""
|
||||
): TreeData[] {
|
||||
let data
|
||||
let data;
|
||||
if (!Array.isArray(origin)) {
|
||||
data = Array.of(Object.assign({}, origin))
|
||||
data = Array.of(Object.assign({}, origin));
|
||||
} else {
|
||||
data = origin
|
||||
data = origin;
|
||||
}
|
||||
const nodeList: TreeData[] = []
|
||||
const { children } = this.config.replaceFields
|
||||
const nodeList: TreeData[] = [];
|
||||
const { children } = this.config.replaceFields;
|
||||
|
||||
const len = data.length
|
||||
const len = data.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const node = this.getNode(data[i], parentKey, i < len - 1)
|
||||
const nodeChildren = Reflect.get(node, children)
|
||||
const nodeHasChildren = !!Reflect.get(node, children)
|
||||
const node = this.getNode(data[i], parentKey, i < len - 1);
|
||||
const nodeChildren = Reflect.get(node, children);
|
||||
const nodeHasChildren = !!Reflect.get(node, children);
|
||||
|
||||
if (nodeHasChildren) {
|
||||
Reflect.set(node, children, this.createTree(nodeChildren, node.id))
|
||||
Reflect.set(node, children, this.createTree(nodeChildren, node.id));
|
||||
}
|
||||
|
||||
nodeList.push(node)
|
||||
nodeList.push(node);
|
||||
}
|
||||
return nodeList
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
getNode(
|
||||
@@ -90,113 +90,113 @@ class Tree {
|
||||
checkedKeys,
|
||||
expandKeys,
|
||||
replaceFields: { children, id, title },
|
||||
} = this.config
|
||||
} = this.config;
|
||||
|
||||
const nodeKey = Reflect.get(origin, id)
|
||||
const nodeTitle = Reflect.get(origin, title)
|
||||
const nodeChildren = Reflect.get(origin, children)
|
||||
const nodeDisabled = !!Reflect.get(origin, 'disabled')
|
||||
const nodeIsLeaf = !!Reflect.get(origin, 'spread')
|
||||
const nodeKey = Reflect.get(origin, id);
|
||||
const nodeTitle = Reflect.get(origin, title);
|
||||
const nodeChildren = Reflect.get(origin, children);
|
||||
const nodeDisabled = !!Reflect.get(origin, "disabled");
|
||||
const nodeIsLeaf = !!Reflect.get(origin, "spread");
|
||||
|
||||
const parentNode = nodeMap.get(parentKey)
|
||||
const parentNode = nodeMap.get(parentKey);
|
||||
|
||||
const node = Object.assign({}, origin, {
|
||||
id: nodeKey,
|
||||
title: nodeTitle,
|
||||
children: nodeChildren ? nodeChildren : [],
|
||||
parentKey: parentKey,
|
||||
isRoot: parentKey === '',
|
||||
isRoot: parentKey === "",
|
||||
isDisabled: ref(false),
|
||||
isChecked: ref(false),
|
||||
isLeaf: ref(false),
|
||||
hasNextSibling: hasNextSibling,
|
||||
parentNode: parentNode || null,
|
||||
})
|
||||
});
|
||||
|
||||
node.isDisabled.value = nodeDisabled
|
||||
node.isDisabled.value = nodeDisabled;
|
||||
node.isChecked.value = parentNode
|
||||
? parentNode.isChecked.value
|
||||
: checkedKeys.includes(nodeKey)
|
||||
: checkedKeys.includes(nodeKey);
|
||||
node.isLeaf.value = parentNode
|
||||
? parentNode.isLeaf.value
|
||||
: expandKeys.includes(nodeKey)
|
||||
node.isLeaf.value = nodeIsLeaf
|
||||
: expandKeys.includes(nodeKey);
|
||||
node.isLeaf.value = nodeIsLeaf;
|
||||
|
||||
if (!nodeMap.has(nodeKey)) {
|
||||
nodeMap.set(nodeKey, node)
|
||||
nodeMap.set(nodeKey, node);
|
||||
}
|
||||
if (!originMap.has(nodeKey)) {
|
||||
originMap.set(nodeKey, origin)
|
||||
originMap.set(nodeKey, origin);
|
||||
}
|
||||
|
||||
return node
|
||||
return node;
|
||||
}
|
||||
|
||||
setChildrenChecked(checked: boolean, nodes: TreeData[]) {
|
||||
const len = nodes.length
|
||||
const len = nodes.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
console.log(nodes[i], checked);
|
||||
nodes[i].isChecked.value = checked
|
||||
nodes[i].isChecked.value = checked;
|
||||
nodes[i].children &&
|
||||
nodes[i].children.length > 0 &&
|
||||
this.setChildrenChecked(checked, nodes[i].children)
|
||||
this.setChildrenChecked(checked, nodes[i].children);
|
||||
}
|
||||
}
|
||||
|
||||
setParentChecked(checked: boolean, parent: TreeData) {
|
||||
if (!parent) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
parent.isChecked.value = checked
|
||||
const pChild = parent.children
|
||||
const pChildChecked = pChild.some((c) => c.isChecked.value)
|
||||
parent.isChecked.value = checked;
|
||||
const pChild = parent.children;
|
||||
const pChildChecked = pChild.some((c) => c.isChecked.value);
|
||||
if (pChildChecked) {
|
||||
parent.isChecked.value = true
|
||||
parent.isChecked.value = true;
|
||||
}
|
||||
if (parent.parentNode) {
|
||||
this.setParentChecked(checked, parent.parentNode)
|
||||
this.setParentChecked(checked, parent.parentNode);
|
||||
}
|
||||
}
|
||||
|
||||
setCheckedKeys(checked: boolean, node: TreeData) {
|
||||
node.isChecked.value = checked
|
||||
node.isChecked.value = checked;
|
||||
// 处理上级
|
||||
if (node.parentNode) {
|
||||
this.setParentChecked(checked, node.parentNode)
|
||||
this.setParentChecked(checked, node.parentNode);
|
||||
}
|
||||
// 处理下级
|
||||
if (node.children) {
|
||||
this.setChildrenChecked(checked, node.children)
|
||||
this.setChildrenChecked(checked, node.children);
|
||||
}
|
||||
console.log(this.getData());
|
||||
}
|
||||
|
||||
getData() {
|
||||
return this.treeData
|
||||
return this.treeData;
|
||||
}
|
||||
|
||||
getKeys() {
|
||||
const checkedKeys = []
|
||||
const expandKeys = []
|
||||
const iterator = this.config.nodeMap[Symbol.iterator]()
|
||||
let next = iterator.next()
|
||||
const checkedKeys = [];
|
||||
const expandKeys = [];
|
||||
const iterator = this.config.nodeMap[Symbol.iterator]();
|
||||
let next = iterator.next();
|
||||
while (!next.done) {
|
||||
const [, node] = next.value
|
||||
const id = Reflect.get(node, this.config.replaceFields.id)
|
||||
const [, node] = next.value;
|
||||
const id = Reflect.get(node, this.config.replaceFields.id);
|
||||
if (node.isChecked.value) {
|
||||
checkedKeys.push(id)
|
||||
checkedKeys.push(id);
|
||||
}
|
||||
if (node.isLeaf.value) {
|
||||
expandKeys.push(id)
|
||||
expandKeys.push(id);
|
||||
}
|
||||
next = iterator.next()
|
||||
next = iterator.next();
|
||||
}
|
||||
return { checkedKeys, expandKeys }
|
||||
return { checkedKeys, expandKeys };
|
||||
}
|
||||
|
||||
getOriginData(key: StringOrNumber): OriginalTreeData {
|
||||
return this.config.originMap.get(key)!
|
||||
return this.config.originMap.get(key)!;
|
||||
}
|
||||
}
|
||||
|
||||
export { Tree }
|
||||
export { Tree };
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
export type StringFn = () => string
|
||||
export type StringOrNumber = string | number
|
||||
export type KeysType = (number | string)[]
|
||||
export type EditType = boolean | ('add' | 'update' | 'delete')
|
||||
export type StringFn = () => string;
|
||||
export type StringOrNumber = string | number;
|
||||
export type KeysType = (number | string)[];
|
||||
export type EditType = boolean | ("add" | "update" | "delete");
|
||||
|
||||
export interface OriginalTreeData {
|
||||
title: StringFn | string
|
||||
id: StringOrNumber
|
||||
field: StringFn | string
|
||||
children?: OriginalTreeData[]
|
||||
disabled?: boolean
|
||||
title: StringFn | string;
|
||||
id: StringOrNumber;
|
||||
field: StringFn | string;
|
||||
children?: OriginalTreeData[];
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export interface TreeProps {
|
||||
checkedKeys?: KeysType
|
||||
expandKeys?: KeysType
|
||||
data: OriginalTreeData
|
||||
showCheckbox?: boolean
|
||||
edit?: EditType
|
||||
accordion?: boolean
|
||||
onlyIconControl?: boolean
|
||||
showLine?: boolean
|
||||
checkedKeys?: KeysType;
|
||||
expandKeys?: KeysType;
|
||||
data: OriginalTreeData;
|
||||
showCheckbox?: boolean;
|
||||
edit?: EditType;
|
||||
accordion?: boolean;
|
||||
onlyIconControl?: boolean;
|
||||
showLine?: boolean;
|
||||
replaceFields?: {
|
||||
id?: string
|
||||
children?: string
|
||||
title?: string
|
||||
}
|
||||
id?: string;
|
||||
children?: string;
|
||||
title?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface TreeEmits {
|
||||
(e: 'update:checkedKeys', keys: KeysType): void
|
||||
(e: 'update:expandKeys', keys: KeysType): void
|
||||
(e: 'node-click', node: OriginalTreeData, event: Event): void
|
||||
(e: "update:checkedKeys", keys: KeysType): void;
|
||||
(e: "update:expandKeys", keys: KeysType): void;
|
||||
(e: "node-click", node: OriginalTreeData, event: Event): void;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { TreeEmits, TreeProps } from './tree.type'
|
||||
import { computed, ComputedRef, watch } from 'vue'
|
||||
import { Tree, TreeData } from './tree'
|
||||
import { TreeEmits, TreeProps } from "./tree.type";
|
||||
import { computed, ComputedRef, watch } from "vue";
|
||||
import { Tree, TreeData } from "./tree";
|
||||
|
||||
export declare type UseTree = (
|
||||
props: TreeProps,
|
||||
emit: TreeEmits
|
||||
) => {
|
||||
tree: Tree
|
||||
nodeList: ComputedRef<TreeData[]>
|
||||
}
|
||||
tree: Tree;
|
||||
nodeList: ComputedRef<TreeData[]>;
|
||||
};
|
||||
|
||||
export const useTree: UseTree = (props: TreeProps, emit: TreeEmits) => {
|
||||
const tree = new Tree(
|
||||
@@ -16,35 +16,35 @@ export const useTree: UseTree = (props: TreeProps, emit: TreeEmits) => {
|
||||
nodeMap: new Map(),
|
||||
originMap: new Map(),
|
||||
replaceFields: {
|
||||
id: 'id',
|
||||
title: 'title',
|
||||
children: 'children',
|
||||
id: "id",
|
||||
title: "title",
|
||||
children: "children",
|
||||
},
|
||||
showCheckbox: props.showCheckbox ?? false,
|
||||
checkedKeys: props.checkedKeys ?? [],
|
||||
expandKeys: props.expandKeys ?? [],
|
||||
},
|
||||
props.data
|
||||
)
|
||||
);
|
||||
|
||||
const nodeList = computed(() => {
|
||||
const nodes = tree.getData()
|
||||
const nodes = tree.getData();
|
||||
console.log(nodes);
|
||||
return nodes
|
||||
})
|
||||
return nodes;
|
||||
});
|
||||
|
||||
watch(
|
||||
() => nodeList,
|
||||
(list) => {
|
||||
const { checkedKeys, expandKeys } = tree.getKeys()
|
||||
emit('update:checkedKeys', checkedKeys)
|
||||
const { checkedKeys, expandKeys } = tree.getKeys();
|
||||
emit("update:checkedKeys", checkedKeys);
|
||||
// emit('update:expandKeys', expandKeys)
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
tree,
|
||||
nodeList,
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user