JSFiddle - React, Tailwind, and code Playground
HTML
<div class="filter">
<div class="filter__color filter__field">
<div class="filter__field__head">
Size
</div>
<div class="filter__radio">
<input type="radio" id="size-s" class="filter__input" name="size" value="S">
<label class="filter__label" for="size-s">S</label>
</div>
<div class="filter__radio">
<input type="radio" id="size-m" class="filter__input" name="size" value="M">
<label class="filter__label" for="size-m">M</label>
</div>
<div class="filter__radio">
<input type="radio" id="size-l" class="filter__input" name="size" value="L">
<label class="filter__label" for="size-l">L</label>
</div>
</div>
<div class="filter__color filter__field">
<div class="filter__field__head">
Color
</div>
<div class="filter__el">
<input type="checkbox" id="color-1" class="filter__input" name="color" value="1">
<label class="filter__label" for="color-1">Color 1</label>
</div>
<div class="filter__el">
<input type="checkbox" id="color-2" class="filter__input" name="color" value="2">
<label class="filter__label" for="color-2">Color 1</label>
</div>
<div class="filter__el">
<input type="checkbox" id="color-3" class="filter__input" name="color" value="3">
<label class="filter__label" for="color-3">Color 1</label>
</div>
<div class="filter__el">
<input type="checkbox" id="color-4" class="filter__input" name="color" value="4">
<label class="filter__label" for="color-4">Color 1</label>
</div>
<div class="filter__el">
<input type="checkbox" id="color-5" class="filter__input"...
CSS
.filter {
background-color: #fff;
max-width: 350px;
padding: 15px;
margin: 0 auto;
margin-top: 50px;
-webkit-box-shadow: 0 24px 59px 0 rgba(51,59,69,.15);
box-shadow: 0 24px 59px 0 rgba(51,59,69,.15);
border-radius: 5px;
font-family: sans-serif;
}
.filter__field {
margin-bottom: 25px;
}
.filter__el {
margin-bottom: 10px;
}
.filter__radio {
display: inline-block;
margin-right: 10px;
}
.filter__el:last-child {
margin-bottom: 0;
}
.filter__field__head {
font-size: 16px;
margin-bottom: 10px;
}
.checkbox {
position: relative;
}
.filter__input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.filter__label {
position: relative;
padding-left: 25px;
cursor: pointer;
display: inline-block;
height: 25px;
line-height: 25px;
font-size: 1rem;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
color: #434d68;
}
[type=checkbox]+.filter__label::before,
[type=checkbox]:not(.filled-in)+.filter__label::after,
[type=radio]+.filter__label::before,
[type=radio]:not(.filled-in)+.filter__label::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
z-index: 0;
border: 2px solid #434d68;
border-radius: 50%;
margin-top: 2px;
-webkit-transition: .2s;
transition: .2s;
}
[type=checkbox]:not(.filled-in)+.filter__label::after {
border: 0;
-webkit-transform: scale(0);
-moz-transform: scale(0);
-ms-transform: scale(0);
-o-transform: scale(0);
transform: scale(0);
}
[type=checkbox]:checked+.filter__label::after,
[type=checkbox]:checked+.filter__label::before,
[type=radio]:checked+.filter__label::after,
[type=radio]:checked+.filter__label::before {
border: 2px solid #7dcfa8;
background-color: #7dcfa8;
}
[type=checkbox]:checked+.checkbox_form {
color: #7dcfa8;
}
select {
width: 100px;
}
option {
...
JavaScript
class Filter {
constructor() {
this.param = this.parsUrl();
}
parsUrl() {
const qd = {};
const testUrl = '?size=L&color=1,3&manufacturer=aaa,eee';
if (testUrl) testUrl.substr(1).split("&").forEach(function (item) {
let s = item.split("="),
k = s[0],
v = s[1] && decodeURIComponent(s[1]);
(qd[k] = qd[k] || []).push(v.split(','))
});
return qd;
}
filterInit() {
for (let key in this.param) {
const elValue = this.param[key][0];
let el = document.getElementsByName(key);
el.forEach(function (el) {
switch (el.type) {
case 'checkbox':
for (let i = 0; i < elValue.length; i++) {
if (elValue[i].toLowerCase() === el.value.toLowerCase()) {
el.checked = true;
}
}
break;
case 'radio':
for (let i = 0; i < elValue.length; i++) {
if (elValue[i].toLowerCase() === el.value.toLowerCase()) {
el.checked = true;
}
}
break;
case 'select-multiple':
/* console.log(el.children); */
/* for (let i = 0; i < elValue.length; i++) {
console.log(el.children[i].value);
console.log(el.children[i].value === elValue[i]);
// if (el.children[i].value === elValue[i]) {
// console.log(el.children[i].value === elValue[i]);
// }
} */
break;
}
});
}
}
}
let filter = new Filter();
filter.filterInit();