I am trying to make a ReactJS application that will showcase a table - I want to have a toggle switch to show/highlight the differences -- in table rows. I've tried adding ref's to the table rows to try and use them as inputs.
current sandbox https://codesandbox.io/s/hopeful-haibt-q489l?file=/src/Home.js
Some demo with JavaScript http://jsfiddle.net/8c4nt2e1/1/
import React, { Component } from "react";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { Diff } from "diff";
import ReactDOM from "react-dom";
class Home extends Component {
componentDidMount() {
console.log("show differences");
}
onShowDiff() {
var name = ReactDOM.findDOMNode(this.refs.row1_0); //.value;
console.log("name", name);
/*
const diff = Diff.diffChars(
document.getElementById('row1_0').value,
document.getElementById('row1_1').value,
document.getElementById('row1_2').value,
document.getElementById('row1_3').value
);*/
//
/*
const diff = Diff.diffChars(document.getElementById('input-1').value, document.getElementById('input-2').value);
const output = diff.reduce((result, change) => {
if (change.added) return result + '<span data-diff="added">' + change.value + '</span>';
if (change.removed) return result + '<span data-diff="removed">' + change.value + '</span>'
return result + change.value;
}, '');
*/
//document.getElementById('output').innerHTML = output;
//console.log(output);
}
onHideDiff() {
//const output = document.getElementById('output').innerHTML;
//document.getElementById('output').innerHTML = output.replace(/<span data-diff=["'](?:added|removed)["']>(.+)<\/span>/gi, '$1');
}
render() {
return (
<div className="Page">
<button id="show-diff" onClick={this.onShowDiff()}>
Show diff
</button>
<button id="hide-diff" onClick={this.onHideDiff()}>
Hide diff
</button>
<div className="table-spec">
<table>
<tr>
<th>spec1</th>
<th>spec2</th>
<th>spec3</th>
<th>spec4</th>
</tr>
<tr>
<td ref="row1_0">480GB Adata SU630 SATA SSD</td>
<td ref="row1_1">512GB Adata XPG SX8200 Pro NVMe SSD</td>
<td ref="row1_2">512GB Adata XPG SX8200 Pro NVMe SSD</td>
<td ref="row1_3">2TB NVMe SSD</td>
</tr>
<tr>
<td ref="row2_0">GeForce RTX 3080 10GB</td>
<td ref="row2_1">GeForce GTX 1660 SUPER 6GB</td>
<td ref="row2_2">Radeon RX 5500 XT 8GB</td>
<td ref="row2_3">GeForce RTX 3090 24GB</td>
</tr>
<tr>
<td ref="row3_0">Intel Core i5 9400F 2.9GHz</td>
<td ref="row3_1">AMD Athlon 3000G 3.5GHz</td>
<td ref="row3_2">AMD Ryzen 7 3700X 3.6GHz</td>
<td ref="row3_3">Intel Core i9 10900K 3.7GHz</td>
</tr>
</table>
</div>
</div>
);
}
}
function mapStateToProps(state) {
return {};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({}, dispatch);
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Home));
idea comes from here https://www.gsmarena.com/compare.php3?idPhone1=9847&idPhone2=9536&idPhone3=9256#diff-*,*,*
