Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(Menu): add widget #478

Merged
merged 8 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
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
15 changes: 6 additions & 9 deletions docs/src/components/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@ Create a menu based on a facet. A menu displays a list of facet values and let t

Name | Type | Default | Description | Required
---|---|---|---|---
attribute | String | | The attribute | yes
limit | Number | 10 | Number of items to show
showMoreLimit | Number | 20 | Number of items to show when the user clicked on "show more items"
showMore | Boolean | `false` | Whether or not to have the option to load more values
sortBy | Array(string) or function | `['isRefined:desc', 'count:desc', 'name:asc']` | array or function to sort the results by
attribute | string | - | The attribute | Yes
limit | number | 10 | Number of items to show | -
showMoreLimit | number | 20 | Number of items to show when the user clicked on "show more" | -
showMore | boolean | `false` | Whether or not to have the option to load more values | -
sortBy | string[] or function | `['name:asc', 'count:desc']` | Array or function to sort the results by | -

## CSS classes

Here's a list of CSS classes exposed by this widget. To better understand the underlying
DOM structure, have a look at the generated DOM in your browser.
Here's a list of CSS classes exposed by this widget. To better understand the underlying DOM structure, have a look at the generated DOM in your browser.

Class name | Description
---|---
`.ais-Menu` | the root div of the widget
`.ais-Menu--noRefinement` | the root div of the widget with no refinement
`.ais-Menu-searchBox` | the search box of the widget
`.ais-Menu-list` | the list of all menu items
`.ais-Menu-item` | the menu list item
`.ais-Menu-item--selected` | the selected menu list item
`.ais-Menu-link` | the clickable menu element
`.ais-Menu-label` | the label of each item
`.ais-Menu-count` | the count of values for each item
`.ais-Menu-noResults` | the div displayed when there are no results
`.ais-Menu-showMore` | the button used to display more categories
`.ais-Menu-showMore--disabled` | the disabled button used to display more categories
80 changes: 43 additions & 37 deletions src/components/Menu.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<template>
<div :class="suit()" v-if="state">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fit is.css?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it fits since the full DOM widget is under the slot. It's just that we have now an extra (useless) div on top of the widget.

<div
v-if="state"
:class="[suit(''), !state.canRefine && suit('', 'noRefinement')]"
>
<slot
:items="items"
:createURL="createURL"
:refine="refine"
:canRefine="canRefine"
:widgetParams="widgetParams"
:isShowingMore="isShowingMore"
:toggleShowMore="toggleShowMore"
:canToggleShowMore="canToggleShowMore"
:items="state.items"
:can-refine="state.canRefine"
:can-toggle-show-more="state.canToggleShowMore"
:is-showing-more="state.isShowingMore"
:refine="state.refine"
:create-URL="state.createURL"
:toggle-show-more="state.toggleShowMore"
>
<ul :class="suit('list')">
<li
v-for="item in state.items"
:class="item.isRefined ? suit('item', 'active') : suit('item')"
:key="item.value"
:class="[suit('item'), item.isRefined && suit('item', 'selected')]"
>
<a
:href="state.createURL(item.value)"
Expand All @@ -28,19 +30,23 @@
</ul>

<button
v-if="showMore && state.canToggleShowMore"
v-if="showShowMoreButton"
:class="[suit('showMore'), !state.canToggleShowMore && suit('showMore', 'disabled')]"
:disabled="!state.canToggleShowMore"
@click.prevent="state.toggleShowMore()"
:class="state.canToggleShowMore ? suit('showMore') : suit('showMore', 'disabled')"
>
{{state.isShowingMore ? showLessLabel : showMoreLabel}}
<slot name="showMoreLabel" :is-showing-more="state.isShowingMore">
{{state.isShowingMore ? 'Show less' : 'Show more'}}
</slot>
</button>
</slot>
</div>
</template>

<script>
import algoliaComponent from '../component';
import isFunction from 'lodash/isFunction';
import { connectMenu } from 'instantsearch.js/es/connectors';
import algoliaComponent from '../component';

export default {
mixins: [algoliaComponent],
Expand All @@ -49,35 +55,40 @@ export default {
type: String,
required: true,
},
// @TODO
// searchable: {
// type: Boolean,
// default: false,
// },
limit: {
type: Number,
default: 10,
},
showMoreLimit: {
type: Number,
default: 20,
},
showMore: {
type: Boolean,
default: false,
},
showMoreLimit: {
type: Number,
},
sortBy: {
default() {
return ['isRefined:desc', 'count:desc', 'name:asc'];
return ['name:asc', 'count:desc'];
},
},
showMoreLabel: {
type: String,
default() {
return 'Show more';
},
},
showLessLabel: {
type: String,
default() {
return 'Show less';
validator(value) {
return Array.isArray(value) || isFunction(value);
},
},
},
beforeCreate() {
this.connector = connectMenu;
},
data() {
return {
widgetName: 'Menu',
};
},
computed: {
widgetParams() {
return {
Expand All @@ -87,13 +98,8 @@ export default {
sortBy: this.sortBy,
};
},
},
data() {
return {
widgetName: 'Menu',
};
},
beforeCreate() {
this.connector = connectMenu;
showShowMoreButton() {
return this.state.canRefine && this.showMore;
},
},
};</script>
Loading