classes useerfilter

This commit is contained in:
Tyrel Souza 2022-09-26 15:56:35 -04:00
parent 299ffe4d44
commit c664806c7b
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
6 changed files with 83 additions and 12 deletions

View File

@ -1,11 +1,22 @@
import Users from './components/Users';
import UserFinder from './components/UserFinder';
import UsersContext from './store/users-context';
const DUMMY_USERS = [
{ id: 'u1', name: 'Max' },
{ id: 'u2', name: 'Manuel' },
{ id: 'u3', name: 'Julie' },
];
function App() {
const usersContext = {
users: DUMMY_USERS
}
return (
<div>
<Users />
</div>
<UsersContext.Provider value={usersContext}>
<UserFinder />
</UsersContext.Provider>
);
}
export default App;
export default App;

View File

@ -2,6 +2,9 @@ import React from "react";
import classes from "./User.module.css";
class User extends React.Component {
componentWillUnmount(){
console.log("User will unmount")
}
render() {
return <li className={classes.user}>{this.props.name}</li>;
}

View File

@ -0,0 +1,47 @@
import { Component, Fragment } from "react";
import UsersContext from "../store/users-context";
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>
<div className={classes.finder}>
<input type="search" onChange={this.searchChangeHandler.bind(this)} />
</div>
<Users users={this.state.filteredUsers} />
</Fragment>
);
}
}
export default UserFinder;

View File

@ -0,0 +1,8 @@
.finder {
margin: 2rem auto;
background-color: white;
padding: 1rem;
border-radius: 12px;
max-width: 40rem;
text-align: center;
}

View File

@ -1,13 +1,8 @@
import React, { useState } from "react";
import React from "react";
import User from "./User";
import classes from "./Users.module.css";
const DUMMY_USERS = [
{ id: "u1", name: "Max" },
{ id: "u2", name: "Manuel" },
{ id: "u3", name: "Julie" },
];
class Users extends React.Component {
constructor() {
@ -29,7 +24,7 @@ class Users extends React.Component {
render() {
const usersList = (
<ul>
{DUMMY_USERS.map((user) => (
{this.props.users.map((user) => (
<User key={user.id} name={user.name} />
))}
</ul>

View File

@ -0,0 +1,7 @@
import React from 'react';
const UsersContext = React.createContext({
users: []
});
export default UsersContext;