Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect checkbox styling #656

Merged
merged 4 commits into from
Feb 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 54 additions & 40 deletions packages/theme/pages/Category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class="breadcrumbs desktop-only"
:breadcrumbs="breadcrumbs"
/>

<div class="navbar section">
<div class="navbar__aside desktop-only">
<LazyHydrate never>
Expand Down Expand Up @@ -57,8 +58,8 @@
<span class="navbar__label desktop-only">{{ $t('Products found') }}</span>
<span class="desktop-only">{{ pagination.totalItems }}</span>
<span class="navbar__label smartphone-only">{{ pagination.totalItems }} {{
$t('Items')
}}</span>
$t('Items')
}}</span>
</div>

<div class="navbar__view">
Expand Down Expand Up @@ -257,7 +258,7 @@
class="sf-button--text products__product-card-horizontal__add-to-wishlist"
@click="addItemToWishlist(product)"
>
{{ isInWishlist({product}) ? $t('Remove from Wishlist') : $t('Save for later') }}
{{ isInWishlist({ product }) ? $t('Remove from Wishlist') : $t('Save for later') }}
</SfButton>
</template>
</SfProductCardHorizontal>
Expand Down Expand Up @@ -339,7 +340,7 @@
:label="`${option.id}${option.count ? ` (${option.count})` : ''}`"
:value="option.value"
:selected="isFilterSelected(facet, option)"
name="priceFilter"
name="filter__price"
@change="() => selectFilter(facet, option)"
/>
</div>
Expand All @@ -365,14 +366,27 @@
:header="facet.label"
class="filters__accordion-item"
>
<SfFilter
v-for="option in facet.options"
:key="`${facet.id}-${option.id}`"
:label="option.id"
:selected="isFilterSelected(facet, option)"
class="filters__item"
@change="() => selectFilter(facet, option)"
/>
<div v-if="facet.id === 'price'">
<SfRadio
v-for="option in facet.options"
:key="`${facet.id}-${option.value}`"
:label="`${option.id}${option.count ? ` (${option.count})` : ''}`"
:value="option.value"
:selected="isFilterSelected(facet, option)"
name="filter__price"
@change="() => selectFilter(facet, option)"
/>
</div>
<div v-else>
<SfFilter
v-for="option in facet.options"
:key="`${facet.id}-${option.id}`"
:label="option.id"
:selected="isFilterSelected(facet, option)"
class="filters__item"
@change="() => selectFilter(facet, option)"
/>
</div>
</SfAccordionItem>
</div>
</SfAccordion>
Expand Down Expand Up @@ -499,12 +513,6 @@ export default defineComponent({
isInCart,
} = useAddToCart();

const selectedFilters = ref(Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(magentoConfig.facets.available)
.map((curr) => [curr, (curr === 'price' ? '' : [])]),
));

const products = computed(() => facetGetters.getProducts(result.value));

const categoryTree = computed(() => categoryGetters.getCategoryTree(
Expand All @@ -514,10 +522,8 @@ export default defineComponent({
));

const breadcrumbs = computed(() => facetGetters.getBreadcrumbs(result.value));

const sortBy = computed(() => facetGetters.getSortOptions(result.value));
const facets = computed(() => facetGetters.getGrouped(result.value, magentoConfig.facets.available));

const pagination = computed(() => facetGetters.getPagination(result.value));

const activeCategory = computed(() => {
Expand Down Expand Up @@ -557,13 +563,16 @@ export default defineComponent({

const isFilterSelected = (facet, option) => {
if (facet.id === 'price') {
return selectedFilters.value[facet.id];
return selectedFilters.value[facet.id] || '';
}

return (selectedFilters.value[facet.id] || []).includes(option.value);
};

/* eslint-disable */
const selectFilter = (facet, option) => {
if (facet.id === 'price') {
// eslint-disable-next-line
selectedFilters.value[facet.id] = option.value;
return;
}
Expand All @@ -575,17 +584,16 @@ export default defineComponent({
if (selectedFilters.value[facet.id].find((f) => f === option.value)) {
selectedFilters.value[facet.id] = selectedFilters.value[
facet.id
]?.filter((f) => f !== option.value);

]?.filter((f) => f !== option.value);
return;
}

selectedFilters.value[facet.id].push(option.value);
};
/* eslint-enable */

const applyFilters = (filters) => {
toggleFilterSidebar();

if (filters) {
selectedFilters.value = filters;
}
Expand All @@ -612,8 +620,29 @@ export default defineComponent({
});
};

const getSelectedFilterValues = () => {
let selectedFilterValues = Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(magentoConfig.facets.available)
.map((curr) => [curr, (curr === 'price' ? '' : [])]),
);
const filters = uiHelpers.getFacetsFromURL().filters;

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.keys(filters).forEach((filter) => {
if (filter === 'price') {
selectedFilterValues[filter] = filters[filter][0];
} else {
selectedFilterValues[filter] = filters[filter];
}
});
return selectedFilterValues;
}

const isProductsLoading = ref(false);
const isCategoriesLoading = ref(false);
const selectedFilters = ref(getSelectedFilterValues());

onSSR(async () => {
isProductsLoading.value = true;
isCategoriesLoading.value = true;
Expand All @@ -625,22 +654,7 @@ export default defineComponent({
isCategoriesLoading.value = false;

if (routeData?.value) {
if (facets.value && facets.value.length > 0) {
selectedFilters.value = facets.value?.reduce(
(prev, curr) => (curr.id === 'price'
? {
...prev,
[curr.id]: curr.options.find((o) => o.selected)?.value,
}
: {
...prev,
[curr.id]: curr.options
?.filter((o) => o.selected)
?.map((o) => o.value),
}),
{},
);
}
selectedFilters.value = getSelectedFilterValues();

await searchCategoryProduct();
isProductsLoading.value = false;
Expand Down