54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import { Component, Fragment } from "react";
|
|
import UsersContext from "../store/users-context";
|
|
import ErrorBoundary from "./ErrorBoundary";
|
|
import classes from "./UserFinder.module.css";
|
|
import Users from "./Users";
|
|
|
|
class UserFinder extends Component {
|
|
static contextType = UsersContext;
|
|
|
|
constructor() {
|
|
super();
|
|
this.state = {
|
|
filteredUsers: [],
|
|
searchTerm: "",
|
|
};
|
|
}
|
|
|
|
componentDidMount() {
|
|
this.setState({ filteredUsers: this.context.users });
|
|
}
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
if (prevState.searchTerm !== this.state.searchTerm) {
|
|
this.setState({
|
|
filteredUsers: this.context.users.filter((user) =>
|
|
user.name.includes(this.state.searchTerm)
|
|
),
|
|
});
|
|
}
|
|
}
|
|
|
|
searchChangeHandler(event) {
|
|
this.setState({ searchTerm: event.target.value });
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<Fragment>
|
|
<ErrorBoundary>
|
|
<div className={classes.finder}>
|
|
<input
|
|
type="search"
|
|
onChange={this.searchChangeHandler.bind(this)}
|
|
/>
|
|
</div>
|
|
<Users users={this.state.filteredUsers} />
|
|
</ErrorBoundary>
|
|
</Fragment>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default UserFinder;
|