Skip to content

Commit 83261bd

Browse files
committed
finished search box
1 parent 283391a commit 83261bd

File tree

5 files changed

+138
-170
lines changed

5 files changed

+138
-170
lines changed

06 - Type Ahead/index-FINISHED.html

Lines changed: 0 additions & 61 deletions
This file was deleted.

06 - Type Ahead/index-START.html

Lines changed: 0 additions & 22 deletions
This file was deleted.

06 - Type Ahead/index.html

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<link rel="stylesheet" type="text/css" href="myStyle.css">
6+
</head>
7+
<body>
8+
9+
<form class="search-form">
10+
<input type="filter" class="search-filter-box" placeholder="City or State">
11+
12+
<ul class="suggestions">
13+
<li>Filter for a city</li>
14+
<li>or a state</li>
15+
</ul>
16+
17+
</form>
18+
19+
<script>
20+
21+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
22+
23+
const cities = [];
24+
25+
// Returns a array with matched name or state
26+
function findMatches(wordToMatch, cities) {
27+
return cities.filter(place => {
28+
// here we need to figure out if the city or state matches what was searched
29+
const regex = new RegExp(wordToMatch, 'gi');
30+
return place.city.match(regex) || place.state.match(regex)
31+
});
32+
}
33+
34+
// Beautify floats
35+
function numberWithCommas(x) {
36+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
37+
}
38+
39+
function displayMatches() {
40+
displayMatchesWith(this.value);
41+
}
42+
43+
function displayMatchesWith(text) {
44+
45+
const matches = findMatches(text, cities);
46+
47+
// Generate new html based on the input to serve back to the DOM
48+
const html = matches.map(place=> {
49+
50+
const regex = new RegExp(text, 'gi');
51+
const cityName = place.city.replace(regex, `<span class="hl">${text}</span>`);
52+
const stateName = place.state.replace(regex, `<span class="hl">${text}</span>`);
53+
54+
return `
55+
<li>
56+
<span class="name">${cityName}, ${stateName}</span>
57+
<span class="population">${numberWithCommas(place.population)}</span>
58+
</li>
59+
`;
60+
61+
}).join('');
62+
63+
suggestions.innerHTML = html;
64+
65+
}
66+
67+
const searchInput = document.querySelector('.search-filter-box');
68+
const suggestions = document.querySelector('.suggestions');
69+
searchInput.addEventListener('change', displayMatches);
70+
searchInput.addEventListener('keyup', displayMatches);
71+
72+
fetch(endpoint)
73+
.then(blob => blob.json()) // convert raw data into json
74+
.then(data => {
75+
cities.push(...data);
76+
displayMatchesWith(''); // display everything as default
77+
});
78+
79+
</script>
80+
81+
</body>
82+
</html>

06 - Type Ahead/myStyle.css

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
html {
2+
font-family: 'helvetica neue';
3+
}
4+
5+
/* Style the filter box */
6+
input {
7+
box-sizing: border-box; /* So the width would also compute the border length */
8+
width: 100%;
9+
padding: 10px;
10+
margin: 0px;
11+
}
12+
13+
/* Style the text inside the filter box */
14+
input.search-filter-box {
15+
font-size: 30px;
16+
font-weight: lighter;
17+
text-align: center;
18+
color: grey;
19+
border: 10px outset #efe6e6;
20+
border-radius: 3px;
21+
}
22+
23+
/* Style the whole suggestions section */
24+
.suggestions {
25+
list-style: none;
26+
position: relative;
27+
width: 80%;
28+
left: 10%;
29+
padding: 0px;
30+
margin: 0px;
31+
}
32+
33+
/* Style each suggestion */
34+
.suggestions li {
35+
padding: 15px;
36+
box-shadow: 0px 1px 2px gray;
37+
38+
font-size: 14px;
39+
text-transform: uppercase;
40+
41+
display: flex;
42+
justify-content: space-between;
43+
}
44+
45+
span.population {
46+
font-size: 12px;
47+
}
48+
49+
.hl {
50+
background:#ffc600;
51+
}
52+
53+
.search-form {
54+
max-width: 400px;
55+
margin: 50px auto; /* center the search form */
56+
}

06 - Type Ahead/style.css

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)