Skip to content

Commit 3e94391

Browse files
committed
fixed default styling and added props for customization
1 parent 953910a commit 3e94391

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# react-diff
22

3-
NOTE: I stole this lib from [cezary/react-diff](https://github.com/cezary/react-diff)
3+
This is a React Native implementation of [react-diff-simple-component](https://github.com/actuallydan/react-diff-simple-component) which was inspired by and updated from [react-diff](https://github.com/cezary/react-diff).
44

55
Highlights differences between two strings, uses the [diff](https://www.npmjs.com/package/diff) module
66

77
## Installation
88

99
```
10-
npm install react-diff
10+
npm install react-native-diff-component
1111
```
1212

1313
## Demo
@@ -18,7 +18,7 @@ http://cezary.github.io/react-diff/
1818

1919
```javascript
2020
import React from "react";
21-
import Diff from "react-diff";
21+
import Diff from "react-native-diff-component";
2222

2323
const MyComponent = () => {
2424
return <Diff inputA="gogol" inputB="google" type="chars" />;

index.js

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,72 @@
11
import PropTypes from "prop-types";
22
import React from "react";
3-
import jsdiff from "diff";
3+
import { View, StyleSheet, Text } from "react-native";
4+
import { diffChars, diffWords, diffSentences } from "diff";
45

56
const fnMap = {
6-
chars: jsdiff.diffChars,
7-
words: jsdiff.diffWords,
8-
sentences: jsdiff.diffSentences,
9-
json: jsdiff.diffJson
7+
chars: diffChars,
8+
words: diffWords,
9+
sentences: diffSentences
1010
};
1111

12-
export default function Diff({ inputA = "", inputB = "", type = "chars" }) {
12+
export default function Diff({
13+
inputA = "",
14+
inputB = "",
15+
type = "chars",
16+
textStyle = {},
17+
addedText = {},
18+
removedText = {},
19+
unchangedText = {},
20+
containerStyle = {}
21+
}) {
22+
let val = 1;
1323
var diff = fnMap[type](inputA, inputB);
24+
console.log(diff);
1425
var result = diff.map(function(part, index) {
15-
var spanStyle = {
16-
backgroundColor: part.added
17-
? "lightgreen"
18-
: part.removed
19-
? "salmon"
20-
: "lightgrey"
21-
};
26+
var spanStyle = part.added
27+
? { ...styles.addedText, ...addedText }
28+
: part.removed
29+
? { ...styles.removedText, ...removedText }
30+
: { ...styles.defaultText, ...unchangedText };
31+
2232
return (
23-
<span key={index} style={spanStyle}>
33+
<Text key={index} style={[styles.defaultText, spanStyle, textStyle]}>
2434
{part.value}
25-
</span>
35+
</Text>
2636
);
2737
});
28-
return <pre className="diff-result">{result}</pre>;
38+
// console.log(result);
39+
return (
40+
<View style={[styles.defaultContainerStyle, containerStyle]}>{result}</View>
41+
);
2942
}
3043

3144
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"])
45+
inputA: PropTypes.oneOfType([PropTypes.string]),
46+
inputB: PropTypes.oneOfType([PropTypes.string]),
47+
type: PropTypes.oneOf(["chars", "words", "sentences"]),
48+
textStyle: PropTypes.object,
49+
containerStyle: PropTypes.object,
50+
addedText: PropTypes.object,
51+
removedText: PropTypes.object,
52+
unchangedText: PropTypes.object
3553
};
54+
55+
var styles = StyleSheet.create({
56+
defaultText: {},
57+
addedText: {
58+
backgroundColor: "lightgreen",
59+
color: "#000000"
60+
},
61+
removedText: {
62+
backgroundColor: "salmon",
63+
textDecorationLine: "line-through",
64+
textDecorationStyle: "solid",
65+
color: "#000000"
66+
},
67+
defaultContainerStyle: {
68+
flexDirection: "row",
69+
flexWrap: "wrap",
70+
alignItems: "flex-end"
71+
}
72+
});

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-diff-component",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "Highlight differences between inputs",
55
"main": "index.js",
66
"scripts": {
@@ -22,10 +22,11 @@
2222
},
2323
"homepage": "https://github.com/actuallydan/react-native-diff-component",
2424
"peerDependencies": {
25-
"react": ">=0.12.0"
25+
"react": ">=0.12.0",
26+
"react-native": "*"
2627
},
2728
"dependencies": {
28-
"diff": "^1.2.0",
29+
"diff": "^4.0.1",
2930
"prop-types": "^15.7.2"
3031
},
3132
"devDependencies": {

0 commit comments

Comments
 (0)