Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sapinder committed Jan 9, 2021
0 parents commit 7a6a2ea
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Type Ahead 👀</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<form class="search-form">
<input type="text" class="search-bar" placeholder="Search a City or a State">
<ul class="suggestions">
</ul>
</form>
<script src="script.js"></script>
</body>
</html>
75 changes: 75 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';

let places, matchedPlaces;

document.addEventListener("DOMContentLoaded", async () => {
places = await fetchData();

const suggestions = document.querySelector(".suggestions");

// listen for change in input
document.querySelector(".search-bar")
.addEventListener("input",
e => handleChange(e.target, suggestions)
);
});


async function fetchData() {
try {
return await fetch(endpoint)
.then(res => res.json())
.then(obj => obj);
}
catch(err) {
console.error(err);
alert("An unknown error occurred! Please open the console for more info.");
}
}

function handleChange(searchBar, suggestions) {
if(searchBar.value) {
const matchedPlaces = findMatches(searchBar.value);

// create <li> nodes for matchedPlaces
const listItems = matchedPlaces.map(place =>
getListItemHTML(place, searchBar.value)
);

suggestions.innerHTML = listItems.join("");
return;
}
// if searchBar is empty
suggestions.innerHTML = "";
}


function findMatches(input) {
return (
places.filter(place => {
const regExp = new RegExp(input, "gi");

// find either city or state should match
return place.city.match(regExp) || place.state.match(regExp);
})
)}


function getListItemHTML(place, input) {
// for highlighting the substring that matches the input
const regExp = new RegExp(input, "gi");
const spanHighlight = `<span class="highlight">${input}</span>`;

// replace substring with highlighted one
const city = place.city.replace(regExp, spanHighlight);
const state = place.state.replace(regExp, spanHighlight);

return `<li>
<span>${city}, ${state}</span>
<span class="population">${formatPopulation(place.population)}</span>
</li>`;
}

function formatPopulation(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
73 changes: 73 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'helvetica neue';
font-size: 20px;
font-weight: 200;
}

*, *:before, *:after {
box-sizing: inherit;
}

input {
width: 100%;
padding: 20px;
}

.search-form {
max-width: 400px;
margin: 50px auto;
}

input {
margin: 0;
text-align: center;
outline: 0;
border: 10px solid #F7F7F7;
width: 120%;
left: -10%;
position: relative;
top: 10px;
z-index: 2;
border-radius: 5px;
font-size: 30px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
}

.suggestions {
margin: 0;
padding: 0;
position: relative;
/*perspective: 20px;*/
}

.suggestions li {
background: white;
list-style: none;
border-bottom: 1px solid #D8D8D8;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.14);
margin: 0;
padding: 20px;
transition: background 0.2s;
display: flex;
justify-content: space-between;
text-transform: capitalize;
}

.suggestions li:nth-child(even) {
transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001);
background: linear-gradient(to bottom, #ffffff 0%,#EFEFEF 100%);
}

.suggestions li:nth-child(odd) {
transform: perspective(100px) rotateX(-3deg) translateY(3px);
background: linear-gradient(to top, #ffffff 0%,#EFEFEF 100%);
}

span.population {
font-size: 15px;
}
.highlight {
background: #ffc600;
}

0 comments on commit 7a6a2ea

Please sign in to comment.