使用algolia自动完成功能创建可扩展材质搜索栏

时间:2018-03-04 15:44:02

标签: html css algolia

我在我的应用中使用algolia autocomplete。我想构建一个扩展的搜索栏,就像这个page上的可扩展示例一样。我有一个带有标题栏的布局,该标题栏具有更大屏幕的搜索输入。在小屏幕上我只有一个放大镜图标,点击切换另一个带有搜索栏的标题,我最初隐藏它。这是fiddle,我无法工作,但希望它仍然可以给你一些概述。这是布局:

<header class="mdc-toolbar mdc-toolbar--fixed toolbar--custom">
  <div class="mdc-toolbar__row">
    <section class="menu mdc-toolbar__section mdc-toolbar__section--align-start">
      <a href="#" class="material-icons mdc-toolbar__menu-icon">menu</a>
      <span class="mdc-toolbar__title">Title</span>
    </section>
    <section class="mdc-toolbar__section">
      <form action="/search" method="get" class="search-field-desktop">
        <div class="mdc-text-field mdc-text-field--box mdc-text-field--with-leading-icon autocomplete-search-field">
          <i class="material-icons mdc-text-field__icon" tabindex="0">search</i>
          <input name="q"
                 type="text"
                 class="mdc-text-field__input search-input-js aa-input-search"
                 placeholder="Search for players and videos ..."
                 aria-label="Full-Width Text Field">
        </div>
      </form>
    </section>
    <section class="mdc-toolbar__section mdc-toolbar__section--align-end" role="toolbar">
      <a href="#" id="search-input-open" class="material-icons mdc-toolbar__icon" aria-label="Search" alt="Search">search</a>
      <a href="#" class="material-icons mdc-toolbar__icon" aria-label="Login" alt="Login">more_vert</a>
    </section>
  </div>
</header>
<header class="search-field-phone mdc-toolbar mdc-toolbar--fixed toolbar--custom">
  <div class="mdc-toolbar__row">
    <a id="search-input-close" class="material-icons mdc-toolbar__menu-icon" tabindex="0">arrow_back</a>
    <form action="/search" method="get">
      <div class="mdc-text-field mdc-text-field--box autocomplete-search-field">
        <input name="q"
                type="text"
                class="mdc-text-field__input search-input-js aa-input-search"
                placeholder="Search for players and videos ..."
                aria-label="Full-Width Text Field"
                autofocus>
      </div>
    </form>
  </div>
</header>

我遇到的问题是,由于某种原因,在小屏幕上看不到下拉菜单,而是在更大的屏幕上。这是scss / css:

.algolia-autocomplete {
  display: flex!important;
  flex: auto!important;
  height: 100%;
}
.aa-dropdown-menu {
  position: relative;
  top: -6px;
  border-radius: 3px;
  margin: 6px 0 0;
  padding: 0;
  text-align: left;
  height: auto;
  position: relative;
  background: $white;
  border: 1px solid #ccc;
  width: 100%;
  left: 0 !important;
  box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.2), 0 2px 3px 0 rgba(0, 0, 0, 0.1);
}

.aa-dropdown-menu .aa-suggestions {
  position: relative;
  z-index: 1000;
}

.aa-dropdown-menu [class^="aa-dataset-"] {
  position: relative;
  border: 0;
  border-radius: 3px;
  overflow: auto;
  padding: 0;
  color: #3c3e42;
  font-weight: 500;
}

.aa-dropdown-menu * {
  box-sizing: border-box;
}

.aa-suggestion {
  display: block;
  width: 100%;
  height: 72px;
  clear: both;

  .mdc-list-item {
    height: 72px;
  }
}
.aa-suggestion span {
  white-space: nowrap !important;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
  float: left;
  line-height: 1em;
  width: calc(100% - 30px);
}
.aa-suggestion.aa-cursor {
  background-color: transparent;
}
.aa-suggestion em {
  color: #00bcd4;
  font-weight: 700;
}
.aa-suggestion img {
  float: left;
  height: 44px;
  width: 44px;
  margin-right: 6px;
}

.aa-suggestion a {
  color: #3c3e42;
}

.aa-suggestions-category {
  font-weight: 700;
  color: #3c3e42;
  border-bottom: 1px solid rgba(102, 105, 105, 0.17);
}

