steeleye / recruitment-ext

13 stars 25 forks source link

Krishan Pareek _ Front End #266

Open krishanpareek809 opened 1 year ago

krishanpareek809 commented 1 year ago

Deployed Link: https://steeleye-react-assignment.web.app

GitHub Repository Link: https://github.com/krishanpareek809/Krishan-Pareek_Front-End

Q1. Explain what the simple List component does. Ans:

Q2. What problems / warnings are there with code? Ans:

Q3. Please fix, optimize, and/or modify the component as much as you think is necessary. Ans:

import React, { useState, useEffect, memo } from 'react';
import PropTypes from 'prop-types';

// Single List Item
const WrappedSingleListItem = ({
  index,
  isSelected,
  onClickHandler,
  text,
}) => {
  return (
    <li
      style={{ backgroundColor: isSelected ? 'green' : 'red'}}
      onClick={() => onClickHandler(index)}
    >
      {text}
    </li>
  );
};

WrappedSingleListItem.propTypes = {
  index: PropTypes.number,
  isSelected: PropTypes.bool,
  onClickHandler: PropTypes.func.isRequired,
  text: PropTypes.string.isRequired,
};

const SingleListItem = memo(WrappedSingleListItem);

// List Component
const WrappedListComponent = ({
  items,
}) => {
  const [selectedIndex,setSelectedIndex] = useState();

  useEffect(() => {
    setSelectedIndex(null);
  }, [items]);

  const handleClick = index => {
    setSelectedIndex(index);
  };

  return (
    <ul style={{ textAlign: 'left' }}>
      {items.map((item, index) => (
        <SingleListItem
          key={index}
          onClickHandler={() => handleClick(index)}
          text={item.text}
          index={index}
          isSelected={selectedIndex===index}
        />
      ))}
    </ul>
  )
};

WrappedListComponent.propTypes = {
  items: PropTypes.arrayOf(PropTypes.shape({
    text: PropTypes.string.isRequired,
  })),
};

WrappedListComponent.defaultProps = {
  items:[
    {
        text:"This is Steeleye Frontend Assignment"
    },
    {
        text:"The Deadline is April 23, 2023"
    },
    {
        text:"We should be punctual towards our work"
    }
  ],
};

const List = memo(WrappedListComponent);

export default List;

In the optimized version, the following changes have been made: