As user types in input field, I want the matching letter in the text to be highlighted in red. So texts displayed is "Hello" and user types in "h" then I would like text to be updated to <span style="color:red">H</span>ello
. I'm trying it with vanilla js way, which is createElement span then add style to it then append a letter, but no luck. What is the react way of accomplishing this? https://codesandbox.io/s/msft-machine-coding-react-ts-to9szx?file=/src/App.tsx:0-1613
App.tsx
import React, { useState, useEffect } from "react";
import "./styles.css";
import { posts } from "./data";
let slicedOne = posts.slice(0, 5);
let slicedTwo = posts.slice(5, 10);
let slicedThree = posts.slice(10);
export default function App() {
const [feeds, setFeeds] = useState([]);
const [currentView, setCurrentView] = useState(0);
const [slicedFeeds, setSlicedFeeds] = useState([
slicedOne,
slicedTwo,
slicedThree
]);
const [texts, setTexts] = useState(
slicedFeeds[currentView].map((feed) => feed.fullText)
);
let arr = [];
const handleOnChange = (e: React.ChangeEvent<HTMLInputElement>) => {
let copiedTexts = texts;
for (let text of copiedTexts) {
for (let i = 0; i < text.length; i++) {
console.log(text[i]);
if (text[i].toLowerCase() === e.target.value) {
console.log("matched");
let span = document.createElement("span"); // this is not working...
span.style.color = "red";
span.append(text[i]);
}
}
}
};
return (
<div>
<div>
<input type="text" onChange={handleOnChange} />
</div>
{slicedFeeds[currentView].map((feed, idx) => {
return (
<div key={feed.dateTimeCreated}>
{feed.from.name} {feed.dateTimeLastModified}
<div>{feed.fullText}</div>
<br></br>
</div>
);
})}
<div>
<button onClick={() => setCurrentView(0)}>1</button>
<button onClick={() => setCurrentView(1)}>2</button>
<button onClick={() => setCurrentView(2)}>3</button>
</div>
</div>
);
}
data.tsx
export const posts = [
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Garima",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hours.Hello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hoursHello this is just some random post created to show an example mock post for this coding challenge. this needs to be completed in two hours",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Reply",
from: {
name: "Viniket",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Reply",
from: {
name: "Bhushan",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Tanvi",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Rahul",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Deeksha",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Swati",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Ketki",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Preksha",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Sonali",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Gagan",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Pratiksha",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Reply",
from: {
name: "Bhagyashree",
email: "garima@gmail.com"
}
},
{
fullText:
"Hello this is just some random post created to show an example mock post for this coding challenge",
dateTimeCreated: "2020-07-20T18:44:11Z",
dateTimeLastModified: "2020-07-20T18:44:11Z",
type: "Post",
from: {
name: "Milind",
email: "garima@gmail.com"
}
}
];