.powered-by-algolia {
  padding-left: 15px;
  border-top: 1px solid rgba(102, 105, 105, 0.17);
  display: flex;
  align-items: center;
  height: 30px;
}

.aa-input-container {
  display: inline-block;
  position: relative; }
.aa-input-search {
  width: 100%;
  height: 30px;
  padding: 12px 28px 12px 12px;
  box-sizing: border-box;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none; }
.aa-input-search::-webkit-search-decoration, 
.aa-input-search::-webkit-search-cancel-button, 
.aa-input-search::-webkit-search-results-button, 
.aa-input-search::-webkit-search-results-decoration {
  display: none;
}
.media {
  margin: 10px 0;
}
.media-body {
  p {
    margin: 0;
  }
}
.toolbar--custom {
  color: $white;

  .mdc-toolbar__row {
    min-height: 56px;
  }

  form, .autocomplete-search-field {
    width: 100%;
  }

  .mdc-text-field--box:not(.mdc-text-field--upgraded) {
    height: 36px;
  }
  .mdc-text-field--box {
    overflow: visible;
    margin: auto;

    .mdc-text-field__icon {
      bottom: 8px;
      font-size: 22px;
    }
  }

  .mdc-text-field--box:after,
  .mdc-text-field--box:before {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  #search-input-open {
    display: none;
  }

  @media only screen and (max-width: 782px) {
    #search-input-open {
      display: block;
    }
    .search-field-desktop {
      display: none;
    }
  }
}

.search-field-phone {
  clip-path: circle(0%);
  visibility: hidden;
  .mdc-text-field {
    clip-path: circle(0%);
  }
}

@mixin search-animated-open {
  -webkit-animation: open 0.3s forwards;
  animation: open 0.3s forwards;
  animation-timing-function: $mdc-animation-standard-curve-timing-function;
}

@mixin search-animated-close {
  -webkit-animation: close 0.3s forwards;
  animation: close 0.3s forwards;
  animation-timing-function: $mdc-animation-standard-curve-timing-function;
}

.search-field-phone-open,  {
  visibility: visible;
  @include search-animated-open;

  .mdc-text-field {
    @include search-animated-open;
  }
}

.search-field-phone-close,  {
  @include search-animated-close;

  .mdc-text-field {
    @include search-animated-close;
  }
}

@keyframes open {
  from {
    clip-path: circle(0 at calc(100% - 68px) 50%);
  }
  to {
    clip-path: circle(150% at calc(100% - 68px) 50%);
  }
}
@keyframes close {
  from {
    clip-path: circle(150% at calc(100% - 68px) 50%);
  }
  to {
    clip-path: circle(0 at calc(100% - 68px) 50%);
    visibility: hidden;
  }
}

整个页面的布局如下所示:

<div id="app">
    @include('layouts.partials.sidebar')
    @include('layouts.partials.navigation')
    <div class="page-content">
        @yield('content')
    </div>
</div>

导航部分是上面显示的标题文件。这是侧边栏部分:

<aside class="mdc-drawer mdc-drawer--temporary mdc-typography">
  <nav class="mdc-drawer__drawer">
    <header class="mdc-drawer__header">
      <div class="mdc-drawer__header-content">
        Header here
      </div>
    </header>
    <nav id="icon-with-text-demo" class="mdc-drawer__content mdc-list">
      <a class="mdc-list-item mdc-list-item--activated" href="#">
        <i class="material-icons mdc-list-item__graphic" aria-hidden="true">inbox</i>Inbox
      </a>
      <a class="mdc-list-item" href="#">
        <i class="material-icons mdc-list-item__graphic" aria-hidden="true">star</i>Star
      </a>
    </nav>
  </nav>
</aside>

这是页面的css:

body {
  margin: 0;
}
#app {
  display: flex;
}
.page-content {
  display: inline-flex;
  flex-direction: column;
  flex-grow: 1;
  height: 100%;
  box-sizing: border-box;
}

我不确定导致此行为的原因是什么,为什么较大屏幕的下拉菜单可见,而较小屏幕的下拉菜单不可用?

更新

我注意到当我在小屏幕上打开搜索输入时,如果我删除了search-field-phone-open,则下拉菜单可见,我不确定该类中的内容是否导致下拉列表不可见?

.search-field-phone-open,  {
  visibility: visible;
  @include search-animated-open;

  .mdc-text-field {
    @include search-animated-open;
  }
}

1 个答案:

答案 0 :(得分:3)

我不确定我是否了解您的需求,但请查看以下代码段:

&#13;
&#13;
      var client = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');
      var index = client.initIndex('contacts');

      $('.input-search').autocomplete({ hint: false }, [
        {
          source: function(searchBar, cb) {
            index.search(searchBar, { hitsPerPage: 5 }, function(error, content) {
              if (error) {
                cb([]);
                return;
              }
              cb(content.hits, content);
            });
          },
          displayKey: 'name',
          templates: {
            suggestion: function(suggestion) {
              return suggestion._highlightResult.name.value;
            }
          }
        }
      ]);
&#13;
.algolia-autocomplete {
  display: flex!important;
  flex: auto!important;
  height: 100%;
}
.aa-dropdown-menu {
  position: relative;
  top: -6px;
  border-radius: 3px;
  margin: 6px 0 0;
  padding: 0;
  text-align: left;
  height: auto;
  position: relative;
  background: $white;
  border: 1px solid #ccc;
  width: 100%;
  left: 0 !important;
  box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.2), 0 2px 3px 0 rgba(0, 0, 0, 0.1);
}

.aa-dropdown-menu:before {
  position: absolute;
  content: '';
  width: 14px;
  height: 14px;
  background: #fff;
  z-index: 0;
  top: -7px;
  border-top: 1px solid #D9D9D9;
  border-right: 1px solid #D9D9D9;
  transform: rotate(-45deg);
  border-radius: 2px;
  z-index: 999;
  display: block;
  left: 24px;
}

.aa-dropdown-menu .aa-suggestions {
  position: relative;
  z-index: 1000;
}

.aa-dropdown-menu [class^="aa-dataset-"] {
  position: relative;
  border: 0;
  border-radius: 3px;
  overflow: auto;
  padding: 8px 8px 8px;
  color: #3c3e42;
  font-weight: 500;
}

.aa-dropdown-menu * {
  box-sizing: border-box;
}

.aa-suggestion {
  padding: 0 4px 0;
  display: block;
  width: 100%;
  height: 38px;
  clear: both;
}
.aa-suggestion span {
  white-space: nowrap !important;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
  float: left;
  line-height: 1em;
  width: calc(100% - 30px);
}
.aa-suggestion.aa-cursor {
  background-color: transparent;
}
.aa-suggestion em {
  color: #00bcd4;
  font-weight: 700;
}
.aa-suggestion img {
  float: left;
  height: 44px;
  width: 44px;
  margin-right: 6px;
}

.aa-suggestion a {
  color: #3c3e42;
}

.aa-suggestions-category {
  font-weight: 700;
  color: #3c3e42;
  border-bottom: 1px solid rgba(102, 105, 105, 0.17);
}

.mdl-textfield {
    margin: -20px !important;
}





/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/materialicons/v36/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -moz-font-feature-settings: 'liga';
  -moz-osx-font-smoothing: grayscale;
}
&#13;
<link href="https://storage.googleapis.com/code.getmdl.io/1.0.1/material.teal-red.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/material-components-web@0.8.0/dist/material-components-web.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
<script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.jquery.min.js"></script>
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.1/material.min.js"></script>
<script src="https://unpkg.com/material-components-web@0.8.0/dist/material-components-web.min.js"></script>


<header class="mdc-toolbar">
  <div class="mdc-toolbar__row">
    <section class="mdc-toolbar__section mdc-toolbar__section--align-start">
      <a href="#" class="material-icons mdc-toolbar__menu-icon">menu</a>
      <span class="mdc-toolbar__title">Title</span>
    </section>
    <section class="mdc-toolbar__section">
      <div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable">
						<label class="mdl-button mdl-js-button mdl-button--icon" for="site-search">
								<i class="material-icons">search</i>
							</label>
							<div class="mdl-textfield__expandable-holder">
								<input class="mdl-textfield__input input-search" name="searchBar" type="search" id="site-search" />
								<label class="mdl-textfield__label" for="site-search">Search</label>
							</div>
						</div>
    </section>
    <section class="mdc-toolbar__section mdc-toolbar__section--align-end">
     Custom icon
    </section>
  </div>
</header>
&#13;
&#13;
&#13;

希望这会对你有所帮助。

<强>参考

我提到了GitHub上提供的mdc-toolbar