Skip to content
This repository was archived by the owner on Sep 15, 2022. It is now read-only.
This repository was archived by the owner on Sep 15, 2022. It is now read-only.

Вопрос по ДЗ #66

Description

@LyryKua

Привет! Смотрю твой курс по ReactJS. Очень доступно и интересно обьясняешь, спасибо что делаешь это!

Вопрос по этому ДЗ следующий. Как обнулить статусы isOpened в контактах на которые не было нажато? Мне интересно как получить список всех контактов? Я понимаю, что это будет где-то в 36 строке.

И маленький вопрос в 48 строке. Как бы ты изменила тернарник в 49 строке?

Спасибо за ответ!

const CONTACTS = [
    {
        id: 1,
        name: "Darth Vader",
        phoneNumber: "+250966666666",
        email: "darth@vader.com",
        img: "img/darth.gif",
    }, {
        id: 2,
        name: "Princess Leia",
        phoneNumber: "+250966344466",
        email: "princess@leia.com",
        img: "img/leia.gif"
    }, {
        id: 3,
        name: "Luke Skywalker",
        phoneNumber: "+250976654433",
        email: "luke@skywalker.com",
        img: "img/luke.gif"
    }, {
        id: 4,
        name: "Chewbacca",
        phoneNumber: "+250456784935",
        email: "chewbacca@starwars.com",
        img: "img/chewbacca.gif"
    }
];

const Contact = React.createClass({
    getInitialState() {
        return ({
            isOpened: false,
        });
    },
    handleOpen() {
        // Здесь нужно установить isOpened: false в тех контактах на которые не было нажато
        this.setState({
            isOpened: !this.state.isOpened,
        });
    },
    render() {
        return (
            <li className="contact" onClick={this.handleOpen}>
                <img className="contact-image" src={this.props.img} alt={this.props.name} width={60} height={60}/>
                <div className="contact-info">
                    <div className="contact-name">{this.props.name}</div>
                    <div className="contact-number">{this.props.phoneNumber}</div>
                    {/* Как зарефаторить код в следующей строке? Как бы ты написала этот тернарник? */}
                    {this.state.isOpened ? <div className="contact-number">{this.props.email}</div> : null}
                </div>
            </li>
        );
    }
});

const ContactList = React.createClass({
    getInitialState() {
        return ({
            displayedContact: CONTACTS,
        });
    },
    handleSearch(event) {
        const searchQuery = event.target.value.toLowerCase();
        const displayedContacts = CONTACTS.filter(item => {
            return item.name.toLowerCase().indexOf(searchQuery) !== -1;
        });
        this.setState({
            displayedContact: displayedContacts,
        });
    },
    render() {
        return (
            <div className="contacts">
                <input className="search-field" type="text" onChange={this.handleSearch}/>
                <ul className="contacts-list">
                    {
                        this.state.displayedContact.map(item => {
                            return (
                                <Contact
                                    key={item.id}
                                    name={item.name}
                                    phoneNumber={item.phoneNumber}
                                    email={item.email}
                                    img={item.img}
                                />
                            );
                        })
                    }
                </ul>
            </div>
        );
    }
});

ReactDOM.render(
    <ContactList/>,
    document.getElementById("root")
);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions