diff --git a/part2/2.12-2.14/src/App.js b/part2/2.12-2.14/src/App.js index 8d24f13..e94df42 100644 --- a/part2/2.12-2.14/src/App.js +++ b/part2/2.12-2.14/src/App.js @@ -7,10 +7,7 @@ const App = () => { const [newSearch, setNewSerach] = useState('') const [CountryList, setCountryList] = useState([]) - - const handleSearchChange =(event) => { - setNewSerach(event.target.value) - } + const [ResultList, setResultList] = useState([]) useEffect(() => { axios @@ -20,15 +17,28 @@ const App = () => { }) }, []) - - const filtered = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase())) - console.log(newSearch.toLowerCase()) + const clickHandler = (index) => { + console.log(index) + const newresult = ResultList + newresult[index].Show = !newresult[index].Show + setResultList([...newresult]) + } + + const handleSearchChange = (event) => { + setNewSerach(event.target.value) + } + + useEffect(() => { + const sfilter = CountryList.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase())) + const filter = sfilter.map(item => ({...item,Show:false})) + setResultList(filter) + }, [newSearch, CountryList]) + return (

Country

- - +
) } diff --git a/part2/2.12-2.14/src/components/Country.js b/part2/2.12-2.14/src/components/Country.js index 460c15b..2e16af0 100644 --- a/part2/2.12-2.14/src/components/Country.js +++ b/part2/2.12-2.14/src/components/Country.js @@ -1,26 +1,36 @@ import React from 'react'; -const Country = (props) => { - const filtered = props.data +const Country = ({data,onClick}) => { + const list = data.length + const clickHandler = (index) => { + onClick(index) + } return (
- {filtered.length > 10 ? - 'Too many matches, specific another filter' : filtered.length !== 1 ? - filtered.map(item => ): - filtered.map(item => )} + {list > 10 ? 'Too many matches, specific another filter' : + list > 1 ? data.map((item, index) => clickHandler(index)}/>) : + data.map(item => ) + }
) } -const CountryLongList = ({data}) => { +const CountryLongList = ({data, onClick}) => { + const show = data.Show + console.log(show) return ( -
-

{data.name}

-
- ) +
+

+ {data.name} + + {show === true && } +

+
+ ); } const CountryDetail = ({data}) => { - console.log(data) return (

{data.name}