API Reference / Angular InstantSearch Widgets / ais-menu
Apr. 24, 2019

You are reading the documentation for Angular InstantSearch v3, which is in beta. You can find the v2 documentation here.

Widget signature
<ais-menu
  attribute="string"

  // Optional parameters
  [limit]="number"
  [showMoreLimit]="number"
  showMoreLabel="string"
  showLessLabel="string"
  [sortBy]="string[]|function"
  [autoHideContainer]="boolean"
  [transformItems]="function"
></ais-configure>

About this widget

The ais-menu allows a user to select a single value to refine from a list.

Requirements

The attributes passed to the attributes prop must be declared as Attributes for faceting on the Algolia dashboard or configured as attributesForFaceting with the Algolia API.

Examples

1
<ais-menu attribute="brand"></ais-menu>

Props

attribute
type: string
Required

The name of the attribute in the record.

1
<ais-menu attribute="brand"></ais-menu>
limit
type: number
default: 10
Optional

The maximum amount of values to show (when not showing more)

1
2
3
4
<ais-menu
  // ...
  [limit]="20"
></ais-menu>
showMoreLimit
type: number
Optional

Amount of items to show if showing more.

1
2
3
4
<ais-menu
  // ...
  [showMoreLimit]="20"
></ais-menu>
showMoreLabel
type: string
default: Show more
Optional

Label of the “Show more” button.

1
2
3
4
<ais-menu
  // ...
  showMoreLabel="See more"
></ais-menu>
showLessLabel
type: string
default: Show less
Optional

Label of the show less button.

1
2
3
4
<ais-menu
  // ...
  showLessLabel="See less"
></ais-menu>
sortBy
type: string[]|Function
default: ["name:asc"]
Optional

How to sort refinements. Must be one or more of the following strings:

  • "count:asc"
  • "count:desc"
  • "name:asc"
  • "name:desc"
  • "isRefined"

It’s also possible to give a function, which receives items two by two, like JavaScript’s Array.sort.

1
2
3
4
<ais-menu
  // ...
  [sortBy]="['name:desc']"
></ais-menu>
autoHideContainer
type: boolean
Optional

Hides the menu if there’s no item to display.

1
2
3
4
<ais-menu
  // ...
  [autoHideContainer]="true"
></ais-menu>
transformItems
type: function
default: x => x
Optional

Receives the items, and is called before displaying them. Should return a new array with the same shape as the original array. Useful for mapping over the items to transform, and remove or reorder them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component({
  template: `
    <ais-menu
      // ...
      [transformItems]="transformItems"
    ></ais-menu>
  `,
})
export class AppComponent {
  transformItems(items) {
    return items.map(item => ({
      ...item,
      label: item.label.toUpperCase(),
    }));
  }
}

Customize the UI - connectMenu

If you want to create your own UI of the ais-menu widget, you can combine the connectMenu connector with the BaseWidget class.

This connector is also used to build other widgets: MenuSelect

1. Extend the BaseWidget class

First of all, you will need to write some boilerplate code in order to initialize correctly the BaseWidget class. This happens in the constructor() of your class extending the BaseWidget class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';

@Component({
  selector: 'app-menu',
  template: '<p>It works!</p>'
})
export class Menu extends BaseWidget {
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('Menu');
  }
}

There are a couple of things happening in this boilerplate:

  • we create a Menu class extending BaseWidget
  • we reference the <ais-instantsearch> parent component instance on the Menu widget class
  • we set app-menu as a selector, so we can use our component as <app-menu></app-menu>

2. Connect your custom widget

The BaseWidget class has a method called createWidget() which takes two arguments: the connector to use and an object of options (instance options) for this connector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';
import { connectMenu } from 'instantsearch.js/es/connectors';

@Component({
  selector: 'app-menu',
  template: '<p>It works!</p>'
})
export class Menu extends BaseWidget {
  public state: {
    // render options
  };
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('Menu');
    this.createWidget(connectMenu, {
      // instance options
      attribute: 'brand',
    });
  }
}

3. Render from the state

Your component instance has access to a this.state property which holds the rendering options of the widget.

public state: {
  items: object[];
  refine: Function;
  createURL: Function;
  isShowingMore: boolean;
  canToggleShowMore: boolean;
  toggleShowMore: Function;
  widgetParams: object;
}
1
2
3
4
5
6
7
8
9
10
11
<select
  class="menu-select"
  (change)="state.refine($event.target.value)"
>
  <option
    *ngFor="let item of state.items"
    [value]="item.value"
    [selected]="item.isRefined">
    {{ item.label }}
  </option>
</select>

Rendering options

items
type: object[]

The elements that can be refined for the current search results. With each item:

  • value: string: the value of the menu item
  • label: string: the label of the menu item
  • count: number: the number of results matched after a refinement is applied
  • isRefined: boolean: whether the refinement is applied
refine
type: function

Toggles a refinement.

createURL
type: function
default: (item.value) => string

Generates a URL for the corresponding search state.

isShowingMore
type: boolean

Whether the menu is displaying all the menu items.

canToggleShowMore
type: boolean

Whether the “Show more” button can be activated (enough items to display more and not already displaying more than the limit).

toggleShowMore
type: function

Toggles the number of displayed values between limit and showMoreLimit.

widgetParams
type: object

All original widget options forwarded to the render function.

Instance options

attribute
type: string
Required

The name of the attribute for faceting.

limit
type: number
default: 10
Optional

How many facet values to retrieve.

showMoreLimit
type: number
default: 10
Optional

How many facet values to retrieve when showing more.

sortBy
type: string[]|function
default: ["isRefined", "name:asc"]
Optional

How to sort refinements. Must be one or more of the following strings:

  • "count:asc"
  • "count:desc"
  • "name:asc"
  • "name:desc"
  • "isRefined"

It’s also possible to give a function, which receives items two by two, like JavaScript’s Array.sort.

transformItems
type: function
Optional

A function to transform the items passed to the templates.

Full example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';
import { connectMenu } from 'instantsearch.js/es/connectors';

@Component({
  selector: 'app-menu',
  template: `
<select
  class="menu-select"
  (change)="state.refine($event.target.value)"
>
  <option
    *ngFor="let item of state.items"
    [value]="item.value"
    [selected]="item.isRefined">
    {{ item.label }}
  </option>
</select>
`
})
export class Menu extends BaseWidget {
  public state: {
     items: object[];
     refine: Function;
     createURL: Function;
     isShowingMore: boolean;
     canToggleShowMore: boolean;
     toggleShowMore: Function;
     widgetParams: object;
  };
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('Menu');
    this.createWidget(connectMenu, {
      // instance options
      attribute: 'brand',
    });
  }
}

HTML output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div class="ais-Menu">
  <div class="ais-Menu-searchBox">
    <!-- SearchBox widget here -->
  </div>
  <ul class="ais-Menu-list">
    <li class="ais-Menu-item ais-Menu-item--selected">
      <a class="ais-Menu-link" href="#">
        <span class="ais-Menu-label">Appliances</span>
        <span class="ais-Menu-count">4,306</span>
      </a>
    </li>
    <li class="ais-Menu-item">
      <a class="ais-Menu-link" href="#">
        <span class="ais-Menu-label">Audio</span>
        <span class="ais-Menu-count">1,570</span>
      </a>
    </li>
  </ul>
  <button class="ais-Menu-showMore">Show more</button>
</div>

Did you find this page helpful?