admin管理员组文章数量:1356216
I am studying React and after implementing the filter functionality I got to think about how to highlight the matched word inside the searched string.
My App.js is:
import React, { Component } from 'react';
import ListValues from './ListValues';
class App extends Component {
state = {
list: ['First string 1', 'second String 2', 'third string 3', 'teste rwe'],
value: ''
}
render() {
return (
<div>
<input
value={this.state.value}
onChange={e => this.setState({ value: e.target.value })}
/>
<ListValues list={this.state.list} value={this.state.value}/>
</div>
);
}
}
export default App;
And my ListValues.js file is:
import React from 'react';
const ListValues = (props) => {
return (
<div>
<ul>
{props.list.filter(name => {
return name.toLowerCase().indexOf(props.value.toLowerCase()) >= 0;
}).map((value, i) => {
return <li key={i}>{value}</li>
})}
</ul>
</div>
);
}
export default ListValues;
The index.js file is the standard code:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './ponents/App';
ReactDOM.render(<App />, document.getElementById('root'));
I am studying React and after implementing the filter functionality I got to think about how to highlight the matched word inside the searched string.
My App.js is:
import React, { Component } from 'react';
import ListValues from './ListValues';
class App extends Component {
state = {
list: ['First string 1', 'second String 2', 'third string 3', 'teste rwe'],
value: ''
}
render() {
return (
<div>
<input
value={this.state.value}
onChange={e => this.setState({ value: e.target.value })}
/>
<ListValues list={this.state.list} value={this.state.value}/>
</div>
);
}
}
export default App;
And my ListValues.js file is:
import React from 'react';
const ListValues = (props) => {
return (
<div>
<ul>
{props.list.filter(name => {
return name.toLowerCase().indexOf(props.value.toLowerCase()) >= 0;
}).map((value, i) => {
return <li key={i}>{value}</li>
})}
</ul>
</div>
);
}
export default ListValues;
The index.js file is the standard code:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './ponents/App';
ReactDOM.render(<App />, document.getElementById('root'));
Share
Improve this question
edited Mar 19, 2019 at 19:58
Matheus Sant'ana
asked Mar 19, 2019 at 19:21
Matheus Sant'anaMatheus Sant'ana
5932 gold badges6 silver badges25 bronze badges
2 Answers
Reset to default 2Unless I'm missing the point here, you could use styled ponents to conditionally apply styling (background colour) based on the value of props that you pass into your ponent.
import styled from 'styled-ponents'
const ListValue = styled.li`
${({ active }) => active && `
background: blue;
`}
`;
const ListValues = (props) => {
return (
<div>
<ul>
{props.list.filter(name => {
return name.toLowerCase().indexOf(props.value.toLowerCase()) >= 0;
}).map((value, i) => {
return <ListValue active={true} key={i}>{value}</ListValue>
})}
</ul>
</div>
);
}
https://www.styled-ponents./docs
I found a ready-made ponent for the requirement. If you want customization, please go through the source code and tweak as per your needs.
react-highlight-words
本文标签: javascriptHighlight searched text in ReactStack Overflow
版权声明:本文标题:javascript - Highlight searched text in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744034359a2579489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论