add lesson-52

parent 0c2d6e53
...@@ -3,9 +3,10 @@ import './List.scss' ...@@ -3,9 +3,10 @@ import './List.scss'
type TProps = { type TProps = {
list: string[] list: string[]
title: string title: string
onDelete?: (item: string) => void
} }
const List = ({list, title}: TProps) => { const List = ({list, title, onDelete}: TProps) => {
return ( return (
<div className='List'> <div className='List'>
<h2>{title}</h2> <h2>{title}</h2>
...@@ -13,7 +14,7 @@ const List = ({list, title}: TProps) => { ...@@ -13,7 +14,7 @@ const List = ({list, title}: TProps) => {
<div> <div>
{ {
list.map((item, index) => ( list.map((item, index) => (
<p key={index}> <p key={index} onClick={() => onDelete && onDelete(item)}>
{item} {item}
</p> </p>
)) ))
......
...@@ -30,7 +30,7 @@ function App() { ...@@ -30,7 +30,7 @@ function App() {
} }
const onClickHandler = () => { const onClickHandler = () => {
const copyGuestList = guestList const copyGuestList = [...guestList]
const randomList = copyGuestList.filter(item => { const randomList = copyGuestList.filter(item => {
const random = Math.floor(Math.random() * 100) const random = Math.floor(Math.random() * 100)
if(random > 50) return item if(random > 50) return item
...@@ -39,6 +39,13 @@ function App() { ...@@ -39,6 +39,13 @@ function App() {
setIncomeList(randomList) setIncomeList(randomList)
} }
const onDeleteHandler = (item: string) => {
const index = guestList.findIndex(str => str === item)
const copyGuestList = [...guestList]
copyGuestList.splice(index, 1)
setGuestList(copyGuestList)
}
return ( return (
<div className="App"> <div className="App">
<Modal show={isModal}/> <Modal show={isModal}/>
...@@ -46,6 +53,7 @@ function App() { ...@@ -46,6 +53,7 @@ function App() {
<List <List
title="Список основных гостей" title="Список основных гостей"
list={guestList.reverse()} list={guestList.reverse()}
onDelete={onDeleteHandler}
/> />
<List <List
title="Список приглашенных гостей" title="Список приглашенных гостей"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment