-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from johnwdunn20/john/react
Created a react component
- Loading branch information
Showing
4 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters