Skip to content

Commit a0309fd

Browse files
committed
initial commit
0 parents  commit a0309fd

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.iml
3+
node_modules/
4+
.DS_Store

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**/.*
2+
example
3+
script
4+
specs
5+
bower.json
6+
karma.conf.js
7+
webpack.config.js
8+
.DS_Store

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# react-diff
2+
3+
NOTE: I stole this lib from [cezary/react-diff](https://github.com/cezary/react-diff)
4+
5+
Highlights differences between two strings, uses the [diff](https://www.npmjs.com/package/diff) module
6+
7+
## Installation
8+
9+
```
10+
npm install react-diff
11+
```
12+
13+
## Demo
14+
15+
http://cezary.github.io/react-diff/
16+
17+
## Example
18+
19+
```javascript
20+
import React from "react";
21+
import Diff from "react-diff";
22+
23+
const MyComponent = () => {
24+
return <Diff inputA="gogol" inputB="google" type="chars" />;
25+
};
26+
```
27+
28+
## License
29+
30+
MIT

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import PropTypes from "prop-types";
2+
import React from "react";
3+
import jsdiff from "diff";
4+
5+
const fnMap = {
6+
chars: jsdiff.diffChars,
7+
words: jsdiff.diffWords,
8+
sentences: jsdiff.diffSentences,
9+
json: jsdiff.diffJson
10+
};
11+
12+
export default function Diff({ inputA = "", inputB = "", type = "chars" }) {
13+
var diff = fnMap[type](inputA, inputB);
14+
var result = diff.map(function(part, index) {
15+
var spanStyle = {
16+
backgroundColor: part.added
17+
? "lightgreen"
18+
: part.removed
19+
? "salmon"
20+
: "lightgrey"
21+
};
22+
return (
23+
<span key={index} style={spanStyle}>
24+
{part.value}
25+
</span>
26+
);
27+
});
28+
return <pre className="diff-result">{result}</pre>;
29+
}
30+
31+
Diff.propTypes = {
32+
inputA: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
33+
inputB: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
34+
type: PropTypes.oneOf(["chars", "words", "sentences", "json"])
35+
};

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "react-diff-component",
3+
"version": "1.0.0",
4+
"description": "Highlight differences between inputs",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/actuallydan/react-diff-component.git"
9+
},
10+
"keywords": [
11+
"react",
12+
"diff",
13+
"component"
14+
],
15+
"author": "Dan Kral <dankral01@gmail.com>",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/actuallydan/react-diff-component/issues"
19+
},
20+
"homepage": "https://github.com/actuallydan/react-diff-component",
21+
"peerDependencies": {
22+
"react": ">=0.12.0"
23+
},
24+
"dependencies": {
25+
"diff": "^1.2.0",
26+
"prop-types": "^15.7.2"
27+
},
28+
"devDependencies": {
29+
"babel": "^5.1.10"
30+
}
31+
}

0 commit comments

Comments
 (0)