Skip to content

Commit

Permalink
Merge pull request #1 from johnwdunn20/john/react
Browse files Browse the repository at this point in the history
Created a react component
  • Loading branch information
johnwdunn20 authored Jan 3, 2024
2 parents 49af257 + 6cd85fb commit 0d8c7aa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Empty file modified packages/jsondiffpatch/bin/jsondiffpatch.js
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions packages/jsondiffpatch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@
},
"devDependencies": {
"@types/jest": "^29.5.10",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"jest": "^29.7.0",
"ncp": "^2.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-jest": "^29.1.1",
"tslib": "^2.6.2",
"typescript": "~5.3.2"
Expand Down
32 changes: 32 additions & 0 deletions packages/jsondiffpatch/src/formatters/react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState, useEffect } from 'react';
import { diff } from '../index.js';
import HTMLFormatter from './html.js';
import type { Delta } from '../types.js';
import './styles/html.css';

type ReactFormatterType = {
oldJson: unknown;
newJson?: unknown;
initallyShowDiff? : boolean;
}
// write unit tests for this component

const ReactFormatterComponent: React.FC<ReactFormatterType> = ({ oldJson, newJson, initallyShowDiff }) => {

const [showDiffOnly, setShowDiffOnly] = useState(initallyShowDiff)

const delta: Delta = diff(oldJson, newJson);
if (!delta) {
return <></>;
}
const htmlDiff = new HTMLFormatter().format(delta, oldJson);
const createMarkupHtml = () => ({ __html: htmlDiff } || '') as { __html: TrustedHTML };

return (
<div className={`json-diff-container ${showDiffOnly ? 'jsondiffpatch-unchanged-hidden' : ''}`}>
<div dangerouslySetInnerHTML={createMarkupHtml()}></div>
</div>
);
};

export default ReactFormatterComponent;
3 changes: 2 additions & 1 deletion packages/jsondiffpatch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": false
"skipLibCheck": false,
"jsx": "react",
},
"include": ["src"]
}

0 comments on commit 0d8c7aa

Please sign in to comment.