|
| 1 | +<script> |
| 2 | +import { GlFormCheckboxGroup, GlFormCheckbox } from '@gitlab/ui'; |
| 3 | +import { mapState, mapActions } from 'vuex'; |
| 4 | +import { intersection } from 'lodash'; |
| 5 | +import { NAV_LINK_COUNT_DEFAULT_CLASSES, LABEL_DEFAULT_CLASSES } from '../constants'; |
| 6 | +import { formatSearchResultCount } from '../../store/utils'; |
| 7 | +
|
| 8 | +export default { |
| 9 | + name: 'CheckboxFilter', |
| 10 | + components: { |
| 11 | + GlFormCheckboxGroup, |
| 12 | + GlFormCheckbox, |
| 13 | + }, |
| 14 | + props: { |
| 15 | + filterData: { |
| 16 | + type: Object, |
| 17 | + required: true, |
| 18 | + }, |
| 19 | + }, |
| 20 | + computed: { |
| 21 | + ...mapState(['query']), |
| 22 | + scope() { |
| 23 | + return this.query.scope; |
| 24 | + }, |
| 25 | + queryFilters() { |
| 26 | + return this.query[this.filterData?.filterParam] || []; |
| 27 | + }, |
| 28 | + dataFilters() { |
| 29 | + return Object.values(this.filterData?.filters || []); |
| 30 | + }, |
| 31 | + flatDataFilterValues() { |
| 32 | + return this.dataFilters.map(({ value }) => value); |
| 33 | + }, |
| 34 | + selectedFilter: { |
| 35 | + get() { |
| 36 | + return intersection(this.flatDataFilterValues, this.queryFilters); |
| 37 | + }, |
| 38 | + set(value) { |
| 39 | + this.setQuery({ key: this.filterData?.filterParam, value }); |
| 40 | + }, |
| 41 | + }, |
| 42 | + labelCountClasses() { |
| 43 | + return [...NAV_LINK_COUNT_DEFAULT_CLASSES, 'gl-text-gray-500']; |
| 44 | + }, |
| 45 | + }, |
| 46 | + methods: { |
| 47 | + ...mapActions(['setQuery']), |
| 48 | + getFormatedCount(count) { |
| 49 | + return formatSearchResultCount(count); |
| 50 | + }, |
| 51 | + }, |
| 52 | + NAV_LINK_COUNT_DEFAULT_CLASSES, |
| 53 | + LABEL_DEFAULT_CLASSES, |
| 54 | +}; |
| 55 | +</script> |
| 56 | +
|
| 57 | +<template> |
| 58 | + <div class="gl-mx-5"> |
| 59 | + <h5 class="gl-mt-0">{{ filterData.header }}</h5> |
| 60 | + <gl-form-checkbox-group v-model="selectedFilter"> |
| 61 | + <gl-form-checkbox |
| 62 | + v-for="f in dataFilters" |
| 63 | + :key="f.label" |
| 64 | + :value="f.label" |
| 65 | + class="gl-flex-grow-1 gl-display-inline-flex gl-justify-content-space-between gl-w-full" |
| 66 | + :class="$options.LABEL_DEFAULT_CLASSES" |
| 67 | + > |
| 68 | + <span |
| 69 | + class="gl-flex-grow-1 gl-display-inline-flex gl-justify-content-space-between gl-w-full" |
| 70 | + > |
| 71 | + <span data-testid="label"> |
| 72 | + {{ f.label }} |
| 73 | + </span> |
| 74 | + <span v-if="f.count" :class="labelCountClasses" data-testid="labelCount"> |
| 75 | + {{ getFormatedCount(f.count) }} |
| 76 | + </span> |
| 77 | + </span> |
| 78 | + </gl-form-checkbox> |
| 79 | + </gl-form-checkbox-group> |
| 80 | + </div> |
| 81 | +</template> |
0 commit comments