Skip to content

Commit 8f6f60e

Browse files
authored
Merge pull request #151 from hsiaosiyuan0/dev
feat: collapsePath
2 parents 2c14ad7 + 1f0ac07 commit 8f6f60e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

example/Basic.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<label>deepCollapseChildren</label>
3939
<input v-model="state.deepCollapseChildren" type="checkbox" />
4040
</div>
41+
<div>
42+
<label>defaultCollapsePath</label>
43+
<input v-model="state.collapsePathPattern" type="input" />
44+
</div>
4145
</div>
4246
</div>
4347
<div class="block">
@@ -46,6 +50,7 @@
4650
:data="state.data"
4751
:deep="state.deep"
4852
:deepCollapseChildren="state.deepCollapseChildren"
53+
:collapsePath="state.collapsePath"
4954
:show-double-quotes="state.showDoubleQuotes"
5055
:show-length="state.showLength"
5156
:show-line="state.showLine"
@@ -75,6 +80,9 @@ const defaultData = {
7580
'Traffic paradise: How to design streets for people and unmanned vehicles in the future?',
7681
source: 'Netease smart',
7782
link: 'http://netease.smart/traffic-paradise/1235',
83+
author: {
84+
names: ['Daniel', 'Mike', 'John'],
85+
},
7886
},
7987
{
8088
news_id: 51182,
@@ -100,8 +108,10 @@ export default defineComponent({
100108
showDoubleQuotes: true,
101109
collapsedOnClickBrackets: true,
102110
useCustomLinkFormatter: false,
103-
deep: 3,
111+
deep: 4,
104112
deepCollapseChildren: false,
113+
collapsePath: /members/,
114+
collapsePathPattern: 'members',
105115
});
106116
107117
const customLinkFormatter = (data, key, path, defaultFormatted) => {
@@ -123,6 +133,17 @@ export default defineComponent({
123133
},
124134
);
125135
136+
watch(
137+
() => state.collapsePath,
138+
newVal => {
139+
try {
140+
state.collapsePath = new RegExp(newVal);
141+
} catch (err) {
142+
// console.log('Regexp ERROR');
143+
}
144+
},
145+
);
146+
126147
return {
127148
state,
128149
customLinkFormatter,

src/components/Tree/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export default defineComponent({
2424
type: Boolean,
2525
default: false,
2626
},
27+
collapsePath: {
28+
type: RegExp,
29+
default: null,
30+
},
2731
// Data root path.
2832
path: {
2933
type: String,
@@ -64,7 +68,13 @@ export default defineComponent({
6468
const depthComparison = props.deepCollapseChildren
6569
? item.level >= props.deep
6670
: item.level === props.deep;
67-
if ((item.type === 'objectStart' || item.type === 'arrayStart') && depthComparison) {
71+
const pathComparison =
72+
depthComparison ||
73+
(props.collapsePath && props.collapsePath.test(item.path));
74+
if (
75+
(item.type === 'objectStart' || item.type === 'arrayStart') &&
76+
(depthComparison || pathComparison)
77+
) {
6878
return {
6979
...acc,
7080
[item.path]: 1,

0 commit comments

Comments
 (0)