Skip to content

Commit b6cece5

Browse files
committed
Update README
1 parent 98b0ca8 commit b6cece5

File tree

1 file changed

+92
-6
lines changed

1 file changed

+92
-6
lines changed

README.md

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,117 @@ 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

48103
```html
49-
<ngx-json-treeview [json]="someObject" [enableClickableValues]="true" (onValueClick)="onValueClick($event)" />
104+
<ngx-json-treeview [json]="someObject" [enableClickableValues]="true" [valueClickHandlers]="customValueClickHandlers" />
50105
```
51106

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

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

0 commit comments

Comments
 (0)