Skip to content

Commit

Permalink
Merge pull request #131 from fredrivett/dev
Browse files Browse the repository at this point in the history
Add deepCollapseChildren prop for allowing deeply collapsed children
  • Loading branch information
Daniel authored Aug 28, 2021
2 parents 4211a5d + aa79ca5 commit 8ab97d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ plugins: [
| ------------------------ | ------ | --------------------------------------------------------------------------------------- | ---------------------------------------------- | -------- |
| data | normal | JSON data | JSON object | - |
| deep | normal | Data depth, data larger than this depth will not be expanded | number | Infinity |
| deepCollapseChildren | normal | Whether children collapsed by `deep` prop should also be collapsed | boolean | false |
| showLength | normal | Whether to show the length when closed | boolean | false |
| showLine | normal | Whether to show the line | boolean | true |
| showDoubleQuotes | normal | Whether to show doublequotes on key | boolean | true |
Expand Down
6 changes: 6 additions & 0 deletions example/Basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@
<option :value="4">4</option>
</select>
</div>
<div>
<label>deepCollapseChildren</label>
<input v-model="deepCollapseChildren" type="checkbox" />
</div>
</div>
</div>
<div class="block">
<h3>vue-json-pretty:</h3>
<vue-json-pretty
:data="state.data"
:deep="state.deep"
:deepCollapseChildren="deepCollapseChildren"
:show-double-quotes="state.showDoubleQuotes"
:show-length="state.showLength"
:show-line="state.showLine"
Expand Down Expand Up @@ -96,6 +101,7 @@ export default defineComponent({
collapsedOnClickBrackets: true,
useCustomLinkFormatter: false,
deep: 3,
deepCollapseChildren: false,
});
const customLinkFormatter = (data, key, path, defaultFormatted) => {
Expand Down
9 changes: 8 additions & 1 deletion src/components/Tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default defineComponent({
type: Number,
default: Infinity,
},
deepCollapseChildren: {
type: Boolean,
default: false,
},
// Data root path.
path: {
type: String,
Expand Down Expand Up @@ -57,9 +61,12 @@ export default defineComponent({
translateY: 0,
visibleData: null as FlatDataType | null,
hiddenPaths: jsonFlatten(props.data, props.path).reduce((acc, item) => {
const depthComparison = this.deepCollapseChildren
? item.level >= this.deep
: item.level === this.deep;
if (
(item.type === 'objectStart' || item.type === 'arrayStart') &&
item.level === props.deep
depthComparison
) {
return {
...acc,
Expand Down

0 comments on commit 8ab97d6

Please sign in to comment.