Skip to content

Commit 1c5bcb7

Browse files
committed
Update README
1 parent 98b0ca8 commit 1c5bcb7

File tree

1 file changed

+97
-6
lines changed

1 file changed

+97
-6
lines changed

README.md

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,122 @@ npm install ngx-json-treeview
2424

2525
## Usage
2626

27-
To render JSON in its fully expanded state.
27+
### Basic Setup
28+
29+
By default, JSON objects are rendered fully expanded:
2830

2931
```html
3032
<ngx-json-treeview [json]="someObject" />
3133
```
3234

33-
To render JSON with all nodes collapsed.
35+
### Controlling Field Expansion
36+
37+
To render the JSON with all nodes initially collapsed:
3438

3539
```html
3640
<ngx-json-treeview [json]="someObject" [expanded]="false" />
3741
```
3842

39-
Alternatively, expand only to a max depth by default.
43+
Or with nodes expanded up to a certain depth:
4044

4145
```html
4246
<ngx-json-treeview [json]="someObject" [depth]="1" />
4347
```
4448

45-
You can enable the user to click on values. Provide `onValueClick` to implement
46-
the desired behavior.
49+
### Clickable Values
50+
51+
You can make values in the JSON tree clickable to trigger custom actions. For
52+
convenience, a default click handler is provided for URLs (which will be opened
53+
in a new tab, when clicked.)
54+
55+
1. **Enable Clickable Values**: Set the `enableClickableValues` input to
56+
`true`. This also enables the default click handler(s) automatically.
57+
58+
```html
59+
<ngx-json-treeview [json]="someObject" [enableClickableValues]="true" />
60+
```
61+
62+
2. **Provide Click Handlers**: Provide your own custom behaviors by passing an
63+
array of `ValueClickHandler` objects to the
64+
`valueClickHandlers` input.
65+
66+
A `ValueClickHandler` has two properties:
67+
68+
- `canHandle`: A function that returns `true` if the handler should apply to
69+
a given value.
70+
- `handler`: The function to execute when the value is clicked.
71+
72+
Only the _first_ handler in the array where `canHandle` returns `true` will
73+
be executed.
74+
75+
#### Example: Copy to Clipboard
76+
77+
Here's how you could implement a handler that copies a string value to the
78+
clipboard when clicked.
79+
80+
**In your component's TypeScript file:**
81+
82+
```typescript
83+
import { Segment, ValueClickHandler } from 'ngx-json-treeview';
84+
85+
// Define a custom click handler
86+
copyToClipboardHandler: ValueClickHandler = {
87+
canHandle: (segment: Segment) => typeof segment.value === 'string',
88+
handler: (segment: Segment) => {
89+
navigator.clipboard.writeText(segment.value).then(() => {
90+
alert(`Copied "${segment.value}" to clipboard!`);
91+
});
92+
},
93+
};
94+
95+
customValueClickHandlers: ValueClickHandler[] = [
96+
this.copyToClipboardHandler,
97+
// Add addt'l custom handlers here
98+
];
99+
```
100+
101+
**In your component's HTML file:**
47102

103+
<!-- prettier-ignore -->
48104
```html
49-
<ngx-json-treeview [json]="someObject" [enableClickableValues]="true" (onValueClick)="onValueClick($event)" />
105+
<ngx-json-treeview
106+
[json]="someObject"
107+
[enableClickableValues]="true"
108+
[valueClickhandlers]="customValueClickHandlers"
109+
/>
50110
```
51111

112+
In this example, any string value will be clickable. When clicked, it will be
113+
copied to the clipboard.
114+
115+
#### Combining Handlers
116+
117+
Custom handlers can be combined alongside the built-in ones (such as the URL
118+
handler). To apply all of the default built-in handlers, you can import the `VALUE_CLICK_HANDLERS` array and spread it into your `customValueClickHandlers`
119+
array. Alternatively, handlers be the imported individually via
120+
`ValueClickHandlers`.
121+
122+
```typescript
123+
import {
124+
ValueClickHandlers,
125+
VALUE_CLICK_HANDLERS,
126+
} from 'ngx-json-treeview';
127+
128+
customValueClickHandlers: ValueClickHandler[] = [
129+
...VALUE_CLICK_HANDLERS,
130+
this.copyToClipboardHandler,
131+
];
132+
133+
// OR
134+
135+
customValueClickHandlers: ValueClickHandler[] = [
136+
ValueClickHandlers.followLinkHandler,
137+
this.copyToClipboardHandler,
138+
];
139+
```
140+
141+
In this example, any string that matches a URL will trigger the `followLinkHandler`, and all other strings will trigger the `copyToClipboardHandler`.
142+
52143
## Theming
53144

54145
You can customize the appearance of the tree view using these CSS variables:

0 commit comments

Comments
 (0)