Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix storefront filters

dominik-zeglen6e4fbf00f15a
diff --git a/saleor/static/js/components/product-filters.js b/saleor/static/js/components/product-filters.js
index 90abdb5f32..7b10308733 100644
--- a/saleor/static/js/components/product-filters.js
+++ b/saleor/static/js/components/product-filters.js
@@ -1,36 +1,21 @@
-export default $(document).ready((e) => {
- $('.filters-menu').on('click', (e) => {
- const menuContainer = $('.filters-menu__body');
- if (menuContainer.hasClass('d-none')) {
- menuContainer.removeClass('d-none');
- } else {
- menuContainer.addClass('d-none');
- }
- });
-
- $('.toggle-filter').each(function () {
- let icon = $(this).find('.collapse-filters-icon');
- let fields = $(this).find('.filter-form-field');
-
- let filterArrowDown = $('.product-filters__attributes').data('icon-down');
- let filterArrowUp = $('.product-filters__attributes').data('icon-up');
+$('.filters-menu').on('click', (e) => {
+ const menuContainer = $('.filters-menu__body');
+ if (menuContainer.hasClass('d-none')) {
+ menuContainer.removeClass('d-none');
+ } else {
+ menuContainer.addClass('d-none');
+ }
+});
- fields.attr('aria-expanded', '');
+$('.filter-section__header').on('click', (event) => {
+ const $target = $(event.currentTarget).parent();
+ if ($target.attr('aria-expanded') === 'true') {
+ $target.attr('aria-expanded', 'false').addClass('filter-section--closed');
+ } else {
+ $target.attr('aria-expanded', 'true').removeClass('filter-section--closed');
+ }
+});
- $(this).find('.filter-label').on('click', () => {
- if (fields.attr('aria-expanded') !== undefined) {
- fields.removeAttr('aria-expanded').find('input[type="checkbox"], input[type="number"]').each((i, field) => {
- if (!field.checked) {
- $(field.parentNode.parentNode).addClass('d-none');
- }
- });
- icon.find('img').attr('src', filterArrowDown);
- } else {
- fields.attr('aria-expanded', '').find('input[type="checkbox"], input[type="number"]').each((i, field) => {
- $(field.parentNode.parentNode).removeClass('d-none');
- });
- icon.find('img').attr('src', filterArrowUp);
- }
- });
- });
+$('.filters-toggle').on('click', () => {
+ $('.filters-menu__body').toggleClass('d-none');
});
diff --git a/saleor/static/scss/components/_filters.scss b/saleor/static/scss/components/_filters.scss
index edccdbb109..540866ed48 100644
--- a/saleor/static/scss/components/_filters.scss
+++ b/saleor/static/scss/components/_filters.scss
@@ -3,15 +3,59 @@
.hide {
display: none;
}
- h3 {
- background: $light-gray;
- padding: $global-padding;
- font-weight: bold;
- cursor: pointer;
- .collapse-filters-icon {
- float: right !important;
- width: 20px;
- height: 20px;
+ .filter-section {
+ &--closed {
+ .filter-section {
+ &__icon {
+ img {
+ transform: rotate(180deg);
+ }
+ }
+ &__content {
+ max-height: $global-padding * .5;
+ }
+ }
+ }
+ &__header {
+ display: grid;
+ grid-template-columns: calc(100% - 20px) 20px;
+ background: $light-gray;
+ padding: $global-padding;
+ cursor: pointer;
+ h3 {
+ font-weight: bold;
+ padding-right: $global-padding * .5;
+ margin: 0;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ }
+ }
+ &__content {
+ transition: $transition-base;
+ overflow: hidden;
+ max-height: 500px;
+ .price-field {
+ padding: $global-padding 0;
+ input {
+ width: 40%;
+ display: inline-block;
+ }
+ span {
+ width: 20%;
+ text-align: center;
+ display: inline-block;
+ }
+ }
+ }
+ &__icon {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ transition: $transition-base;
+ img {
+ width: 20px;
+ height: 20px;
+ }
}
}
ul {
@@ -27,7 +71,6 @@
}
button {
width: 100%;
- margin-top: $global-margin;
}
&__attributes {
ul {
@@ -39,28 +82,9 @@
}
}
}
- &__price-range {
- h3 {
- margin-bottom: $global-margin;
- span {
- width: 100%;
- margin: 0;
- display: inline;
- }
- }
- input {
- width: 40%;
- display: inline-block;
- }
- span {
- width: 20%;
- text-align: center;
- display: inline-block;
- }
- }
&__title {
@include media-breakpoint-down(sm) {
- padding: $global-padding*3 0 $global-padding*2;
+ padding: $global-padding *2 0;
}
@media (max-width: 370px) {
padding-top: 5rem;
@@ -74,7 +98,7 @@
&__categories {
padding: $global-padding 0 0;
@include media-breakpoint-down(sm) {
- padding: $global-padding*3 0 $global-padding*2;
+ padding: $global-padding * 3 0 $global-padding * 2;
}
@media (max-width: 370px) {
padding-top: 5rem;
@@ -108,7 +132,7 @@
}
}
&__childs {
- padding-left: $global-padding*2.5;
+ padding-left: $global-padding * 2.5;
a {
font-size: 1rem;
}
@@ -140,10 +164,14 @@
}
}
+.filters-toggle {
+ margin: 1.1rem 0 1rem;
+ cursor: pointer;
+}
.sort-by {
text-align: right;
position: relative;
- margin: 1rem 0;
+ margin: 1rem 0 1rem 5rem;
.btn-link {
padding-right: $global-padding;
color: $body-color;
@@ -164,7 +192,7 @@
top: 2.5rem;
right: 0;
z-index: 999;
- padding: $global-padding $global-padding*2;
+ padding: $global-padding $global-padding * 2;
li {
text-align: left;
padding-top: $global-padding/2;
@@ -196,7 +224,7 @@
}
}
&:before {
- content: '';
+ content: "";
display: block;
width: 15px;
height: 10px;
diff --git a/templates/product/_filters.html b/templates/product/_filters.html
index 1bd7744ee2..23fff12c2b 100644
--- a/templates/product/_filters.html
+++ b/templates/product/_filters.html
@@ -29,41 +29,45 @@
{% endcomment %}
<input type="hidden" name="sort_by" value="{% if request.GET.sort_by %}{{ request.GET.sort_by }}{% endif %}">
{% elif field.name == 'price' %}
- <div class="toggle-filter product-filters__price-range">
- <h3 class="filter-label">
- {{ field.label }}
- <div class="collapse-filters-icon">
+ {% else %}
+ <div class="filter-section" aria-expanded="true">
+ <div class="filter-section__header">
+ <h3>{{ field.label }}</h3>
+ <div class="filter-section__icon">
<img class="lazyload lazypreload" data-src="{% static "images/chevron-up.svg" %}">
</div>
- </h3>
- <div class="filter-form-field" style="display:block">
- <div class="mb-3">
- <input id="{{ field.auto_id }}_0" name="{{ field.name }}_0"
- value="{% if field.value.0 %}{{ field.value.0 }}{% endif %}" type="number" min="0"
- class="form-control d-inline"
- placeholder="{% trans 'from' context 'Product price filter' %}"><span>-</span><input id="{{ field.auto_id }}_1" name="{{ field.name }}_1"
- value="{% if field.value.1 %}{{ field.value.1 }}{% endif %}" type="number" min="0"
- class="form-control d-inline" placeholder="{% trans 'to' context 'Product price filter' %}">
+ </div>
+ <div class="filter-section__content">
+ <div class="filter-form-field">
+ {{ field }}
</div>
</div>
</div>
- {% else %}
- <div class="toggle-filter">
- <h3 class="filter-label">
- {{ field.label }}
- <div class="collapse-filters-icon">
+ {% endif %}
+ {% endfor %}
+ {% for field in filter_set.form %}
+ {% if field.name == 'price' %}
+ <div class="filter-section" aria-expanded="true">
+ <div class="filter-section__header">
+ <h3>{{ field.label }}</h3>
+ <div class="filter-section__icon">
<img class="lazyload lazypreload" data-src="{% static "images/chevron-up.svg" %}">
</div>
- </h3>
- <div class="filter-form-field" style="display:block">
- {{ field }}
+ </div>
+ <div class="filter-section__content">
+ <div class="filter-form-field price-field">
+ <input id="{{ field.auto_id }}_0" name="{{ field.name }}_0"
+ value="{% if field.value.0 %}{{ field.value.0 }}{% endif %}" type="number" min="0"
+ class="form-control d-inline"
+ placeholder="{% trans 'from' context 'Product price filter' %}"/><span>-</span><input id="{{ field.auto_id }}_1" name="{{ field.name }}_1"
+ value="{% if field.value.1 %}{{ field.value.1 }}{% endif %}" type="number" min="0"
+ class="form-control d-inline" placeholder="{% trans 'to' context 'Product price filter' %}"/>
+ </div>
+ <button class="btn primary" type="submit">{% trans 'Update' context 'Product price filter' %}</button>
</div>
</div>
{% endif %}
{% endfor %}
- <div class="product-filters">
- <button class="btn primary" type="submit">{% trans 'Update' context 'Product price filter' %}</button>
- </div>
</form>
</div>
</div>
diff --git a/templates/product/product_list_base.html b/templates/product/product_list_base.html
index 6ec257985e..1c91345ffd 100644
--- a/templates/product/product_list_base.html
+++ b/templates/product/product_list_base.html
@@ -38,6 +38,9 @@
</ul>
</div>
<div class="col-md-5">
+ <h3 class="d-md-none float-left filters-toggle">
+ {% trans 'Filters' context 'Filter heading title' %}
+ </h3>
<div class="sort-by">
<div class="click-area d-none"></div>
<button class="btn btn-link">