@splicetree/core 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -5,6 +5,10 @@
5
5
  * - 若传入 `D`,表示函数可选择性接收该参数类型
6
6
  */
7
7
  type Fn<T, D = undefined> = [D] extends [undefined] ? () => T : (data?: D) => T;
8
+ /** 可能是数组的类型 */
9
+ type MaybeArray<T> = T | T[];
10
+ /** 可能是函数的类型 */
11
+ type MaybeFn<T, Args extends any[] = []> = T | ((...args: Args) => T);
8
12
  /**
9
13
  * 输入数据的通用结构
10
14
  * - 表示原始树数据的任意键值对对象
@@ -121,6 +125,7 @@ interface SpliceTreeConfiguration {
121
125
  * 设为 'deepest' 表示默认展开所有层级
122
126
  */
123
127
  defaultExpandedLevel?: number | 'deepest';
128
+ autoExpandParent?: boolean;
124
129
  }
125
130
  interface UseSpliceTreeOptions<T = SpliceTreeData> {
126
131
  /**
@@ -279,4 +284,4 @@ type CreateSpliceTree = <T = SpliceTreeData>(data: T[], options?: UseSpliceTreeO
279
284
  */
280
285
  declare function createSpliceTree<T extends SpliceTreeData = SpliceTreeData>(data: T[], options?: UseSpliceTreeOptions<T>): SpliceTreeInstance<T>;
281
286
  //#endregion
282
- export { CreateSpliceTree, Fn, SpliceTreeConfiguration, SpliceTreeData, SpliceTreeEventName, SpliceTreeEventPayload, SpliceTreeEventPayloadMap, SpliceTreeEvents, SpliceTreeInstance, SpliceTreeNode, SpliceTreePlugin, SpliceTreePluginContext, UseSpliceTreeOptions, createSpliceTree };
287
+ export { CreateSpliceTree, Fn, MaybeArray, MaybeFn, SpliceTreeConfiguration, SpliceTreeData, SpliceTreeEventName, SpliceTreeEventPayload, SpliceTreeEventPayloadMap, SpliceTreeEvents, SpliceTreeInstance, SpliceTreeNode, SpliceTreePlugin, SpliceTreePluginContext, UseSpliceTreeOptions, createSpliceTree };
package/dist/index.js CHANGED
@@ -92,7 +92,7 @@ function buildTree(data, keyField, parentField, expandedKeys = /* @__PURE__ */ n
92
92
 
93
93
  //#endregion
94
94
  //#region src/utils/expand.ts
95
- function initDefaultExpansion(map, expanded, def, lvl) {
95
+ function initDefaultExpansion(map, expanded, def, lvl, autoExpandParent) {
96
96
  const expandAll = () => {
97
97
  for (const id of map.keys()) expanded.add(id);
98
98
  };
@@ -104,7 +104,18 @@ function initDefaultExpansion(map, expanded, def, lvl) {
104
104
  return;
105
105
  }
106
106
  if (Array.isArray(def)) {
107
- for (const id of def) expanded.add(id);
107
+ for (const id of def) {
108
+ expanded.add(id);
109
+ if (autoExpandParent) {
110
+ let cur = map.get(id);
111
+ while (cur) {
112
+ const p = cur.getParent();
113
+ if (!p) break;
114
+ expanded.add(p.id);
115
+ cur = p;
116
+ }
117
+ }
118
+ }
108
119
  if (lvl === "deepest") {
109
120
  expandAll();
110
121
  return;
@@ -332,6 +343,7 @@ function createSpliceTree(data, options = {}) {
332
343
  const parentField = cfg.parentField ?? "parent";
333
344
  const defaultExpanded = cfg.defaultExpanded;
334
345
  const defaultExpandedLevel = cfg.defaultExpandedLevel;
346
+ const autoExpandParent = cfg.autoExpandParent;
335
347
  const events = createEmitter();
336
348
  const expandedKeys = createReactive(/* @__PURE__ */ new Set(), (payload) => {
337
349
  events.emit({
@@ -340,7 +352,7 @@ function createSpliceTree(data, options = {}) {
340
352
  });
341
353
  });
342
354
  let { roots, map, parentCache, childrenCache } = buildTree(data, keyField, parentField, expandedKeys);
343
- initDefaultExpansion(map, expandedKeys, defaultExpanded, defaultExpandedLevel);
355
+ initDefaultExpansion(map, expandedKeys, defaultExpanded, defaultExpandedLevel, autoExpandParent);
344
356
  const emitVisibility = () => {
345
357
  events.emit({
346
358
  name: "visibility",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/core",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "1.0.0",
5
5
  "author": {
6
6
  "email": "michael.cocova@gmail.com",
7
7
  "name": "Michael Cocova"