Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajmal112 committed May 31, 2023
1 parent 666cf75 commit d3cf837
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
76 changes: 76 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 {
width: 900px;
margin: 50px auto;
display: flex;
gap: 140px;
}

input.search , input.search2 {
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: 40px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
}

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

.suggestions li , .suggestions2 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) ,.suggestions2 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) , .suggestions2 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;
}

.hl {
background: #ffc600;
}
58 changes: 58 additions & 0 deletions assets/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
let cities = [];

fetch(endpoint)
.then(res => res.json())
.then(data => cities.push(...data))
.catch(err => console.error(err));

function findMatches(wordtoMatch, cities) {
return cities.filter(place => {
const regEx = new RegExp(wordtoMatch, 'gi');
return place.city.match(regEx) || place.state.match(regEx);
});
}

function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

function displayMatches() {
const match = findMatches(this.value, cities);
let html = match.map(place => {
const regex = new RegExp(this.value, 'gi');
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`);
return `
<li>
<span class="name">${cityName}</span>
<span class="population">${numberWithCommas(place.population)}</span>
</li>
`;
}).join("");
suggestions.innerHTML = html;
}

function displayMatches2() {
const match = findMatches(this.value, cities);
let html2 = match.map(place => {
const regex = new RegExp(this.value, 'gi');
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`);
return `
<li>
<span class="name">${stateName}</span>
<span class="population">${numberWithCommas(place.population)}</span>
</li>
`;
}).join("");
suggestions2.innerHTML = html2;
}

let searchbar = document.querySelector(".search");
let searchbar2 = document.querySelector(".search2");
let suggestions = document.querySelector(".suggestions");
let suggestions2 = document.querySelector(".suggestions2");

searchbar.addEventListener("change", displayMatches);
searchbar.addEventListener("keyup", displayMatches);
searchbar2.addEventListener("change", displayMatches2);
searchbar2.addEventListener("keyup", displayMatches2);
28 changes: 28 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Type Ahead 👀</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="https://fav.farm/🔥" />
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<form class="search-form">
<div class="divsearch1">
<input type="text" class="search" placeholder="City">
<ul class="suggestions">
<li>Filter for a city</li>
</ul>
</div>
<div class="divsearch2">
<input type="text" class="search2" placeholder="State">
<ul class="suggestions2">
<li>Filter for a State</li>
</ul>
</div>
</form>

<script src="assets/js/index.js"></script>
</body>
</html>

0 comments on commit d3cf837

Please sign in to comment.