|
| 1 | +import { format as formatDate } from 'date-fns'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { |
| 4 | + Formatter, |
| 5 | + JsonPrettyViewer, |
| 6 | + prettifyLabel, |
| 7 | +} from 'react-json-friendly-viewer'; |
| 8 | + |
| 9 | +const data = { |
| 10 | + name: 'react-json-friendly-viewer', |
| 11 | + version: 'unknown', |
| 12 | + description: |
| 13 | + 'This is a beautiful and user-friendly component to display your JSON data that provides sensible default that even your business users can understand it instantly', |
| 14 | + private: false, |
| 15 | + changes: 1000, |
| 16 | + dependencies: { |
| 17 | + react: 'latest', |
| 18 | + }, |
| 19 | + createdAt: '1990-10-13T10:51:05.570Z', |
| 20 | + keywords: ['react', 'json viewer'], |
| 21 | + experiences: [ |
| 22 | + { |
| 23 | + from: '2013', |
| 24 | + to: '2015', |
| 25 | + job: 'janitor', |
| 26 | + projects: [ |
| 27 | + { |
| 28 | + venue: 'National Museum', |
| 29 | + }, |
| 30 | + ], |
| 31 | + }, |
| 32 | + ], |
| 33 | + referrer: null, |
| 34 | +}; |
| 35 | + |
| 36 | +const datePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; |
| 37 | + |
| 38 | +const formatter: Partial<Formatter> = { |
| 39 | + string: (value: string) => { |
| 40 | + return datePattern.test(value) |
| 41 | + ? formatDate(new Date(value), 'd MMM yyyy, h:mm a') |
| 42 | + : value; |
| 43 | + }, |
| 44 | + arrayItem: (index, parentName) => |
| 45 | + prettifyLabel(`${parentName || 'Item'} ${index + 1}`), |
| 46 | + none: () => `(None)`, |
| 47 | +}; |
| 48 | + |
| 49 | +export const AllCustomizationExample = () => { |
| 50 | + const [fieldLabel, setFieldLabel] = React.useState('Props'); |
| 51 | + const [valueLabel, setValueLabel] = React.useState('Value'); |
| 52 | + const [customFormatter, toggleCustomFormatter] = React.useReducer( |
| 53 | + (o) => !o, |
| 54 | + true |
| 55 | + ); |
| 56 | + const [mergePrimitiveArray, toggleMergePrimitiveArray] = React.useReducer( |
| 57 | + (o) => !o, |
| 58 | + false |
| 59 | + ); |
| 60 | + const [customRender, toggleCustomRender] = React.useReducer((o) => !o, true); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div> |
| 64 | + <div style={{ display: 'flex', flexWrap: 'wrap', gap: 16, padding: 16 }}> |
| 65 | + <div> |
| 66 | + <label htmlFor="fieldLabel">fieldLabel</label> |
| 67 | + <input |
| 68 | + id="fieldLabel" |
| 69 | + value={fieldLabel} |
| 70 | + onChange={(ev) => setFieldLabel(ev.target.value)} |
| 71 | + /> |
| 72 | + </div> |
| 73 | + <div> |
| 74 | + <label htmlFor="valueLabel">valueLabel</label> |
| 75 | + <input |
| 76 | + id="valueLabel" |
| 77 | + value={valueLabel} |
| 78 | + onChange={(ev) => setValueLabel(ev.target.value)} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + <div> |
| 82 | + Custom formatter |
| 83 | + <button type="button" onClick={toggleCustomFormatter}> |
| 84 | + {customFormatter ? 'True' : 'False'} |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + <div> |
| 88 | + mergePrimitiveArray |
| 89 | + <button type="button" onClick={toggleMergePrimitiveArray}> |
| 90 | + {mergePrimitiveArray ? 'True' : 'False'} |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + <div> |
| 94 | + Custom renderValue |
| 95 | + <button type="button" onClick={toggleCustomRender}> |
| 96 | + {customRender ? 'True' : 'False'} |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + <JsonPrettyViewer |
| 101 | + json={data} |
| 102 | + formatter={customFormatter ? formatter : undefined} |
| 103 | + fieldLabel={fieldLabel} |
| 104 | + valueLabel={valueLabel} |
| 105 | + mergePrimitiveArray={mergePrimitiveArray} |
| 106 | + renderValue={ |
| 107 | + customRender |
| 108 | + ? (value) => ( |
| 109 | + <span |
| 110 | + style={{ |
| 111 | + display: 'block', |
| 112 | + overflow: 'hidden', |
| 113 | + whiteSpace: 'nowrap', |
| 114 | + textOverflow: 'ellipsis', |
| 115 | + width: '100%', |
| 116 | + }} |
| 117 | + > |
| 118 | + {value} |
| 119 | + </span> |
| 120 | + ) |
| 121 | + : undefined |
| 122 | + } |
| 123 | + /> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +}; |
0 commit comments