|
| 1 | +## Basic Example |
| 2 | + |
| 3 | +Pass any javascript json object to `json` props and it will be rendered in a user-friendly table. |
| 4 | + |
| 5 | +```tsx |
| 6 | +import { JsonPrettyViewer } from 'react-json-friendly-viewer'; |
| 7 | + |
| 8 | +<JsonPrettyViewer |
| 9 | + json={{ |
| 10 | + name: 'react-json-friendly-viewer', |
| 11 | + private: false, |
| 12 | + changes: 1000, |
| 13 | + dependencies: { |
| 14 | + react: 'latest', |
| 15 | + }, |
| 16 | + createdAt: '1990-10-13T10:51:05.570Z', |
| 17 | + hobbies: ['reading', 'eating'], |
| 18 | + experiences: [ |
| 19 | + { |
| 20 | + job: 'janitor', |
| 21 | + projects: [ |
| 22 | + { |
| 23 | + venue: 'National Museum', |
| 24 | + }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + ], |
| 28 | + referrer: null, |
| 29 | + }} |
| 30 | +/>; |
| 31 | +``` |
| 32 | + |
| 33 | +## Array Example |
| 34 | + |
| 35 | +Array can be displayed as well. |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { JsonPrettyViewer } from 'react-json-friendly-viewer'; |
| 39 | + |
| 40 | +<JsonPrettyViewer |
| 41 | + json={[ |
| 42 | + { |
| 43 | + firstName: 'Malcolm', |
| 44 | + isTired: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + firstName: 'Steve', |
| 48 | + isTired: false, |
| 49 | + }, |
| 50 | + ]} |
| 51 | +/>; |
| 52 | +``` |
| 53 | + |
| 54 | +## Playground |
| 55 | + |
| 56 | +Use the panel at the bottom to tweak the props. |
| 57 | + |
| 58 | +```tsx |
| 59 | +import { usePropsEditor } from 'react-showroom/client'; |
| 60 | + |
| 61 | +const Demo = () => { |
| 62 | + const propsEditor = usePropsEditor(); |
| 63 | + |
| 64 | + return ( |
| 65 | + <JsonPrettyViewer |
| 66 | + {...propsEditor.props} |
| 67 | + json={{ |
| 68 | + name: 'react-json-friendly-viewer', |
| 69 | + private: false, |
| 70 | + changes: 1000, |
| 71 | + dependencies: { |
| 72 | + react: 'latest', |
| 73 | + }, |
| 74 | + createdAt: '1990-10-13T10:51:05.570Z', |
| 75 | + hobbies: ['reading', 'eating'], |
| 76 | + experiences: [ |
| 77 | + { |
| 78 | + job: 'janitor', |
| 79 | + projects: [ |
| 80 | + { |
| 81 | + venue: 'National Museum', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + ], |
| 86 | + referrer: null, |
| 87 | + }} |
| 88 | + /> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +<Demo />; |
| 93 | +``` |
| 94 | + |
| 95 | +## Custom Format |
| 96 | + |
| 97 | +Pass `formatter` props to change how values are displayed. |
| 98 | + |
| 99 | +```tsx |
| 100 | +import { format as formatDate } from 'date-fns'; |
| 101 | +import { |
| 102 | + JsonPrettyViewer, |
| 103 | + Formatter, |
| 104 | + prettifyLabel, |
| 105 | +} from 'react-json-friendly-viewer'; |
| 106 | + |
| 107 | +const datePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; |
| 108 | + |
| 109 | +const formatter: Partial<Formatter> = { |
| 110 | + string: (value: string) => { |
| 111 | + return datePattern.test(value) |
| 112 | + ? formatDate(new Date(value), 'd MMM yyyy, h:mm a') |
| 113 | + : value; |
| 114 | + }, |
| 115 | + arrayItem: (index, parentName) => |
| 116 | + prettifyLabel(`${parentName || 'Item'} ${index + 1}`), |
| 117 | + none: () => `(None)`, |
| 118 | +}; |
| 119 | + |
| 120 | +<JsonPrettyViewer |
| 121 | + formatter={formatter} |
| 122 | + json={{ |
| 123 | + name: 'react-json-friendly-viewer', |
| 124 | + private: false, |
| 125 | + changes: 1000, |
| 126 | + dependencies: { |
| 127 | + react: 'latest', |
| 128 | + }, |
| 129 | + createdAt: '1990-10-13T10:51:05.570Z', |
| 130 | + hobbies: ['reading', 'eating'], |
| 131 | + experiences: [ |
| 132 | + { |
| 133 | + job: 'janitor', |
| 134 | + projects: [ |
| 135 | + { |
| 136 | + venue: 'National Museum', |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + ], |
| 141 | + referrer: null, |
| 142 | + }} |
| 143 | +/>; |
| 144 | +``` |
0 commit comments