Skip to content

Commit c645f4e

Browse files
author
Elvin Dzhavadov
committed
use a custom component to render the root node
1 parent 7a130d1 commit c645f4e

File tree

5 files changed

+99
-20
lines changed

5 files changed

+99
-20
lines changed

README.md

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

3-
Highlight the difference between two string in a React component
3+
Highlight the difference between two strings in a React component
44

55
[![NPM](https://img.shields.io/npm/v/react-string-diff.svg)](https://www.npmjs.com/package/react-string-diff)
66
[![MIN](https://badgen.net/bundlephobia/min/react-string-diff)](https://bundlephobia.com/result?p=react-string-diff)
@@ -16,13 +16,14 @@ npm i react-string-diff
1616
```
1717
## Props
1818

19-
| Prop | Type | Default | Description |
20-
| ------------------------- | --------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------|
19+
| Prop | Type | Default | Description |
20+
| ------------------------- | --------------- | ------------------------------ | -----------------------------------------------------------------------------------------------------------------------------------------|
2121
| oldValue | `string*` | | Old value as string. |
2222
| newValue | `string*` | | New value as string. |
23-
| method | `DiffMethod` | `DiffMethod.Chars` | JsDiff text diff method used for diffing strings. Check out [supported methods](#methods). |
24-
| styles | `DiffStyles` | `defaultStyles` | Switch between `unified` and `split` view. |
25-
| className | `string` | | [Overriding styles](#overriding-styles) |
23+
| component | `elementType` | `div` | The component used for the root node. Either a string to use a HTML element or a component |
24+
| className | `string` | | Css class for a strings div wrapper |
25+
| method | `DiffMethod` | `DiffMethod.Chars` | JsDiff text diff method used for diffing strings. Check out [supported methods](#methods). |
26+
| styles | `DiffStyles` | `defaultStyles` | [Overriding styles](#overriding-styles). |
2627

2728

2829
## Usage

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"module": "dist/react-string-diff.esm.js",
3434
"devDependencies": {
35+
"@testing-library/react": "^10.4.7",
3536
"@types/diff": "^4.0.2",
3637
"@types/react": "^16.9.43",
3738
"@types/react-dom": "^16.9.8",

src/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export enum DiffMethod {
88
Lines = 'diffLines',
99
TrimmedLines = 'diffTrimmedLines',
1010
Sentences = 'diffSentences',
11-
CSS = 'diffCss'
11+
CSS = 'diffCss',
1212
}
1313

1414
type DiffStyles = {
@@ -19,12 +19,12 @@ type DiffStyles = {
1919

2020
const defaultStyle: DiffStyles = {
2121
added: {
22-
backgroundColor: 'lightgreen'
22+
backgroundColor: 'lightgreen',
2323
},
2424
removed: {
25-
backgroundColor: 'salmon'
25+
backgroundColor: 'salmon',
2626
},
27-
default: {}
27+
default: {},
2828
};
2929

3030
export type StringDiffProps = {
@@ -33,14 +33,16 @@ export type StringDiffProps = {
3333
method?: DiffMethod;
3434
styles?: DiffStyles;
3535
className?: string;
36+
component?: React.ElementType;
3637
};
3738

3839
export const StringDiff: React.FC<StringDiffProps> = ({
3940
oldValue,
4041
newValue,
4142
method = DiffMethod.Chars,
4243
styles = defaultStyle,
43-
className
44+
className,
45+
component: Component = 'div',
4446
}) => {
4547
const result = useMemo(() => {
4648
const diff = Diff[method](oldValue, newValue);
@@ -62,5 +64,5 @@ export const StringDiff: React.FC<StringDiffProps> = ({
6264
);
6365
});
6466
}, [oldValue, newValue, method]);
65-
return <div className={className}>{result}</div>;
67+
return <Component className={className}>{result}</Component>;
6668
};

test/string-diff.test.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import * as React from 'react';
2-
import * as ReactDOM from 'react-dom';
2+
import { render, screen } from '@testing-library/react';
33
import { StringDiff } from '../src';
44

55
describe('it', () => {
6-
it('renders without crashing', () => {
7-
const div = document.createElement('div');
8-
ReactDOM.render(<StringDiff newValue="testNew" oldValue="test" />, div);
9-
ReactDOM.unmountComponentAtNode(div);
6+
it('should display added', () => {
7+
render(<StringDiff newValue="testNew" oldValue="test" />);
8+
const diffNode = screen.getByText(/New/i);
9+
expect(diffNode.style.backgroundColor).toEqual('lightgreen');
10+
});
11+
it('should display added', () => {
12+
render(<StringDiff newValue="test" oldValue="testOld" />);
13+
const diffNode = screen.getByText(/Old/i);
14+
expect(diffNode.style.backgroundColor).toEqual('salmon');
15+
});
16+
it('should change parent dom element', () => {
17+
const { container } = render(
18+
<StringDiff component="p" newValue="test" oldValue="testOld" />
19+
);
20+
expect(container.querySelector('p')?.textContent).toEqual('testOld');
1021
});
1122
});

yarn.lock

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@
773773
core-js-pure "^3.0.0"
774774
regenerator-runtime "^0.13.4"
775775

776-
"@babel/runtime@^7.10.2", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4":
776+
"@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4":
777777
version "7.10.5"
778778
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.10.5.tgz#303d8bd440ecd5a491eae6117fd3367698674c5c"
779779
integrity sha512-otddXKhdNn7d0ptoFRHtMLa8LqDxLYwTjB4nYgM1yy5N6gU/MUf8zqyyLltCH3yAVitBzmwK4us+DD0l/MauAg==
@@ -969,6 +969,16 @@
969969
"@types/istanbul-reports" "^1.1.1"
970970
"@types/yargs" "^13.0.0"
971971

972+
"@jest/types@^25.5.0":
973+
version "25.5.0"
974+
resolved "https://registry.yarnpkg.com/@jest/types/-/types-25.5.0.tgz#4d6a4793f7b9599fc3680877b856a97dbccf2a9d"
975+
integrity sha512-OXD0RgQ86Tu3MazKo8bnrkDRaDXXMGUqd+kTtLtK1Zb7CRzQcaSRPPPV37SvYTdevXEBVxe0HXylEjs8ibkmCw==
976+
dependencies:
977+
"@types/istanbul-lib-coverage" "^2.0.0"
978+
"@types/istanbul-reports" "^1.1.1"
979+
"@types/yargs" "^15.0.0"
980+
chalk "^3.0.0"
981+
972982
"@rollup/plugin-commonjs@^11.0.0":
973983
version "11.1.0"
974984
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz#60636c7a722f54b41e419e1709df05c7234557ef"
@@ -1017,6 +1027,30 @@
10171027
estree-walker "^1.0.1"
10181028
picomatch "^2.2.2"
10191029

1030+
"@testing-library/dom@^7.17.1":
1031+
version "7.21.5"
1032+
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-7.21.5.tgz#d87312efc5039313f9ea246ed722d808f2ffcbb3"
1033+
integrity sha512-zZqC5T/9Upjs0/3hyrYNpGxw75dr/bLLD27pUdb3WWJ50JHwutvnQ1FJNHbVth9f2hLzEnh7hBdZ9pD++8pJ8g==
1034+
dependencies:
1035+
"@babel/runtime" "^7.10.3"
1036+
"@types/aria-query" "^4.2.0"
1037+
aria-query "^4.2.2"
1038+
dom-accessibility-api "^0.4.6"
1039+
pretty-format "^25.5.0"
1040+
1041+
"@testing-library/react@^10.4.7":
1042+
version "10.4.7"
1043+
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-10.4.7.tgz#fc14847fb70a5e93576b8f7f0d1490ead02a9061"
1044+
integrity sha512-hUYbum3X2f1ZKusKfPaooKNYqE/GtPiQ+D2HJaJ4pkxeNJQFVUEvAvEh9+3QuLdBeTWkDMNY5NSijc5+pGdM4Q==
1045+
dependencies:
1046+
"@babel/runtime" "^7.10.3"
1047+
"@testing-library/dom" "^7.17.1"
1048+
1049+
"@types/aria-query@^4.2.0":
1050+
version "4.2.0"
1051+
resolved "https://registry.yarnpkg.com/@types/aria-query/-/aria-query-4.2.0.tgz#14264692a9d6e2fa4db3df5e56e94b5e25647ac0"
1052+
integrity sha512-iIgQNzCm0v7QMhhe4Jjn9uRh+I6GoPmt03CbEtwx3ao8/EfoQcmgtqH4vQ5Db/lxiIGaWDv6nwvunuh0RyX0+A==
1053+
10201054
"@types/babel__core@^7.1.0":
10211055
version "7.1.9"
10221056
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.9.tgz#77e59d438522a6fb898fa43dc3455c6e72f3963d"
@@ -1166,6 +1200,13 @@
11661200
dependencies:
11671201
"@types/yargs-parser" "*"
11681202

1203+
"@types/yargs@^15.0.0":
1204+
version "15.0.5"
1205+
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-15.0.5.tgz#947e9a6561483bdee9adffc983e91a6902af8b79"
1206+
integrity sha512-Dk/IDOPtOgubt/IaevIUbTgV7doaKkoorvOyYM2CMwuDyP89bekI7H4xLIwunNYiK9jhCkmc6pUrJk3cj2AB9w==
1207+
dependencies:
1208+
"@types/yargs-parser" "*"
1209+
11691210
"@typescript-eslint/eslint-plugin@3.6.0":
11701211
version "3.6.0"
11711212
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-3.6.0.tgz#ba2b6cae478b8fca3f2e58ff1313e4198eea2d8a"
@@ -1371,7 +1412,7 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
13711412
dependencies:
13721413
color-convert "^1.9.0"
13731414

1374-
ansi-styles@^4.1.0:
1415+
ansi-styles@^4.0.0, ansi-styles@^4.1.0:
13751416
version "4.2.1"
13761417
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.1.tgz#90ae75c424d008d2624c5bf29ead3177ebfcf359"
13771418
integrity sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==
@@ -1842,6 +1883,14 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.2:
18421883
escape-string-regexp "^1.0.5"
18431884
supports-color "^5.3.0"
18441885

1886+
chalk@^3.0.0:
1887+
version "3.0.0"
1888+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
1889+
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
1890+
dependencies:
1891+
ansi-styles "^4.1.0"
1892+
supports-color "^7.1.0"
1893+
18451894
chalk@^4.0.0, chalk@^4.1.0:
18461895
version "4.1.0"
18471896
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
@@ -2203,6 +2252,11 @@ doctrine@^3.0.0:
22032252
dependencies:
22042253
esutils "^2.0.2"
22052254

2255+
dom-accessibility-api@^0.4.6:
2256+
version "0.4.6"
2257+
resolved "https://registry.yarnpkg.com/dom-accessibility-api/-/dom-accessibility-api-0.4.6.tgz#f3f2af68aee01b1c862f37918d41841bb1aaf92a"
2258+
integrity sha512-qxFVFR/ymtfamEQT/AsYLe048sitxFCoCHiM+vuOdR3fE94i3so2SCFJxyz/RxV69PZ+9FgToYWOd7eqJqcbYw==
2259+
22062260
domexception@^1.0.1:
22072261
version "1.0.1"
22082262
resolved "https://registry.yarnpkg.com/domexception/-/domexception-1.0.1.tgz#937442644ca6a31261ef36e3ec677fe805582c90"
@@ -4857,6 +4911,16 @@ pretty-format@^24.9.0:
48574911
ansi-styles "^3.2.0"
48584912
react-is "^16.8.4"
48594913

4914+
pretty-format@^25.5.0:
4915+
version "25.5.0"
4916+
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-25.5.0.tgz#7873c1d774f682c34b8d48b6743a2bf2ac55791a"
4917+
integrity sha512-kbo/kq2LQ/A/is0PQwsEHM7Ca6//bGPPvU6UnsdDRSKTWxT/ru/xb88v4BJf6a69H+uTytOEsTusT9ksd/1iWQ==
4918+
dependencies:
4919+
"@jest/types" "^25.5.0"
4920+
ansi-regex "^5.0.0"
4921+
ansi-styles "^4.0.0"
4922+
react-is "^16.12.0"
4923+
48604924
progress-estimator@^0.2.2:
48614925
version "0.2.2"
48624926
resolved "https://registry.yarnpkg.com/progress-estimator/-/progress-estimator-0.2.2.tgz#1c3947a5782ea56e40c8fccc290ac7ceeb1b91cb"
@@ -4922,7 +4986,7 @@ react-dom@^16.13.1:
49224986
prop-types "^15.6.2"
49234987
scheduler "^0.19.1"
49244988

4925-
react-is@^16.8.1, react-is@^16.8.4:
4989+
react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4:
49264990
version "16.13.1"
49274991
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
49284992
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

0 commit comments

Comments
 (0)