SearchBar搜索栏

一般可位于 NavBar 下方,通过『取消按钮』退出激活状态。

规则

  • 应该在 placeholder 里提供提示文字,提醒用户输入相关内容,比如:双11特卖。
  • 在搜索栏下方,可提供有用的标签文案,帮助用户通过点击直接完成输入,比如:列出一些最近搜索的关键词。

代码演示

基本用法

最简单的用法。

  1. import { Component, ElementRef, Renderer2 } from '@angular/core';
  2. @Component({
  3. selector: 'demo-search-bar-basic',
  4. template: `
  5. <div class="am-demo-page">
  6. <div class="am-demo-bd">
  7. <div class="am-wingblank am-wingblank-lg">
  8. <div class="sub-title">Normal</div>
  9. </div>
  10. <div style="border-bottom: 1px solid #ddd;">
  11. <SearchBar [placeholder]="'Search'" [maxLength]="8"></SearchBar>
  12. </div>
  13. <div class="am-whitespace am-whitespace-md"></div>
  14. <div class="am-wingblank am-wingblank-lg">
  15. <div class="sub-title">AutoFocus when enter page</div>
  16. </div>
  17. <div style="border-bottom: 1px solid #ddd;">
  18. <SearchBar [placeholder]="'自动获取光标'" [setFocus]="autoFocus"></SearchBar>
  19. </div>
  20. <div class="am-wingblank am-wingblank-lg">
  21. <div class="sub-title">Focus by operation</div>
  22. </div>
  23. <div style="border-bottom: 1px solid #ddd;">
  24. <SearchBar [placeholder]="'手动获取获取光标'" [setFocus]="focusObj"></SearchBar>
  25. </div>
  26. <a role="button" class="am-button" (click)="handleClick()"><span>click to focus</span></a>
  27. <div class="am-wingblank am-wingblank-lg">
  28. <div class="sub-title">Show cancel button</div>
  29. </div>
  30. <div style="border-bottom: 1px solid #ddd;">
  31. <SearchBar
  32. [(ngModel)]="value"
  33. [placeholder]="'Search'"
  34. [showCancelButton]="true"
  35. (onBlur)="blur()"
  36. (onFocus)="focus()"
  37. (onCancel)="cancel()"
  38. (onClear)="clear(value)"
  39. (onSubmit)="submit(value)"
  40. (onChange)="change($event)"
  41. ></SearchBar>
  42. </div>
  43. </div>
  44. </div>
  45. `,
  46. styles: [
  47. `
  48. .am-search {
  49. border-bottom: 1px solid #ddd;
  50. }
  51. .sub-title {
  52. color: #888;
  53. font-size: 14px;
  54. padding: 30px 0 18px 0;
  55. }
  56. .am-wingblank .am-wingblank-lg {
  57. margin-left: 15px;
  58. margin-right: 15px;
  59. }
  60. .am-wingblank {
  61. margin-left: 8px;
  62. margin-right: 8px;
  63. }
  64. .am-button {
  65. display: block;
  66. outline: 0 none;
  67. -webkit-appearance: none;
  68. -webkit-box-sizing: border-box;
  69. box-sizing: border-box;
  70. padding: 0;
  71. text-align: center;
  72. font-size: 18px;
  73. height: 47px;
  74. line-height: 47px;
  75. overflow: hidden;
  76. text-overflow: ellipsis;
  77. word-break: break-word;
  78. white-space: nowrap;
  79. color: #000;
  80. background-color: #fff;
  81. border: 1px solid #ddd;
  82. border-radius: 5px;
  83. margin: 0 15px 15px 15px;
  84. }
  85. `
  86. ]
  87. })
  88. export class DemoSearchBarBasicComponent {
  89. value: string = '美食';
  90. autoFocus = {
  91. focusValue: true
  92. };
  93. focusObj = {
  94. focusValue: false,
  95. date: new Date()
  96. };
  97. constructor(private _element: ElementRef, private _renderer: Renderer2) {}
  98. change($event) {
  99. console.log($event, 'onChange');
  100. }
  101. submit(value) {
  102. console.log(value, 'onSubmit');
  103. }
  104. clear(value) {
  105. console.log(value, 'onClear');
  106. }
  107. focus() {
  108. console.log('onFocus');
  109. }
  110. blur() {
  111. console.log('onBlur');
  112. }
  113. cancel() {
  114. console.log('onCancel');
  115. }
  116. handleClick() {
  117. this.focusObj = {
  118. focusValue: true,
  119. date: new Date()
  120. };
  121. }
  122. }

SearchBar搜索栏 - 图1

API

参数说明类型默认值
[defaultValue]搜索框的默认值string-
[value]搜索框的当前值string-
[placeholder]placeholderstring-
[showCancelButton]是否一直显示取消按钮booleanfalse
[cancelText]定制取消按钮的文字string‘取消’
[disabled]设置禁用booleanfalse
[maxLength]最多允许输入的字符个数number-
[(ngModel)]当前值,可双向绑定string-
(ngModelChange)值改变时回调EventEmitter<string>-
(onSubmit)submit 事件 (点击键盘的 enter)EventEmitter<string>-
(onChange)change 事件的回调EventEmitter<string>-
(onFocus)focus 事件的回调EventEmitter<void>-
(onBlur)blur 事件的回调EventEmitter<void>-
(onCancel)点击取消按钮触发EventEmitter<string>-
(onClear)点击 clear 图标触发EventEmitter<string>-
(focus)SearchBar 聚焦时的回调事件EventEmitter<void>-