Skip to content

Commit 1840962

Browse files
authored
feat: support passing a fn to determine if value is clickable (#5)
for example, match only hyperlinks
2 parents fbf511c + 70eb48d commit 1840962

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-json-treeview",
3-
"version": "19.1.0",
3+
"version": "19.2.0",
44
"license": "Apache-2.0",
55
"author": {
66
"name": "Michael J Doyle",

projects/demo/src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ <h3>Clickable Nodes</h3>
2121
<ngx-json-treeview
2222
[json]="json"
2323
[expanded]="false"
24+
[isClickableValue]="isClickableValue"
2425
[enableClickableValues]="true"
2526
(onValueClick)="onValueClick($event)" />
2627
</div>

projects/demo/src/app/app.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class AppComponent {
4242
},
4343
};
4444

45+
isClickableValue(segment: Segment) {
46+
return ['object', 'array', 'string'].includes(segment.type ?? '');
47+
}
48+
4549
onValueClick(segment: Segment) {
4650
this.currentSegment.set(segment);
4751
}

projects/ngx-json-treeview/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-json-treeview",
3-
"version": "19.1.0",
3+
"version": "19.2.0",
44
"license": "Apache-2.0",
55
"author": {
66
"name": "Michael J Doyle",

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
@if (!expandable || !segment.expanded) {
2020
<span
2121
[class]="expandable ? 'segment-label' : 'segment-value'"
22-
[class.clickable]="enableClickableValues()"
23-
(click)="onValueClickHandler(segment)">
22+
[class.clickable]="
23+
enableClickableValues() && isClickableValue()(segment)
24+
"
25+
(click)="
26+
isClickableValue()(segment)
27+
? onValueClickHandler(segment)
28+
: undefined
29+
">
2430
{{ segment.description }}
2531
</span>
2632
} @else if (expandable && segment.expanded) {
@@ -39,6 +45,7 @@
3945
[json]="segment.value"
4046
[expanded]="expanded()"
4147
[depth]="depth()"
48+
[isClickableValue]="isClickableValue()"
4249
[enableClickableValues]="enableClickableValues()"
4350
[_parent]="segment"
4451
[_currentDepth]="_currentDepth() + 1"

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface Segment {
2727
path: string;
2828
}
2929

30+
export type IsClickableValueFn = (segment: Segment) => boolean;
31+
3032
/**
3133
* Renders JSON data in an expandable and collapsible tree structure.
3234
* Allows users to navigate complex data hierarchies visually.
@@ -73,6 +75,23 @@ export class NgxJsonTreeviewComponent {
7375
*/
7476
enableClickableValues = input<boolean>(false);
7577

78+
/**
79+
* A function that determines if a specific value node should be considered
80+
* clickable. This provides more granular control than the global
81+
* `enableClickableValues` flag.
82+
*
83+
* The function receives the `Segment` object and should return `true` if the
84+
* value is clickable, `false` otherwise. This check is only performed if
85+
* `enableClickableValues` is also `true`.
86+
*
87+
* @param segment - The segment being evaluated.
88+
* @returns `true` if the segment's value should be clickable, `false`
89+
* otherwise.
90+
* @default () => true - By default, all values are considered clickable if
91+
* `enableClickableValues` is true.
92+
*/
93+
isClickableValue = input<IsClickableValueFn>(() => true);
94+
7695
/**
7796
* If `enableClickableValues` is set to `true`, emits a `Segment` object when
7897
* a value node is clicked. The emitted `Segment` contains details about the

0 commit comments

Comments
 (0)